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*");
167 SDT_PROBE_DEFINE5(fusefs, , io, io_dispatch_filehandles_closed, "struct vnode*",
168 "struct uio*", "int", "bool", "struct ucred*");
170 fuse_io_dispatch(struct vnode *vp, struct uio *uio, int ioflag, bool pages,
171 struct ucred *cred, pid_t pid)
173 struct fuse_filehandle *fufh;
176 bool closefufh = false;
178 MPASS(vp->v_type == VREG || vp->v_type == VDIR);
180 fflag = (uio->uio_rw == UIO_READ) ? FREAD : FWRITE;
181 err = fuse_filehandle_getrw(vp, fflag, &fufh, cred, pid);
182 if (err == EBADF && vnode_mount(vp)->mnt_flag & MNT_EXPORTED) {
184 * nfsd will do I/O without first doing VOP_OPEN. We
185 * must implicitly open the file here
187 err = fuse_filehandle_open(vp, fflag, &fufh, curthread, cred);
191 SDT_PROBE5(fusefs, , io, io_dispatch_filehandles_closed,
192 vp, uio, ioflag, pages, cred);
193 printf("FUSE: io dispatch: filehandles are closed\n");
198 SDT_PROBE5(fusefs, , io, io_dispatch, vp, uio, ioflag, cred, fufh);
201 * Ideally, when the daemon asks for direct io at open time, the
202 * standard file flag should be set according to this, so that would
203 * just change the default mode, which later on could be changed via
205 * But this doesn't work, the O_DIRECT flag gets cleared at some point
206 * (don't know where). So to make any use of the Fuse direct_io option,
207 * we hardwire it into the file's private data (similarly to Linux,
210 directio = (ioflag & IO_DIRECT) || !fsess_opt_datacache(vnode_mount(vp));
212 switch (uio->uio_rw) {
215 SDT_PROBE2(fusefs, , io, trace, 1,
216 "direct read of vnode");
217 err = fuse_read_directbackend(vp, uio, cred, fufh);
219 SDT_PROBE2(fusefs, , io, trace, 1,
220 "buffered read of vnode");
221 err = fuse_read_biobackend(vp, uio, ioflag, cred, fufh,
227 const int iosize = fuse_iosize(vp);
228 off_t start, end, filesize;
230 SDT_PROBE2(fusefs, , io, trace, 1,
231 "direct write of vnode");
233 err = fuse_vnode_size(vp, &filesize, cred, curthread);
237 start = uio->uio_offset;
238 end = start + uio->uio_resid;
240 * Invalidate the write cache unless we're coming from
241 * VOP_PUTPAGES, in which case we're writing _from_ the
245 v_inval_buf_range(vp, start, end, iosize);
246 err = fuse_write_directbackend(vp, uio, cred, fufh,
247 filesize, ioflag, pages);
249 SDT_PROBE2(fusefs, , io, trace, 1,
250 "buffered write of vnode");
251 if (fuse_data_cache_mode == FUSE_CACHE_WT)
253 err = fuse_write_biobackend(vp, uio, cred, fufh, ioflag,
256 fuse_io_clear_suid_on_write(vp, cred, uio->uio_td);
259 panic("uninterpreted mode passed to fuse_io_dispatch");
264 fuse_filehandle_close(vp, fufh, curthread, cred);
269 SDT_PROBE_DEFINE4(fusefs, , io, read_bio_backend_start, "int", "int", "int", "int");
270 SDT_PROBE_DEFINE2(fusefs, , io, read_bio_backend_feed, "int", "struct buf*");
271 SDT_PROBE_DEFINE4(fusefs, , io, read_bio_backend_end, "int", "ssize_t", "int",
274 fuse_read_biobackend(struct vnode *vp, struct uio *uio, int ioflag,
275 struct ucred *cred, struct fuse_filehandle *fufh, pid_t pid)
279 struct fuse_data *data;
280 daddr_t lbn, nextlbn;
281 int bcount, nextsize;
282 int err, n = 0, on = 0, seqcount;
285 const int biosize = fuse_iosize(vp);
286 mp = vnode_mount(vp);
287 data = fuse_get_mpdata(mp);
289 if (uio->uio_offset < 0)
292 seqcount = ioflag >> IO_SEQSHIFT;
294 err = fuse_vnode_size(vp, &filesize, cred, curthread);
298 for (err = 0, bp = NULL; uio->uio_resid > 0; bp = NULL) {
299 if (fuse_isdeadfs(vp)) {
303 if (filesize - uio->uio_offset <= 0)
305 lbn = uio->uio_offset / biosize;
306 on = uio->uio_offset & (biosize - 1);
308 if ((off_t)lbn * biosize >= filesize) {
310 } else if ((off_t)(lbn + 1) * biosize > filesize) {
311 bcount = filesize - (off_t)lbn *biosize;
316 nextsize = MIN(biosize, filesize - nextlbn * biosize);
318 SDT_PROBE4(fusefs, , io, read_bio_backend_start,
319 biosize, (int)lbn, on, bcount);
321 if (bcount < biosize) {
322 /* If near EOF, don't do readahead */
323 err = bread(vp, lbn, bcount, NOCRED, &bp);
324 } else if ((vp->v_mount->mnt_flag & MNT_NOCLUSTERR) == 0) {
325 /* Try clustered read */
326 long totread = uio->uio_resid + on;
327 seqcount = MIN(seqcount,
328 data->max_readahead_blocks + 1);
329 err = cluster_read(vp, filesize, lbn, bcount, NOCRED,
330 totread, seqcount, 0, &bp);
331 } else if (seqcount > 1 && data->max_readahead_blocks >= 1) {
332 /* Try non-clustered readahead */
333 err = breadn(vp, lbn, bcount, &nextlbn, &nextsize, 1,
336 /* Just read what was requested */
337 err = bread(vp, lbn, bcount, NOCRED, &bp);
347 * on is the offset into the current bp. Figure out how many
348 * bytes we can copy out of the bp. Note that bcount is
349 * NOT DEV_BSIZE aligned.
351 * Then figure out how many bytes we can copy into the uio.
355 if (on < bcount - (intptr_t)bp->b_fsprivate1)
356 n = MIN((unsigned)(bcount - (intptr_t)bp->b_fsprivate1 - on),
359 SDT_PROBE2(fusefs, , io, read_bio_backend_feed, n, bp);
360 err = uiomove(bp->b_data + on, n, uio);
362 vfs_bio_brelse(bp, ioflag);
363 SDT_PROBE4(fusefs, , io, read_bio_backend_end, err,
364 uio->uio_resid, n, bp);
365 if ((intptr_t)bp->b_fsprivate1 > 0) {
366 /* Short read indicates EOF */
367 (void)fuse_vnode_setsize(vp, uio->uio_offset);
368 bp->b_fsprivate1 = (void*)0;
376 SDT_PROBE_DEFINE1(fusefs, , io, read_directbackend_start,
377 "struct fuse_read_in*");
378 SDT_PROBE_DEFINE3(fusefs, , io, read_directbackend_complete,
379 "struct fuse_dispatcher*", "struct fuse_read_in*", "struct uio*");
382 fuse_read_directbackend(struct vnode *vp, struct uio *uio,
383 struct ucred *cred, struct fuse_filehandle *fufh)
385 struct fuse_data *data;
386 struct fuse_dispatcher fdi;
387 struct fuse_read_in *fri;
390 data = fuse_get_mpdata(vp->v_mount);
392 if (uio->uio_resid == 0)
398 * XXX In "normal" case we use an intermediate kernel buffer for
399 * transmitting data from daemon's context to ours. Eventually, we should
400 * get rid of this. Anyway, if the target uio lives in sysspace (we are
401 * called from pageops), and the input data doesn't need kernel-side
402 * processing (we are not called from readdir) we can already invoke
403 * an optimized, "peer-to-peer" I/O routine.
405 while (uio->uio_resid > 0) {
406 fdi.iosize = sizeof(*fri);
407 fdisp_make_vp(&fdi, FUSE_READ, vp, uio->uio_td, cred);
409 fri->fh = fufh->fh_id;
410 fri->offset = uio->uio_offset;
411 fri->size = MIN(uio->uio_resid,
412 fuse_get_mpdata(vp->v_mount)->max_read);
413 if (fuse_libabi_geq(data, 7, 9)) {
414 /* See comment regarding FUSE_WRITE_LOCKOWNER */
416 fri->flags = fufh_type_2_fflags(fufh->fufh_type);
419 SDT_PROBE1(fusefs, , io, read_directbackend_start, fri);
421 if ((err = fdisp_wait_answ(&fdi)))
424 SDT_PROBE3(fusefs, , io, read_directbackend_complete,
427 if ((err = uiomove(fdi.answ, MIN(fri->size, fdi.iosize), uio)))
429 if (fdi.iosize < fri->size) {
431 * Short read. Should only happen at EOF or with
444 fuse_write_directbackend(struct vnode *vp, struct uio *uio,
445 struct ucred *cred, struct fuse_filehandle *fufh, off_t filesize,
446 int ioflag, bool pages)
448 struct fuse_vnode_data *fvdat = VTOFUD(vp);
449 struct fuse_data *data;
450 struct fuse_write_in *fwi;
451 struct fuse_write_out *fwo;
452 struct fuse_dispatcher fdi;
455 off_t as_written_offset;
458 bool direct_io = fufh->fuse_open_flags & FOPEN_DIRECT_IO;
459 uint32_t write_flags;
461 data = fuse_get_mpdata(vp->v_mount);
464 * Don't set FUSE_WRITE_LOCKOWNER in write_flags. It can't be set
465 * accurately when using POSIX AIO, libfuse doesn't use it, and I'm not
466 * aware of any file systems that do. It was an attempt to add
467 * Linux-style mandatory locking to the FUSE protocol, but mandatory
468 * locking is deprecated even on Linux. See Linux commit
469 * f33321141b273d60cbb3a8f56a5489baad82ba5e .
472 * Set FUSE_WRITE_CACHE whenever we don't know the uid, gid, and/or pid
473 * that originated a write. For example when writing from the
474 * writeback cache. I don't know of a single file system that cares,
475 * but the protocol says we're supposed to do this.
477 write_flags = !pages && (
478 (ioflag & IO_DIRECT) ||
479 !fsess_opt_datacache(vnode_mount(vp)) ||
480 fuse_data_cache_mode != FUSE_CACHE_WB) ? 0 : FUSE_WRITE_CACHE;
482 if (uio->uio_resid == 0)
485 if (ioflag & IO_APPEND)
486 uio_setoffset(uio, filesize);
488 if (vn_rlimit_fsize(vp, uio, uio->uio_td))
493 while (uio->uio_resid > 0) {
494 chunksize = MIN(uio->uio_resid, data->max_write);
496 fdi.iosize = sizeof(*fwi) + chunksize;
497 fdisp_make_vp(&fdi, FUSE_WRITE, vp, uio->uio_td, cred);
500 fwi->fh = fufh->fh_id;
501 fwi->offset = uio->uio_offset;
502 fwi->size = chunksize;
503 fwi->write_flags = write_flags;
504 if (fuse_libabi_geq(data, 7, 9)) {
505 fwi->flags = fufh_type_2_fflags(fufh->fufh_type);
506 fwi_data = (char *)fdi.indata + sizeof(*fwi);
508 fwi_data = (char *)fdi.indata +
509 FUSE_COMPAT_WRITE_IN_SIZE;
512 if ((err = uiomove(fwi_data, chunksize, uio)))
516 err = fdisp_wait_answ(&fdi);
517 if (err == ERESTART || err == EINTR || err == EWOULDBLOCK) {
519 * Rewind the uio so dofilewrite will know it's
522 uio->uio_resid += fwi->size;
523 uio->uio_offset -= fwi->size;
525 * Change ERESTART into EINTR because we can't rewind
526 * uio->uio_iov. Basically, once uiomove(9) has been
527 * called, it's impossible to restart a syscall.
536 fwo = ((struct fuse_write_out *)fdi.answ);
538 /* Adjust the uio in the case of short writes */
539 diff = fwi->size - fwo->size;
540 as_written_offset = uio->uio_offset - diff;
542 if (as_written_offset - diff > filesize &&
543 fuse_data_cache_mode != FUSE_CACHE_UC)
544 fuse_vnode_setsize(vp, as_written_offset);
545 if (as_written_offset - diff >= filesize)
546 fvdat->flag &= ~FN_SIZECHANGE;
549 printf("WARNING: misbehaving FUSE filesystem "
550 "wrote more data than we provided it\n");
553 } else if (diff > 0) {
556 printf("WARNING: misbehaving FUSE filesystem: "
557 "short writes are only allowed with "
560 if (ioflag & IO_DIRECT) {
562 uio->uio_resid += diff;
563 uio->uio_offset -= diff;
566 /* Resend the unwritten portion of data */
567 fdi.iosize = sizeof(*fwi) + diff;
568 /* Refresh fdi without clearing data buffer */
569 fdisp_refresh_vp(&fdi, FUSE_WRITE, vp,
572 MPASS2(fwi == fdi.indata, "FUSE dispatcher "
573 "reallocated despite no increase in "
575 void *src = (char*)fwi_data + fwo->size;
576 memmove(fwi_data, src, diff);
577 fwi->fh = fufh->fh_id;
578 fwi->offset = as_written_offset;
580 fwi->write_flags = write_flags;
591 SDT_PROBE_DEFINE6(fusefs, , io, write_biobackend_start, "int64_t", "int", "int",
592 "struct uio*", "int", "bool");
593 SDT_PROBE_DEFINE2(fusefs, , io, write_biobackend_append_race, "long", "int");
594 SDT_PROBE_DEFINE2(fusefs, , io, write_biobackend_issue, "int", "struct buf*");
597 fuse_write_biobackend(struct vnode *vp, struct uio *uio,
598 struct ucred *cred, struct fuse_filehandle *fufh, int ioflag, pid_t pid)
600 struct fuse_vnode_data *fvdat = VTOFUD(vp);
605 int n, on, seqcount, err = 0;
608 const int biosize = fuse_iosize(vp);
610 seqcount = ioflag >> IO_SEQSHIFT;
612 KASSERT(uio->uio_rw == UIO_WRITE, ("fuse_write_biobackend mode"));
613 if (vp->v_type != VREG)
615 if (uio->uio_offset < 0)
617 if (uio->uio_resid == 0)
620 err = fuse_vnode_size(vp, &filesize, cred, curthread);
624 if (ioflag & IO_APPEND)
625 uio_setoffset(uio, filesize);
627 if (vn_rlimit_fsize(vp, uio, uio->uio_td))
631 bool direct_append, extending;
633 if (fuse_isdeadfs(vp)) {
637 lbn = uio->uio_offset / biosize;
638 on = uio->uio_offset & (biosize - 1);
639 n = MIN((unsigned)(biosize - on), uio->uio_resid);
642 /* Get or create a buffer for the write */
643 direct_append = uio->uio_offset == filesize && n;
644 if (uio->uio_offset + n < filesize) {
646 if ((off_t)(lbn + 1) * biosize < filesize) {
647 /* Not the file's last block */
650 /* The file's last block */
651 bcount = filesize - (off_t)lbn * biosize;
657 if (howmany(((off_t)lbn * biosize + on + n - 1), PAGE_SIZE) >=
658 howmany(filesize, PAGE_SIZE))
664 * Take care to preserve the buffer's B_CACHE state so
665 * as not to cause an unnecessary read.
667 bp = getblk(vp, lbn, on, PCATCH, 0, 0);
669 uint32_t save = bp->b_flags & B_CACHE;
670 allocbuf(bp, bcount);
674 bp = getblk(vp, lbn, bcount, PCATCH, 0, 0);
682 * Extend file _after_ locking buffer so we won't race
685 err = fuse_vnode_setsize(vp, uio->uio_offset + n);
686 filesize = uio->uio_offset + n;
687 fvdat->flag |= FN_SIZECHANGE;
694 SDT_PROBE6(fusefs, , io, write_biobackend_start,
695 lbn, on, n, uio, bcount, direct_append);
697 * Issue a READ if B_CACHE is not set. In special-append
698 * mode, B_CACHE is based on the buffer prior to the write
699 * op and is typically set, avoiding the read. If a read
700 * is required in special append mode, the server will
701 * probably send us a short-read since we extended the file
702 * on our end, resulting in b_resid == 0 and, thusly,
703 * B_CACHE getting set.
705 * We can also avoid issuing the read if the write covers
706 * the entire buffer. We have to make sure the buffer state
707 * is reasonable in this case since we will not be initiating
708 * I/O. See the comments in kern/vfs_bio.c's getblk() for
711 * B_CACHE may also be set due to the buffer being cached
715 if (on == 0 && n == bcount) {
716 bp->b_flags |= B_CACHE;
717 bp->b_flags &= ~B_INVAL;
718 bp->b_ioflags &= ~BIO_ERROR;
720 if ((bp->b_flags & B_CACHE) == 0) {
721 bp->b_iocmd = BIO_READ;
722 vfs_busy_pages(bp, 0);
723 fuse_io_strategy(vp, bp);
724 if ((err = bp->b_error)) {
729 if (bp->b_wcred == NOCRED)
730 bp->b_wcred = crhold(cred);
733 * If dirtyend exceeds file size, chop it down. This should
734 * not normally occur but there is an append race where it
735 * might occur XXX, so we log it.
737 * If the chopping creates a reverse-indexed or degenerate
738 * situation with dirtyoff/end, we 0 both of them.
740 if (bp->b_dirtyend > bcount) {
741 SDT_PROBE2(fusefs, , io, write_biobackend_append_race,
742 (long)bp->b_blkno * biosize,
743 bp->b_dirtyend - bcount);
744 bp->b_dirtyend = bcount;
746 if (bp->b_dirtyoff >= bp->b_dirtyend)
747 bp->b_dirtyoff = bp->b_dirtyend = 0;
750 * If the new write will leave a contiguous dirty
751 * area, just update the b_dirtyoff and b_dirtyend,
752 * otherwise force a write rpc of the old dirty area.
754 * While it is possible to merge discontiguous writes due to
755 * our having a B_CACHE buffer ( and thus valid read data
756 * for the hole), we don't because it could lead to
757 * significant cache coherency problems with multiple clients,
758 * especially if locking is implemented later on.
760 * as an optimization we could theoretically maintain
761 * a linked list of discontinuous areas, but we would still
762 * have to commit them separately so there isn't much
763 * advantage to it except perhaps a bit of asynchronization.
766 if (bp->b_dirtyend > 0 &&
767 (on > bp->b_dirtyend || (on + n) < bp->b_dirtyoff)) {
769 * Yes, we mean it. Write out everything to "storage"
770 * immediately, without hesitation. (Apart from other
771 * reasons: the only way to know if a write is valid
772 * if its actually written out.)
774 SDT_PROBE2(fusefs, , io, write_biobackend_issue, 0, bp);
776 if (bp->b_error == EINTR) {
782 err = uiomove((char *)bp->b_data + on, n, uio);
785 bp->b_ioflags |= BIO_ERROR;
789 /* TODO: vfs_bio_clrbuf like ffs_write does? */
792 * Only update dirtyoff/dirtyend if not a degenerate
796 if (bp->b_dirtyend > 0) {
797 bp->b_dirtyoff = MIN(on, bp->b_dirtyoff);
798 bp->b_dirtyend = MAX((on + n), bp->b_dirtyend);
801 bp->b_dirtyend = on + n;
803 vfs_bio_set_valid(bp, on, n);
806 vfs_bio_set_flags(bp, ioflag);
808 if (ioflag & IO_SYNC) {
809 SDT_PROBE2(fusefs, , io, write_biobackend_issue, 2, bp);
811 } else if (vm_page_count_severe() ||
812 buf_dirty_count_severe() ||
813 (ioflag & IO_ASYNC)) {
814 bp->b_flags |= B_CLUSTEROK;
815 SDT_PROBE2(fusefs, , io, write_biobackend_issue, 3, bp);
817 } else if (on == 0 && n == bcount) {
818 if ((vp->v_mount->mnt_flag & MNT_NOCLUSTERW) == 0) {
819 bp->b_flags |= B_CLUSTEROK;
820 SDT_PROBE2(fusefs, , io, write_biobackend_issue,
822 cluster_write(vp, bp, filesize, seqcount, 0);
824 SDT_PROBE2(fusefs, , io, write_biobackend_issue,
828 } else if (ioflag & IO_DIRECT) {
829 bp->b_flags |= B_CLUSTEROK;
830 SDT_PROBE2(fusefs, , io, write_biobackend_issue, 6, bp);
833 bp->b_flags &= ~B_CLUSTEROK;
834 SDT_PROBE2(fusefs, , io, write_biobackend_issue, 7, bp);
839 } while (uio->uio_resid > 0 && n > 0);
845 fuse_io_strategy(struct vnode *vp, struct buf *bp)
847 struct fuse_vnode_data *fvdat = VTOFUD(vp);
848 struct fuse_filehandle *fufh;
856 /* We don't know the true pid when we're dealing with the cache */
859 const int biosize = fuse_iosize(vp);
861 MPASS(vp->v_type == VREG || vp->v_type == VDIR);
862 MPASS(bp->b_iocmd == BIO_READ || bp->b_iocmd == BIO_WRITE);
864 fflag = bp->b_iocmd == BIO_READ ? FREAD : FWRITE;
865 cred = bp->b_iocmd == BIO_READ ? bp->b_rcred : bp->b_wcred;
866 error = fuse_filehandle_getrw(vp, fflag, &fufh, cred, pid);
867 bp->b_fsprivate1 = (void*)(intptr_t)0;
868 if (bp->b_iocmd == BIO_READ && error == EBADF) {
870 * This may be a read-modify-write operation on a cached file
871 * opened O_WRONLY. The FUSE protocol allows this.
873 error = fuse_filehandle_get(vp, FWRITE, &fufh, cred, pid);
876 printf("FUSE: strategy: filehandles are closed\n");
877 bp->b_ioflags |= BIO_ERROR;
885 uiop->uio_iovcnt = 1;
886 uiop->uio_segflg = UIO_SYSSPACE;
887 uiop->uio_td = curthread;
890 * clear BIO_ERROR and B_INVAL state prior to initiating the I/O. We
891 * do this here so we do not have to do it in all the code that
894 bp->b_flags &= ~B_INVAL;
895 bp->b_ioflags &= ~BIO_ERROR;
897 KASSERT(!(bp->b_flags & B_DONE),
898 ("fuse_io_strategy: bp %p already marked done", bp));
899 if (bp->b_iocmd == BIO_READ) {
902 io.iov_len = uiop->uio_resid = bp->b_bcount;
903 io.iov_base = bp->b_data;
904 uiop->uio_rw = UIO_READ;
906 uiop->uio_offset = ((off_t)bp->b_lblkno) * biosize;
907 error = fuse_read_directbackend(vp, uiop, cred, fufh);
908 left = uiop->uio_resid;
910 * Store the amount we failed to read in the buffer's private
911 * field, so callers can truncate the file if necessary'
913 bp->b_fsprivate1 = (void*)(intptr_t)left;
915 if (!error && uiop->uio_resid) {
916 int nread = bp->b_bcount - uiop->uio_resid;
917 bzero((char *)bp->b_data + nread, left);
919 if (fuse_data_cache_mode != FUSE_CACHE_WB ||
920 (fvdat->flag & FN_SIZECHANGE) == 0) {
922 * A short read with no error, when not using
923 * direct io, and when no writes are cached,
924 * indicates EOF. Update the file size
925 * accordingly. We must still bzero the
926 * remaining buffer so uninitialized data
927 * doesn't get exposed by a future truncate
928 * that extends the file.
930 * To prevent lock order problems, we must
931 * truncate the file upstack
933 SDT_PROBE2(fusefs, , io, trace, 1,
934 "Short read of a clean file");
938 * If dirty writes _are_ cached beyond EOF,
939 * that indicates a newly created hole that the
940 * server doesn't know about.
941 * XXX: we don't currently track whether dirty
942 * writes are cached beyond EOF, before EOF, or
945 SDT_PROBE2(fusefs, , io, trace, 1,
946 "Short read of a dirty file");
952 bp->b_ioflags |= BIO_ERROR;
957 * Setup for actual write
959 error = fuse_vnode_size(vp, &filesize, cred, curthread);
961 bp->b_ioflags |= BIO_ERROR;
967 if ((off_t)bp->b_lblkno * biosize + bp->b_dirtyend > filesize)
968 bp->b_dirtyend = filesize -
969 (off_t)bp->b_lblkno * biosize;
971 if (bp->b_dirtyend > bp->b_dirtyoff) {
972 io.iov_len = uiop->uio_resid = bp->b_dirtyend
974 uiop->uio_offset = (off_t)bp->b_lblkno * biosize
976 io.iov_base = (char *)bp->b_data + bp->b_dirtyoff;
977 uiop->uio_rw = UIO_WRITE;
979 error = fuse_write_directbackend(vp, uiop, cred, fufh,
982 if (error == EINTR || error == ETIMEDOUT) {
983 bp->b_flags &= ~(B_INVAL | B_NOCACHE);
984 if ((bp->b_flags & B_PAGING) == 0) {
986 bp->b_flags &= ~B_DONE;
988 if ((error == EINTR || error == ETIMEDOUT) &&
989 (bp->b_flags & B_ASYNC) == 0)
990 bp->b_flags |= B_EINTR;
993 bp->b_ioflags |= BIO_ERROR;
994 bp->b_flags |= B_INVAL;
997 bp->b_dirtyoff = bp->b_dirtyend = 0;
1005 bp->b_resid = uiop->uio_resid;
1011 fuse_io_flushbuf(struct vnode *vp, int waitfor, struct thread *td)
1014 return (vn_fsync_buf(vp, waitfor));
1018 * Flush and invalidate all dirty buffers. If another process is already
1019 * doing the flush, just wait for completion.
1022 fuse_io_invalbuf(struct vnode *vp, struct thread *td)
1024 struct fuse_vnode_data *fvdat = VTOFUD(vp);
1027 if (vp->v_iflag & VI_DOOMED)
1030 ASSERT_VOP_ELOCKED(vp, "fuse_io_invalbuf");
1032 while (fvdat->flag & FN_FLUSHINPROG) {
1033 struct proc *p = td->td_proc;
1035 if (vp->v_mount->mnt_kern_flag & MNTK_UNMOUNTF)
1037 fvdat->flag |= FN_FLUSHWANT;
1038 tsleep(&fvdat->flag, PRIBIO + 2, "fusevinv", 2 * hz);
1042 if (SIGNOTEMPTY(p->p_siglist) ||
1043 SIGNOTEMPTY(td->td_siglist))
1050 fvdat->flag |= FN_FLUSHINPROG;
1052 if (vp->v_bufobj.bo_object != NULL) {
1053 VM_OBJECT_WLOCK(vp->v_bufobj.bo_object);
1054 vm_object_page_clean(vp->v_bufobj.bo_object, 0, 0, OBJPC_SYNC);
1055 VM_OBJECT_WUNLOCK(vp->v_bufobj.bo_object);
1057 error = vinvalbuf(vp, V_SAVE, PCATCH, 0);
1059 if (error == ERESTART || error == EINTR) {
1060 fvdat->flag &= ~FN_FLUSHINPROG;
1061 if (fvdat->flag & FN_FLUSHWANT) {
1062 fvdat->flag &= ~FN_FLUSHWANT;
1063 wakeup(&fvdat->flag);
1067 error = vinvalbuf(vp, V_SAVE, PCATCH, 0);
1069 fvdat->flag &= ~FN_FLUSHINPROG;
1070 if (fvdat->flag & FN_FLUSHWANT) {
1071 fvdat->flag &= ~FN_FLUSHWANT;
1072 wakeup(&fvdat->flag);