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 * Copyright (c) 2019 The FreeBSD Foundation
38 * Portions of this software were developed by BFF Storage Systems, LLC under
39 * sponsorship from the FreeBSD Foundation.
41 * Redistribution and use in source and binary forms, with or without
42 * modification, are permitted provided that the following conditions
44 * 1. Redistributions of source code must retain the above copyright
45 * notice, this list of conditions and the following disclaimer.
46 * 2. Redistributions in binary form must reproduce the above copyright
47 * notice, this list of conditions and the following disclaimer in the
48 * documentation and/or other materials provided with the distribution.
50 * THIS SOFTWARE IS PROVIDED BY AUTHOR AND CONTRIBUTORS ``AS IS'' AND
51 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
52 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
53 * ARE DISCLAIMED. IN NO EVENT SHALL AUTHOR OR CONTRIBUTORS BE LIABLE
54 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
55 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
56 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
57 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
58 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
59 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
63 #include <sys/cdefs.h>
64 __FBSDID("$FreeBSD$");
66 #include <sys/types.h>
67 #include <sys/module.h>
68 #include <sys/systm.h>
69 #include <sys/errno.h>
70 #include <sys/param.h>
71 #include <sys/kernel.h>
74 #include <sys/malloc.h>
75 #include <sys/queue.h>
78 #include <sys/mutex.h>
79 #include <sys/rwlock.h>
82 #include <sys/mount.h>
83 #include <sys/vnode.h>
85 #include <sys/unistd.h>
86 #include <sys/filedesc.h>
88 #include <sys/fcntl.h>
91 #include <sys/sysctl.h>
92 #include <sys/vmmeter.h>
95 #include <vm/vm_extern.h>
97 #include <vm/vm_map.h>
98 #include <vm/vm_page.h>
99 #include <vm/vm_object.h>
102 #include "fuse_file.h"
103 #include "fuse_node.h"
104 #include "fuse_internal.h"
105 #include "fuse_ipc.h"
109 * Set in a struct buf to indicate that the write came from the buffer cache
110 * and the originating cred and pid are no longer known.
112 #define B_FUSEFS_WRITE_CACHE B_FS_FLAG1
114 SDT_PROVIDER_DECLARE(fusefs);
117 * arg0: verbosity. Higher numbers give more verbose messages
118 * arg1: Textual message
120 SDT_PROBE_DEFINE2(fusefs, , io, trace, "int", "char*");
123 fuse_inval_buf_range(struct vnode *vp, off_t filesize, off_t start, off_t end);
125 fuse_io_clear_suid_on_write(struct vnode *vp, struct ucred *cred,
128 fuse_read_directbackend(struct vnode *vp, struct uio *uio,
129 struct ucred *cred, struct fuse_filehandle *fufh);
131 fuse_read_biobackend(struct vnode *vp, struct uio *uio, int ioflag,
132 struct ucred *cred, struct fuse_filehandle *fufh, pid_t pid);
134 fuse_write_directbackend(struct vnode *vp, struct uio *uio,
135 struct ucred *cred, struct fuse_filehandle *fufh, off_t filesize,
136 int ioflag, bool pages);
138 fuse_write_biobackend(struct vnode *vp, struct uio *uio,
139 struct ucred *cred, struct fuse_filehandle *fufh, int ioflag, pid_t pid);
141 /* Invalidate a range of cached data, whether dirty of not */
143 fuse_inval_buf_range(struct vnode *vp, off_t filesize, off_t start, off_t end)
146 daddr_t left_lbn, end_lbn, right_lbn;
148 int iosize, left_on, right_on, right_blksize;
150 iosize = fuse_iosize(vp);
151 left_lbn = start / iosize;
152 end_lbn = howmany(end, iosize);
153 left_on = start & (iosize - 1);
155 bp = getblk(vp, left_lbn, iosize, PCATCH, 0, 0);
156 if ((bp->b_flags & B_CACHE) != 0 && bp->b_dirtyend >= left_on) {
158 * Flush the dirty buffer, because we don't have a
159 * byte-granular way to record which parts of the
164 return (bp->b_error);
169 right_on = end & (iosize - 1);
171 right_lbn = end / iosize;
172 new_filesize = MAX(filesize, end);
173 right_blksize = MIN(iosize, new_filesize - iosize * right_lbn);
174 bp = getblk(vp, right_lbn, right_blksize, PCATCH, 0, 0);
175 if ((bp->b_flags & B_CACHE) != 0 && bp->b_dirtyoff < right_on) {
177 * Flush the dirty buffer, because we don't have a
178 * byte-granular way to record which parts of the
183 return (bp->b_error);
189 v_inval_buf_range(vp, left_lbn, end_lbn, iosize);
194 * FreeBSD clears the SUID and SGID bits on any write by a non-root user.
197 fuse_io_clear_suid_on_write(struct vnode *vp, struct ucred *cred,
200 struct fuse_data *data;
205 mp = vnode_mount(vp);
206 data = fuse_get_mpdata(mp);
207 dataflags = data->dataflags;
209 if (dataflags & FSESS_DEFAULT_PERMISSIONS) {
210 if (priv_check_cred(cred, PRIV_VFS_RETAINSUGID)) {
211 fuse_internal_getattr(vp, &va, cred, td);
212 if (va.va_mode & (S_ISUID | S_ISGID)) {
213 mode_t mode = va.va_mode & ~(S_ISUID | S_ISGID);
214 /* Clear all vattr fields except mode */
219 * Ignore fuse_internal_setattr's return value,
220 * because at this point the write operation has
221 * already succeeded and we don't want to return
222 * failing status for that.
224 (void)fuse_internal_setattr(vp, &va, td, NULL);
230 SDT_PROBE_DEFINE5(fusefs, , io, io_dispatch, "struct vnode*", "struct uio*",
231 "int", "struct ucred*", "struct fuse_filehandle*");
232 SDT_PROBE_DEFINE4(fusefs, , io, io_dispatch_filehandles_closed, "struct vnode*",
233 "struct uio*", "int", "struct ucred*");
235 fuse_io_dispatch(struct vnode *vp, struct uio *uio, int ioflag,
236 struct ucred *cred, pid_t pid)
238 struct fuse_filehandle *fufh;
241 bool closefufh = false;
243 MPASS(vp->v_type == VREG || vp->v_type == VDIR);
245 fflag = (uio->uio_rw == UIO_READ) ? FREAD : FWRITE;
246 err = fuse_filehandle_getrw(vp, fflag, &fufh, cred, pid);
247 if (err == EBADF && vnode_mount(vp)->mnt_flag & MNT_EXPORTED) {
249 * nfsd will do I/O without first doing VOP_OPEN. We
250 * must implicitly open the file here
252 err = fuse_filehandle_open(vp, fflag, &fufh, curthread, cred);
256 SDT_PROBE4(fusefs, , io, io_dispatch_filehandles_closed,
257 vp, uio, ioflag, cred);
258 printf("FUSE: io dispatch: filehandles are closed\n");
263 SDT_PROBE5(fusefs, , io, io_dispatch, vp, uio, ioflag, cred, fufh);
266 * Ideally, when the daemon asks for direct io at open time, the
267 * standard file flag should be set according to this, so that would
268 * just change the default mode, which later on could be changed via
270 * But this doesn't work, the O_DIRECT flag gets cleared at some point
271 * (don't know where). So to make any use of the Fuse direct_io option,
272 * we hardwire it into the file's private data (similarly to Linux,
275 directio = (ioflag & IO_DIRECT) || !fsess_opt_datacache(vnode_mount(vp));
277 switch (uio->uio_rw) {
280 SDT_PROBE2(fusefs, , io, trace, 1,
281 "direct read of vnode");
282 err = fuse_read_directbackend(vp, uio, cred, fufh);
284 SDT_PROBE2(fusefs, , io, trace, 1,
285 "buffered read of vnode");
286 err = fuse_read_biobackend(vp, uio, ioflag, cred, fufh,
291 fuse_vnode_update(vp, FN_MTIMECHANGE | FN_CTIMECHANGE);
293 off_t start, end, filesize;
295 SDT_PROBE2(fusefs, , io, trace, 1,
296 "direct write of vnode");
298 err = fuse_vnode_size(vp, &filesize, cred, curthread);
302 start = uio->uio_offset;
303 end = start + uio->uio_resid;
304 KASSERT((ioflag & (IO_VMIO | IO_DIRECT)) !=
305 (IO_VMIO | IO_DIRECT),
306 ("IO_DIRECT used for a cache flush?"));
307 /* Invalidate the write cache when writing directly */
308 err = fuse_inval_buf_range(vp, filesize, start, end);
311 err = fuse_write_directbackend(vp, uio, cred, fufh,
312 filesize, ioflag, false);
314 SDT_PROBE2(fusefs, , io, trace, 1,
315 "buffered write of vnode");
316 if (!fsess_opt_writeback(vnode_mount(vp)))
318 err = fuse_write_biobackend(vp, uio, cred, fufh, ioflag,
321 fuse_io_clear_suid_on_write(vp, cred, uio->uio_td);
324 panic("uninterpreted mode passed to fuse_io_dispatch");
329 fuse_filehandle_close(vp, fufh, curthread, cred);
334 SDT_PROBE_DEFINE4(fusefs, , io, read_bio_backend_start, "int", "int", "int", "int");
335 SDT_PROBE_DEFINE2(fusefs, , io, read_bio_backend_feed, "int", "struct buf*");
336 SDT_PROBE_DEFINE4(fusefs, , io, read_bio_backend_end, "int", "ssize_t", "int",
339 fuse_read_biobackend(struct vnode *vp, struct uio *uio, int ioflag,
340 struct ucred *cred, struct fuse_filehandle *fufh, pid_t pid)
344 struct fuse_data *data;
345 daddr_t lbn, nextlbn;
346 int bcount, nextsize;
347 int err, n = 0, on = 0, seqcount;
350 const int biosize = fuse_iosize(vp);
351 mp = vnode_mount(vp);
352 data = fuse_get_mpdata(mp);
354 if (uio->uio_offset < 0)
357 seqcount = ioflag >> IO_SEQSHIFT;
359 err = fuse_vnode_size(vp, &filesize, cred, curthread);
363 for (err = 0, bp = NULL; uio->uio_resid > 0; bp = NULL) {
364 if (fuse_isdeadfs(vp)) {
368 if (filesize - uio->uio_offset <= 0)
370 lbn = uio->uio_offset / biosize;
371 on = uio->uio_offset & (biosize - 1);
373 if ((off_t)lbn * biosize >= filesize) {
375 } else if ((off_t)(lbn + 1) * biosize > filesize) {
376 bcount = filesize - (off_t)lbn *biosize;
381 nextsize = MIN(biosize, filesize - nextlbn * biosize);
383 SDT_PROBE4(fusefs, , io, read_bio_backend_start,
384 biosize, (int)lbn, on, bcount);
386 if (bcount < biosize) {
387 /* If near EOF, don't do readahead */
388 err = bread(vp, lbn, bcount, NOCRED, &bp);
389 } else if ((vp->v_mount->mnt_flag & MNT_NOCLUSTERR) == 0) {
390 /* Try clustered read */
391 long totread = uio->uio_resid + on;
392 seqcount = MIN(seqcount,
393 data->max_readahead_blocks + 1);
394 err = cluster_read(vp, filesize, lbn, bcount, NOCRED,
395 totread, seqcount, 0, &bp);
396 } else if (seqcount > 1 && data->max_readahead_blocks >= 1) {
397 /* Try non-clustered readahead */
398 err = breadn(vp, lbn, bcount, &nextlbn, &nextsize, 1,
401 /* Just read what was requested */
402 err = bread(vp, lbn, bcount, NOCRED, &bp);
412 * on is the offset into the current bp. Figure out how many
413 * bytes we can copy out of the bp. Note that bcount is
414 * NOT DEV_BSIZE aligned.
416 * Then figure out how many bytes we can copy into the uio.
420 if (on < bcount - bp->b_resid)
421 n = MIN((unsigned)(bcount - bp->b_resid - on),
424 SDT_PROBE2(fusefs, , io, read_bio_backend_feed, n, bp);
425 err = uiomove(bp->b_data + on, n, uio);
427 vfs_bio_brelse(bp, ioflag);
428 SDT_PROBE4(fusefs, , io, read_bio_backend_end, err,
429 uio->uio_resid, n, bp);
430 if (bp->b_resid > 0) {
431 /* Short read indicates EOF */
439 SDT_PROBE_DEFINE1(fusefs, , io, read_directbackend_start,
440 "struct fuse_read_in*");
441 SDT_PROBE_DEFINE3(fusefs, , io, read_directbackend_complete,
442 "struct fuse_dispatcher*", "struct fuse_read_in*", "struct uio*");
445 fuse_read_directbackend(struct vnode *vp, struct uio *uio,
446 struct ucred *cred, struct fuse_filehandle *fufh)
448 struct fuse_data *data;
449 struct fuse_dispatcher fdi;
450 struct fuse_read_in *fri;
453 data = fuse_get_mpdata(vp->v_mount);
455 if (uio->uio_resid == 0)
461 * XXX In "normal" case we use an intermediate kernel buffer for
462 * transmitting data from daemon's context to ours. Eventually, we should
463 * get rid of this. Anyway, if the target uio lives in sysspace (we are
464 * called from pageops), and the input data doesn't need kernel-side
465 * processing (we are not called from readdir) we can already invoke
466 * an optimized, "peer-to-peer" I/O routine.
468 while (uio->uio_resid > 0) {
469 fdi.iosize = sizeof(*fri);
470 fdisp_make_vp(&fdi, FUSE_READ, vp, uio->uio_td, cred);
472 fri->fh = fufh->fh_id;
473 fri->offset = uio->uio_offset;
474 fri->size = MIN(uio->uio_resid,
475 fuse_get_mpdata(vp->v_mount)->max_read);
476 if (fuse_libabi_geq(data, 7, 9)) {
477 /* See comment regarding FUSE_WRITE_LOCKOWNER */
479 fri->flags = fufh_type_2_fflags(fufh->fufh_type);
482 SDT_PROBE1(fusefs, , io, read_directbackend_start, fri);
484 if ((err = fdisp_wait_answ(&fdi)))
487 SDT_PROBE3(fusefs, , io, read_directbackend_complete,
490 if ((err = uiomove(fdi.answ, MIN(fri->size, fdi.iosize), uio)))
492 if (fdi.iosize < fri->size) {
494 * Short read. Should only happen at EOF or with
507 fuse_write_directbackend(struct vnode *vp, struct uio *uio,
508 struct ucred *cred, struct fuse_filehandle *fufh, off_t filesize,
509 int ioflag, bool pages)
511 struct fuse_vnode_data *fvdat = VTOFUD(vp);
512 struct fuse_data *data;
513 struct fuse_write_in *fwi;
514 struct fuse_write_out *fwo;
515 struct fuse_dispatcher fdi;
518 off_t as_written_offset;
521 bool direct_io = fufh->fuse_open_flags & FOPEN_DIRECT_IO;
522 bool wrote_anything = false;
523 uint32_t write_flags;
525 data = fuse_get_mpdata(vp->v_mount);
528 * Don't set FUSE_WRITE_LOCKOWNER in write_flags. It can't be set
529 * accurately when using POSIX AIO, libfuse doesn't use it, and I'm not
530 * aware of any file systems that do. It was an attempt to add
531 * Linux-style mandatory locking to the FUSE protocol, but mandatory
532 * locking is deprecated even on Linux. See Linux commit
533 * f33321141b273d60cbb3a8f56a5489baad82ba5e .
536 * Set FUSE_WRITE_CACHE whenever we don't know the uid, gid, and/or pid
537 * that originated a write. For example when writing from the
538 * writeback cache. I don't know of a single file system that cares,
539 * but the protocol says we're supposed to do this.
541 write_flags = !pages && (
542 (ioflag & IO_DIRECT) ||
543 !fsess_opt_datacache(vnode_mount(vp)) ||
544 !fsess_opt_writeback(vnode_mount(vp))) ? 0 : FUSE_WRITE_CACHE;
546 if (uio->uio_resid == 0)
549 if (ioflag & IO_APPEND)
550 uio_setoffset(uio, filesize);
552 if (vn_rlimit_fsize(vp, uio, uio->uio_td))
557 while (uio->uio_resid > 0) {
560 if (fuse_libabi_geq(data, 7, 9)) {
561 sizeof_fwi = sizeof(*fwi);
563 sizeof_fwi = FUSE_COMPAT_WRITE_IN_SIZE;
566 chunksize = MIN(uio->uio_resid, data->max_write);
568 fdi.iosize = sizeof_fwi + chunksize;
569 fdisp_make_vp(&fdi, FUSE_WRITE, vp, uio->uio_td, cred);
572 fwi->fh = fufh->fh_id;
573 fwi->offset = uio->uio_offset;
574 fwi->size = chunksize;
575 fwi->write_flags = write_flags;
576 if (fuse_libabi_geq(data, 7, 9)) {
577 fwi->flags = fufh_type_2_fflags(fufh->fufh_type);
579 fwi_data = (char *)fdi.indata + sizeof_fwi;
581 if ((err = uiomove(fwi_data, chunksize, uio)))
585 err = fdisp_wait_answ(&fdi);
586 if (err == ERESTART || err == EINTR || err == EWOULDBLOCK) {
588 * Rewind the uio so dofilewrite will know it's
591 uio->uio_resid += fwi->size;
592 uio->uio_offset -= fwi->size;
594 * Change ERESTART into EINTR because we can't rewind
595 * uio->uio_iov. Basically, once uiomove(9) has been
596 * called, it's impossible to restart a syscall.
604 wrote_anything = true;
607 fwo = ((struct fuse_write_out *)fdi.answ);
609 /* Adjust the uio in the case of short writes */
610 diff = fwi->size - fwo->size;
611 as_written_offset = uio->uio_offset - diff;
613 if (as_written_offset - diff > filesize)
614 fuse_vnode_setsize(vp, as_written_offset);
615 if (as_written_offset - diff >= filesize)
616 fvdat->flag &= ~FN_SIZECHANGE;
619 printf("WARNING: misbehaving FUSE filesystem "
620 "wrote more data than we provided it\n");
623 } else if (diff > 0) {
626 printf("WARNING: misbehaving FUSE filesystem: "
627 "short writes are only allowed with "
630 if (ioflag & IO_DIRECT) {
632 uio->uio_resid += diff;
633 uio->uio_offset -= diff;
636 /* Resend the unwritten portion of data */
637 fdi.iosize = sizeof_fwi + diff;
638 /* Refresh fdi without clearing data buffer */
639 fdisp_refresh_vp(&fdi, FUSE_WRITE, vp,
642 MPASS2(fwi == fdi.indata, "FUSE dispatcher "
643 "reallocated despite no increase in "
645 void *src = (char*)fwi_data + fwo->size;
646 memmove(fwi_data, src, diff);
647 fwi->fh = fufh->fh_id;
648 fwi->offset = as_written_offset;
650 fwi->write_flags = write_flags;
659 fuse_vnode_undirty_cached_timestamps(vp);
664 SDT_PROBE_DEFINE6(fusefs, , io, write_biobackend_start, "int64_t", "int", "int",
665 "struct uio*", "int", "bool");
666 SDT_PROBE_DEFINE2(fusefs, , io, write_biobackend_append_race, "long", "int");
667 SDT_PROBE_DEFINE2(fusefs, , io, write_biobackend_issue, "int", "struct buf*");
670 fuse_write_biobackend(struct vnode *vp, struct uio *uio,
671 struct ucred *cred, struct fuse_filehandle *fufh, int ioflag, pid_t pid)
673 struct fuse_vnode_data *fvdat = VTOFUD(vp);
678 int n, on, seqcount, err = 0;
681 const int biosize = fuse_iosize(vp);
683 seqcount = ioflag >> IO_SEQSHIFT;
685 KASSERT(uio->uio_rw == UIO_WRITE, ("fuse_write_biobackend mode"));
686 if (vp->v_type != VREG)
688 if (uio->uio_offset < 0)
690 if (uio->uio_resid == 0)
693 err = fuse_vnode_size(vp, &filesize, cred, curthread);
697 if (ioflag & IO_APPEND)
698 uio_setoffset(uio, filesize);
700 if (vn_rlimit_fsize(vp, uio, uio->uio_td))
704 bool direct_append, extending;
706 if (fuse_isdeadfs(vp)) {
710 lbn = uio->uio_offset / biosize;
711 on = uio->uio_offset & (biosize - 1);
712 n = MIN((unsigned)(biosize - on), uio->uio_resid);
715 /* Get or create a buffer for the write */
716 direct_append = uio->uio_offset == filesize && n;
717 if (uio->uio_offset + n < filesize) {
719 if ((off_t)(lbn + 1) * biosize < filesize) {
720 /* Not the file's last block */
723 /* The file's last block */
724 bcount = filesize - (off_t)lbn * biosize;
730 if (howmany(((off_t)lbn * biosize + on + n - 1), PAGE_SIZE) >=
731 howmany(filesize, PAGE_SIZE))
737 * Take care to preserve the buffer's B_CACHE state so
738 * as not to cause an unnecessary read.
740 bp = getblk(vp, lbn, on, PCATCH, 0, 0);
742 uint32_t save = bp->b_flags & B_CACHE;
743 allocbuf(bp, bcount);
747 bp = getblk(vp, lbn, bcount, PCATCH, 0, 0);
755 * Extend file _after_ locking buffer so we won't race
758 err = fuse_vnode_setsize(vp, uio->uio_offset + n);
759 filesize = uio->uio_offset + n;
760 fvdat->flag |= FN_SIZECHANGE;
767 SDT_PROBE6(fusefs, , io, write_biobackend_start,
768 lbn, on, n, uio, bcount, direct_append);
770 * Issue a READ if B_CACHE is not set. In special-append
771 * mode, B_CACHE is based on the buffer prior to the write
772 * op and is typically set, avoiding the read. If a read
773 * is required in special append mode, the server will
774 * probably send us a short-read since we extended the file
775 * on our end, resulting in b_resid == 0 and, thusly,
776 * B_CACHE getting set.
778 * We can also avoid issuing the read if the write covers
779 * the entire buffer. We have to make sure the buffer state
780 * is reasonable in this case since we will not be initiating
781 * I/O. See the comments in kern/vfs_bio.c's getblk() for
784 * B_CACHE may also be set due to the buffer being cached
788 if (on == 0 && n == bcount) {
789 bp->b_flags |= B_CACHE;
790 bp->b_flags &= ~B_INVAL;
791 bp->b_ioflags &= ~BIO_ERROR;
793 if ((bp->b_flags & B_CACHE) == 0) {
794 bp->b_iocmd = BIO_READ;
795 vfs_busy_pages(bp, 0);
796 fuse_io_strategy(vp, bp);
797 if ((err = bp->b_error)) {
801 if (bp->b_resid > 0) {
803 * Short read indicates EOF. Update file size
804 * from the server and try again.
806 SDT_PROBE2(fusefs, , io, trace, 1,
807 "Short read during a RMW");
809 err = fuse_vnode_size(vp, &filesize, cred,
817 if (bp->b_wcred == NOCRED)
818 bp->b_wcred = crhold(cred);
821 * If dirtyend exceeds file size, chop it down. This should
822 * not normally occur but there is an append race where it
823 * might occur XXX, so we log it.
825 * If the chopping creates a reverse-indexed or degenerate
826 * situation with dirtyoff/end, we 0 both of them.
828 if (bp->b_dirtyend > bcount) {
829 SDT_PROBE2(fusefs, , io, write_biobackend_append_race,
830 (long)bp->b_blkno * biosize,
831 bp->b_dirtyend - bcount);
832 bp->b_dirtyend = bcount;
834 if (bp->b_dirtyoff >= bp->b_dirtyend)
835 bp->b_dirtyoff = bp->b_dirtyend = 0;
838 * If the new write will leave a contiguous dirty
839 * area, just update the b_dirtyoff and b_dirtyend,
840 * otherwise force a write rpc of the old dirty area.
842 * While it is possible to merge discontiguous writes due to
843 * our having a B_CACHE buffer ( and thus valid read data
844 * for the hole), we don't because it could lead to
845 * significant cache coherency problems with multiple clients,
846 * especially if locking is implemented later on.
848 * as an optimization we could theoretically maintain
849 * a linked list of discontinuous areas, but we would still
850 * have to commit them separately so there isn't much
851 * advantage to it except perhaps a bit of asynchronization.
854 if (bp->b_dirtyend > 0 &&
855 (on > bp->b_dirtyend || (on + n) < bp->b_dirtyoff)) {
857 * Yes, we mean it. Write out everything to "storage"
858 * immediately, without hesitation. (Apart from other
859 * reasons: the only way to know if a write is valid
860 * if its actually written out.)
862 SDT_PROBE2(fusefs, , io, write_biobackend_issue, 0, bp);
864 if (bp->b_error == EINTR) {
870 err = uiomove((char *)bp->b_data + on, n, uio);
873 bp->b_ioflags |= BIO_ERROR;
877 /* TODO: vfs_bio_clrbuf like ffs_write does? */
880 * Only update dirtyoff/dirtyend if not a degenerate
884 if (bp->b_dirtyend > 0) {
885 bp->b_dirtyoff = MIN(on, bp->b_dirtyoff);
886 bp->b_dirtyend = MAX((on + n), bp->b_dirtyend);
889 bp->b_dirtyend = on + n;
891 vfs_bio_set_valid(bp, on, n);
894 vfs_bio_set_flags(bp, ioflag);
896 bp->b_flags |= B_FUSEFS_WRITE_CACHE;
897 if (ioflag & IO_SYNC) {
898 SDT_PROBE2(fusefs, , io, write_biobackend_issue, 2, bp);
899 if (!(ioflag & IO_VMIO))
900 bp->b_flags &= ~B_FUSEFS_WRITE_CACHE;
902 } else if (vm_page_count_severe() ||
903 buf_dirty_count_severe() ||
904 (ioflag & IO_ASYNC)) {
905 bp->b_flags |= B_CLUSTEROK;
906 SDT_PROBE2(fusefs, , io, write_biobackend_issue, 3, bp);
908 } else if (on == 0 && n == bcount) {
909 if ((vp->v_mount->mnt_flag & MNT_NOCLUSTERW) == 0) {
910 bp->b_flags |= B_CLUSTEROK;
911 SDT_PROBE2(fusefs, , io, write_biobackend_issue,
913 cluster_write(vp, bp, filesize, seqcount, 0);
915 SDT_PROBE2(fusefs, , io, write_biobackend_issue,
919 } else if (ioflag & IO_DIRECT) {
920 bp->b_flags |= B_CLUSTEROK;
921 SDT_PROBE2(fusefs, , io, write_biobackend_issue, 6, bp);
924 bp->b_flags &= ~B_CLUSTEROK;
925 SDT_PROBE2(fusefs, , io, write_biobackend_issue, 7, bp);
930 } while (uio->uio_resid > 0 && n > 0);
936 fuse_io_strategy(struct vnode *vp, struct buf *bp)
938 struct fuse_vnode_data *fvdat = VTOFUD(vp);
939 struct fuse_filehandle *fufh;
947 /* We don't know the true pid when we're dealing with the cache */
950 const int biosize = fuse_iosize(vp);
952 MPASS(vp->v_type == VREG || vp->v_type == VDIR);
953 MPASS(bp->b_iocmd == BIO_READ || bp->b_iocmd == BIO_WRITE);
955 fflag = bp->b_iocmd == BIO_READ ? FREAD : FWRITE;
956 cred = bp->b_iocmd == BIO_READ ? bp->b_rcred : bp->b_wcred;
957 error = fuse_filehandle_getrw(vp, fflag, &fufh, cred, pid);
958 if (bp->b_iocmd == BIO_READ && error == EBADF) {
960 * This may be a read-modify-write operation on a cached file
961 * opened O_WRONLY. The FUSE protocol allows this.
963 error = fuse_filehandle_get(vp, FWRITE, &fufh, cred, pid);
966 printf("FUSE: strategy: filehandles are closed\n");
967 bp->b_ioflags |= BIO_ERROR;
975 uiop->uio_iovcnt = 1;
976 uiop->uio_segflg = UIO_SYSSPACE;
977 uiop->uio_td = curthread;
980 * clear BIO_ERROR and B_INVAL state prior to initiating the I/O. We
981 * do this here so we do not have to do it in all the code that
984 bp->b_flags &= ~B_INVAL;
985 bp->b_ioflags &= ~BIO_ERROR;
987 KASSERT(!(bp->b_flags & B_DONE),
988 ("fuse_io_strategy: bp %p already marked done", bp));
989 if (bp->b_iocmd == BIO_READ) {
992 io.iov_len = uiop->uio_resid = bp->b_bcount;
993 io.iov_base = bp->b_data;
994 uiop->uio_rw = UIO_READ;
996 uiop->uio_offset = ((off_t)bp->b_lblkno) * biosize;
997 error = fuse_read_directbackend(vp, uiop, cred, fufh);
999 * Store the amount we failed to read in the buffer's private
1000 * field, so callers can truncate the file if necessary'
1003 if (!error && uiop->uio_resid) {
1004 int nread = bp->b_bcount - uiop->uio_resid;
1005 left = uiop->uio_resid;
1006 bzero((char *)bp->b_data + nread, left);
1008 if ((fvdat->flag & FN_SIZECHANGE) == 0) {
1010 * A short read with no error, when not using
1011 * direct io, and when no writes are cached,
1012 * indicates EOF caused by a server-side
1013 * truncation. Clear the attr cache so we'll
1014 * pick up the new file size and timestamps.
1016 * We must still bzero the remaining buffer so
1017 * uninitialized data doesn't get exposed by a
1018 * future truncate that extends the file.
1020 * To prevent lock order problems, we must
1021 * truncate the file upstack, not here.
1023 SDT_PROBE2(fusefs, , io, trace, 1,
1024 "Short read of a clean file");
1025 fuse_vnode_clear_attr_cache(vp);
1028 * If dirty writes _are_ cached beyond EOF,
1029 * that indicates a newly created hole that the
1030 * server doesn't know about. Those don't pose
1032 * XXX: we don't currently track whether dirty
1033 * writes are cached beyond EOF, before EOF, or
1036 SDT_PROBE2(fusefs, , io, trace, 1,
1037 "Short read of a dirty file");
1038 uiop->uio_resid = 0;
1043 bp->b_ioflags |= BIO_ERROR;
1044 bp->b_error = error;
1048 * Setup for actual write
1050 error = fuse_vnode_size(vp, &filesize, cred, curthread);
1052 bp->b_ioflags |= BIO_ERROR;
1053 bp->b_error = error;
1058 if ((off_t)bp->b_lblkno * biosize + bp->b_dirtyend > filesize)
1059 bp->b_dirtyend = filesize -
1060 (off_t)bp->b_lblkno * biosize;
1062 if (bp->b_dirtyend > bp->b_dirtyoff) {
1063 io.iov_len = uiop->uio_resid = bp->b_dirtyend
1065 uiop->uio_offset = (off_t)bp->b_lblkno * biosize
1067 io.iov_base = (char *)bp->b_data + bp->b_dirtyoff;
1068 uiop->uio_rw = UIO_WRITE;
1070 bool pages = bp->b_flags & B_FUSEFS_WRITE_CACHE;
1071 error = fuse_write_directbackend(vp, uiop, cred, fufh,
1072 filesize, 0, pages);
1074 if (error == EINTR || error == ETIMEDOUT) {
1075 bp->b_flags &= ~(B_INVAL | B_NOCACHE);
1076 if ((bp->b_flags & B_PAGING) == 0) {
1078 bp->b_flags &= ~B_DONE;
1080 if ((error == EINTR || error == ETIMEDOUT) &&
1081 (bp->b_flags & B_ASYNC) == 0)
1082 bp->b_flags |= B_EINTR;
1085 bp->b_ioflags |= BIO_ERROR;
1086 bp->b_flags |= B_INVAL;
1087 bp->b_error = error;
1089 bp->b_dirtyoff = bp->b_dirtyend = 0;
1097 bp->b_resid = uiop->uio_resid;
1103 fuse_io_flushbuf(struct vnode *vp, int waitfor, struct thread *td)
1106 return (vn_fsync_buf(vp, waitfor));
1110 * Flush and invalidate all dirty buffers. If another process is already
1111 * doing the flush, just wait for completion.
1114 fuse_io_invalbuf(struct vnode *vp, struct thread *td)
1116 struct fuse_vnode_data *fvdat = VTOFUD(vp);
1119 if (VN_IS_DOOMED(vp))
1122 ASSERT_VOP_ELOCKED(vp, "fuse_io_invalbuf");
1124 while (fvdat->flag & FN_FLUSHINPROG) {
1125 struct proc *p = td->td_proc;
1127 if (vp->v_mount->mnt_kern_flag & MNTK_UNMOUNTF)
1129 fvdat->flag |= FN_FLUSHWANT;
1130 tsleep(&fvdat->flag, PRIBIO + 2, "fusevinv", 2 * hz);
1134 if (SIGNOTEMPTY(p->p_siglist) ||
1135 SIGNOTEMPTY(td->td_siglist))
1142 fvdat->flag |= FN_FLUSHINPROG;
1144 if (vp->v_bufobj.bo_object != NULL) {
1145 VM_OBJECT_WLOCK(vp->v_bufobj.bo_object);
1146 vm_object_page_clean(vp->v_bufobj.bo_object, 0, 0, OBJPC_SYNC);
1147 VM_OBJECT_WUNLOCK(vp->v_bufobj.bo_object);
1149 error = vinvalbuf(vp, V_SAVE, PCATCH, 0);
1151 if (error == ERESTART || error == EINTR) {
1152 fvdat->flag &= ~FN_FLUSHINPROG;
1153 if (fvdat->flag & FN_FLUSHWANT) {
1154 fvdat->flag &= ~FN_FLUSHWANT;
1155 wakeup(&fvdat->flag);
1159 error = vinvalbuf(vp, V_SAVE, PCATCH, 0);
1161 fvdat->flag &= ~FN_FLUSHINPROG;
1162 if (fvdat->flag & FN_FLUSHWANT) {
1163 fvdat->flag &= ~FN_FLUSHWANT;
1164 wakeup(&fvdat->flag);