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"
104 * Set in a struct buf to indicate that the write came from the buffer cache
105 * and the originating cred and pid are no longer known.
107 #define B_FUSEFS_WRITE_CACHE B_FS_FLAG1
109 SDT_PROVIDER_DECLARE(fusefs);
112 * arg0: verbosity. Higher numbers give more verbose messages
113 * arg1: Textual message
115 SDT_PROBE_DEFINE2(fusefs, , io, trace, "int", "char*");
118 fuse_io_clear_suid_on_write(struct vnode *vp, struct ucred *cred,
121 fuse_read_directbackend(struct vnode *vp, struct uio *uio,
122 struct ucred *cred, struct fuse_filehandle *fufh);
124 fuse_read_biobackend(struct vnode *vp, struct uio *uio, int ioflag,
125 struct ucred *cred, struct fuse_filehandle *fufh, pid_t pid);
127 fuse_write_directbackend(struct vnode *vp, struct uio *uio,
128 struct ucred *cred, struct fuse_filehandle *fufh, off_t filesize,
129 int ioflag, bool pages);
131 fuse_write_biobackend(struct vnode *vp, struct uio *uio,
132 struct ucred *cred, struct fuse_filehandle *fufh, int ioflag, pid_t pid);
135 * FreeBSD clears the SUID and SGID bits on any write by a non-root user.
138 fuse_io_clear_suid_on_write(struct vnode *vp, struct ucred *cred,
141 struct fuse_data *data;
146 mp = vnode_mount(vp);
147 data = fuse_get_mpdata(mp);
148 dataflags = data->dataflags;
150 if (dataflags & FSESS_DEFAULT_PERMISSIONS) {
151 if (priv_check_cred(cred, PRIV_VFS_RETAINSUGID)) {
152 fuse_internal_getattr(vp, &va, cred, td);
153 if (va.va_mode & (S_ISUID | S_ISGID)) {
154 mode_t mode = va.va_mode & ~(S_ISUID | S_ISGID);
155 /* Clear all vattr fields except mode */
160 * Ignore fuse_internal_setattr's return value,
161 * because at this point the write operation has
162 * already succeeded and we don't want to return
163 * failing status for that.
165 (void)fuse_internal_setattr(vp, &va, td, NULL);
171 SDT_PROBE_DEFINE5(fusefs, , io, io_dispatch, "struct vnode*", "struct uio*",
172 "int", "struct ucred*", "struct fuse_filehandle*");
173 SDT_PROBE_DEFINE4(fusefs, , io, io_dispatch_filehandles_closed, "struct vnode*",
174 "struct uio*", "int", "struct ucred*");
176 fuse_io_dispatch(struct vnode *vp, struct uio *uio, int ioflag,
177 struct ucred *cred, pid_t pid)
179 struct fuse_filehandle *fufh;
182 bool closefufh = false;
184 MPASS(vp->v_type == VREG || vp->v_type == VDIR);
186 fflag = (uio->uio_rw == UIO_READ) ? FREAD : FWRITE;
187 err = fuse_filehandle_getrw(vp, fflag, &fufh, cred, pid);
188 if (err == EBADF && vnode_mount(vp)->mnt_flag & MNT_EXPORTED) {
190 * nfsd will do I/O without first doing VOP_OPEN. We
191 * must implicitly open the file here
193 err = fuse_filehandle_open(vp, fflag, &fufh, curthread, cred);
197 SDT_PROBE4(fusefs, , io, io_dispatch_filehandles_closed,
198 vp, uio, ioflag, cred);
199 printf("FUSE: io dispatch: filehandles are closed\n");
204 SDT_PROBE5(fusefs, , io, io_dispatch, vp, uio, ioflag, cred, fufh);
207 * Ideally, when the daemon asks for direct io at open time, the
208 * standard file flag should be set according to this, so that would
209 * just change the default mode, which later on could be changed via
211 * But this doesn't work, the O_DIRECT flag gets cleared at some point
212 * (don't know where). So to make any use of the Fuse direct_io option,
213 * we hardwire it into the file's private data (similarly to Linux,
216 directio = (ioflag & IO_DIRECT) || !fsess_opt_datacache(vnode_mount(vp));
218 switch (uio->uio_rw) {
221 SDT_PROBE2(fusefs, , io, trace, 1,
222 "direct read of vnode");
223 err = fuse_read_directbackend(vp, uio, cred, fufh);
225 SDT_PROBE2(fusefs, , io, trace, 1,
226 "buffered read of vnode");
227 err = fuse_read_biobackend(vp, uio, ioflag, cred, fufh,
232 fuse_vnode_update(vp, FN_MTIMECHANGE | FN_CTIMECHANGE);
234 const int iosize = fuse_iosize(vp);
235 off_t start, end, filesize;
237 SDT_PROBE2(fusefs, , io, trace, 1,
238 "direct write of vnode");
240 err = fuse_vnode_size(vp, &filesize, cred, curthread);
244 start = uio->uio_offset;
245 end = start + uio->uio_resid;
246 KASSERT((ioflag & (IO_VMIO | IO_DIRECT)) !=
247 (IO_VMIO | IO_DIRECT),
248 ("IO_DIRECT used for a cache flush?"));
249 /* Invalidate the write cache when writing directly */
250 v_inval_buf_range(vp, start, end, iosize);
251 err = fuse_write_directbackend(vp, uio, cred, fufh,
252 filesize, ioflag, false);
254 SDT_PROBE2(fusefs, , io, trace, 1,
255 "buffered write of vnode");
256 if (fuse_data_cache_mode == FUSE_CACHE_WT)
258 err = fuse_write_biobackend(vp, uio, cred, fufh, ioflag,
261 fuse_io_clear_suid_on_write(vp, cred, uio->uio_td);
264 panic("uninterpreted mode passed to fuse_io_dispatch");
269 fuse_filehandle_close(vp, fufh, curthread, cred);
274 SDT_PROBE_DEFINE4(fusefs, , io, read_bio_backend_start, "int", "int", "int", "int");
275 SDT_PROBE_DEFINE2(fusefs, , io, read_bio_backend_feed, "int", "struct buf*");
276 SDT_PROBE_DEFINE4(fusefs, , io, read_bio_backend_end, "int", "ssize_t", "int",
279 fuse_read_biobackend(struct vnode *vp, struct uio *uio, int ioflag,
280 struct ucred *cred, struct fuse_filehandle *fufh, pid_t pid)
284 struct fuse_data *data;
285 daddr_t lbn, nextlbn;
286 int bcount, nextsize;
287 int err, n = 0, on = 0, seqcount;
290 const int biosize = fuse_iosize(vp);
291 mp = vnode_mount(vp);
292 data = fuse_get_mpdata(mp);
294 if (uio->uio_offset < 0)
297 seqcount = ioflag >> IO_SEQSHIFT;
299 err = fuse_vnode_size(vp, &filesize, cred, curthread);
303 for (err = 0, bp = NULL; uio->uio_resid > 0; bp = NULL) {
304 if (fuse_isdeadfs(vp)) {
308 if (filesize - uio->uio_offset <= 0)
310 lbn = uio->uio_offset / biosize;
311 on = uio->uio_offset & (biosize - 1);
313 if ((off_t)lbn * biosize >= filesize) {
315 } else if ((off_t)(lbn + 1) * biosize > filesize) {
316 bcount = filesize - (off_t)lbn *biosize;
321 nextsize = MIN(biosize, filesize - nextlbn * biosize);
323 SDT_PROBE4(fusefs, , io, read_bio_backend_start,
324 biosize, (int)lbn, on, bcount);
326 if (bcount < biosize) {
327 /* If near EOF, don't do readahead */
328 err = bread(vp, lbn, bcount, NOCRED, &bp);
329 } else if ((vp->v_mount->mnt_flag & MNT_NOCLUSTERR) == 0) {
330 /* Try clustered read */
331 long totread = uio->uio_resid + on;
332 seqcount = MIN(seqcount,
333 data->max_readahead_blocks + 1);
334 err = cluster_read(vp, filesize, lbn, bcount, NOCRED,
335 totread, seqcount, 0, &bp);
336 } else if (seqcount > 1 && data->max_readahead_blocks >= 1) {
337 /* Try non-clustered readahead */
338 err = breadn(vp, lbn, bcount, &nextlbn, &nextsize, 1,
341 /* Just read what was requested */
342 err = bread(vp, lbn, bcount, NOCRED, &bp);
352 * on is the offset into the current bp. Figure out how many
353 * bytes we can copy out of the bp. Note that bcount is
354 * NOT DEV_BSIZE aligned.
356 * Then figure out how many bytes we can copy into the uio.
360 if (on < bcount - bp->b_resid)
361 n = MIN((unsigned)(bcount - bp->b_resid - on),
364 SDT_PROBE2(fusefs, , io, read_bio_backend_feed, n, bp);
365 err = uiomove(bp->b_data + on, n, uio);
367 vfs_bio_brelse(bp, ioflag);
368 SDT_PROBE4(fusefs, , io, read_bio_backend_end, err,
369 uio->uio_resid, n, bp);
370 if (bp->b_resid > 0) {
371 /* Short read indicates EOF */
379 SDT_PROBE_DEFINE1(fusefs, , io, read_directbackend_start,
380 "struct fuse_read_in*");
381 SDT_PROBE_DEFINE3(fusefs, , io, read_directbackend_complete,
382 "struct fuse_dispatcher*", "struct fuse_read_in*", "struct uio*");
385 fuse_read_directbackend(struct vnode *vp, struct uio *uio,
386 struct ucred *cred, struct fuse_filehandle *fufh)
388 struct fuse_data *data;
389 struct fuse_dispatcher fdi;
390 struct fuse_read_in *fri;
393 data = fuse_get_mpdata(vp->v_mount);
395 if (uio->uio_resid == 0)
401 * XXX In "normal" case we use an intermediate kernel buffer for
402 * transmitting data from daemon's context to ours. Eventually, we should
403 * get rid of this. Anyway, if the target uio lives in sysspace (we are
404 * called from pageops), and the input data doesn't need kernel-side
405 * processing (we are not called from readdir) we can already invoke
406 * an optimized, "peer-to-peer" I/O routine.
408 while (uio->uio_resid > 0) {
409 fdi.iosize = sizeof(*fri);
410 fdisp_make_vp(&fdi, FUSE_READ, vp, uio->uio_td, cred);
412 fri->fh = fufh->fh_id;
413 fri->offset = uio->uio_offset;
414 fri->size = MIN(uio->uio_resid,
415 fuse_get_mpdata(vp->v_mount)->max_read);
416 if (fuse_libabi_geq(data, 7, 9)) {
417 /* See comment regarding FUSE_WRITE_LOCKOWNER */
419 fri->flags = fufh_type_2_fflags(fufh->fufh_type);
422 SDT_PROBE1(fusefs, , io, read_directbackend_start, fri);
424 if ((err = fdisp_wait_answ(&fdi)))
427 SDT_PROBE3(fusefs, , io, read_directbackend_complete,
430 if ((err = uiomove(fdi.answ, MIN(fri->size, fdi.iosize), uio)))
432 if (fdi.iosize < fri->size) {
434 * Short read. Should only happen at EOF or with
447 fuse_write_directbackend(struct vnode *vp, struct uio *uio,
448 struct ucred *cred, struct fuse_filehandle *fufh, off_t filesize,
449 int ioflag, bool pages)
451 struct fuse_vnode_data *fvdat = VTOFUD(vp);
452 struct fuse_data *data;
453 struct fuse_write_in *fwi;
454 struct fuse_write_out *fwo;
455 struct fuse_dispatcher fdi;
458 off_t as_written_offset;
461 bool direct_io = fufh->fuse_open_flags & FOPEN_DIRECT_IO;
462 bool wrote_anything = false;
463 uint32_t write_flags;
465 data = fuse_get_mpdata(vp->v_mount);
468 * Don't set FUSE_WRITE_LOCKOWNER in write_flags. It can't be set
469 * accurately when using POSIX AIO, libfuse doesn't use it, and I'm not
470 * aware of any file systems that do. It was an attempt to add
471 * Linux-style mandatory locking to the FUSE protocol, but mandatory
472 * locking is deprecated even on Linux. See Linux commit
473 * f33321141b273d60cbb3a8f56a5489baad82ba5e .
476 * Set FUSE_WRITE_CACHE whenever we don't know the uid, gid, and/or pid
477 * that originated a write. For example when writing from the
478 * writeback cache. I don't know of a single file system that cares,
479 * but the protocol says we're supposed to do this.
481 write_flags = !pages && (
482 (ioflag & IO_DIRECT) ||
483 !fsess_opt_datacache(vnode_mount(vp)) ||
484 fuse_data_cache_mode != FUSE_CACHE_WB) ? 0 : FUSE_WRITE_CACHE;
486 if (uio->uio_resid == 0)
489 if (ioflag & IO_APPEND)
490 uio_setoffset(uio, filesize);
492 if (vn_rlimit_fsize(vp, uio, uio->uio_td))
497 while (uio->uio_resid > 0) {
498 chunksize = MIN(uio->uio_resid, data->max_write);
500 fdi.iosize = sizeof(*fwi) + chunksize;
501 fdisp_make_vp(&fdi, FUSE_WRITE, vp, uio->uio_td, cred);
504 fwi->fh = fufh->fh_id;
505 fwi->offset = uio->uio_offset;
506 fwi->size = chunksize;
507 fwi->write_flags = write_flags;
508 if (fuse_libabi_geq(data, 7, 9)) {
509 fwi->flags = fufh_type_2_fflags(fufh->fufh_type);
510 fwi_data = (char *)fdi.indata + sizeof(*fwi);
512 fwi_data = (char *)fdi.indata +
513 FUSE_COMPAT_WRITE_IN_SIZE;
516 if ((err = uiomove(fwi_data, chunksize, uio)))
520 err = fdisp_wait_answ(&fdi);
521 if (err == ERESTART || err == EINTR || err == EWOULDBLOCK) {
523 * Rewind the uio so dofilewrite will know it's
526 uio->uio_resid += fwi->size;
527 uio->uio_offset -= fwi->size;
529 * Change ERESTART into EINTR because we can't rewind
530 * uio->uio_iov. Basically, once uiomove(9) has been
531 * called, it's impossible to restart a syscall.
539 wrote_anything = true;
542 fwo = ((struct fuse_write_out *)fdi.answ);
544 /* Adjust the uio in the case of short writes */
545 diff = fwi->size - fwo->size;
546 as_written_offset = uio->uio_offset - diff;
548 if (as_written_offset - diff > filesize)
549 fuse_vnode_setsize(vp, as_written_offset);
550 if (as_written_offset - diff >= filesize)
551 fvdat->flag &= ~FN_SIZECHANGE;
554 printf("WARNING: misbehaving FUSE filesystem "
555 "wrote more data than we provided it\n");
558 } else if (diff > 0) {
561 printf("WARNING: misbehaving FUSE filesystem: "
562 "short writes are only allowed with "
565 if (ioflag & IO_DIRECT) {
567 uio->uio_resid += diff;
568 uio->uio_offset -= diff;
571 /* Resend the unwritten portion of data */
572 fdi.iosize = sizeof(*fwi) + diff;
573 /* Refresh fdi without clearing data buffer */
574 fdisp_refresh_vp(&fdi, FUSE_WRITE, vp,
577 MPASS2(fwi == fdi.indata, "FUSE dispatcher "
578 "reallocated despite no increase in "
580 void *src = (char*)fwi_data + fwo->size;
581 memmove(fwi_data, src, diff);
582 fwi->fh = fufh->fh_id;
583 fwi->offset = as_written_offset;
585 fwi->write_flags = write_flags;
594 fuse_vnode_undirty_cached_timestamps(vp);
599 SDT_PROBE_DEFINE6(fusefs, , io, write_biobackend_start, "int64_t", "int", "int",
600 "struct uio*", "int", "bool");
601 SDT_PROBE_DEFINE2(fusefs, , io, write_biobackend_append_race, "long", "int");
602 SDT_PROBE_DEFINE2(fusefs, , io, write_biobackend_issue, "int", "struct buf*");
605 fuse_write_biobackend(struct vnode *vp, struct uio *uio,
606 struct ucred *cred, struct fuse_filehandle *fufh, int ioflag, pid_t pid)
608 struct fuse_vnode_data *fvdat = VTOFUD(vp);
613 int n, on, seqcount, err = 0;
616 const int biosize = fuse_iosize(vp);
618 seqcount = ioflag >> IO_SEQSHIFT;
620 KASSERT(uio->uio_rw == UIO_WRITE, ("fuse_write_biobackend mode"));
621 if (vp->v_type != VREG)
623 if (uio->uio_offset < 0)
625 if (uio->uio_resid == 0)
628 err = fuse_vnode_size(vp, &filesize, cred, curthread);
632 if (ioflag & IO_APPEND)
633 uio_setoffset(uio, filesize);
635 if (vn_rlimit_fsize(vp, uio, uio->uio_td))
639 bool direct_append, extending;
641 if (fuse_isdeadfs(vp)) {
645 lbn = uio->uio_offset / biosize;
646 on = uio->uio_offset & (biosize - 1);
647 n = MIN((unsigned)(biosize - on), uio->uio_resid);
650 /* Get or create a buffer for the write */
651 direct_append = uio->uio_offset == filesize && n;
652 if (uio->uio_offset + n < filesize) {
654 if ((off_t)(lbn + 1) * biosize < filesize) {
655 /* Not the file's last block */
658 /* The file's last block */
659 bcount = filesize - (off_t)lbn * biosize;
665 if (howmany(((off_t)lbn * biosize + on + n - 1), PAGE_SIZE) >=
666 howmany(filesize, PAGE_SIZE))
672 * Take care to preserve the buffer's B_CACHE state so
673 * as not to cause an unnecessary read.
675 bp = getblk(vp, lbn, on, PCATCH, 0, 0);
677 uint32_t save = bp->b_flags & B_CACHE;
678 allocbuf(bp, bcount);
682 bp = getblk(vp, lbn, bcount, PCATCH, 0, 0);
690 * Extend file _after_ locking buffer so we won't race
693 err = fuse_vnode_setsize(vp, uio->uio_offset + n);
694 filesize = uio->uio_offset + n;
695 fvdat->flag |= FN_SIZECHANGE;
702 SDT_PROBE6(fusefs, , io, write_biobackend_start,
703 lbn, on, n, uio, bcount, direct_append);
705 * Issue a READ if B_CACHE is not set. In special-append
706 * mode, B_CACHE is based on the buffer prior to the write
707 * op and is typically set, avoiding the read. If a read
708 * is required in special append mode, the server will
709 * probably send us a short-read since we extended the file
710 * on our end, resulting in b_resid == 0 and, thusly,
711 * B_CACHE getting set.
713 * We can also avoid issuing the read if the write covers
714 * the entire buffer. We have to make sure the buffer state
715 * is reasonable in this case since we will not be initiating
716 * I/O. See the comments in kern/vfs_bio.c's getblk() for
719 * B_CACHE may also be set due to the buffer being cached
723 if (on == 0 && n == bcount) {
724 bp->b_flags |= B_CACHE;
725 bp->b_flags &= ~B_INVAL;
726 bp->b_ioflags &= ~BIO_ERROR;
728 if ((bp->b_flags & B_CACHE) == 0) {
729 bp->b_iocmd = BIO_READ;
730 vfs_busy_pages(bp, 0);
731 fuse_io_strategy(vp, bp);
732 if ((err = bp->b_error)) {
736 if (bp->b_resid > 0) {
738 * Short read indicates EOF. Update file size
739 * from the server and try again.
741 SDT_PROBE2(fusefs, , io, trace, 1,
742 "Short read during a RMW");
744 err = fuse_vnode_size(vp, &filesize, cred,
752 if (bp->b_wcred == NOCRED)
753 bp->b_wcred = crhold(cred);
756 * If dirtyend exceeds file size, chop it down. This should
757 * not normally occur but there is an append race where it
758 * might occur XXX, so we log it.
760 * If the chopping creates a reverse-indexed or degenerate
761 * situation with dirtyoff/end, we 0 both of them.
763 if (bp->b_dirtyend > bcount) {
764 SDT_PROBE2(fusefs, , io, write_biobackend_append_race,
765 (long)bp->b_blkno * biosize,
766 bp->b_dirtyend - bcount);
767 bp->b_dirtyend = bcount;
769 if (bp->b_dirtyoff >= bp->b_dirtyend)
770 bp->b_dirtyoff = bp->b_dirtyend = 0;
773 * If the new write will leave a contiguous dirty
774 * area, just update the b_dirtyoff and b_dirtyend,
775 * otherwise force a write rpc of the old dirty area.
777 * While it is possible to merge discontiguous writes due to
778 * our having a B_CACHE buffer ( and thus valid read data
779 * for the hole), we don't because it could lead to
780 * significant cache coherency problems with multiple clients,
781 * especially if locking is implemented later on.
783 * as an optimization we could theoretically maintain
784 * a linked list of discontinuous areas, but we would still
785 * have to commit them separately so there isn't much
786 * advantage to it except perhaps a bit of asynchronization.
789 if (bp->b_dirtyend > 0 &&
790 (on > bp->b_dirtyend || (on + n) < bp->b_dirtyoff)) {
792 * Yes, we mean it. Write out everything to "storage"
793 * immediately, without hesitation. (Apart from other
794 * reasons: the only way to know if a write is valid
795 * if its actually written out.)
797 SDT_PROBE2(fusefs, , io, write_biobackend_issue, 0, bp);
799 if (bp->b_error == EINTR) {
805 err = uiomove((char *)bp->b_data + on, n, uio);
808 bp->b_ioflags |= BIO_ERROR;
812 /* TODO: vfs_bio_clrbuf like ffs_write does? */
815 * Only update dirtyoff/dirtyend if not a degenerate
819 if (bp->b_dirtyend > 0) {
820 bp->b_dirtyoff = MIN(on, bp->b_dirtyoff);
821 bp->b_dirtyend = MAX((on + n), bp->b_dirtyend);
824 bp->b_dirtyend = on + n;
826 vfs_bio_set_valid(bp, on, n);
829 vfs_bio_set_flags(bp, ioflag);
831 bp->b_flags |= B_FUSEFS_WRITE_CACHE;
832 if (ioflag & IO_SYNC) {
833 SDT_PROBE2(fusefs, , io, write_biobackend_issue, 2, bp);
834 if (!(ioflag & IO_VMIO))
835 bp->b_flags &= ~B_FUSEFS_WRITE_CACHE;
837 } else if (vm_page_count_severe() ||
838 buf_dirty_count_severe() ||
839 (ioflag & IO_ASYNC)) {
840 bp->b_flags |= B_CLUSTEROK;
841 SDT_PROBE2(fusefs, , io, write_biobackend_issue, 3, bp);
843 } else if (on == 0 && n == bcount) {
844 if ((vp->v_mount->mnt_flag & MNT_NOCLUSTERW) == 0) {
845 bp->b_flags |= B_CLUSTEROK;
846 SDT_PROBE2(fusefs, , io, write_biobackend_issue,
848 cluster_write(vp, bp, filesize, seqcount, 0);
850 SDT_PROBE2(fusefs, , io, write_biobackend_issue,
854 } else if (ioflag & IO_DIRECT) {
855 bp->b_flags |= B_CLUSTEROK;
856 SDT_PROBE2(fusefs, , io, write_biobackend_issue, 6, bp);
859 bp->b_flags &= ~B_CLUSTEROK;
860 SDT_PROBE2(fusefs, , io, write_biobackend_issue, 7, bp);
865 } while (uio->uio_resid > 0 && n > 0);
871 fuse_io_strategy(struct vnode *vp, struct buf *bp)
873 struct fuse_vnode_data *fvdat = VTOFUD(vp);
874 struct fuse_filehandle *fufh;
882 /* We don't know the true pid when we're dealing with the cache */
885 const int biosize = fuse_iosize(vp);
887 MPASS(vp->v_type == VREG || vp->v_type == VDIR);
888 MPASS(bp->b_iocmd == BIO_READ || bp->b_iocmd == BIO_WRITE);
890 fflag = bp->b_iocmd == BIO_READ ? FREAD : FWRITE;
891 cred = bp->b_iocmd == BIO_READ ? bp->b_rcred : bp->b_wcred;
892 error = fuse_filehandle_getrw(vp, fflag, &fufh, cred, pid);
893 if (bp->b_iocmd == BIO_READ && error == EBADF) {
895 * This may be a read-modify-write operation on a cached file
896 * opened O_WRONLY. The FUSE protocol allows this.
898 error = fuse_filehandle_get(vp, FWRITE, &fufh, cred, pid);
901 printf("FUSE: strategy: filehandles are closed\n");
902 bp->b_ioflags |= BIO_ERROR;
910 uiop->uio_iovcnt = 1;
911 uiop->uio_segflg = UIO_SYSSPACE;
912 uiop->uio_td = curthread;
915 * clear BIO_ERROR and B_INVAL state prior to initiating the I/O. We
916 * do this here so we do not have to do it in all the code that
919 bp->b_flags &= ~B_INVAL;
920 bp->b_ioflags &= ~BIO_ERROR;
922 KASSERT(!(bp->b_flags & B_DONE),
923 ("fuse_io_strategy: bp %p already marked done", bp));
924 if (bp->b_iocmd == BIO_READ) {
927 io.iov_len = uiop->uio_resid = bp->b_bcount;
928 io.iov_base = bp->b_data;
929 uiop->uio_rw = UIO_READ;
931 uiop->uio_offset = ((off_t)bp->b_lblkno) * biosize;
932 error = fuse_read_directbackend(vp, uiop, cred, fufh);
934 * Store the amount we failed to read in the buffer's private
935 * field, so callers can truncate the file if necessary'
938 if (!error && uiop->uio_resid) {
939 int nread = bp->b_bcount - uiop->uio_resid;
940 left = uiop->uio_resid;
941 bzero((char *)bp->b_data + nread, left);
943 if ((fvdat->flag & FN_SIZECHANGE) == 0) {
945 * A short read with no error, when not using
946 * direct io, and when no writes are cached,
947 * indicates EOF caused by a server-side
948 * truncation. Clear the attr cache so we'll
949 * pick up the new file size and timestamps.
951 * We must still bzero the remaining buffer so
952 * uninitialized data doesn't get exposed by a
953 * future truncate that extends the file.
955 * To prevent lock order problems, we must
956 * truncate the file upstack, not here.
958 SDT_PROBE2(fusefs, , io, trace, 1,
959 "Short read of a clean file");
960 fuse_vnode_clear_attr_cache(vp);
963 * If dirty writes _are_ cached beyond EOF,
964 * that indicates a newly created hole that the
965 * server doesn't know about. Those don't pose
967 * XXX: we don't currently track whether dirty
968 * writes are cached beyond EOF, before EOF, or
971 SDT_PROBE2(fusefs, , io, trace, 1,
972 "Short read of a dirty file");
978 bp->b_ioflags |= BIO_ERROR;
983 * Setup for actual write
985 error = fuse_vnode_size(vp, &filesize, cred, curthread);
987 bp->b_ioflags |= BIO_ERROR;
993 if ((off_t)bp->b_lblkno * biosize + bp->b_dirtyend > filesize)
994 bp->b_dirtyend = filesize -
995 (off_t)bp->b_lblkno * biosize;
997 if (bp->b_dirtyend > bp->b_dirtyoff) {
998 io.iov_len = uiop->uio_resid = bp->b_dirtyend
1000 uiop->uio_offset = (off_t)bp->b_lblkno * biosize
1002 io.iov_base = (char *)bp->b_data + bp->b_dirtyoff;
1003 uiop->uio_rw = UIO_WRITE;
1005 bool pages = bp->b_flags & B_FUSEFS_WRITE_CACHE;
1006 error = fuse_write_directbackend(vp, uiop, cred, fufh,
1007 filesize, 0, pages);
1009 if (error == EINTR || error == ETIMEDOUT) {
1010 bp->b_flags &= ~(B_INVAL | B_NOCACHE);
1011 if ((bp->b_flags & B_PAGING) == 0) {
1013 bp->b_flags &= ~B_DONE;
1015 if ((error == EINTR || error == ETIMEDOUT) &&
1016 (bp->b_flags & B_ASYNC) == 0)
1017 bp->b_flags |= B_EINTR;
1020 bp->b_ioflags |= BIO_ERROR;
1021 bp->b_flags |= B_INVAL;
1022 bp->b_error = error;
1024 bp->b_dirtyoff = bp->b_dirtyend = 0;
1032 bp->b_resid = uiop->uio_resid;
1038 fuse_io_flushbuf(struct vnode *vp, int waitfor, struct thread *td)
1041 return (vn_fsync_buf(vp, waitfor));
1045 * Flush and invalidate all dirty buffers. If another process is already
1046 * doing the flush, just wait for completion.
1049 fuse_io_invalbuf(struct vnode *vp, struct thread *td)
1051 struct fuse_vnode_data *fvdat = VTOFUD(vp);
1054 if (vp->v_iflag & VI_DOOMED)
1057 ASSERT_VOP_ELOCKED(vp, "fuse_io_invalbuf");
1059 while (fvdat->flag & FN_FLUSHINPROG) {
1060 struct proc *p = td->td_proc;
1062 if (vp->v_mount->mnt_kern_flag & MNTK_UNMOUNTF)
1064 fvdat->flag |= FN_FLUSHWANT;
1065 tsleep(&fvdat->flag, PRIBIO + 2, "fusevinv", 2 * hz);
1069 if (SIGNOTEMPTY(p->p_siglist) ||
1070 SIGNOTEMPTY(td->td_siglist))
1077 fvdat->flag |= FN_FLUSHINPROG;
1079 if (vp->v_bufobj.bo_object != NULL) {
1080 VM_OBJECT_WLOCK(vp->v_bufobj.bo_object);
1081 vm_object_page_clean(vp->v_bufobj.bo_object, 0, 0, OBJPC_SYNC);
1082 VM_OBJECT_WUNLOCK(vp->v_bufobj.bo_object);
1084 error = vinvalbuf(vp, V_SAVE, PCATCH, 0);
1086 if (error == ERESTART || error == EINTR) {
1087 fvdat->flag &= ~FN_FLUSHINPROG;
1088 if (fvdat->flag & FN_FLUSHWANT) {
1089 fvdat->flag &= ~FN_FLUSHWANT;
1090 wakeup(&fvdat->flag);
1094 error = vinvalbuf(vp, V_SAVE, PCATCH, 0);
1096 fvdat->flag &= ~FN_FLUSHINPROG;
1097 if (fvdat->flag & FN_FLUSHWANT) {
1098 fvdat->flag &= ~FN_FLUSHWANT;
1099 wakeup(&fvdat->flag);