xref: /freebsd/sys/fs/fuse/fuse_io.c (revision 8ecc41918066422d6788a67251b22d11a6efeddf)
151369649SPedro F. Giffuni /*-
251369649SPedro F. Giffuni  * SPDX-License-Identifier: BSD-3-Clause
351369649SPedro F. Giffuni  *
45fe58019SAttilio Rao  * Copyright (c) 2007-2009 Google Inc.
55fe58019SAttilio Rao  * All rights reserved.
65fe58019SAttilio Rao  *
75fe58019SAttilio Rao  * Redistribution and use in source and binary forms, with or without
85fe58019SAttilio Rao  * modification, are permitted provided that the following conditions are
95fe58019SAttilio Rao  * met:
105fe58019SAttilio Rao  *
115fe58019SAttilio Rao  * * Redistributions of source code must retain the above copyright
125fe58019SAttilio Rao  *   notice, this list of conditions and the following disclaimer.
135fe58019SAttilio Rao  * * Redistributions in binary form must reproduce the above
145fe58019SAttilio Rao  *   copyright notice, this list of conditions and the following disclaimer
155fe58019SAttilio Rao  *   in the documentation and/or other materials provided with the
165fe58019SAttilio Rao  *   distribution.
175fe58019SAttilio Rao  * * Neither the name of Google Inc. nor the names of its
185fe58019SAttilio Rao  *   contributors may be used to endorse or promote products derived from
195fe58019SAttilio Rao  *   this software without specific prior written permission.
205fe58019SAttilio Rao  *
215fe58019SAttilio Rao  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
225fe58019SAttilio Rao  * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
235fe58019SAttilio Rao  * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
245fe58019SAttilio Rao  * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
255fe58019SAttilio Rao  * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
265fe58019SAttilio Rao  * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
275fe58019SAttilio Rao  * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
285fe58019SAttilio Rao  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
295fe58019SAttilio Rao  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
305fe58019SAttilio Rao  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
315fe58019SAttilio Rao  * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
325fe58019SAttilio Rao  *
335fe58019SAttilio Rao  * Copyright (C) 2005 Csaba Henk.
345fe58019SAttilio Rao  * All rights reserved.
355fe58019SAttilio Rao  *
368aafc8c3SAlan Somers  * Copyright (c) 2019 The FreeBSD Foundation
378aafc8c3SAlan Somers  *
388aafc8c3SAlan Somers  * Portions of this software were developed by BFF Storage Systems, LLC under
398aafc8c3SAlan Somers  * sponsorship from the FreeBSD Foundation.
408aafc8c3SAlan Somers  *
415fe58019SAttilio Rao  * Redistribution and use in source and binary forms, with or without
425fe58019SAttilio Rao  * modification, are permitted provided that the following conditions
435fe58019SAttilio Rao  * are met:
445fe58019SAttilio Rao  * 1. Redistributions of source code must retain the above copyright
455fe58019SAttilio Rao  *    notice, this list of conditions and the following disclaimer.
465fe58019SAttilio Rao  * 2. Redistributions in binary form must reproduce the above copyright
475fe58019SAttilio Rao  *    notice, this list of conditions and the following disclaimer in the
485fe58019SAttilio Rao  *    documentation and/or other materials provided with the distribution.
495fe58019SAttilio Rao  *
505fe58019SAttilio Rao  * THIS SOFTWARE IS PROVIDED BY AUTHOR AND CONTRIBUTORS ``AS IS'' AND
515fe58019SAttilio Rao  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
525fe58019SAttilio Rao  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
535fe58019SAttilio Rao  * ARE DISCLAIMED.  IN NO EVENT SHALL AUTHOR OR CONTRIBUTORS BE LIABLE
545fe58019SAttilio Rao  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
555fe58019SAttilio Rao  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
565fe58019SAttilio Rao  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
575fe58019SAttilio Rao  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
585fe58019SAttilio Rao  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
595fe58019SAttilio Rao  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
605fe58019SAttilio Rao  * SUCH DAMAGE.
615fe58019SAttilio Rao  */
625fe58019SAttilio Rao 
635fe58019SAttilio Rao #include <sys/types.h>
64df38ada2SBjoern A. Zeeb #include <sys/param.h>
655fe58019SAttilio Rao #include <sys/module.h>
665fe58019SAttilio Rao #include <sys/systm.h>
675fe58019SAttilio Rao #include <sys/errno.h>
685fe58019SAttilio Rao #include <sys/param.h>
695fe58019SAttilio Rao #include <sys/kernel.h>
705fe58019SAttilio Rao #include <sys/conf.h>
715fe58019SAttilio Rao #include <sys/uio.h>
725fe58019SAttilio Rao #include <sys/malloc.h>
735fe58019SAttilio Rao #include <sys/queue.h>
745fe58019SAttilio Rao #include <sys/lock.h>
755fe58019SAttilio Rao #include <sys/sx.h>
765fe58019SAttilio Rao #include <sys/mutex.h>
7789f6b863SAttilio Rao #include <sys/rwlock.h>
78a90e32deSAlan Somers #include <sys/priv.h>
795fe58019SAttilio Rao #include <sys/proc.h>
805fe58019SAttilio Rao #include <sys/mount.h>
815fe58019SAttilio Rao #include <sys/vnode.h>
825fe58019SAttilio Rao #include <sys/stat.h>
835fe58019SAttilio Rao #include <sys/unistd.h>
845fe58019SAttilio Rao #include <sys/filedesc.h>
855fe58019SAttilio Rao #include <sys/file.h>
865fe58019SAttilio Rao #include <sys/fcntl.h>
875fe58019SAttilio Rao #include <sys/bio.h>
885fe58019SAttilio Rao #include <sys/buf.h>
895fe58019SAttilio Rao #include <sys/sysctl.h>
90a87e0831SAlan Somers #include <sys/vmmeter.h>
915fe58019SAttilio Rao 
925fe58019SAttilio Rao #include <vm/vm.h>
935fe58019SAttilio Rao #include <vm/vm_extern.h>
945fe58019SAttilio Rao #include <vm/pmap.h>
955fe58019SAttilio Rao #include <vm/vm_map.h>
965fe58019SAttilio Rao #include <vm/vm_page.h>
975fe58019SAttilio Rao #include <vm/vm_object.h>
98b068bb09SKonstantin Belousov #include <vm/vnode_pager.h>
995fe58019SAttilio Rao 
1005fe58019SAttilio Rao #include "fuse.h"
1015fe58019SAttilio Rao #include "fuse_file.h"
1025fe58019SAttilio Rao #include "fuse_node.h"
1035fe58019SAttilio Rao #include "fuse_internal.h"
1045fe58019SAttilio Rao #include "fuse_ipc.h"
1055fe58019SAttilio Rao #include "fuse_io.h"
1065fe58019SAttilio Rao 
107b9e20197SAlan Somers /*
108b9e20197SAlan Somers  * Set in a struct buf to indicate that the write came from the buffer cache
109b9e20197SAlan Somers  * and the originating cred and pid are no longer known.
110b9e20197SAlan Somers  */
111b9e20197SAlan Somers #define B_FUSEFS_WRITE_CACHE B_FS_FLAG1
112b9e20197SAlan Somers 
113419e7ff6SAlan Somers SDT_PROVIDER_DECLARE(fusefs);
114cf169498SAlan Somers /*
115cf169498SAlan Somers  * Fuse trace probe:
116cf169498SAlan Somers  * arg0: verbosity.  Higher numbers give more verbose messages
117cf169498SAlan Somers  * arg1: Textual message
118cf169498SAlan Somers  */
119419e7ff6SAlan Somers SDT_PROBE_DEFINE2(fusefs, , io, trace, "int", "char*");
1205fe58019SAttilio Rao 
121a87e0831SAlan Somers SDT_PROBE_DEFINE4(fusefs, , io, read_bio_backend_start, "int", "int", "int", "int");
122dff3a6b4SAlan Somers SDT_PROBE_DEFINE2(fusefs, , io, read_bio_backend_feed, "int", "struct buf*");
123a87e0831SAlan Somers SDT_PROBE_DEFINE4(fusefs, , io, read_bio_backend_end, "int", "ssize_t", "int",
124a87e0831SAlan Somers 		"struct buf*");
125dc433e15SAlan Somers int
fuse_read_biobackend(struct vnode * vp,struct uio * uio,int ioflag,struct ucred * cred,struct fuse_filehandle * fufh,pid_t pid)12621d4686cSAlan Somers fuse_read_biobackend(struct vnode *vp, struct uio *uio, int ioflag,
127f8d4af10SAlan Somers     struct ucred *cred, struct fuse_filehandle *fufh, pid_t pid)
1285fe58019SAttilio Rao {
1295fe58019SAttilio Rao 	struct buf *bp;
130d569012fSAlan Somers 	struct mount *mp;
131d569012fSAlan Somers 	struct fuse_data *data;
132d569012fSAlan Somers 	daddr_t lbn, nextlbn;
133d569012fSAlan Somers 	int bcount, nextsize;
134d569012fSAlan Somers 	int err, n = 0, on = 0, seqcount;
1355fe58019SAttilio Rao 	off_t filesize;
1365fe58019SAttilio Rao 
1375fe58019SAttilio Rao 	const int biosize = fuse_iosize(vp);
138d569012fSAlan Somers 	mp = vnode_mount(vp);
139d569012fSAlan Somers 	data = fuse_get_mpdata(mp);
1405fe58019SAttilio Rao 
1415fe58019SAttilio Rao 	if (uio->uio_offset < 0)
1425fe58019SAttilio Rao 		return (EINVAL);
1435fe58019SAttilio Rao 
144d569012fSAlan Somers 	seqcount = ioflag >> IO_SEQSHIFT;
145d569012fSAlan Somers 
1463d15b234SAlan Somers 	err = fuse_vnode_size(vp, &filesize, cred, curthread);
1473d15b234SAlan Somers 	if (err)
1483d15b234SAlan Somers 		return err;
1495fe58019SAttilio Rao 
15021d4686cSAlan Somers 	for (err = 0, bp = NULL; uio->uio_resid > 0; bp = NULL) {
1515fe58019SAttilio Rao 		if (fuse_isdeadfs(vp)) {
1525fe58019SAttilio Rao 			err = ENXIO;
1535fe58019SAttilio Rao 			break;
1545fe58019SAttilio Rao 		}
15521d4686cSAlan Somers 		if (filesize - uio->uio_offset <= 0)
15621d4686cSAlan Somers 			break;
1575fe58019SAttilio Rao 		lbn = uio->uio_offset / biosize;
1585fe58019SAttilio Rao 		on = uio->uio_offset & (biosize - 1);
1595fe58019SAttilio Rao 
1605fe58019SAttilio Rao 		if ((off_t)lbn * biosize >= filesize) {
1615fe58019SAttilio Rao 			bcount = 0;
1625fe58019SAttilio Rao 		} else if ((off_t)(lbn + 1) * biosize > filesize) {
1635fe58019SAttilio Rao 			bcount = filesize - (off_t)lbn *biosize;
16421d4686cSAlan Somers 		} else {
16521d4686cSAlan Somers 			bcount = biosize;
1665fe58019SAttilio Rao 		}
167d569012fSAlan Somers 		nextlbn = lbn + 1;
168d569012fSAlan Somers 		nextsize = MIN(biosize, filesize - nextlbn * biosize);
1695fe58019SAttilio Rao 
170a87e0831SAlan Somers 		SDT_PROBE4(fusefs, , io, read_bio_backend_start,
171a87e0831SAlan Somers 			biosize, (int)lbn, on, bcount);
172a87e0831SAlan Somers 
173d569012fSAlan Somers 		if (bcount < biosize) {
174d569012fSAlan Somers 			/* If near EOF, don't do readahead */
17521d4686cSAlan Somers 			err = bread(vp, lbn, bcount, NOCRED, &bp);
176402b609cSAlan Somers 		} else if ((vp->v_mount->mnt_flag & MNT_NOCLUSTERR) == 0) {
177402b609cSAlan Somers 			/* Try clustered read */
178402b609cSAlan Somers 			long totread = uio->uio_resid + on;
179402b609cSAlan Somers 			seqcount = MIN(seqcount,
180a1c9f4adSAlan Somers 				data->max_readahead_blocks + 1);
181402b609cSAlan Somers 			err = cluster_read(vp, filesize, lbn, bcount, NOCRED,
182402b609cSAlan Somers 				totread, seqcount, 0, &bp);
183a1c9f4adSAlan Somers 		} else if (seqcount > 1 && data->max_readahead_blocks >= 1) {
184d569012fSAlan Somers 			/* Try non-clustered readahead */
185d569012fSAlan Somers 			err = breadn(vp, lbn, bcount, &nextlbn, &nextsize, 1,
186d569012fSAlan Somers 				NOCRED, &bp);
187d569012fSAlan Somers 		} else {
188d569012fSAlan Somers 			/* Just read what was requested */
189d569012fSAlan Somers 			err = bread(vp, lbn, bcount, NOCRED, &bp);
190d569012fSAlan Somers 		}
191d569012fSAlan Somers 
1925fe58019SAttilio Rao 		if (err) {
1935fe58019SAttilio Rao 			brelse(bp);
19421d4686cSAlan Somers 			bp = NULL;
19521d4686cSAlan Somers 			break;
1965fe58019SAttilio Rao 		}
19721d4686cSAlan Somers 
1985fe58019SAttilio Rao 		/*
1995fe58019SAttilio Rao 	         * on is the offset into the current bp.  Figure out how many
2005fe58019SAttilio Rao 	         * bytes we can copy out of the bp.  Note that bcount is
2015fe58019SAttilio Rao 	         * NOT DEV_BSIZE aligned.
2025fe58019SAttilio Rao 	         *
2035fe58019SAttilio Rao 	         * Then figure out how many bytes we can copy into the uio.
2045fe58019SAttilio Rao 	         */
2055fe58019SAttilio Rao 
2065fe58019SAttilio Rao 		n = 0;
207b9e20197SAlan Somers 		if (on < bcount - bp->b_resid)
208b9e20197SAlan Somers 			n = MIN((unsigned)(bcount - bp->b_resid - on),
209aef22f2dSAlan Somers 			    uio->uio_resid);
2105fe58019SAttilio Rao 		if (n > 0) {
211dff3a6b4SAlan Somers 			SDT_PROBE2(fusefs, , io, read_bio_backend_feed, n, bp);
2125fe58019SAttilio Rao 			err = uiomove(bp->b_data + on, n, uio);
2135fe58019SAttilio Rao 		}
21421d4686cSAlan Somers 		vfs_bio_brelse(bp, ioflag);
215a87e0831SAlan Somers 		SDT_PROBE4(fusefs, , io, read_bio_backend_end, err,
216a87e0831SAlan Somers 			uio->uio_resid, n, bp);
217b9e20197SAlan Somers 		if (bp->b_resid > 0) {
218aef22f2dSAlan Somers 			/* Short read indicates EOF */
219aef22f2dSAlan Somers 			break;
220aef22f2dSAlan Somers 		}
22121d4686cSAlan Somers 	}
2225fe58019SAttilio Rao 
2235fe58019SAttilio Rao 	return (err);
2245fe58019SAttilio Rao }
2255fe58019SAttilio Rao 
226419e7ff6SAlan Somers SDT_PROBE_DEFINE1(fusefs, , io, read_directbackend_start,
227419e7ff6SAlan Somers 	"struct fuse_read_in*");
228dff3a6b4SAlan Somers SDT_PROBE_DEFINE3(fusefs, , io, read_directbackend_complete,
229dff3a6b4SAlan Somers 	"struct fuse_dispatcher*", "struct fuse_read_in*", "struct uio*");
230cf169498SAlan Somers 
231dc433e15SAlan Somers int
fuse_read_directbackend(struct vnode * vp,struct uio * uio,struct ucred * cred,struct fuse_filehandle * fufh)2325fe58019SAttilio Rao fuse_read_directbackend(struct vnode *vp, struct uio *uio,
2335fe58019SAttilio Rao     struct ucred *cred, struct fuse_filehandle *fufh)
2345fe58019SAttilio Rao {
23516bd2d47SAlan Somers 	struct fuse_data *data;
2365fe58019SAttilio Rao 	struct fuse_dispatcher fdi;
2375fe58019SAttilio Rao 	struct fuse_read_in *fri;
2385fe58019SAttilio Rao 	int err = 0;
2395fe58019SAttilio Rao 
24016bd2d47SAlan Somers 	data = fuse_get_mpdata(vp->v_mount);
24116bd2d47SAlan Somers 
2425fe58019SAttilio Rao 	if (uio->uio_resid == 0)
2435fe58019SAttilio Rao 		return (0);
2445fe58019SAttilio Rao 
2455fe58019SAttilio Rao 	fdisp_init(&fdi, 0);
2465fe58019SAttilio Rao 
2475fe58019SAttilio Rao 	/*
2485fe58019SAttilio Rao          * XXX In "normal" case we use an intermediate kernel buffer for
2495fe58019SAttilio Rao          * transmitting data from daemon's context to ours. Eventually, we should
2505fe58019SAttilio Rao          * get rid of this. Anyway, if the target uio lives in sysspace (we are
2515fe58019SAttilio Rao          * called from pageops), and the input data doesn't need kernel-side
2525fe58019SAttilio Rao          * processing (we are not called from readdir) we can already invoke
2535fe58019SAttilio Rao          * an optimized, "peer-to-peer" I/O routine.
2545fe58019SAttilio Rao          */
2555fe58019SAttilio Rao 	while (uio->uio_resid > 0) {
2565fe58019SAttilio Rao 		fdi.iosize = sizeof(*fri);
2575fe58019SAttilio Rao 		fdisp_make_vp(&fdi, FUSE_READ, vp, uio->uio_td, cred);
2585fe58019SAttilio Rao 		fri = fdi.indata;
2595fe58019SAttilio Rao 		fri->fh = fufh->fh_id;
2605fe58019SAttilio Rao 		fri->offset = uio->uio_offset;
2615fe58019SAttilio Rao 		fri->size = MIN(uio->uio_resid,
2625fe58019SAttilio Rao 		    fuse_get_mpdata(vp->v_mount)->max_read);
26316bd2d47SAlan Somers 		if (fuse_libabi_geq(data, 7, 9)) {
26416bd2d47SAlan Somers 			/* See comment regarding FUSE_WRITE_LOCKOWNER */
26516bd2d47SAlan Somers 			fri->read_flags = 0;
266d4fd0c81SAlan Somers 			fri->flags = fufh_type_2_fflags(fufh->fufh_type);
26716bd2d47SAlan Somers 		}
2685fe58019SAttilio Rao 
269419e7ff6SAlan Somers 		SDT_PROBE1(fusefs, , io, read_directbackend_start, fri);
2705fe58019SAttilio Rao 
2715fe58019SAttilio Rao 		if ((err = fdisp_wait_answ(&fdi)))
2725fe58019SAttilio Rao 			goto out;
2735fe58019SAttilio Rao 
274dff3a6b4SAlan Somers 		SDT_PROBE3(fusefs, , io, read_directbackend_complete,
275dff3a6b4SAlan Somers 			&fdi, fri, uio);
2765fe58019SAttilio Rao 
2775fe58019SAttilio Rao 		if ((err = uiomove(fdi.answ, MIN(fri->size, fdi.iosize), uio)))
2785fe58019SAttilio Rao 			break;
279aef22f2dSAlan Somers 		if (fdi.iosize < fri->size) {
280aef22f2dSAlan Somers 			/*
281aef22f2dSAlan Somers 			 * Short read.  Should only happen at EOF or with
282aef22f2dSAlan Somers 			 * direct io.
283aef22f2dSAlan Somers 			 */
2845fe58019SAttilio Rao 			break;
2855fe58019SAttilio Rao 		}
286aef22f2dSAlan Somers 	}
2875fe58019SAttilio Rao 
2885fe58019SAttilio Rao out:
2895fe58019SAttilio Rao 	fdisp_destroy(&fdi);
2905fe58019SAttilio Rao 	return (err);
2915fe58019SAttilio Rao }
2925fe58019SAttilio Rao 
293dc433e15SAlan Somers int
fuse_write_directbackend(struct vnode * vp,struct uio * uio,struct ucred * cred,struct fuse_filehandle * fufh,off_t filesize,int ioflag,bool pages)2945fe58019SAttilio Rao fuse_write_directbackend(struct vnode *vp, struct uio *uio,
2953d15b234SAlan Somers     struct ucred *cred, struct fuse_filehandle *fufh, off_t filesize,
296bda39894SAlan Somers     int ioflag, bool pages)
2975fe58019SAttilio Rao {
2985fe58019SAttilio Rao 	struct fuse_vnode_data *fvdat = VTOFUD(vp);
29916bd2d47SAlan Somers 	struct fuse_data *data;
3005fe58019SAttilio Rao 	struct fuse_write_in *fwi;
30112292a99SAlan Somers 	struct fuse_write_out *fwo;
3025fe58019SAttilio Rao 	struct fuse_dispatcher fdi;
3035fe58019SAttilio Rao 	size_t chunksize;
304be280f60SAlan Somers 	ssize_t r;
30512292a99SAlan Somers 	void *fwi_data;
30612292a99SAlan Somers 	off_t as_written_offset;
3075fe58019SAttilio Rao 	int diff;
3085fe58019SAttilio Rao 	int err = 0;
30912292a99SAlan Somers 	bool direct_io = fufh->fuse_open_flags & FOPEN_DIRECT_IO;
310788af953SAlan Somers 	bool wrote_anything = false;
311bda39894SAlan Somers 	uint32_t write_flags;
3125fe58019SAttilio Rao 
31316bd2d47SAlan Somers 	data = fuse_get_mpdata(vp->v_mount);
31416bd2d47SAlan Somers 
315bda39894SAlan Somers 	/*
316bda39894SAlan Somers 	 * Don't set FUSE_WRITE_LOCKOWNER in write_flags.  It can't be set
317bda39894SAlan Somers 	 * accurately when using POSIX AIO, libfuse doesn't use it, and I'm not
318bda39894SAlan Somers 	 * aware of any file systems that do.  It was an attempt to add
319bda39894SAlan Somers 	 * Linux-style mandatory locking to the FUSE protocol, but mandatory
320bda39894SAlan Somers 	 * locking is deprecated even on Linux.  See Linux commit
321bda39894SAlan Somers 	 * f33321141b273d60cbb3a8f56a5489baad82ba5e .
322bda39894SAlan Somers 	 */
323bda39894SAlan Somers 	/*
324bda39894SAlan Somers 	 * Set FUSE_WRITE_CACHE whenever we don't know the uid, gid, and/or pid
325bda39894SAlan Somers 	 * that originated a write.  For example when writing from the
326bda39894SAlan Somers 	 * writeback cache.  I don't know of a single file system that cares,
327bda39894SAlan Somers 	 * but the protocol says we're supposed to do this.
328bda39894SAlan Somers 	 */
329bda39894SAlan Somers 	write_flags = !pages && (
330bda39894SAlan Somers 		(ioflag & IO_DIRECT) ||
331bda39894SAlan Somers 		!fsess_opt_datacache(vnode_mount(vp)) ||
332f8ebf1cdSAlan Somers 		!fsess_opt_writeback(vnode_mount(vp))) ? 0 : FUSE_WRITE_CACHE;
333bda39894SAlan Somers 
334bb751fbbSConrad Meyer 	if (uio->uio_resid == 0)
3355fe58019SAttilio Rao 		return (0);
3363d15b234SAlan Somers 
337bb751fbbSConrad Meyer 	if (ioflag & IO_APPEND)
3383d15b234SAlan Somers 		uio_setoffset(uio, filesize);
3395fe58019SAttilio Rao 
340be280f60SAlan Somers 	err = vn_rlimit_fsizex(vp, uio, 0, &r, uio->uio_td);
341be280f60SAlan Somers 	if (err != 0) {
342be280f60SAlan Somers 		vn_rlimit_fsizex_res(uio, r);
343cc65a412SKonstantin Belousov 		return (err);
344be280f60SAlan Somers 	}
345a639731bSAlan Somers 
3465fe58019SAttilio Rao 	fdisp_init(&fdi, 0);
3475fe58019SAttilio Rao 
3485fe58019SAttilio Rao 	while (uio->uio_resid > 0) {
3496c0c3620SAlan Somers 		size_t sizeof_fwi;
3506c0c3620SAlan Somers 
3516c0c3620SAlan Somers 		if (fuse_libabi_geq(data, 7, 9)) {
3526c0c3620SAlan Somers 			sizeof_fwi = sizeof(*fwi);
3536c0c3620SAlan Somers 		} else {
3546c0c3620SAlan Somers 			sizeof_fwi = FUSE_COMPAT_WRITE_IN_SIZE;
3556c0c3620SAlan Somers 		}
3566c0c3620SAlan Somers 
35716bd2d47SAlan Somers 		chunksize = MIN(uio->uio_resid, data->max_write);
3585fe58019SAttilio Rao 
3596c0c3620SAlan Somers 		fdi.iosize = sizeof_fwi + chunksize;
3605fe58019SAttilio Rao 		fdisp_make_vp(&fdi, FUSE_WRITE, vp, uio->uio_td, cred);
3615fe58019SAttilio Rao 
3625fe58019SAttilio Rao 		fwi = fdi.indata;
3635fe58019SAttilio Rao 		fwi->fh = fufh->fh_id;
3645fe58019SAttilio Rao 		fwi->offset = uio->uio_offset;
3655fe58019SAttilio Rao 		fwi->size = chunksize;
366bda39894SAlan Somers 		fwi->write_flags = write_flags;
36716bd2d47SAlan Somers 		if (fuse_libabi_geq(data, 7, 9)) {
368d4fd0c81SAlan Somers 			fwi->flags = fufh_type_2_fflags(fufh->fufh_type);
36916bd2d47SAlan Somers 		}
3706c0c3620SAlan Somers 		fwi_data = (char *)fdi.indata + sizeof_fwi;
3715fe58019SAttilio Rao 
37212292a99SAlan Somers 		if ((err = uiomove(fwi_data, chunksize, uio)))
3735fe58019SAttilio Rao 			break;
3745fe58019SAttilio Rao 
37512292a99SAlan Somers retry:
376723c7768SAlan Somers 		err = fdisp_wait_answ(&fdi);
377723c7768SAlan Somers 		if (err == ERESTART || err == EINTR || err == EWOULDBLOCK) {
378723c7768SAlan Somers 			/*
379723c7768SAlan Somers 			 * Rewind the uio so dofilewrite will know it's
380723c7768SAlan Somers 			 * incomplete
381723c7768SAlan Somers 			 */
382723c7768SAlan Somers 			uio->uio_resid += fwi->size;
383723c7768SAlan Somers 			uio->uio_offset -= fwi->size;
384723c7768SAlan Somers 			/*
385723c7768SAlan Somers 			 * Change ERESTART into EINTR because we can't rewind
386723c7768SAlan Somers 			 * uio->uio_iov.  Basically, once uiomove(9) has been
387723c7768SAlan Somers 			 * called, it's impossible to restart a syscall.
388723c7768SAlan Somers 			 */
389723c7768SAlan Somers 			if (err == ERESTART)
390723c7768SAlan Somers 				err = EINTR;
3915fe58019SAttilio Rao 			break;
392723c7768SAlan Somers 		} else if (err) {
393723c7768SAlan Somers 			break;
394788af953SAlan Somers 		} else {
395788af953SAlan Somers 			wrote_anything = true;
396723c7768SAlan Somers 		}
3975fe58019SAttilio Rao 
39812292a99SAlan Somers 		fwo = ((struct fuse_write_out *)fdi.answ);
39912292a99SAlan Somers 
4003a1b3c6aSAlan Somers 		if (fwo->size > fwi->size) {
4013a1b3c6aSAlan Somers 			fuse_warn(data, FSESS_WARN_WROTE_LONG,
4023a1b3c6aSAlan Somers 				"wrote more data than we provided it.");
4033a1b3c6aSAlan Somers 			/* This is bonkers.  Clear attr cache. */
4043a1b3c6aSAlan Somers 			fvdat->flag &= ~FN_SIZECHANGE;
4053a1b3c6aSAlan Somers 			fuse_vnode_clear_attr_cache(vp);
4063a1b3c6aSAlan Somers 			err = EINVAL;
4073a1b3c6aSAlan Somers 			break;
4083a1b3c6aSAlan Somers 		}
4093a1b3c6aSAlan Somers 
41084c4fd1fSAlan Somers 		/* Adjust the uio in the case of short writes */
41112292a99SAlan Somers 		diff = fwi->size - fwo->size;
4123a1b3c6aSAlan Somers 
41312292a99SAlan Somers 		as_written_offset = uio->uio_offset - diff;
41412292a99SAlan Somers 
41513d593a5SAlan Somers 		if (as_written_offset - diff > filesize) {
4165d94aaacSAlan Somers 			fuse_vnode_setsize(vp, as_written_offset, false);
41713d593a5SAlan Somers 			getnanouptime(&fvdat->last_local_modify);
41813d593a5SAlan Somers 		}
4192013b723SAlan Somers 		if (as_written_offset - diff >= filesize)
42012292a99SAlan Somers 			fvdat->flag &= ~FN_SIZECHANGE;
42112292a99SAlan Somers 
4223a1b3c6aSAlan Somers 		if (diff > 0) {
42312292a99SAlan Somers 			/* Short write */
42412292a99SAlan Somers 			if (!direct_io) {
4250b9a5c6fSAlan Somers 				fuse_warn(data, FSESS_WARN_SHORT_WRITE,
42612292a99SAlan Somers 					"short writes are only allowed with "
4270b9a5c6fSAlan Somers 					"direct_io.");
4285fe58019SAttilio Rao 			}
42912292a99SAlan Somers 			if (ioflag & IO_DIRECT) {
43012292a99SAlan Somers 				/* Return early */
4315fe58019SAttilio Rao 				uio->uio_resid += diff;
4325fe58019SAttilio Rao 				uio->uio_offset -= diff;
43312292a99SAlan Somers 				break;
43412292a99SAlan Somers 			} else {
43512292a99SAlan Somers 				/* Resend the unwritten portion of data */
4366c0c3620SAlan Somers 				fdi.iosize = sizeof_fwi + diff;
43712292a99SAlan Somers 				/* Refresh fdi without clearing data buffer */
43812292a99SAlan Somers 				fdisp_refresh_vp(&fdi, FUSE_WRITE, vp,
43912292a99SAlan Somers 					uio->uio_td, cred);
44012292a99SAlan Somers 				fwi = fdi.indata;
44112292a99SAlan Somers 				MPASS2(fwi == fdi.indata, "FUSE dispatcher "
44212292a99SAlan Somers 					"reallocated despite no increase in "
44312292a99SAlan Somers 					"size?");
44412292a99SAlan Somers 				void *src = (char*)fwi_data + fwo->size;
44512292a99SAlan Somers 				memmove(fwi_data, src, diff);
44612292a99SAlan Somers 				fwi->fh = fufh->fh_id;
44712292a99SAlan Somers 				fwi->offset = as_written_offset;
44812292a99SAlan Somers 				fwi->size = diff;
449bda39894SAlan Somers 				fwi->write_flags = write_flags;
45012292a99SAlan Somers 				goto retry;
45112292a99SAlan Somers 			}
452194e691aSConrad Meyer 		}
4535fe58019SAttilio Rao 	}
4545fe58019SAttilio Rao 
4555fe58019SAttilio Rao 	fdisp_destroy(&fdi);
4565fe58019SAttilio Rao 
457788af953SAlan Somers 	if (wrote_anything)
45891972cfcSAlan Somers 		fuse_vnode_undirty_cached_timestamps(vp, false);
459788af953SAlan Somers 
460be280f60SAlan Somers 	vn_rlimit_fsizex_res(uio, r);
4615fe58019SAttilio Rao 	return (err);
4625fe58019SAttilio Rao }
4635fe58019SAttilio Rao 
464419e7ff6SAlan Somers SDT_PROBE_DEFINE6(fusefs, , io, write_biobackend_start, "int64_t", "int", "int",
465cf169498SAlan Somers 		"struct uio*", "int", "bool");
466419e7ff6SAlan Somers SDT_PROBE_DEFINE2(fusefs, , io, write_biobackend_append_race, "long", "int");
467dff3a6b4SAlan Somers SDT_PROBE_DEFINE2(fusefs, , io, write_biobackend_issue, "int", "struct buf*");
468cf169498SAlan Somers 
469dc433e15SAlan Somers int
fuse_write_biobackend(struct vnode * vp,struct uio * uio,struct ucred * cred,struct fuse_filehandle * fufh,int ioflag,pid_t pid)4705fe58019SAttilio Rao fuse_write_biobackend(struct vnode *vp, struct uio *uio,
471f8d4af10SAlan Somers     struct ucred *cred, struct fuse_filehandle *fufh, int ioflag, pid_t pid)
4725fe58019SAttilio Rao {
4732013b723SAlan Somers 	struct fuse_vnode_data *fvdat = VTOFUD(vp);
4745fe58019SAttilio Rao 	struct buf *bp;
4755fe58019SAttilio Rao 	daddr_t lbn;
4763d15b234SAlan Somers 	off_t filesize;
477be280f60SAlan Somers 	ssize_t r;
4785fe58019SAttilio Rao 	int bcount;
4798eecd9ceSAlan Somers 	int n, on, seqcount, err = 0;
4805fe58019SAttilio Rao 
4815fe58019SAttilio Rao 	const int biosize = fuse_iosize(vp);
4825fe58019SAttilio Rao 
4838eecd9ceSAlan Somers 	seqcount = ioflag >> IO_SEQSHIFT;
4848eecd9ceSAlan Somers 
485ddc51e45SAlan Somers 	KASSERT(uio->uio_rw == UIO_WRITE, ("fuse_write_biobackend mode"));
4865fe58019SAttilio Rao 	if (vp->v_type != VREG)
4875fe58019SAttilio Rao 		return (EIO);
4885fe58019SAttilio Rao 	if (uio->uio_offset < 0)
4895fe58019SAttilio Rao 		return (EINVAL);
4905fe58019SAttilio Rao 	if (uio->uio_resid == 0)
4915fe58019SAttilio Rao 		return (0);
4923d15b234SAlan Somers 
4933d15b234SAlan Somers 	err = fuse_vnode_size(vp, &filesize, cred, curthread);
4943d15b234SAlan Somers 	if (err)
4953d15b234SAlan Somers 		return err;
4963d15b234SAlan Somers 
4973d94054cSBaptiste Daroussin 	if (ioflag & IO_APPEND)
4983d15b234SAlan Somers 		uio_setoffset(uio, filesize);
4995fe58019SAttilio Rao 
500be280f60SAlan Somers 	err = vn_rlimit_fsizex(vp, uio, 0, &r, uio->uio_td);
501be280f60SAlan Somers 	if (err != 0) {
502be280f60SAlan Somers 		vn_rlimit_fsizex_res(uio, r);
503cc65a412SKonstantin Belousov 		return (err);
504be280f60SAlan Somers 	}
505a639731bSAlan Somers 
5065fe58019SAttilio Rao 	do {
507011bca99SAlan Somers 		bool direct_append, extending;
508011bca99SAlan Somers 
5095fe58019SAttilio Rao 		if (fuse_isdeadfs(vp)) {
5105fe58019SAttilio Rao 			err = ENXIO;
5115fe58019SAttilio Rao 			break;
5125fe58019SAttilio Rao 		}
5135fe58019SAttilio Rao 		lbn = uio->uio_offset / biosize;
5145fe58019SAttilio Rao 		on = uio->uio_offset & (biosize - 1);
5155fe58019SAttilio Rao 		n = MIN((unsigned)(biosize - on), uio->uio_resid);
5165fe58019SAttilio Rao 
5175fe58019SAttilio Rao again:
518011bca99SAlan Somers 		/* Get or create a buffer for the write */
519011bca99SAlan Somers 		direct_append = uio->uio_offset == filesize && n;
520dff3a6b4SAlan Somers 		if (uio->uio_offset + n < filesize) {
521011bca99SAlan Somers 			extending = false;
522011bca99SAlan Somers 			if ((off_t)(lbn + 1) * biosize < filesize) {
523011bca99SAlan Somers 				/* Not the file's last block */
524011bca99SAlan Somers 				bcount = biosize;
525011bca99SAlan Somers 			} else {
526011bca99SAlan Somers 				/* The file's last block */
527011bca99SAlan Somers 				bcount = filesize - (off_t)lbn * biosize;
5285fe58019SAttilio Rao 			}
529011bca99SAlan Somers 		} else {
530011bca99SAlan Somers 			extending = true;
531011bca99SAlan Somers 			bcount = on + n;
532011bca99SAlan Somers 		}
533011bca99SAlan Somers 		if (direct_append) {
534011bca99SAlan Somers 			/*
535011bca99SAlan Somers 			 * Take care to preserve the buffer's B_CACHE state so
536011bca99SAlan Somers 			 * as not to cause an unnecessary read.
537011bca99SAlan Somers 			 */
538011bca99SAlan Somers 			bp = getblk(vp, lbn, on, PCATCH, 0, 0);
539011bca99SAlan Somers 			if (bp != NULL) {
540011bca99SAlan Somers 				uint32_t save = bp->b_flags & B_CACHE;
5415fe58019SAttilio Rao 				allocbuf(bp, bcount);
5425fe58019SAttilio Rao 				bp->b_flags |= save;
5435fe58019SAttilio Rao 			}
5445fe58019SAttilio Rao 		} else {
5455fe58019SAttilio Rao 			bp = getblk(vp, lbn, bcount, PCATCH, 0, 0);
546011bca99SAlan Somers 		}
547011bca99SAlan Somers 		if (!bp) {
548011bca99SAlan Somers 			err = EINTR;
549011bca99SAlan Somers 			break;
550011bca99SAlan Somers 		}
551011bca99SAlan Somers 		if (extending) {
552011bca99SAlan Somers 			/*
553011bca99SAlan Somers 			 * Extend file _after_ locking buffer so we won't race
554011bca99SAlan Somers 			 * with other readers
555011bca99SAlan Somers 			 */
5565d94aaacSAlan Somers 			err = fuse_vnode_setsize(vp, uio->uio_offset + n, false);
5578eecd9ceSAlan Somers 			filesize = uio->uio_offset + n;
55813d593a5SAlan Somers 			getnanouptime(&fvdat->last_local_modify);
5592013b723SAlan Somers 			fvdat->flag |= FN_SIZECHANGE;
5605fe58019SAttilio Rao 			if (err) {
5615fe58019SAttilio Rao 				brelse(bp);
5625fe58019SAttilio Rao 				break;
5635fe58019SAttilio Rao 			}
5645fe58019SAttilio Rao 		}
5655fe58019SAttilio Rao 
566011bca99SAlan Somers 		SDT_PROBE6(fusefs, , io, write_biobackend_start,
567011bca99SAlan Somers 			lbn, on, n, uio, bcount, direct_append);
5685fe58019SAttilio Rao 		/*
5695fe58019SAttilio Rao 	         * Issue a READ if B_CACHE is not set.  In special-append
5705fe58019SAttilio Rao 	         * mode, B_CACHE is based on the buffer prior to the write
5715fe58019SAttilio Rao 	         * op and is typically set, avoiding the read.  If a read
5725fe58019SAttilio Rao 	         * is required in special append mode, the server will
5735fe58019SAttilio Rao 	         * probably send us a short-read since we extended the file
5745fe58019SAttilio Rao 	         * on our end, resulting in b_resid == 0 and, thusly,
5755fe58019SAttilio Rao 	         * B_CACHE getting set.
5765fe58019SAttilio Rao 	         *
5775fe58019SAttilio Rao 	         * We can also avoid issuing the read if the write covers
5785fe58019SAttilio Rao 	         * the entire buffer.  We have to make sure the buffer state
5795fe58019SAttilio Rao 	         * is reasonable in this case since we will not be initiating
5805fe58019SAttilio Rao 	         * I/O.  See the comments in kern/vfs_bio.c's getblk() for
5815fe58019SAttilio Rao 	         * more information.
5825fe58019SAttilio Rao 	         *
5835fe58019SAttilio Rao 	         * B_CACHE may also be set due to the buffer being cached
5845fe58019SAttilio Rao 	         * normally.
5855fe58019SAttilio Rao 	         */
5865fe58019SAttilio Rao 
5875fe58019SAttilio Rao 		if (on == 0 && n == bcount) {
5885fe58019SAttilio Rao 			bp->b_flags |= B_CACHE;
5895fe58019SAttilio Rao 			bp->b_flags &= ~B_INVAL;
5905fe58019SAttilio Rao 			bp->b_ioflags &= ~BIO_ERROR;
5915fe58019SAttilio Rao 		}
5925fe58019SAttilio Rao 		if ((bp->b_flags & B_CACHE) == 0) {
5935fe58019SAttilio Rao 			bp->b_iocmd = BIO_READ;
5945fe58019SAttilio Rao 			vfs_busy_pages(bp, 0);
5955fe58019SAttilio Rao 			fuse_io_strategy(vp, bp);
5965fe58019SAttilio Rao 			if ((err = bp->b_error)) {
5975fe58019SAttilio Rao 				brelse(bp);
5985fe58019SAttilio Rao 				break;
5995fe58019SAttilio Rao 			}
600b9e20197SAlan Somers 			if (bp->b_resid > 0) {
601b9e20197SAlan Somers 				/*
602b9e20197SAlan Somers 				 * Short read indicates EOF.  Update file size
603b9e20197SAlan Somers 				 * from the server and try again.
604b9e20197SAlan Somers 				 */
605b9e20197SAlan Somers 				SDT_PROBE2(fusefs, , io, trace, 1,
606b9e20197SAlan Somers 					"Short read during a RMW");
607b9e20197SAlan Somers 				brelse(bp);
608b9e20197SAlan Somers 				err = fuse_vnode_size(vp, &filesize, cred,
609b9e20197SAlan Somers 				    curthread);
610b9e20197SAlan Somers 				if (err)
611b9e20197SAlan Somers 					break;
612b9e20197SAlan Somers 				else
613b9e20197SAlan Somers 					goto again;
614b9e20197SAlan Somers 			}
6155fe58019SAttilio Rao 		}
6165fe58019SAttilio Rao 		if (bp->b_wcred == NOCRED)
6175fe58019SAttilio Rao 			bp->b_wcred = crhold(cred);
6185fe58019SAttilio Rao 
6195fe58019SAttilio Rao 		/*
6205fe58019SAttilio Rao 	         * If dirtyend exceeds file size, chop it down.  This should
6215fe58019SAttilio Rao 	         * not normally occur but there is an append race where it
6225fe58019SAttilio Rao 	         * might occur XXX, so we log it.
6235fe58019SAttilio Rao 	         *
6245fe58019SAttilio Rao 	         * If the chopping creates a reverse-indexed or degenerate
6255fe58019SAttilio Rao 	         * situation with dirtyoff/end, we 0 both of them.
6265fe58019SAttilio Rao 	         */
6275fe58019SAttilio Rao 		if (bp->b_dirtyend > bcount) {
628419e7ff6SAlan Somers 			SDT_PROBE2(fusefs, , io, write_biobackend_append_race,
6295fe58019SAttilio Rao 			    (long)bp->b_blkno * biosize,
6305fe58019SAttilio Rao 			    bp->b_dirtyend - bcount);
6315fe58019SAttilio Rao 			bp->b_dirtyend = bcount;
6325fe58019SAttilio Rao 		}
6335fe58019SAttilio Rao 		if (bp->b_dirtyoff >= bp->b_dirtyend)
6345fe58019SAttilio Rao 			bp->b_dirtyoff = bp->b_dirtyend = 0;
6355fe58019SAttilio Rao 
6365fe58019SAttilio Rao 		/*
6375fe58019SAttilio Rao 	         * If the new write will leave a contiguous dirty
6385fe58019SAttilio Rao 	         * area, just update the b_dirtyoff and b_dirtyend,
6395fe58019SAttilio Rao 	         * otherwise force a write rpc of the old dirty area.
6405fe58019SAttilio Rao 	         *
6415fe58019SAttilio Rao 	         * While it is possible to merge discontiguous writes due to
6425fe58019SAttilio Rao 	         * our having a B_CACHE buffer ( and thus valid read data
6435fe58019SAttilio Rao 	         * for the hole), we don't because it could lead to
6445fe58019SAttilio Rao 	         * significant cache coherency problems with multiple clients,
6455fe58019SAttilio Rao 	         * especially if locking is implemented later on.
6465fe58019SAttilio Rao 	         *
6475fe58019SAttilio Rao 	         * as an optimization we could theoretically maintain
6485fe58019SAttilio Rao 	         * a linked list of discontinuous areas, but we would still
6495fe58019SAttilio Rao 	         * have to commit them separately so there isn't much
6505fe58019SAttilio Rao 	         * advantage to it except perhaps a bit of asynchronization.
6515fe58019SAttilio Rao 	         */
6525fe58019SAttilio Rao 
6535fe58019SAttilio Rao 		if (bp->b_dirtyend > 0 &&
6545fe58019SAttilio Rao 		    (on > bp->b_dirtyend || (on + n) < bp->b_dirtyoff)) {
6555fe58019SAttilio Rao 			/*
6565fe58019SAttilio Rao 	                 * Yes, we mean it. Write out everything to "storage"
657b3a15dddSPedro F. Giffuni 	                 * immediately, without hesitation. (Apart from other
6585fe58019SAttilio Rao 	                 * reasons: the only way to know if a write is valid
6595fe58019SAttilio Rao 	                 * if its actually written out.)
6605fe58019SAttilio Rao 	                 */
661dff3a6b4SAlan Somers 			SDT_PROBE2(fusefs, , io, write_biobackend_issue, 0, bp);
6625fe58019SAttilio Rao 			bwrite(bp);
6635fe58019SAttilio Rao 			if (bp->b_error == EINTR) {
6645fe58019SAttilio Rao 				err = EINTR;
6655fe58019SAttilio Rao 				break;
6665fe58019SAttilio Rao 			}
6675fe58019SAttilio Rao 			goto again;
6685fe58019SAttilio Rao 		}
6695fe58019SAttilio Rao 		err = uiomove((char *)bp->b_data + on, n, uio);
6705fe58019SAttilio Rao 
6715fe58019SAttilio Rao 		if (err) {
6725fe58019SAttilio Rao 			bp->b_ioflags |= BIO_ERROR;
6735fe58019SAttilio Rao 			bp->b_error = err;
6745fe58019SAttilio Rao 			brelse(bp);
6755fe58019SAttilio Rao 			break;
676a87e0831SAlan Somers 			/* TODO: vfs_bio_clrbuf like ffs_write does? */
6775fe58019SAttilio Rao 		}
6785fe58019SAttilio Rao 		/*
6795fe58019SAttilio Rao 	         * Only update dirtyoff/dirtyend if not a degenerate
6805fe58019SAttilio Rao 	         * condition.
6815fe58019SAttilio Rao 	         */
6825fe58019SAttilio Rao 		if (n) {
6835fe58019SAttilio Rao 			if (bp->b_dirtyend > 0) {
6845fe58019SAttilio Rao 				bp->b_dirtyoff = MIN(on, bp->b_dirtyoff);
6855fe58019SAttilio Rao 				bp->b_dirtyend = MAX((on + n), bp->b_dirtyend);
6865fe58019SAttilio Rao 			} else {
6875fe58019SAttilio Rao 				bp->b_dirtyoff = on;
6885fe58019SAttilio Rao 				bp->b_dirtyend = on + n;
6895fe58019SAttilio Rao 			}
6905fe58019SAttilio Rao 			vfs_bio_set_valid(bp, on, n);
6915fe58019SAttilio Rao 		}
692a87e0831SAlan Somers 
693a87e0831SAlan Somers 		vfs_bio_set_flags(bp, ioflag);
694a87e0831SAlan Somers 
695b9e20197SAlan Somers 		bp->b_flags |= B_FUSEFS_WRITE_CACHE;
69684879e46SAlan Somers 		if (ioflag & IO_SYNC) {
697dff3a6b4SAlan Somers 			SDT_PROBE2(fusefs, , io, write_biobackend_issue, 2, bp);
698b9e20197SAlan Somers 			if (!(ioflag & IO_VMIO))
699b9e20197SAlan Somers 				bp->b_flags &= ~B_FUSEFS_WRITE_CACHE;
700a87e0831SAlan Somers 			err = bwrite(bp);
701a87e0831SAlan Somers 		} else if (vm_page_count_severe() ||
702a87e0831SAlan Somers 			    buf_dirty_count_severe() ||
703a87e0831SAlan Somers 			    (ioflag & IO_ASYNC)) {
7048eecd9ceSAlan Somers 			bp->b_flags |= B_CLUSTEROK;
705dff3a6b4SAlan Somers 			SDT_PROBE2(fusefs, , io, write_biobackend_issue, 3, bp);
706a87e0831SAlan Somers 			bawrite(bp);
707a87e0831SAlan Somers 		} else if (on == 0 && n == bcount) {
708dff3a6b4SAlan Somers 			if ((vp->v_mount->mnt_flag & MNT_NOCLUSTERW) == 0) {
7098eecd9ceSAlan Somers 				bp->b_flags |= B_CLUSTEROK;
710dff3a6b4SAlan Somers 				SDT_PROBE2(fusefs, , io, write_biobackend_issue,
711dff3a6b4SAlan Somers 					4, bp);
7122bfd8992SKonstantin Belousov 				cluster_write(vp, &fvdat->clusterw, bp,
7132bfd8992SKonstantin Belousov 				    filesize, seqcount, 0);
714dff3a6b4SAlan Somers 			} else {
715dff3a6b4SAlan Somers 				SDT_PROBE2(fusefs, , io, write_biobackend_issue,
716dff3a6b4SAlan Somers 					5, bp);
717a87e0831SAlan Somers 				bawrite(bp);
718dff3a6b4SAlan Somers 			}
719a87e0831SAlan Somers 		} else if (ioflag & IO_DIRECT) {
7208eecd9ceSAlan Somers 			bp->b_flags |= B_CLUSTEROK;
721dff3a6b4SAlan Somers 			SDT_PROBE2(fusefs, , io, write_biobackend_issue, 6, bp);
722a87e0831SAlan Somers 			bawrite(bp);
723a87e0831SAlan Somers 		} else {
724a87e0831SAlan Somers 			bp->b_flags &= ~B_CLUSTEROK;
725dff3a6b4SAlan Somers 			SDT_PROBE2(fusefs, , io, write_biobackend_issue, 7, bp);
726a87e0831SAlan Somers 			bdwrite(bp);
727a87e0831SAlan Somers 		}
7285fe58019SAttilio Rao 		if (err)
7295fe58019SAttilio Rao 			break;
7305fe58019SAttilio Rao 	} while (uio->uio_resid > 0 && n > 0);
7315fe58019SAttilio Rao 
732be280f60SAlan Somers 	vn_rlimit_fsizex_res(uio, r);
7335fe58019SAttilio Rao 	return (err);
7345fe58019SAttilio Rao }
7355fe58019SAttilio Rao 
7365fe58019SAttilio Rao int
fuse_io_strategy(struct vnode * vp,struct buf * bp)7375fe58019SAttilio Rao fuse_io_strategy(struct vnode *vp, struct buf *bp)
7385fe58019SAttilio Rao {
739aef22f2dSAlan Somers 	struct fuse_vnode_data *fvdat = VTOFUD(vp);
7405fe58019SAttilio Rao 	struct fuse_filehandle *fufh;
7415fe58019SAttilio Rao 	struct ucred *cred;
7425fe58019SAttilio Rao 	struct uio *uiop;
7435fe58019SAttilio Rao 	struct uio uio;
7445fe58019SAttilio Rao 	struct iovec io;
7453d15b234SAlan Somers 	off_t filesize;
7465fe58019SAttilio Rao 	int error = 0;
7479f10f423SAlan Somers 	int fflag;
748f8d4af10SAlan Somers 	/* We don't know the true pid when we're dealing with the cache */
749f8d4af10SAlan Somers 	pid_t pid = 0;
7505fe58019SAttilio Rao 
7515fe58019SAttilio Rao 	const int biosize = fuse_iosize(vp);
7525fe58019SAttilio Rao 
7532810826dSAttilio Rao 	MPASS(vp->v_type == VREG || vp->v_type == VDIR);
7545fe58019SAttilio Rao 	MPASS(bp->b_iocmd == BIO_READ || bp->b_iocmd == BIO_WRITE);
7555fe58019SAttilio Rao 
7569f10f423SAlan Somers 	fflag = bp->b_iocmd == BIO_READ ? FREAD : FWRITE;
757f8d4af10SAlan Somers 	cred = bp->b_iocmd == BIO_READ ? bp->b_rcred : bp->b_wcred;
7589f10f423SAlan Somers 	error = fuse_filehandle_getrw(vp, fflag, &fufh, cred, pid);
7595fccbf31SAlan Somers 	if (bp->b_iocmd == BIO_READ && error == EBADF) {
7605fccbf31SAlan Somers 		/*
7615fccbf31SAlan Somers 		 * This may be a read-modify-write operation on a cached file
7625fccbf31SAlan Somers 		 * opened O_WRONLY.  The FUSE protocol allows this.
7635fccbf31SAlan Somers 		 */
7649f10f423SAlan Somers 		error = fuse_filehandle_get(vp, FWRITE, &fufh, cred, pid);
7655fccbf31SAlan Somers 	}
7665fe58019SAttilio Rao 	if (error) {
7675fe58019SAttilio Rao 		printf("FUSE: strategy: filehandles are closed\n");
7685fe58019SAttilio Rao 		bp->b_ioflags |= BIO_ERROR;
7695fe58019SAttilio Rao 		bp->b_error = error;
77098852a32SAlan Somers 		bufdone(bp);
7715fe58019SAttilio Rao 		return (error);
7725fe58019SAttilio Rao 	}
7735fe58019SAttilio Rao 
7745fe58019SAttilio Rao 	uiop = &uio;
7755fe58019SAttilio Rao 	uiop->uio_iov = &io;
7765fe58019SAttilio Rao 	uiop->uio_iovcnt = 1;
7775fe58019SAttilio Rao 	uiop->uio_segflg = UIO_SYSSPACE;
7785fe58019SAttilio Rao 	uiop->uio_td = curthread;
7795fe58019SAttilio Rao 
7805fe58019SAttilio Rao 	/*
7815fe58019SAttilio Rao          * clear BIO_ERROR and B_INVAL state prior to initiating the I/O.  We
7825fe58019SAttilio Rao          * do this here so we do not have to do it in all the code that
7835fe58019SAttilio Rao          * calls us.
7845fe58019SAttilio Rao          */
7855fe58019SAttilio Rao 	bp->b_flags &= ~B_INVAL;
7865fe58019SAttilio Rao 	bp->b_ioflags &= ~BIO_ERROR;
7875fe58019SAttilio Rao 
7885fe58019SAttilio Rao 	KASSERT(!(bp->b_flags & B_DONE),
7895fe58019SAttilio Rao 	    ("fuse_io_strategy: bp %p already marked done", bp));
7905fe58019SAttilio Rao 	if (bp->b_iocmd == BIO_READ) {
79117575badSAlan Somers 		ssize_t left;
79217575badSAlan Somers 
7935fe58019SAttilio Rao 		io.iov_len = uiop->uio_resid = bp->b_bcount;
7945fe58019SAttilio Rao 		io.iov_base = bp->b_data;
7955fe58019SAttilio Rao 		uiop->uio_rw = UIO_READ;
7965fe58019SAttilio Rao 
797a87e0831SAlan Somers 		uiop->uio_offset = ((off_t)bp->b_lblkno) * biosize;
7985fe58019SAttilio Rao 		error = fuse_read_directbackend(vp, uiop, cred, fufh);
79917575badSAlan Somers 		/*
80017575badSAlan Somers 		 * Store the amount we failed to read in the buffer's private
80117575badSAlan Somers 		 * field, so callers can truncate the file if necessary'
80217575badSAlan Somers 		 */
8035fe58019SAttilio Rao 
804bad4c94dSAlan Somers 		if (!error && uiop->uio_resid) {
80544f654fdSAlan Somers 			int nread = bp->b_bcount - uiop->uio_resid;
806b9e20197SAlan Somers 			left = uiop->uio_resid;
80744f654fdSAlan Somers 			bzero((char *)bp->b_data + nread, left);
80844f654fdSAlan Somers 
809b9e20197SAlan Somers 			if ((fvdat->flag & FN_SIZECHANGE) == 0) {
810aef22f2dSAlan Somers 				/*
81144f654fdSAlan Somers 				 * A short read with no error, when not using
81244f654fdSAlan Somers 				 * direct io, and when no writes are cached,
813b9e20197SAlan Somers 				 * indicates EOF caused by a server-side
814b9e20197SAlan Somers 				 * truncation.  Clear the attr cache so we'll
815b9e20197SAlan Somers 				 * pick up the new file size and timestamps.
816b9e20197SAlan Somers 				 *
817b9e20197SAlan Somers 				 * We must still bzero the remaining buffer so
818b9e20197SAlan Somers 				 * uninitialized data doesn't get exposed by a
819b9e20197SAlan Somers 				 * future truncate that extends the file.
82044f654fdSAlan Somers 				 *
82117575badSAlan Somers 				 * To prevent lock order problems, we must
822b9e20197SAlan Somers 				 * truncate the file upstack, not here.
823aef22f2dSAlan Somers 				 */
82444f654fdSAlan Somers 				SDT_PROBE2(fusefs, , io, trace, 1,
82544f654fdSAlan Somers 					"Short read of a clean file");
826b9e20197SAlan Somers 				fuse_vnode_clear_attr_cache(vp);
827aef22f2dSAlan Somers 			} else {
828aef22f2dSAlan Somers 				/*
829aef22f2dSAlan Somers 				 * If dirty writes _are_ cached beyond EOF,
830aef22f2dSAlan Somers 				 * that indicates a newly created hole that the
831b9e20197SAlan Somers 				 * server doesn't know about.  Those don't pose
832b9e20197SAlan Somers 				 * any problem.
833aef22f2dSAlan Somers 				 * XXX: we don't currently track whether dirty
834aef22f2dSAlan Somers 				 * writes are cached beyond EOF, before EOF, or
835aef22f2dSAlan Somers 				 * both.
836aef22f2dSAlan Somers 				 */
837aef22f2dSAlan Somers 				SDT_PROBE2(fusefs, , io, trace, 1,
838aef22f2dSAlan Somers 					"Short read of a dirty file");
8395fe58019SAttilio Rao 				uiop->uio_resid = 0;
8405fe58019SAttilio Rao 			}
841aef22f2dSAlan Somers 		}
8425fe58019SAttilio Rao 		if (error) {
8435fe58019SAttilio Rao 			bp->b_ioflags |= BIO_ERROR;
8445fe58019SAttilio Rao 			bp->b_error = error;
8455fe58019SAttilio Rao 		}
8465fe58019SAttilio Rao 	} else {
8475fe58019SAttilio Rao 		/*
8485fe58019SAttilio Rao 	         * Setup for actual write
8495fe58019SAttilio Rao 	         */
850032a5bd5SAlan Somers 		/*
851032a5bd5SAlan Somers 		 * If the file's size is cached, use that value, even if the
852032a5bd5SAlan Somers 		 * cache is expired.  At this point we're already committed to
853032a5bd5SAlan Somers 		 * writing something.  If the FUSE server has changed the
854032a5bd5SAlan Somers 		 * file's size behind our back, it's too late for us to do
855032a5bd5SAlan Somers 		 * anything about it.  In particular, we can't invalidate any
856032a5bd5SAlan Somers 		 * part of the file's buffers because VOP_STRATEGY is called
857032a5bd5SAlan Somers 		 * with them already locked.
858032a5bd5SAlan Somers 		 */
859032a5bd5SAlan Somers 		filesize = fvdat->cached_attrs.va_size;
860032a5bd5SAlan Somers 		/* filesize must've been cached by fuse_vnop_open.  */
861032a5bd5SAlan Somers 		KASSERT(filesize != VNOVAL, ("filesize should've been cached"));
8623d15b234SAlan Somers 
863a87e0831SAlan Somers 		if ((off_t)bp->b_lblkno * biosize + bp->b_dirtyend > filesize)
8643d15b234SAlan Somers 			bp->b_dirtyend = filesize -
865a87e0831SAlan Somers 				(off_t)bp->b_lblkno * biosize;
8665fe58019SAttilio Rao 
8675fe58019SAttilio Rao 		if (bp->b_dirtyend > bp->b_dirtyoff) {
8685fe58019SAttilio Rao 			io.iov_len = uiop->uio_resid = bp->b_dirtyend
8695fe58019SAttilio Rao 			    - bp->b_dirtyoff;
870a87e0831SAlan Somers 			uiop->uio_offset = (off_t)bp->b_lblkno * biosize
8715fe58019SAttilio Rao 			    + bp->b_dirtyoff;
8725fe58019SAttilio Rao 			io.iov_base = (char *)bp->b_data + bp->b_dirtyoff;
8735fe58019SAttilio Rao 			uiop->uio_rw = UIO_WRITE;
8745fe58019SAttilio Rao 
875b9e20197SAlan Somers 			bool pages = bp->b_flags & B_FUSEFS_WRITE_CACHE;
8763d15b234SAlan Somers 			error = fuse_write_directbackend(vp, uiop, cred, fufh,
877b9e20197SAlan Somers 				filesize, 0, pages);
8785fe58019SAttilio Rao 
879ddc51e45SAlan Somers 			if (error == EINTR || error == ETIMEDOUT) {
8805fe58019SAttilio Rao 				bp->b_flags &= ~(B_INVAL | B_NOCACHE);
8815fe58019SAttilio Rao 				if ((bp->b_flags & B_PAGING) == 0) {
8825fe58019SAttilio Rao 					bdirty(bp);
8835fe58019SAttilio Rao 					bp->b_flags &= ~B_DONE;
8845fe58019SAttilio Rao 				}
8855fe58019SAttilio Rao 				if ((error == EINTR || error == ETIMEDOUT) &&
8865fe58019SAttilio Rao 				    (bp->b_flags & B_ASYNC) == 0)
8875fe58019SAttilio Rao 					bp->b_flags |= B_EINTR;
8885fe58019SAttilio Rao 			} else {
8895fe58019SAttilio Rao 				if (error) {
8905fe58019SAttilio Rao 					bp->b_ioflags |= BIO_ERROR;
8915fe58019SAttilio Rao 					bp->b_flags |= B_INVAL;
8925fe58019SAttilio Rao 					bp->b_error = error;
8935fe58019SAttilio Rao 				}
8945fe58019SAttilio Rao 				bp->b_dirtyoff = bp->b_dirtyend = 0;
8955fe58019SAttilio Rao 			}
8965fe58019SAttilio Rao 		} else {
8975fe58019SAttilio Rao 			bp->b_resid = 0;
8985fe58019SAttilio Rao 			bufdone(bp);
8995fe58019SAttilio Rao 			return (0);
9005fe58019SAttilio Rao 		}
9015fe58019SAttilio Rao 	}
9025fe58019SAttilio Rao 	bp->b_resid = uiop->uio_resid;
9035fe58019SAttilio Rao 	bufdone(bp);
9045fe58019SAttilio Rao 	return (error);
9055fe58019SAttilio Rao }
9065fe58019SAttilio Rao 
9075fe58019SAttilio Rao int
fuse_io_flushbuf(struct vnode * vp,int waitfor,struct thread * td)9085fe58019SAttilio Rao fuse_io_flushbuf(struct vnode *vp, int waitfor, struct thread *td)
9095fe58019SAttilio Rao {
9105fe58019SAttilio Rao 
911ae909414SKonstantin Belousov 	return (vn_fsync_buf(vp, waitfor));
9125fe58019SAttilio Rao }
9135fe58019SAttilio Rao 
9145fe58019SAttilio Rao /*
9155fe58019SAttilio Rao  * Flush and invalidate all dirty buffers. If another process is already
9165fe58019SAttilio Rao  * doing the flush, just wait for completion.
9175fe58019SAttilio Rao  */
9185fe58019SAttilio Rao int
fuse_io_invalbuf(struct vnode * vp,struct thread * td)9195fe58019SAttilio Rao fuse_io_invalbuf(struct vnode *vp, struct thread *td)
9205fe58019SAttilio Rao {
9215fe58019SAttilio Rao 	struct fuse_vnode_data *fvdat = VTOFUD(vp);
9225fe58019SAttilio Rao 	int error = 0;
9235fe58019SAttilio Rao 
924abd80ddbSMateusz Guzik 	if (VN_IS_DOOMED(vp))
9255fe58019SAttilio Rao 		return 0;
9265fe58019SAttilio Rao 
9275fe58019SAttilio Rao 	ASSERT_VOP_ELOCKED(vp, "fuse_io_invalbuf");
9285fe58019SAttilio Rao 
9295fe58019SAttilio Rao 	while (fvdat->flag & FN_FLUSHINPROG) {
9305fe58019SAttilio Rao 		struct proc *p = td->td_proc;
9315fe58019SAttilio Rao 
9325fe58019SAttilio Rao 		if (vp->v_mount->mnt_kern_flag & MNTK_UNMOUNTF)
9335fe58019SAttilio Rao 			return EIO;
9345fe58019SAttilio Rao 		fvdat->flag |= FN_FLUSHWANT;
935*8ecc4191SOlivier Certner 		tsleep(&fvdat->flag, PRIBIO, "fusevinv", 2 * hz);
9365fe58019SAttilio Rao 		error = 0;
9375fe58019SAttilio Rao 		if (p != NULL) {
9385fe58019SAttilio Rao 			PROC_LOCK(p);
9395fe58019SAttilio Rao 			if (SIGNOTEMPTY(p->p_siglist) ||
9405fe58019SAttilio Rao 			    SIGNOTEMPTY(td->td_siglist))
9415fe58019SAttilio Rao 				error = EINTR;
9425fe58019SAttilio Rao 			PROC_UNLOCK(p);
9435fe58019SAttilio Rao 		}
9445fe58019SAttilio Rao 		if (error == EINTR)
9455fe58019SAttilio Rao 			return EINTR;
9465fe58019SAttilio Rao 	}
9475fe58019SAttilio Rao 	fvdat->flag |= FN_FLUSHINPROG;
9485fe58019SAttilio Rao 
949b068bb09SKonstantin Belousov 	vnode_pager_clean_sync(vp);
9505fe58019SAttilio Rao 	error = vinvalbuf(vp, V_SAVE, PCATCH, 0);
9515fe58019SAttilio Rao 	while (error) {
9525fe58019SAttilio Rao 		if (error == ERESTART || error == EINTR) {
9535fe58019SAttilio Rao 			fvdat->flag &= ~FN_FLUSHINPROG;
9545fe58019SAttilio Rao 			if (fvdat->flag & FN_FLUSHWANT) {
9555fe58019SAttilio Rao 				fvdat->flag &= ~FN_FLUSHWANT;
9565fe58019SAttilio Rao 				wakeup(&fvdat->flag);
9575fe58019SAttilio Rao 			}
9585fe58019SAttilio Rao 			return EINTR;
9595fe58019SAttilio Rao 		}
9605fe58019SAttilio Rao 		error = vinvalbuf(vp, V_SAVE, PCATCH, 0);
9615fe58019SAttilio Rao 	}
9625fe58019SAttilio Rao 	fvdat->flag &= ~FN_FLUSHINPROG;
9635fe58019SAttilio Rao 	if (fvdat->flag & FN_FLUSHWANT) {
9645fe58019SAttilio Rao 		fvdat->flag &= ~FN_FLUSHWANT;
9655fe58019SAttilio Rao 		wakeup(&fvdat->flag);
9665fe58019SAttilio Rao 	}
9675fe58019SAttilio Rao 	return (error);
9685fe58019SAttilio Rao }
969