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>
77 #include <sys/mount.h>
78 #include <sys/vnode.h>
80 #include <sys/unistd.h>
81 #include <sys/filedesc.h>
83 #include <sys/fcntl.h>
86 #include <sys/sysctl.h>
89 #include <vm/vm_extern.h>
91 #include <vm/vm_map.h>
92 #include <vm/vm_page.h>
93 #include <vm/vm_object.h>
96 #include "fuse_file.h"
97 #include "fuse_node.h"
98 #include "fuse_internal.h"
102 SDT_PROVIDER_DECLARE(fusefs);
105 * arg0: verbosity. Higher numbers give more verbose messages
106 * arg1: Textual message
108 SDT_PROBE_DEFINE2(fusefs, , io, trace, "int", "char*");
111 fuse_io_clear_suid_on_write(struct vnode *vp, struct ucred *cred,
114 fuse_read_directbackend(struct vnode *vp, struct uio *uio,
115 struct ucred *cred, struct fuse_filehandle *fufh);
117 fuse_read_biobackend(struct vnode *vp, struct uio *uio, int ioflag,
118 struct ucred *cred, struct fuse_filehandle *fufh, pid_t pid);
120 fuse_write_directbackend(struct vnode *vp, struct uio *uio,
121 struct ucred *cred, struct fuse_filehandle *fufh, off_t filesize,
122 int ioflag, bool pages);
124 fuse_write_biobackend(struct vnode *vp, struct uio *uio,
125 struct ucred *cred, struct fuse_filehandle *fufh, int ioflag, pid_t pid);
128 * FreeBSD clears the SUID and SGID bits on any write by a non-root user.
131 fuse_io_clear_suid_on_write(struct vnode *vp, struct ucred *cred,
134 struct fuse_data *data;
139 mp = vnode_mount(vp);
140 data = fuse_get_mpdata(mp);
141 dataflags = data->dataflags;
143 if (dataflags & FSESS_DEFAULT_PERMISSIONS) {
144 if (priv_check_cred(cred, PRIV_VFS_RETAINSUGID)) {
145 fuse_internal_getattr(vp, &va, cred, td);
146 if (va.va_mode & (S_ISUID | S_ISGID)) {
147 mode_t mode = va.va_mode & ~(S_ISUID | S_ISGID);
148 /* Clear all vattr fields except mode */
153 * Ignore fuse_internal_setattr's return value,
154 * because at this point the write operation has
155 * already succeeded and we don't want to return
156 * failing status for that.
158 (void)fuse_internal_setattr(vp, &va, td, NULL);
164 SDT_PROBE_DEFINE5(fusefs, , io, io_dispatch, "struct vnode*", "struct uio*",
165 "int", "struct ucred*", "struct fuse_filehandle*");
167 fuse_io_dispatch(struct vnode *vp, struct uio *uio, int ioflag, bool pages,
168 struct ucred *cred, pid_t pid)
170 struct fuse_filehandle *fufh;
173 bool closefufh = false;
175 MPASS(vp->v_type == VREG || vp->v_type == VDIR);
177 fflag = (uio->uio_rw == UIO_READ) ? FREAD : FWRITE;
178 err = fuse_filehandle_getrw(vp, fflag, &fufh, cred, pid);
179 if (err == EBADF && vnode_mount(vp)->mnt_flag & MNT_EXPORTED) {
181 * nfsd will do I/O without first doing VOP_OPEN. We
182 * must implicitly open the file here
184 err = fuse_filehandle_open(vp, fflag, &fufh, curthread, cred);
188 printf("FUSE: io dispatch: filehandles are closed\n");
193 SDT_PROBE5(fusefs, , io, io_dispatch, vp, uio, ioflag, cred, fufh);
196 * Ideally, when the daemon asks for direct io at open time, the
197 * standard file flag should be set according to this, so that would
198 * just change the default mode, which later on could be changed via
200 * But this doesn't work, the O_DIRECT flag gets cleared at some point
201 * (don't know where). So to make any use of the Fuse direct_io option,
202 * we hardwire it into the file's private data (similarly to Linux,
205 directio = (ioflag & IO_DIRECT) || !fsess_opt_datacache(vnode_mount(vp));
207 switch (uio->uio_rw) {
210 SDT_PROBE2(fusefs, , io, trace, 1,
211 "direct read of vnode");
212 err = fuse_read_directbackend(vp, uio, cred, fufh);
214 SDT_PROBE2(fusefs, , io, trace, 1,
215 "buffered read of vnode");
216 err = fuse_read_biobackend(vp, uio, ioflag, cred, fufh,
222 * Kludge: simulate write-through caching via write-around
223 * caching. Same effect, as far as never caching dirty data,
224 * but slightly pessimal in that newly written data is not
227 if (directio || fuse_data_cache_mode == FUSE_CACHE_WT) {
228 const int iosize = fuse_iosize(vp);
229 off_t start, end, filesize;
231 SDT_PROBE2(fusefs, , io, trace, 1,
232 "direct write of vnode");
234 err = fuse_vnode_size(vp, &filesize, cred, curthread);
238 start = uio->uio_offset;
239 end = start + uio->uio_resid;
241 * Invalidate the write cache unless we're coming from
242 * VOP_PUTPAGES, in which case we're writing _from_ the
246 v_inval_buf_range(vp, start, end, iosize);
247 err = fuse_write_directbackend(vp, uio, cred, fufh,
248 filesize, ioflag, pages);
250 SDT_PROBE2(fusefs, , io, trace, 1,
251 "buffered write of vnode");
252 err = fuse_write_biobackend(vp, uio, cred, fufh, ioflag,
255 fuse_io_clear_suid_on_write(vp, cred, uio->uio_td);
258 panic("uninterpreted mode passed to fuse_io_dispatch");
263 fuse_filehandle_close(vp, fufh, curthread, cred);
268 SDT_PROBE_DEFINE3(fusefs, , io, read_bio_backend_start, "int", "int", "int");
269 SDT_PROBE_DEFINE2(fusefs, , io, read_bio_backend_feed, "int", "int");
270 SDT_PROBE_DEFINE3(fusefs, , io, read_bio_backend_end, "int", "ssize_t", "int");
272 fuse_read_biobackend(struct vnode *vp, struct uio *uio, int ioflag,
273 struct ucred *cred, struct fuse_filehandle *fufh, pid_t pid)
278 int err, n = 0, on = 0;
281 const int biosize = fuse_iosize(vp);
283 if (uio->uio_offset < 0)
286 err = fuse_vnode_size(vp, &filesize, cred, curthread);
290 for (err = 0, bp = NULL; uio->uio_resid > 0; bp = NULL) {
291 if (fuse_isdeadfs(vp)) {
295 if (filesize - uio->uio_offset <= 0)
297 lbn = uio->uio_offset / biosize;
298 on = uio->uio_offset & (biosize - 1);
300 SDT_PROBE3(fusefs, , io, read_bio_backend_start,
301 biosize, (int)lbn, on);
303 if ((off_t)lbn * biosize >= filesize) {
305 } else if ((off_t)(lbn + 1) * biosize > filesize) {
306 bcount = filesize - (off_t)lbn *biosize;
311 /* TODO: readahead. See ext2_read for an example */
312 err = bread(vp, lbn, bcount, NOCRED, &bp);
320 * on is the offset into the current bp. Figure out how many
321 * bytes we can copy out of the bp. Note that bcount is
322 * NOT DEV_BSIZE aligned.
324 * Then figure out how many bytes we can copy into the uio.
329 n = MIN((unsigned)(bcount - on), uio->uio_resid);
331 SDT_PROBE2(fusefs, , io, read_bio_backend_feed,
332 n, n + (int)bp->b_resid);
333 err = uiomove(bp->b_data + on, n, uio);
335 vfs_bio_brelse(bp, ioflag);
336 SDT_PROBE3(fusefs, , io, read_bio_backend_end, err,
343 SDT_PROBE_DEFINE1(fusefs, , io, read_directbackend_start,
344 "struct fuse_read_in*");
345 SDT_PROBE_DEFINE2(fusefs, , io, read_directbackend_complete,
346 "struct fuse_dispatcher*", "struct uio*");
349 fuse_read_directbackend(struct vnode *vp, struct uio *uio,
350 struct ucred *cred, struct fuse_filehandle *fufh)
352 struct fuse_data *data;
353 struct fuse_dispatcher fdi;
354 struct fuse_read_in *fri;
357 data = fuse_get_mpdata(vp->v_mount);
359 if (uio->uio_resid == 0)
365 * XXX In "normal" case we use an intermediate kernel buffer for
366 * transmitting data from daemon's context to ours. Eventually, we should
367 * get rid of this. Anyway, if the target uio lives in sysspace (we are
368 * called from pageops), and the input data doesn't need kernel-side
369 * processing (we are not called from readdir) we can already invoke
370 * an optimized, "peer-to-peer" I/O routine.
372 while (uio->uio_resid > 0) {
373 fdi.iosize = sizeof(*fri);
374 fdisp_make_vp(&fdi, FUSE_READ, vp, uio->uio_td, cred);
376 fri->fh = fufh->fh_id;
377 fri->offset = uio->uio_offset;
378 fri->size = MIN(uio->uio_resid,
379 fuse_get_mpdata(vp->v_mount)->max_read);
380 if (fuse_libabi_geq(data, 7, 9)) {
381 /* See comment regarding FUSE_WRITE_LOCKOWNER */
383 fri->flags = fufh_type_2_fflags(fufh->fufh_type);
386 SDT_PROBE1(fusefs, , io, read_directbackend_start, fri);
388 if ((err = fdisp_wait_answ(&fdi)))
391 SDT_PROBE2(fusefs, , io, read_directbackend_complete,
394 if ((err = uiomove(fdi.answ, MIN(fri->size, fdi.iosize), uio)))
396 if (fdi.iosize < fri->size)
406 fuse_write_directbackend(struct vnode *vp, struct uio *uio,
407 struct ucred *cred, struct fuse_filehandle *fufh, off_t filesize,
408 int ioflag, bool pages)
410 struct fuse_vnode_data *fvdat = VTOFUD(vp);
411 struct fuse_data *data;
412 struct fuse_write_in *fwi;
413 struct fuse_write_out *fwo;
414 struct fuse_dispatcher fdi;
417 off_t as_written_offset;
420 bool direct_io = fufh->fuse_open_flags & FOPEN_DIRECT_IO;
421 uint32_t write_flags;
423 data = fuse_get_mpdata(vp->v_mount);
426 * Don't set FUSE_WRITE_LOCKOWNER in write_flags. It can't be set
427 * accurately when using POSIX AIO, libfuse doesn't use it, and I'm not
428 * aware of any file systems that do. It was an attempt to add
429 * Linux-style mandatory locking to the FUSE protocol, but mandatory
430 * locking is deprecated even on Linux. See Linux commit
431 * f33321141b273d60cbb3a8f56a5489baad82ba5e .
434 * Set FUSE_WRITE_CACHE whenever we don't know the uid, gid, and/or pid
435 * that originated a write. For example when writing from the
436 * writeback cache. I don't know of a single file system that cares,
437 * but the protocol says we're supposed to do this.
439 write_flags = !pages && (
440 (ioflag & IO_DIRECT) ||
441 !fsess_opt_datacache(vnode_mount(vp)) ||
442 fuse_data_cache_mode != FUSE_CACHE_WB) ? 0 : FUSE_WRITE_CACHE;
444 if (uio->uio_resid == 0)
447 if (ioflag & IO_APPEND)
448 uio_setoffset(uio, filesize);
450 if (vn_rlimit_fsize(vp, uio, uio->uio_td))
455 while (uio->uio_resid > 0) {
456 chunksize = MIN(uio->uio_resid, data->max_write);
458 fdi.iosize = sizeof(*fwi) + chunksize;
459 fdisp_make_vp(&fdi, FUSE_WRITE, vp, uio->uio_td, cred);
462 fwi->fh = fufh->fh_id;
463 fwi->offset = uio->uio_offset;
464 fwi->size = chunksize;
465 fwi->write_flags = write_flags;
466 if (fuse_libabi_geq(data, 7, 9)) {
467 fwi->flags = fufh_type_2_fflags(fufh->fufh_type);
468 fwi_data = (char *)fdi.indata + sizeof(*fwi);
470 fwi_data = (char *)fdi.indata +
471 FUSE_COMPAT_WRITE_IN_SIZE;
474 if ((err = uiomove(fwi_data, chunksize, uio)))
478 err = fdisp_wait_answ(&fdi);
479 if (err == ERESTART || err == EINTR || err == EWOULDBLOCK) {
481 * Rewind the uio so dofilewrite will know it's
484 uio->uio_resid += fwi->size;
485 uio->uio_offset -= fwi->size;
487 * Change ERESTART into EINTR because we can't rewind
488 * uio->uio_iov. Basically, once uiomove(9) has been
489 * called, it's impossible to restart a syscall.
498 fwo = ((struct fuse_write_out *)fdi.answ);
500 /* Adjust the uio in the case of short writes */
501 diff = fwi->size - fwo->size;
502 as_written_offset = uio->uio_offset - diff;
504 if (as_written_offset - diff > filesize &&
505 fuse_data_cache_mode != FUSE_CACHE_UC)
506 fuse_vnode_setsize(vp, cred, as_written_offset);
507 if (as_written_offset - diff >= filesize)
508 fvdat->flag &= ~FN_SIZECHANGE;
511 printf("WARNING: misbehaving FUSE filesystem "
512 "wrote more data than we provided it\n");
515 } else if (diff > 0) {
518 printf("WARNING: misbehaving FUSE filesystem: "
519 "short writes are only allowed with "
522 if (ioflag & IO_DIRECT) {
524 uio->uio_resid += diff;
525 uio->uio_offset -= diff;
528 /* Resend the unwritten portion of data */
529 fdi.iosize = sizeof(*fwi) + diff;
530 /* Refresh fdi without clearing data buffer */
531 fdisp_refresh_vp(&fdi, FUSE_WRITE, vp,
534 MPASS2(fwi == fdi.indata, "FUSE dispatcher "
535 "reallocated despite no increase in "
537 void *src = (char*)fwi_data + fwo->size;
538 memmove(fwi_data, src, diff);
539 fwi->fh = fufh->fh_id;
540 fwi->offset = as_written_offset;
542 fwi->write_flags = write_flags;
553 SDT_PROBE_DEFINE6(fusefs, , io, write_biobackend_start, "int64_t", "int", "int",
554 "struct uio*", "int", "bool");
555 SDT_PROBE_DEFINE2(fusefs, , io, write_biobackend_append_race, "long", "int");
558 fuse_write_biobackend(struct vnode *vp, struct uio *uio,
559 struct ucred *cred, struct fuse_filehandle *fufh, int ioflag, pid_t pid)
561 struct fuse_vnode_data *fvdat = VTOFUD(vp);
568 const int biosize = fuse_iosize(vp);
570 KASSERT(uio->uio_rw == UIO_WRITE, ("ncl_write mode"));
571 if (vp->v_type != VREG)
573 if (uio->uio_offset < 0)
575 if (uio->uio_resid == 0)
578 err = fuse_vnode_size(vp, &filesize, cred, curthread);
582 if (ioflag & IO_APPEND)
583 uio_setoffset(uio, filesize);
585 if (vn_rlimit_fsize(vp, uio, uio->uio_td))
589 * Find all of this file's B_NEEDCOMMIT buffers. If our writes
590 * would exceed the local maximum per-file write commit size when
591 * combined with those, we must decide whether to flush,
592 * go synchronous, or return err. We don't bother checking
593 * IO_UNIT -- we just make all writes atomic anyway, as there's
594 * no point optimizing for something that really won't ever happen.
597 if (fuse_isdeadfs(vp)) {
601 lbn = uio->uio_offset / biosize;
602 on = uio->uio_offset & (biosize - 1);
603 n = MIN((unsigned)(biosize - on), uio->uio_resid);
607 * Handle direct append and file extension cases, calculate
608 * unaligned buffer size.
610 if (uio->uio_offset == filesize && n) {
612 * Get the buffer (in its pre-append state to maintain
613 * B_CACHE if it was previously set). Resize the
614 * nfsnode after we have locked the buffer to prevent
615 * readers from reading garbage.
618 SDT_PROBE6(fusefs, , io, write_biobackend_start,
619 lbn, on, n, uio, bcount, true);
620 bp = getblk(vp, lbn, bcount, PCATCH, 0, 0);
625 err = fuse_vnode_setsize(vp, cred,
626 uio->uio_offset + n);
627 fvdat->flag |= FN_SIZECHANGE;
633 save = bp->b_flags & B_CACHE;
635 allocbuf(bp, bcount);
640 * Obtain the locked cache block first, and then
641 * adjust the file's size as appropriate.
644 if ((off_t)lbn * biosize + bcount < filesize) {
645 if ((off_t)(lbn + 1) * biosize < filesize)
648 bcount = filesize - (off_t)lbn *biosize;
650 SDT_PROBE6(fusefs, , io, write_biobackend_start,
651 lbn, on, n, uio, bcount, false);
652 bp = getblk(vp, lbn, bcount, PCATCH, 0, 0);
653 if (bp && uio->uio_offset + n > filesize) {
654 err = fuse_vnode_setsize(vp, cred,
655 uio->uio_offset + n);
656 fvdat->flag |= FN_SIZECHANGE;
669 * Issue a READ if B_CACHE is not set. In special-append
670 * mode, B_CACHE is based on the buffer prior to the write
671 * op and is typically set, avoiding the read. If a read
672 * is required in special append mode, the server will
673 * probably send us a short-read since we extended the file
674 * on our end, resulting in b_resid == 0 and, thusly,
675 * B_CACHE getting set.
677 * We can also avoid issuing the read if the write covers
678 * the entire buffer. We have to make sure the buffer state
679 * is reasonable in this case since we will not be initiating
680 * I/O. See the comments in kern/vfs_bio.c's getblk() for
683 * B_CACHE may also be set due to the buffer being cached
687 if (on == 0 && n == bcount) {
688 bp->b_flags |= B_CACHE;
689 bp->b_flags &= ~B_INVAL;
690 bp->b_ioflags &= ~BIO_ERROR;
692 if ((bp->b_flags & B_CACHE) == 0) {
693 bp->b_iocmd = BIO_READ;
694 vfs_busy_pages(bp, 0);
695 fuse_io_strategy(vp, bp);
696 if ((err = bp->b_error)) {
701 if (bp->b_wcred == NOCRED)
702 bp->b_wcred = crhold(cred);
705 * If dirtyend exceeds file size, chop it down. This should
706 * not normally occur but there is an append race where it
707 * might occur XXX, so we log it.
709 * If the chopping creates a reverse-indexed or degenerate
710 * situation with dirtyoff/end, we 0 both of them.
713 if (bp->b_dirtyend > bcount) {
714 SDT_PROBE2(fusefs, , io, write_biobackend_append_race,
715 (long)bp->b_blkno * biosize,
716 bp->b_dirtyend - bcount);
717 bp->b_dirtyend = bcount;
719 if (bp->b_dirtyoff >= bp->b_dirtyend)
720 bp->b_dirtyoff = bp->b_dirtyend = 0;
723 * If the new write will leave a contiguous dirty
724 * area, just update the b_dirtyoff and b_dirtyend,
725 * otherwise force a write rpc of the old dirty area.
727 * While it is possible to merge discontiguous writes due to
728 * our having a B_CACHE buffer ( and thus valid read data
729 * for the hole), we don't because it could lead to
730 * significant cache coherency problems with multiple clients,
731 * especially if locking is implemented later on.
733 * as an optimization we could theoretically maintain
734 * a linked list of discontinuous areas, but we would still
735 * have to commit them separately so there isn't much
736 * advantage to it except perhaps a bit of asynchronization.
739 if (bp->b_dirtyend > 0 &&
740 (on > bp->b_dirtyend || (on + n) < bp->b_dirtyoff)) {
742 * Yes, we mean it. Write out everything to "storage"
743 * immediately, without hesitation. (Apart from other
744 * reasons: the only way to know if a write is valid
745 * if its actually written out.)
748 if (bp->b_error == EINTR) {
754 err = uiomove((char *)bp->b_data + on, n, uio);
757 * Since this block is being modified, it must be written
758 * again and not just committed. Since write clustering does
759 * not work for the stage 1 data write, only the stage 2
760 * commit rpc, we have to clear B_CLUSTEROK as well.
762 bp->b_flags &= ~(B_NEEDCOMMIT | B_CLUSTEROK);
765 bp->b_ioflags |= BIO_ERROR;
771 * Only update dirtyoff/dirtyend if not a degenerate
775 if (bp->b_dirtyend > 0) {
776 bp->b_dirtyoff = MIN(on, bp->b_dirtyoff);
777 bp->b_dirtyend = MAX((on + n), bp->b_dirtyend);
780 bp->b_dirtyend = on + n;
782 vfs_bio_set_valid(bp, on, n);
787 } while (uio->uio_resid > 0 && n > 0);
793 fuse_io_strategy(struct vnode *vp, struct buf *bp)
795 struct fuse_filehandle *fufh;
803 /* We don't know the true pid when we're dealing with the cache */
806 const int biosize = fuse_iosize(vp);
808 MPASS(vp->v_type == VREG || vp->v_type == VDIR);
809 MPASS(bp->b_iocmd == BIO_READ || bp->b_iocmd == BIO_WRITE);
811 fflag = bp->b_iocmd == BIO_READ ? FREAD : FWRITE;
812 cred = bp->b_iocmd == BIO_READ ? bp->b_rcred : bp->b_wcred;
813 error = fuse_filehandle_getrw(vp, fflag, &fufh, cred, pid);
814 if (bp->b_iocmd == BIO_READ && error == EBADF) {
816 * This may be a read-modify-write operation on a cached file
817 * opened O_WRONLY. The FUSE protocol allows this.
819 error = fuse_filehandle_get(vp, FWRITE, &fufh, cred, pid);
822 printf("FUSE: strategy: filehandles are closed\n");
823 bp->b_ioflags |= BIO_ERROR;
831 uiop->uio_iovcnt = 1;
832 uiop->uio_segflg = UIO_SYSSPACE;
833 uiop->uio_td = curthread;
836 * clear BIO_ERROR and B_INVAL state prior to initiating the I/O. We
837 * do this here so we do not have to do it in all the code that
840 bp->b_flags &= ~B_INVAL;
841 bp->b_ioflags &= ~BIO_ERROR;
843 KASSERT(!(bp->b_flags & B_DONE),
844 ("fuse_io_strategy: bp %p already marked done", bp));
845 if (bp->b_iocmd == BIO_READ) {
846 io.iov_len = uiop->uio_resid = bp->b_bcount;
847 io.iov_base = bp->b_data;
848 uiop->uio_rw = UIO_READ;
850 uiop->uio_offset = ((off_t)bp->b_blkno) * biosize;
851 error = fuse_read_directbackend(vp, uiop, cred, fufh);
853 if (!error && uiop->uio_resid) {
855 * If we had a short read with no error, we must have
856 * hit a file hole. We should zero-fill the remainder.
857 * This can also occur if the server hits the file EOF.
859 * Holes used to be able to occur due to pending
860 * writes, but that is not possible any longer.
862 int nread = bp->b_bcount - uiop->uio_resid;
863 int left = uiop->uio_resid;
866 bzero((char *)bp->b_data + nread, left);
870 bp->b_ioflags |= BIO_ERROR;
875 * If we only need to commit, try to commit
877 if (bp->b_flags & B_NEEDCOMMIT) {
878 SDT_PROBE2(fusefs, , io, trace, 1,
879 "write: B_NEEDCOMMIT flags set");
882 * Setup for actual write
884 error = fuse_vnode_size(vp, &filesize, cred, curthread);
886 bp->b_ioflags |= BIO_ERROR;
892 if ((off_t)bp->b_blkno * biosize + bp->b_dirtyend > filesize)
893 bp->b_dirtyend = filesize -
894 (off_t)bp->b_blkno * biosize;
896 if (bp->b_dirtyend > bp->b_dirtyoff) {
897 io.iov_len = uiop->uio_resid = bp->b_dirtyend
899 uiop->uio_offset = (off_t)bp->b_blkno * biosize
901 io.iov_base = (char *)bp->b_data + bp->b_dirtyoff;
902 uiop->uio_rw = UIO_WRITE;
904 error = fuse_write_directbackend(vp, uiop, cred, fufh,
907 if (error == EINTR || error == ETIMEDOUT
908 || (!error && (bp->b_flags & B_NEEDCOMMIT))) {
910 bp->b_flags &= ~(B_INVAL | B_NOCACHE);
911 if ((bp->b_flags & B_PAGING) == 0) {
913 bp->b_flags &= ~B_DONE;
915 if ((error == EINTR || error == ETIMEDOUT) &&
916 (bp->b_flags & B_ASYNC) == 0)
917 bp->b_flags |= B_EINTR;
920 bp->b_ioflags |= BIO_ERROR;
921 bp->b_flags |= B_INVAL;
924 bp->b_dirtyoff = bp->b_dirtyend = 0;
932 bp->b_resid = uiop->uio_resid;
938 fuse_io_flushbuf(struct vnode *vp, int waitfor, struct thread *td)
941 return (vn_fsync_buf(vp, waitfor));
945 * Flush and invalidate all dirty buffers. If another process is already
946 * doing the flush, just wait for completion.
949 fuse_io_invalbuf(struct vnode *vp, struct thread *td)
951 struct fuse_vnode_data *fvdat = VTOFUD(vp);
954 if (vp->v_iflag & VI_DOOMED)
957 ASSERT_VOP_ELOCKED(vp, "fuse_io_invalbuf");
959 while (fvdat->flag & FN_FLUSHINPROG) {
960 struct proc *p = td->td_proc;
962 if (vp->v_mount->mnt_kern_flag & MNTK_UNMOUNTF)
964 fvdat->flag |= FN_FLUSHWANT;
965 tsleep(&fvdat->flag, PRIBIO + 2, "fusevinv", 2 * hz);
969 if (SIGNOTEMPTY(p->p_siglist) ||
970 SIGNOTEMPTY(td->td_siglist))
977 fvdat->flag |= FN_FLUSHINPROG;
979 if (vp->v_bufobj.bo_object != NULL) {
980 VM_OBJECT_WLOCK(vp->v_bufobj.bo_object);
981 vm_object_page_clean(vp->v_bufobj.bo_object, 0, 0, OBJPC_SYNC);
982 VM_OBJECT_WUNLOCK(vp->v_bufobj.bo_object);
984 error = vinvalbuf(vp, V_SAVE, PCATCH, 0);
986 if (error == ERESTART || error == EINTR) {
987 fvdat->flag &= ~FN_FLUSHINPROG;
988 if (fvdat->flag & FN_FLUSHWANT) {
989 fvdat->flag &= ~FN_FLUSHWANT;
990 wakeup(&fvdat->flag);
994 error = vinvalbuf(vp, V_SAVE, PCATCH, 0);
996 fvdat->flag &= ~FN_FLUSHINPROG;
997 if (fvdat->flag & FN_FLUSHWANT) {
998 fvdat->flag &= ~FN_FLUSHWANT;
999 wakeup(&fvdat->flag);