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)
276 int err, n = 0, on = 0;
279 const int biosize = fuse_iosize(vp);
281 if (uio->uio_offset < 0)
284 err = fuse_vnode_size(vp, &filesize, cred, curthread);
288 for (err = 0, bp = NULL; uio->uio_resid > 0; bp = NULL) {
289 if (fuse_isdeadfs(vp)) {
293 if (filesize - uio->uio_offset <= 0)
295 lbn = uio->uio_offset / biosize;
296 on = uio->uio_offset & (biosize - 1);
298 if ((off_t)lbn * biosize >= filesize) {
300 } else if ((off_t)(lbn + 1) * biosize > filesize) {
301 bcount = filesize - (off_t)lbn *biosize;
306 SDT_PROBE4(fusefs, , io, read_bio_backend_start,
307 biosize, (int)lbn, on, bcount);
309 /* TODO: readahead. See ext2_read for an example */
310 err = bread(vp, lbn, bcount, NOCRED, &bp);
318 * on is the offset into the current bp. Figure out how many
319 * bytes we can copy out of the bp. Note that bcount is
320 * NOT DEV_BSIZE aligned.
322 * Then figure out how many bytes we can copy into the uio.
327 n = MIN((unsigned)(bcount - on), uio->uio_resid);
329 SDT_PROBE2(fusefs, , io, read_bio_backend_feed, n, bp);
330 err = uiomove(bp->b_data + on, n, uio);
332 vfs_bio_brelse(bp, ioflag);
333 SDT_PROBE4(fusefs, , io, read_bio_backend_end, err,
334 uio->uio_resid, n, bp);
340 SDT_PROBE_DEFINE1(fusefs, , io, read_directbackend_start,
341 "struct fuse_read_in*");
342 SDT_PROBE_DEFINE3(fusefs, , io, read_directbackend_complete,
343 "struct fuse_dispatcher*", "struct fuse_read_in*", "struct uio*");
346 fuse_read_directbackend(struct vnode *vp, struct uio *uio,
347 struct ucred *cred, struct fuse_filehandle *fufh)
349 struct fuse_data *data;
350 struct fuse_dispatcher fdi;
351 struct fuse_read_in *fri;
354 data = fuse_get_mpdata(vp->v_mount);
356 if (uio->uio_resid == 0)
362 * XXX In "normal" case we use an intermediate kernel buffer for
363 * transmitting data from daemon's context to ours. Eventually, we should
364 * get rid of this. Anyway, if the target uio lives in sysspace (we are
365 * called from pageops), and the input data doesn't need kernel-side
366 * processing (we are not called from readdir) we can already invoke
367 * an optimized, "peer-to-peer" I/O routine.
369 while (uio->uio_resid > 0) {
370 fdi.iosize = sizeof(*fri);
371 fdisp_make_vp(&fdi, FUSE_READ, vp, uio->uio_td, cred);
373 fri->fh = fufh->fh_id;
374 fri->offset = uio->uio_offset;
375 fri->size = MIN(uio->uio_resid,
376 fuse_get_mpdata(vp->v_mount)->max_read);
377 if (fuse_libabi_geq(data, 7, 9)) {
378 /* See comment regarding FUSE_WRITE_LOCKOWNER */
380 fri->flags = fufh_type_2_fflags(fufh->fufh_type);
383 SDT_PROBE1(fusefs, , io, read_directbackend_start, fri);
385 if ((err = fdisp_wait_answ(&fdi)))
388 SDT_PROBE3(fusefs, , io, read_directbackend_complete,
391 if ((err = uiomove(fdi.answ, MIN(fri->size, fdi.iosize), uio)))
393 if (fdi.iosize < fri->size)
403 fuse_write_directbackend(struct vnode *vp, struct uio *uio,
404 struct ucred *cred, struct fuse_filehandle *fufh, off_t filesize,
405 int ioflag, bool pages)
407 struct fuse_vnode_data *fvdat = VTOFUD(vp);
408 struct fuse_data *data;
409 struct fuse_write_in *fwi;
410 struct fuse_write_out *fwo;
411 struct fuse_dispatcher fdi;
414 off_t as_written_offset;
417 bool direct_io = fufh->fuse_open_flags & FOPEN_DIRECT_IO;
418 uint32_t write_flags;
420 data = fuse_get_mpdata(vp->v_mount);
423 * Don't set FUSE_WRITE_LOCKOWNER in write_flags. It can't be set
424 * accurately when using POSIX AIO, libfuse doesn't use it, and I'm not
425 * aware of any file systems that do. It was an attempt to add
426 * Linux-style mandatory locking to the FUSE protocol, but mandatory
427 * locking is deprecated even on Linux. See Linux commit
428 * f33321141b273d60cbb3a8f56a5489baad82ba5e .
431 * Set FUSE_WRITE_CACHE whenever we don't know the uid, gid, and/or pid
432 * that originated a write. For example when writing from the
433 * writeback cache. I don't know of a single file system that cares,
434 * but the protocol says we're supposed to do this.
436 write_flags = !pages && (
437 (ioflag & IO_DIRECT) ||
438 !fsess_opt_datacache(vnode_mount(vp)) ||
439 fuse_data_cache_mode != FUSE_CACHE_WB) ? 0 : FUSE_WRITE_CACHE;
441 if (uio->uio_resid == 0)
444 if (ioflag & IO_APPEND)
445 uio_setoffset(uio, filesize);
447 if (vn_rlimit_fsize(vp, uio, uio->uio_td))
452 while (uio->uio_resid > 0) {
453 chunksize = MIN(uio->uio_resid, data->max_write);
455 fdi.iosize = sizeof(*fwi) + chunksize;
456 fdisp_make_vp(&fdi, FUSE_WRITE, vp, uio->uio_td, cred);
459 fwi->fh = fufh->fh_id;
460 fwi->offset = uio->uio_offset;
461 fwi->size = chunksize;
462 fwi->write_flags = write_flags;
463 if (fuse_libabi_geq(data, 7, 9)) {
464 fwi->flags = fufh_type_2_fflags(fufh->fufh_type);
465 fwi_data = (char *)fdi.indata + sizeof(*fwi);
467 fwi_data = (char *)fdi.indata +
468 FUSE_COMPAT_WRITE_IN_SIZE;
471 if ((err = uiomove(fwi_data, chunksize, uio)))
475 err = fdisp_wait_answ(&fdi);
476 if (err == ERESTART || err == EINTR || err == EWOULDBLOCK) {
478 * Rewind the uio so dofilewrite will know it's
481 uio->uio_resid += fwi->size;
482 uio->uio_offset -= fwi->size;
484 * Change ERESTART into EINTR because we can't rewind
485 * uio->uio_iov. Basically, once uiomove(9) has been
486 * called, it's impossible to restart a syscall.
495 fwo = ((struct fuse_write_out *)fdi.answ);
497 /* Adjust the uio in the case of short writes */
498 diff = fwi->size - fwo->size;
499 as_written_offset = uio->uio_offset - diff;
501 if (as_written_offset - diff > filesize &&
502 fuse_data_cache_mode != FUSE_CACHE_UC)
503 fuse_vnode_setsize(vp, as_written_offset);
504 if (as_written_offset - diff >= filesize)
505 fvdat->flag &= ~FN_SIZECHANGE;
508 printf("WARNING: misbehaving FUSE filesystem "
509 "wrote more data than we provided it\n");
512 } else if (diff > 0) {
515 printf("WARNING: misbehaving FUSE filesystem: "
516 "short writes are only allowed with "
519 if (ioflag & IO_DIRECT) {
521 uio->uio_resid += diff;
522 uio->uio_offset -= diff;
525 /* Resend the unwritten portion of data */
526 fdi.iosize = sizeof(*fwi) + diff;
527 /* Refresh fdi without clearing data buffer */
528 fdisp_refresh_vp(&fdi, FUSE_WRITE, vp,
531 MPASS2(fwi == fdi.indata, "FUSE dispatcher "
532 "reallocated despite no increase in "
534 void *src = (char*)fwi_data + fwo->size;
535 memmove(fwi_data, src, diff);
536 fwi->fh = fufh->fh_id;
537 fwi->offset = as_written_offset;
539 fwi->write_flags = write_flags;
550 SDT_PROBE_DEFINE6(fusefs, , io, write_biobackend_start, "int64_t", "int", "int",
551 "struct uio*", "int", "bool");
552 SDT_PROBE_DEFINE2(fusefs, , io, write_biobackend_append_race, "long", "int");
553 SDT_PROBE_DEFINE2(fusefs, , io, write_biobackend_issue, "int", "struct buf*");
556 fuse_write_biobackend(struct vnode *vp, struct uio *uio,
557 struct ucred *cred, struct fuse_filehandle *fufh, int ioflag, pid_t pid)
559 struct fuse_vnode_data *fvdat = VTOFUD(vp);
564 int n, on, seqcount, err = 0;
567 const int biosize = fuse_iosize(vp);
569 seqcount = ioflag >> IO_SEQSHIFT;
571 KASSERT(uio->uio_rw == UIO_WRITE, ("fuse_write_biobackend mode"));
572 if (vp->v_type != VREG)
574 if (uio->uio_offset < 0)
576 if (uio->uio_resid == 0)
579 err = fuse_vnode_size(vp, &filesize, cred, curthread);
583 if (ioflag & IO_APPEND)
584 uio_setoffset(uio, filesize);
586 if (vn_rlimit_fsize(vp, uio, uio->uio_td))
590 bool direct_append, extending;
592 if (fuse_isdeadfs(vp)) {
596 lbn = uio->uio_offset / biosize;
597 on = uio->uio_offset & (biosize - 1);
598 n = MIN((unsigned)(biosize - on), uio->uio_resid);
601 /* Get or create a buffer for the write */
602 direct_append = uio->uio_offset == filesize && n;
603 if (uio->uio_offset + n < filesize) {
605 if ((off_t)(lbn + 1) * biosize < filesize) {
606 /* Not the file's last block */
609 /* The file's last block */
610 bcount = filesize - (off_t)lbn * biosize;
616 if (howmany(((off_t)lbn * biosize + on + n - 1), PAGE_SIZE) >=
617 howmany(filesize, PAGE_SIZE))
623 * Take care to preserve the buffer's B_CACHE state so
624 * as not to cause an unnecessary read.
626 bp = getblk(vp, lbn, on, PCATCH, 0, 0);
628 uint32_t save = bp->b_flags & B_CACHE;
629 allocbuf(bp, bcount);
633 bp = getblk(vp, lbn, bcount, PCATCH, 0, 0);
641 * Extend file _after_ locking buffer so we won't race
644 err = fuse_vnode_setsize(vp, uio->uio_offset + n);
645 filesize = uio->uio_offset + n;
646 fvdat->flag |= FN_SIZECHANGE;
653 SDT_PROBE6(fusefs, , io, write_biobackend_start,
654 lbn, on, n, uio, bcount, direct_append);
656 * Issue a READ if B_CACHE is not set. In special-append
657 * mode, B_CACHE is based on the buffer prior to the write
658 * op and is typically set, avoiding the read. If a read
659 * is required in special append mode, the server will
660 * probably send us a short-read since we extended the file
661 * on our end, resulting in b_resid == 0 and, thusly,
662 * B_CACHE getting set.
664 * We can also avoid issuing the read if the write covers
665 * the entire buffer. We have to make sure the buffer state
666 * is reasonable in this case since we will not be initiating
667 * I/O. See the comments in kern/vfs_bio.c's getblk() for
670 * B_CACHE may also be set due to the buffer being cached
674 if (on == 0 && n == bcount) {
675 bp->b_flags |= B_CACHE;
676 bp->b_flags &= ~B_INVAL;
677 bp->b_ioflags &= ~BIO_ERROR;
679 if ((bp->b_flags & B_CACHE) == 0) {
680 bp->b_iocmd = BIO_READ;
681 vfs_busy_pages(bp, 0);
682 fuse_io_strategy(vp, bp);
683 if ((err = bp->b_error)) {
688 if (bp->b_wcred == NOCRED)
689 bp->b_wcred = crhold(cred);
692 * If dirtyend exceeds file size, chop it down. This should
693 * not normally occur but there is an append race where it
694 * might occur XXX, so we log it.
696 * If the chopping creates a reverse-indexed or degenerate
697 * situation with dirtyoff/end, we 0 both of them.
699 if (bp->b_dirtyend > bcount) {
700 SDT_PROBE2(fusefs, , io, write_biobackend_append_race,
701 (long)bp->b_blkno * biosize,
702 bp->b_dirtyend - bcount);
703 bp->b_dirtyend = bcount;
705 if (bp->b_dirtyoff >= bp->b_dirtyend)
706 bp->b_dirtyoff = bp->b_dirtyend = 0;
709 * If the new write will leave a contiguous dirty
710 * area, just update the b_dirtyoff and b_dirtyend,
711 * otherwise force a write rpc of the old dirty area.
713 * While it is possible to merge discontiguous writes due to
714 * our having a B_CACHE buffer ( and thus valid read data
715 * for the hole), we don't because it could lead to
716 * significant cache coherency problems with multiple clients,
717 * especially if locking is implemented later on.
719 * as an optimization we could theoretically maintain
720 * a linked list of discontinuous areas, but we would still
721 * have to commit them separately so there isn't much
722 * advantage to it except perhaps a bit of asynchronization.
725 if (bp->b_dirtyend > 0 &&
726 (on > bp->b_dirtyend || (on + n) < bp->b_dirtyoff)) {
728 * Yes, we mean it. Write out everything to "storage"
729 * immediately, without hesitation. (Apart from other
730 * reasons: the only way to know if a write is valid
731 * if its actually written out.)
733 SDT_PROBE2(fusefs, , io, write_biobackend_issue, 0, bp);
735 if (bp->b_error == EINTR) {
741 err = uiomove((char *)bp->b_data + on, n, uio);
744 bp->b_ioflags |= BIO_ERROR;
748 /* TODO: vfs_bio_clrbuf like ffs_write does? */
751 * Only update dirtyoff/dirtyend if not a degenerate
755 if (bp->b_dirtyend > 0) {
756 bp->b_dirtyoff = MIN(on, bp->b_dirtyoff);
757 bp->b_dirtyend = MAX((on + n), bp->b_dirtyend);
760 bp->b_dirtyend = on + n;
762 vfs_bio_set_valid(bp, on, n);
765 vfs_bio_set_flags(bp, ioflag);
769 * When writing the last page of a file we must write
770 * synchronously. If we didn't, then a subsequent
771 * operation could extend the file, making the last
772 * page of this buffer invalid because it would only be
775 * As an optimization, it would be allowable to only
776 * write the last page synchronously. Or, it should be
777 * possible to synchronously flush the last
778 * already-written page whenever extending a file with
779 * ftruncate or another write.
781 SDT_PROBE2(fusefs, , io, write_biobackend_issue, 1, bp);
783 } else if (ioflag & IO_SYNC) {
784 SDT_PROBE2(fusefs, , io, write_biobackend_issue, 2, bp);
786 } else if (vm_page_count_severe() ||
787 buf_dirty_count_severe() ||
788 (ioflag & IO_ASYNC)) {
789 bp->b_flags |= B_CLUSTEROK;
790 SDT_PROBE2(fusefs, , io, write_biobackend_issue, 3, bp);
792 } else if (on == 0 && n == bcount) {
793 if ((vp->v_mount->mnt_flag & MNT_NOCLUSTERW) == 0) {
794 bp->b_flags |= B_CLUSTEROK;
795 SDT_PROBE2(fusefs, , io, write_biobackend_issue,
797 cluster_write(vp, bp, filesize, seqcount, 0);
799 SDT_PROBE2(fusefs, , io, write_biobackend_issue,
803 } else if (ioflag & IO_DIRECT) {
804 bp->b_flags |= B_CLUSTEROK;
805 SDT_PROBE2(fusefs, , io, write_biobackend_issue, 6, bp);
808 bp->b_flags &= ~B_CLUSTEROK;
809 SDT_PROBE2(fusefs, , io, write_biobackend_issue, 7, bp);
814 } while (uio->uio_resid > 0 && n > 0);
820 fuse_io_strategy(struct vnode *vp, struct buf *bp)
822 struct fuse_filehandle *fufh;
830 /* We don't know the true pid when we're dealing with the cache */
833 const int biosize = fuse_iosize(vp);
835 MPASS(vp->v_type == VREG || vp->v_type == VDIR);
836 MPASS(bp->b_iocmd == BIO_READ || bp->b_iocmd == BIO_WRITE);
838 fflag = bp->b_iocmd == BIO_READ ? FREAD : FWRITE;
839 cred = bp->b_iocmd == BIO_READ ? bp->b_rcred : bp->b_wcred;
840 error = fuse_filehandle_getrw(vp, fflag, &fufh, cred, pid);
841 if (bp->b_iocmd == BIO_READ && error == EBADF) {
843 * This may be a read-modify-write operation on a cached file
844 * opened O_WRONLY. The FUSE protocol allows this.
846 error = fuse_filehandle_get(vp, FWRITE, &fufh, cred, pid);
849 printf("FUSE: strategy: filehandles are closed\n");
850 bp->b_ioflags |= BIO_ERROR;
858 uiop->uio_iovcnt = 1;
859 uiop->uio_segflg = UIO_SYSSPACE;
860 uiop->uio_td = curthread;
863 * clear BIO_ERROR and B_INVAL state prior to initiating the I/O. We
864 * do this here so we do not have to do it in all the code that
867 bp->b_flags &= ~B_INVAL;
868 bp->b_ioflags &= ~BIO_ERROR;
870 KASSERT(!(bp->b_flags & B_DONE),
871 ("fuse_io_strategy: bp %p already marked done", bp));
872 if (bp->b_iocmd == BIO_READ) {
873 io.iov_len = uiop->uio_resid = bp->b_bcount;
874 io.iov_base = bp->b_data;
875 uiop->uio_rw = UIO_READ;
877 uiop->uio_offset = ((off_t)bp->b_lblkno) * biosize;
878 error = fuse_read_directbackend(vp, uiop, cred, fufh);
880 if (!error && uiop->uio_resid) {
882 * If we had a short read with no error, we must have
883 * hit a file hole. We should zero-fill the remainder.
884 * This can also occur if the server hits the file EOF.
886 * Holes used to be able to occur due to pending
887 * writes, but that is not possible any longer.
889 int nread = bp->b_bcount - uiop->uio_resid;
890 int left = uiop->uio_resid;
893 bzero((char *)bp->b_data + nread, left);
897 bp->b_ioflags |= BIO_ERROR;
902 * Setup for actual write
904 error = fuse_vnode_size(vp, &filesize, cred, curthread);
906 bp->b_ioflags |= BIO_ERROR;
912 if ((off_t)bp->b_lblkno * biosize + bp->b_dirtyend > filesize)
913 bp->b_dirtyend = filesize -
914 (off_t)bp->b_lblkno * biosize;
916 if (bp->b_dirtyend > bp->b_dirtyoff) {
917 io.iov_len = uiop->uio_resid = bp->b_dirtyend
919 uiop->uio_offset = (off_t)bp->b_lblkno * biosize
921 io.iov_base = (char *)bp->b_data + bp->b_dirtyoff;
922 uiop->uio_rw = UIO_WRITE;
924 error = fuse_write_directbackend(vp, uiop, cred, fufh,
927 if (error == EINTR || error == ETIMEDOUT) {
928 bp->b_flags &= ~(B_INVAL | B_NOCACHE);
929 if ((bp->b_flags & B_PAGING) == 0) {
931 bp->b_flags &= ~B_DONE;
933 if ((error == EINTR || error == ETIMEDOUT) &&
934 (bp->b_flags & B_ASYNC) == 0)
935 bp->b_flags |= B_EINTR;
938 bp->b_ioflags |= BIO_ERROR;
939 bp->b_flags |= B_INVAL;
942 bp->b_dirtyoff = bp->b_dirtyend = 0;
950 bp->b_resid = uiop->uio_resid;
956 fuse_io_flushbuf(struct vnode *vp, int waitfor, struct thread *td)
959 return (vn_fsync_buf(vp, waitfor));
963 * Flush and invalidate all dirty buffers. If another process is already
964 * doing the flush, just wait for completion.
967 fuse_io_invalbuf(struct vnode *vp, struct thread *td)
969 struct fuse_vnode_data *fvdat = VTOFUD(vp);
972 if (vp->v_iflag & VI_DOOMED)
975 ASSERT_VOP_ELOCKED(vp, "fuse_io_invalbuf");
977 while (fvdat->flag & FN_FLUSHINPROG) {
978 struct proc *p = td->td_proc;
980 if (vp->v_mount->mnt_kern_flag & MNTK_UNMOUNTF)
982 fvdat->flag |= FN_FLUSHWANT;
983 tsleep(&fvdat->flag, PRIBIO + 2, "fusevinv", 2 * hz);
987 if (SIGNOTEMPTY(p->p_siglist) ||
988 SIGNOTEMPTY(td->td_siglist))
995 fvdat->flag |= FN_FLUSHINPROG;
997 if (vp->v_bufobj.bo_object != NULL) {
998 VM_OBJECT_WLOCK(vp->v_bufobj.bo_object);
999 vm_object_page_clean(vp->v_bufobj.bo_object, 0, 0, OBJPC_SYNC);
1000 VM_OBJECT_WUNLOCK(vp->v_bufobj.bo_object);
1002 error = vinvalbuf(vp, V_SAVE, PCATCH, 0);
1004 if (error == ERESTART || error == EINTR) {
1005 fvdat->flag &= ~FN_FLUSHINPROG;
1006 if (fvdat->flag & FN_FLUSHWANT) {
1007 fvdat->flag &= ~FN_FLUSHWANT;
1008 wakeup(&fvdat->flag);
1012 error = vinvalbuf(vp, V_SAVE, PCATCH, 0);
1014 fvdat->flag &= ~FN_FLUSHINPROG;
1015 if (fvdat->flag & FN_FLUSHWANT) {
1016 fvdat->flag &= ~FN_FLUSHWANT;
1017 wakeup(&fvdat->flag);