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>
87 #include <sys/vmmeter.h>
90 #include <vm/vm_extern.h>
92 #include <vm/vm_map.h>
93 #include <vm/vm_page.h>
94 #include <vm/vm_object.h>
97 #include "fuse_file.h"
98 #include "fuse_node.h"
99 #include "fuse_internal.h"
100 #include "fuse_ipc.h"
103 SDT_PROVIDER_DECLARE(fusefs);
106 * arg0: verbosity. Higher numbers give more verbose messages
107 * arg1: Textual message
109 SDT_PROBE_DEFINE2(fusefs, , io, trace, "int", "char*");
112 fuse_io_clear_suid_on_write(struct vnode *vp, struct ucred *cred,
115 fuse_read_directbackend(struct vnode *vp, struct uio *uio,
116 struct ucred *cred, struct fuse_filehandle *fufh);
118 fuse_read_biobackend(struct vnode *vp, struct uio *uio, int ioflag,
119 struct ucred *cred, struct fuse_filehandle *fufh, pid_t pid);
121 fuse_write_directbackend(struct vnode *vp, struct uio *uio,
122 struct ucred *cred, struct fuse_filehandle *fufh, off_t filesize,
123 int ioflag, bool pages);
125 fuse_write_biobackend(struct vnode *vp, struct uio *uio,
126 struct ucred *cred, struct fuse_filehandle *fufh, int ioflag, pid_t pid);
129 * FreeBSD clears the SUID and SGID bits on any write by a non-root user.
132 fuse_io_clear_suid_on_write(struct vnode *vp, struct ucred *cred,
135 struct fuse_data *data;
140 mp = vnode_mount(vp);
141 data = fuse_get_mpdata(mp);
142 dataflags = data->dataflags;
144 if (dataflags & FSESS_DEFAULT_PERMISSIONS) {
145 if (priv_check_cred(cred, PRIV_VFS_RETAINSUGID)) {
146 fuse_internal_getattr(vp, &va, cred, td);
147 if (va.va_mode & (S_ISUID | S_ISGID)) {
148 mode_t mode = va.va_mode & ~(S_ISUID | S_ISGID);
149 /* Clear all vattr fields except mode */
154 * Ignore fuse_internal_setattr's return value,
155 * because at this point the write operation has
156 * already succeeded and we don't want to return
157 * failing status for that.
159 (void)fuse_internal_setattr(vp, &va, td, NULL);
165 SDT_PROBE_DEFINE5(fusefs, , io, io_dispatch, "struct vnode*", "struct uio*",
166 "int", "struct ucred*", "struct fuse_filehandle*");
168 fuse_io_dispatch(struct vnode *vp, struct uio *uio, int ioflag, bool pages,
169 struct ucred *cred, pid_t pid)
171 struct fuse_filehandle *fufh;
174 bool closefufh = false;
176 MPASS(vp->v_type == VREG || vp->v_type == VDIR);
178 fflag = (uio->uio_rw == UIO_READ) ? FREAD : FWRITE;
179 err = fuse_filehandle_getrw(vp, fflag, &fufh, cred, pid);
180 if (err == EBADF && vnode_mount(vp)->mnt_flag & MNT_EXPORTED) {
182 * nfsd will do I/O without first doing VOP_OPEN. We
183 * must implicitly open the file here
185 err = fuse_filehandle_open(vp, fflag, &fufh, curthread, cred);
189 printf("FUSE: io dispatch: filehandles are closed\n");
194 SDT_PROBE5(fusefs, , io, io_dispatch, vp, uio, ioflag, cred, fufh);
197 * Ideally, when the daemon asks for direct io at open time, the
198 * standard file flag should be set according to this, so that would
199 * just change the default mode, which later on could be changed via
201 * But this doesn't work, the O_DIRECT flag gets cleared at some point
202 * (don't know where). So to make any use of the Fuse direct_io option,
203 * we hardwire it into the file's private data (similarly to Linux,
206 directio = (ioflag & IO_DIRECT) || !fsess_opt_datacache(vnode_mount(vp));
208 switch (uio->uio_rw) {
211 SDT_PROBE2(fusefs, , io, trace, 1,
212 "direct read of vnode");
213 err = fuse_read_directbackend(vp, uio, cred, fufh);
215 SDT_PROBE2(fusefs, , io, trace, 1,
216 "buffered read of vnode");
217 err = fuse_read_biobackend(vp, uio, ioflag, cred, fufh,
223 const int iosize = fuse_iosize(vp);
224 off_t start, end, filesize;
226 SDT_PROBE2(fusefs, , io, trace, 1,
227 "direct write of vnode");
229 err = fuse_vnode_size(vp, &filesize, cred, curthread);
233 start = uio->uio_offset;
234 end = start + uio->uio_resid;
236 * Invalidate the write cache unless we're coming from
237 * VOP_PUTPAGES, in which case we're writing _from_ the
241 v_inval_buf_range(vp, start, end, iosize);
242 err = fuse_write_directbackend(vp, uio, cred, fufh,
243 filesize, ioflag, pages);
245 SDT_PROBE2(fusefs, , io, trace, 1,
246 "buffered write of vnode");
247 if (fuse_data_cache_mode == FUSE_CACHE_WT)
249 err = fuse_write_biobackend(vp, uio, cred, fufh, ioflag,
252 fuse_io_clear_suid_on_write(vp, cred, uio->uio_td);
255 panic("uninterpreted mode passed to fuse_io_dispatch");
260 fuse_filehandle_close(vp, fufh, curthread, cred);
265 SDT_PROBE_DEFINE4(fusefs, , io, read_bio_backend_start, "int", "int", "int", "int");
266 SDT_PROBE_DEFINE2(fusefs, , io, read_bio_backend_feed, "int", "struct buf*");
267 SDT_PROBE_DEFINE4(fusefs, , io, read_bio_backend_end, "int", "ssize_t", "int",
270 fuse_read_biobackend(struct vnode *vp, struct uio *uio, int ioflag,
271 struct ucred *cred, struct fuse_filehandle *fufh, pid_t pid)
275 struct fuse_data *data;
276 daddr_t lbn, nextlbn;
277 int bcount, nextsize;
278 int err, n = 0, on = 0, seqcount;
281 const int biosize = fuse_iosize(vp);
282 mp = vnode_mount(vp);
283 data = fuse_get_mpdata(mp);
285 if (uio->uio_offset < 0)
288 seqcount = ioflag >> IO_SEQSHIFT;
290 err = fuse_vnode_size(vp, &filesize, cred, curthread);
294 for (err = 0, bp = NULL; uio->uio_resid > 0; bp = NULL) {
295 if (fuse_isdeadfs(vp)) {
299 if (filesize - uio->uio_offset <= 0)
301 lbn = uio->uio_offset / biosize;
302 on = uio->uio_offset & (biosize - 1);
304 if ((off_t)lbn * biosize >= filesize) {
306 } else if ((off_t)(lbn + 1) * biosize > filesize) {
307 bcount = filesize - (off_t)lbn *biosize;
312 nextsize = MIN(biosize, filesize - nextlbn * biosize);
314 SDT_PROBE4(fusefs, , io, read_bio_backend_start,
315 biosize, (int)lbn, on, bcount);
317 if (bcount < biosize) {
318 /* If near EOF, don't do readahead */
319 err = bread(vp, lbn, bcount, NOCRED, &bp);
320 /* TODO: clustered read */
321 } else if (seqcount > 1 && data->max_readahead >= nextsize) {
322 /* Try non-clustered readahead */
323 err = breadn(vp, lbn, bcount, &nextlbn, &nextsize, 1,
326 /* Just read what was requested */
327 err = bread(vp, lbn, bcount, NOCRED, &bp);
337 * on is the offset into the current bp. Figure out how many
338 * bytes we can copy out of the bp. Note that bcount is
339 * NOT DEV_BSIZE aligned.
341 * Then figure out how many bytes we can copy into the uio.
346 n = MIN((unsigned)(bcount - on), uio->uio_resid);
348 SDT_PROBE2(fusefs, , io, read_bio_backend_feed, n, bp);
349 err = uiomove(bp->b_data + on, n, uio);
351 vfs_bio_brelse(bp, ioflag);
352 SDT_PROBE4(fusefs, , io, read_bio_backend_end, err,
353 uio->uio_resid, n, bp);
359 SDT_PROBE_DEFINE1(fusefs, , io, read_directbackend_start,
360 "struct fuse_read_in*");
361 SDT_PROBE_DEFINE3(fusefs, , io, read_directbackend_complete,
362 "struct fuse_dispatcher*", "struct fuse_read_in*", "struct uio*");
365 fuse_read_directbackend(struct vnode *vp, struct uio *uio,
366 struct ucred *cred, struct fuse_filehandle *fufh)
368 struct fuse_data *data;
369 struct fuse_dispatcher fdi;
370 struct fuse_read_in *fri;
373 data = fuse_get_mpdata(vp->v_mount);
375 if (uio->uio_resid == 0)
381 * XXX In "normal" case we use an intermediate kernel buffer for
382 * transmitting data from daemon's context to ours. Eventually, we should
383 * get rid of this. Anyway, if the target uio lives in sysspace (we are
384 * called from pageops), and the input data doesn't need kernel-side
385 * processing (we are not called from readdir) we can already invoke
386 * an optimized, "peer-to-peer" I/O routine.
388 while (uio->uio_resid > 0) {
389 fdi.iosize = sizeof(*fri);
390 fdisp_make_vp(&fdi, FUSE_READ, vp, uio->uio_td, cred);
392 fri->fh = fufh->fh_id;
393 fri->offset = uio->uio_offset;
394 fri->size = MIN(uio->uio_resid,
395 fuse_get_mpdata(vp->v_mount)->max_read);
396 if (fuse_libabi_geq(data, 7, 9)) {
397 /* See comment regarding FUSE_WRITE_LOCKOWNER */
399 fri->flags = fufh_type_2_fflags(fufh->fufh_type);
402 SDT_PROBE1(fusefs, , io, read_directbackend_start, fri);
404 if ((err = fdisp_wait_answ(&fdi)))
407 SDT_PROBE3(fusefs, , io, read_directbackend_complete,
410 if ((err = uiomove(fdi.answ, MIN(fri->size, fdi.iosize), uio)))
412 if (fdi.iosize < fri->size)
422 fuse_write_directbackend(struct vnode *vp, struct uio *uio,
423 struct ucred *cred, struct fuse_filehandle *fufh, off_t filesize,
424 int ioflag, bool pages)
426 struct fuse_vnode_data *fvdat = VTOFUD(vp);
427 struct fuse_data *data;
428 struct fuse_write_in *fwi;
429 struct fuse_write_out *fwo;
430 struct fuse_dispatcher fdi;
433 off_t as_written_offset;
436 bool direct_io = fufh->fuse_open_flags & FOPEN_DIRECT_IO;
437 uint32_t write_flags;
439 data = fuse_get_mpdata(vp->v_mount);
442 * Don't set FUSE_WRITE_LOCKOWNER in write_flags. It can't be set
443 * accurately when using POSIX AIO, libfuse doesn't use it, and I'm not
444 * aware of any file systems that do. It was an attempt to add
445 * Linux-style mandatory locking to the FUSE protocol, but mandatory
446 * locking is deprecated even on Linux. See Linux commit
447 * f33321141b273d60cbb3a8f56a5489baad82ba5e .
450 * Set FUSE_WRITE_CACHE whenever we don't know the uid, gid, and/or pid
451 * that originated a write. For example when writing from the
452 * writeback cache. I don't know of a single file system that cares,
453 * but the protocol says we're supposed to do this.
455 write_flags = !pages && (
456 (ioflag & IO_DIRECT) ||
457 !fsess_opt_datacache(vnode_mount(vp)) ||
458 fuse_data_cache_mode != FUSE_CACHE_WB) ? 0 : FUSE_WRITE_CACHE;
460 if (uio->uio_resid == 0)
463 if (ioflag & IO_APPEND)
464 uio_setoffset(uio, filesize);
466 if (vn_rlimit_fsize(vp, uio, uio->uio_td))
471 while (uio->uio_resid > 0) {
472 chunksize = MIN(uio->uio_resid, data->max_write);
474 fdi.iosize = sizeof(*fwi) + chunksize;
475 fdisp_make_vp(&fdi, FUSE_WRITE, vp, uio->uio_td, cred);
478 fwi->fh = fufh->fh_id;
479 fwi->offset = uio->uio_offset;
480 fwi->size = chunksize;
481 fwi->write_flags = write_flags;
482 if (fuse_libabi_geq(data, 7, 9)) {
483 fwi->flags = fufh_type_2_fflags(fufh->fufh_type);
484 fwi_data = (char *)fdi.indata + sizeof(*fwi);
486 fwi_data = (char *)fdi.indata +
487 FUSE_COMPAT_WRITE_IN_SIZE;
490 if ((err = uiomove(fwi_data, chunksize, uio)))
494 err = fdisp_wait_answ(&fdi);
495 if (err == ERESTART || err == EINTR || err == EWOULDBLOCK) {
497 * Rewind the uio so dofilewrite will know it's
500 uio->uio_resid += fwi->size;
501 uio->uio_offset -= fwi->size;
503 * Change ERESTART into EINTR because we can't rewind
504 * uio->uio_iov. Basically, once uiomove(9) has been
505 * called, it's impossible to restart a syscall.
514 fwo = ((struct fuse_write_out *)fdi.answ);
516 /* Adjust the uio in the case of short writes */
517 diff = fwi->size - fwo->size;
518 as_written_offset = uio->uio_offset - diff;
520 if (as_written_offset - diff > filesize &&
521 fuse_data_cache_mode != FUSE_CACHE_UC)
522 fuse_vnode_setsize(vp, as_written_offset);
523 if (as_written_offset - diff >= filesize)
524 fvdat->flag &= ~FN_SIZECHANGE;
527 printf("WARNING: misbehaving FUSE filesystem "
528 "wrote more data than we provided it\n");
531 } else if (diff > 0) {
534 printf("WARNING: misbehaving FUSE filesystem: "
535 "short writes are only allowed with "
538 if (ioflag & IO_DIRECT) {
540 uio->uio_resid += diff;
541 uio->uio_offset -= diff;
544 /* Resend the unwritten portion of data */
545 fdi.iosize = sizeof(*fwi) + diff;
546 /* Refresh fdi without clearing data buffer */
547 fdisp_refresh_vp(&fdi, FUSE_WRITE, vp,
550 MPASS2(fwi == fdi.indata, "FUSE dispatcher "
551 "reallocated despite no increase in "
553 void *src = (char*)fwi_data + fwo->size;
554 memmove(fwi_data, src, diff);
555 fwi->fh = fufh->fh_id;
556 fwi->offset = as_written_offset;
558 fwi->write_flags = write_flags;
569 SDT_PROBE_DEFINE6(fusefs, , io, write_biobackend_start, "int64_t", "int", "int",
570 "struct uio*", "int", "bool");
571 SDT_PROBE_DEFINE2(fusefs, , io, write_biobackend_append_race, "long", "int");
572 SDT_PROBE_DEFINE2(fusefs, , io, write_biobackend_issue, "int", "struct buf*");
575 fuse_write_biobackend(struct vnode *vp, struct uio *uio,
576 struct ucred *cred, struct fuse_filehandle *fufh, int ioflag, pid_t pid)
578 struct fuse_vnode_data *fvdat = VTOFUD(vp);
583 int n, on, seqcount, err = 0;
586 const int biosize = fuse_iosize(vp);
588 seqcount = ioflag >> IO_SEQSHIFT;
590 KASSERT(uio->uio_rw == UIO_WRITE, ("fuse_write_biobackend mode"));
591 if (vp->v_type != VREG)
593 if (uio->uio_offset < 0)
595 if (uio->uio_resid == 0)
598 err = fuse_vnode_size(vp, &filesize, cred, curthread);
602 if (ioflag & IO_APPEND)
603 uio_setoffset(uio, filesize);
605 if (vn_rlimit_fsize(vp, uio, uio->uio_td))
609 bool direct_append, extending;
611 if (fuse_isdeadfs(vp)) {
615 lbn = uio->uio_offset / biosize;
616 on = uio->uio_offset & (biosize - 1);
617 n = MIN((unsigned)(biosize - on), uio->uio_resid);
620 /* Get or create a buffer for the write */
621 direct_append = uio->uio_offset == filesize && n;
622 if (uio->uio_offset + n < filesize) {
624 if ((off_t)(lbn + 1) * biosize < filesize) {
625 /* Not the file's last block */
628 /* The file's last block */
629 bcount = filesize - (off_t)lbn * biosize;
635 if (howmany(((off_t)lbn * biosize + on + n - 1), PAGE_SIZE) >=
636 howmany(filesize, PAGE_SIZE))
642 * Take care to preserve the buffer's B_CACHE state so
643 * as not to cause an unnecessary read.
645 bp = getblk(vp, lbn, on, PCATCH, 0, 0);
647 uint32_t save = bp->b_flags & B_CACHE;
648 allocbuf(bp, bcount);
652 bp = getblk(vp, lbn, bcount, PCATCH, 0, 0);
660 * Extend file _after_ locking buffer so we won't race
663 err = fuse_vnode_setsize(vp, uio->uio_offset + n);
664 filesize = uio->uio_offset + n;
665 fvdat->flag |= FN_SIZECHANGE;
672 SDT_PROBE6(fusefs, , io, write_biobackend_start,
673 lbn, on, n, uio, bcount, direct_append);
675 * Issue a READ if B_CACHE is not set. In special-append
676 * mode, B_CACHE is based on the buffer prior to the write
677 * op and is typically set, avoiding the read. If a read
678 * is required in special append mode, the server will
679 * probably send us a short-read since we extended the file
680 * on our end, resulting in b_resid == 0 and, thusly,
681 * B_CACHE getting set.
683 * We can also avoid issuing the read if the write covers
684 * the entire buffer. We have to make sure the buffer state
685 * is reasonable in this case since we will not be initiating
686 * I/O. See the comments in kern/vfs_bio.c's getblk() for
689 * B_CACHE may also be set due to the buffer being cached
693 if (on == 0 && n == bcount) {
694 bp->b_flags |= B_CACHE;
695 bp->b_flags &= ~B_INVAL;
696 bp->b_ioflags &= ~BIO_ERROR;
698 if ((bp->b_flags & B_CACHE) == 0) {
699 bp->b_iocmd = BIO_READ;
700 vfs_busy_pages(bp, 0);
701 fuse_io_strategy(vp, bp);
702 if ((err = bp->b_error)) {
707 if (bp->b_wcred == NOCRED)
708 bp->b_wcred = crhold(cred);
711 * If dirtyend exceeds file size, chop it down. This should
712 * not normally occur but there is an append race where it
713 * might occur XXX, so we log it.
715 * If the chopping creates a reverse-indexed or degenerate
716 * situation with dirtyoff/end, we 0 both of them.
718 if (bp->b_dirtyend > bcount) {
719 SDT_PROBE2(fusefs, , io, write_biobackend_append_race,
720 (long)bp->b_blkno * biosize,
721 bp->b_dirtyend - bcount);
722 bp->b_dirtyend = bcount;
724 if (bp->b_dirtyoff >= bp->b_dirtyend)
725 bp->b_dirtyoff = bp->b_dirtyend = 0;
728 * If the new write will leave a contiguous dirty
729 * area, just update the b_dirtyoff and b_dirtyend,
730 * otherwise force a write rpc of the old dirty area.
732 * While it is possible to merge discontiguous writes due to
733 * our having a B_CACHE buffer ( and thus valid read data
734 * for the hole), we don't because it could lead to
735 * significant cache coherency problems with multiple clients,
736 * especially if locking is implemented later on.
738 * as an optimization we could theoretically maintain
739 * a linked list of discontinuous areas, but we would still
740 * have to commit them separately so there isn't much
741 * advantage to it except perhaps a bit of asynchronization.
744 if (bp->b_dirtyend > 0 &&
745 (on > bp->b_dirtyend || (on + n) < bp->b_dirtyoff)) {
747 * Yes, we mean it. Write out everything to "storage"
748 * immediately, without hesitation. (Apart from other
749 * reasons: the only way to know if a write is valid
750 * if its actually written out.)
752 SDT_PROBE2(fusefs, , io, write_biobackend_issue, 0, bp);
754 if (bp->b_error == EINTR) {
760 err = uiomove((char *)bp->b_data + on, n, uio);
763 bp->b_ioflags |= BIO_ERROR;
767 /* TODO: vfs_bio_clrbuf like ffs_write does? */
770 * Only update dirtyoff/dirtyend if not a degenerate
774 if (bp->b_dirtyend > 0) {
775 bp->b_dirtyoff = MIN(on, bp->b_dirtyoff);
776 bp->b_dirtyend = MAX((on + n), bp->b_dirtyend);
779 bp->b_dirtyend = on + n;
781 vfs_bio_set_valid(bp, on, n);
784 vfs_bio_set_flags(bp, ioflag);
788 * When writing the last page of a file we must write
789 * synchronously. If we didn't, then a subsequent
790 * operation could extend the file, making the last
791 * page of this buffer invalid because it would only be
794 * As an optimization, it would be allowable to only
795 * write the last page synchronously. Or, it should be
796 * possible to synchronously flush the last
797 * already-written page whenever extending a file with
798 * ftruncate or another write.
800 SDT_PROBE2(fusefs, , io, write_biobackend_issue, 1, bp);
802 } else if (ioflag & IO_SYNC) {
803 SDT_PROBE2(fusefs, , io, write_biobackend_issue, 2, bp);
805 } else if (vm_page_count_severe() ||
806 buf_dirty_count_severe() ||
807 (ioflag & IO_ASYNC)) {
808 bp->b_flags |= B_CLUSTEROK;
809 SDT_PROBE2(fusefs, , io, write_biobackend_issue, 3, bp);
811 } else if (on == 0 && n == bcount) {
812 if ((vp->v_mount->mnt_flag & MNT_NOCLUSTERW) == 0) {
813 bp->b_flags |= B_CLUSTEROK;
814 SDT_PROBE2(fusefs, , io, write_biobackend_issue,
816 cluster_write(vp, bp, filesize, seqcount, 0);
818 SDT_PROBE2(fusefs, , io, write_biobackend_issue,
822 } else if (ioflag & IO_DIRECT) {
823 bp->b_flags |= B_CLUSTEROK;
824 SDT_PROBE2(fusefs, , io, write_biobackend_issue, 6, bp);
827 bp->b_flags &= ~B_CLUSTEROK;
828 SDT_PROBE2(fusefs, , io, write_biobackend_issue, 7, bp);
833 } while (uio->uio_resid > 0 && n > 0);
839 fuse_io_strategy(struct vnode *vp, struct buf *bp)
841 struct fuse_filehandle *fufh;
849 /* We don't know the true pid when we're dealing with the cache */
852 const int biosize = fuse_iosize(vp);
854 MPASS(vp->v_type == VREG || vp->v_type == VDIR);
855 MPASS(bp->b_iocmd == BIO_READ || bp->b_iocmd == BIO_WRITE);
857 fflag = bp->b_iocmd == BIO_READ ? FREAD : FWRITE;
858 cred = bp->b_iocmd == BIO_READ ? bp->b_rcred : bp->b_wcred;
859 error = fuse_filehandle_getrw(vp, fflag, &fufh, cred, pid);
860 if (bp->b_iocmd == BIO_READ && error == EBADF) {
862 * This may be a read-modify-write operation on a cached file
863 * opened O_WRONLY. The FUSE protocol allows this.
865 error = fuse_filehandle_get(vp, FWRITE, &fufh, cred, pid);
868 printf("FUSE: strategy: filehandles are closed\n");
869 bp->b_ioflags |= BIO_ERROR;
877 uiop->uio_iovcnt = 1;
878 uiop->uio_segflg = UIO_SYSSPACE;
879 uiop->uio_td = curthread;
882 * clear BIO_ERROR and B_INVAL state prior to initiating the I/O. We
883 * do this here so we do not have to do it in all the code that
886 bp->b_flags &= ~B_INVAL;
887 bp->b_ioflags &= ~BIO_ERROR;
889 KASSERT(!(bp->b_flags & B_DONE),
890 ("fuse_io_strategy: bp %p already marked done", bp));
891 if (bp->b_iocmd == BIO_READ) {
892 io.iov_len = uiop->uio_resid = bp->b_bcount;
893 io.iov_base = bp->b_data;
894 uiop->uio_rw = UIO_READ;
896 uiop->uio_offset = ((off_t)bp->b_lblkno) * biosize;
897 error = fuse_read_directbackend(vp, uiop, cred, fufh);
899 if (!error && uiop->uio_resid) {
901 * If we had a short read with no error, we must have
902 * hit a file hole. We should zero-fill the remainder.
903 * This can also occur if the server hits the file EOF.
905 * Holes used to be able to occur due to pending
906 * writes, but that is not possible any longer.
908 int nread = bp->b_bcount - uiop->uio_resid;
909 int left = uiop->uio_resid;
912 bzero((char *)bp->b_data + nread, left);
916 bp->b_ioflags |= BIO_ERROR;
921 * Setup for actual write
923 error = fuse_vnode_size(vp, &filesize, cred, curthread);
925 bp->b_ioflags |= BIO_ERROR;
931 if ((off_t)bp->b_lblkno * biosize + bp->b_dirtyend > filesize)
932 bp->b_dirtyend = filesize -
933 (off_t)bp->b_lblkno * biosize;
935 if (bp->b_dirtyend > bp->b_dirtyoff) {
936 io.iov_len = uiop->uio_resid = bp->b_dirtyend
938 uiop->uio_offset = (off_t)bp->b_lblkno * biosize
940 io.iov_base = (char *)bp->b_data + bp->b_dirtyoff;
941 uiop->uio_rw = UIO_WRITE;
943 error = fuse_write_directbackend(vp, uiop, cred, fufh,
946 if (error == EINTR || error == ETIMEDOUT) {
947 bp->b_flags &= ~(B_INVAL | B_NOCACHE);
948 if ((bp->b_flags & B_PAGING) == 0) {
950 bp->b_flags &= ~B_DONE;
952 if ((error == EINTR || error == ETIMEDOUT) &&
953 (bp->b_flags & B_ASYNC) == 0)
954 bp->b_flags |= B_EINTR;
957 bp->b_ioflags |= BIO_ERROR;
958 bp->b_flags |= B_INVAL;
961 bp->b_dirtyoff = bp->b_dirtyend = 0;
969 bp->b_resid = uiop->uio_resid;
975 fuse_io_flushbuf(struct vnode *vp, int waitfor, struct thread *td)
978 return (vn_fsync_buf(vp, waitfor));
982 * Flush and invalidate all dirty buffers. If another process is already
983 * doing the flush, just wait for completion.
986 fuse_io_invalbuf(struct vnode *vp, struct thread *td)
988 struct fuse_vnode_data *fvdat = VTOFUD(vp);
991 if (vp->v_iflag & VI_DOOMED)
994 ASSERT_VOP_ELOCKED(vp, "fuse_io_invalbuf");
996 while (fvdat->flag & FN_FLUSHINPROG) {
997 struct proc *p = td->td_proc;
999 if (vp->v_mount->mnt_kern_flag & MNTK_UNMOUNTF)
1001 fvdat->flag |= FN_FLUSHWANT;
1002 tsleep(&fvdat->flag, PRIBIO + 2, "fusevinv", 2 * hz);
1006 if (SIGNOTEMPTY(p->p_siglist) ||
1007 SIGNOTEMPTY(td->td_siglist))
1014 fvdat->flag |= FN_FLUSHINPROG;
1016 if (vp->v_bufobj.bo_object != NULL) {
1017 VM_OBJECT_WLOCK(vp->v_bufobj.bo_object);
1018 vm_object_page_clean(vp->v_bufobj.bo_object, 0, 0, OBJPC_SYNC);
1019 VM_OBJECT_WUNLOCK(vp->v_bufobj.bo_object);
1021 error = vinvalbuf(vp, V_SAVE, PCATCH, 0);
1023 if (error == ERESTART || error == EINTR) {
1024 fvdat->flag &= ~FN_FLUSHINPROG;
1025 if (fvdat->flag & FN_FLUSHWANT) {
1026 fvdat->flag &= ~FN_FLUSHWANT;
1027 wakeup(&fvdat->flag);
1031 error = vinvalbuf(vp, V_SAVE, PCATCH, 0);
1033 fvdat->flag &= ~FN_FLUSHINPROG;
1034 if (fvdat->flag & FN_FLUSHWANT) {
1035 fvdat->flag &= ~FN_FLUSHWANT;
1036 wakeup(&fvdat->flag);