fuse_io.c revision 331722
1/*
2 * Copyright (c) 2007-2009 Google Inc.
3 * All rights reserved.
4 *
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions are
7 * met:
8 *
9 * * Redistributions of source code must retain the above copyright
10 *   notice, this list of conditions and the following disclaimer.
11 * * Redistributions in binary form must reproduce the above
12 *   copyright notice, this list of conditions and the following disclaimer
13 *   in the documentation and/or other materials provided with the
14 *   distribution.
15 * * Neither the name of Google Inc. nor the names of its
16 *   contributors may be used to endorse or promote products derived from
17 *   this software without specific prior written permission.
18 *
19 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
20 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
21 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
22 * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
23 * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
24 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
25 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
26 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
27 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
28 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
29 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
30 *
31 * Copyright (C) 2005 Csaba Henk.
32 * All rights reserved.
33 *
34 * Redistribution and use in source and binary forms, with or without
35 * modification, are permitted provided that the following conditions
36 * are met:
37 * 1. Redistributions of source code must retain the above copyright
38 *    notice, this list of conditions and the following disclaimer.
39 * 2. Redistributions in binary form must reproduce the above copyright
40 *    notice, this list of conditions and the following disclaimer in the
41 *    documentation and/or other materials provided with the distribution.
42 *
43 * THIS SOFTWARE IS PROVIDED BY AUTHOR AND CONTRIBUTORS ``AS IS'' AND
44 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
45 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
46 * ARE DISCLAIMED.  IN NO EVENT SHALL AUTHOR OR CONTRIBUTORS BE LIABLE
47 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
48 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
49 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
50 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
51 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
52 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
53 * SUCH DAMAGE.
54 */
55
56#include <sys/cdefs.h>
57__FBSDID("$FreeBSD: stable/11/sys/fs/fuse/fuse_io.c 331722 2018-03-29 02:50:57Z eadler $");
58
59#include <sys/types.h>
60#include <sys/module.h>
61#include <sys/systm.h>
62#include <sys/errno.h>
63#include <sys/param.h>
64#include <sys/kernel.h>
65#include <sys/conf.h>
66#include <sys/uio.h>
67#include <sys/malloc.h>
68#include <sys/queue.h>
69#include <sys/lock.h>
70#include <sys/sx.h>
71#include <sys/mutex.h>
72#include <sys/rwlock.h>
73#include <sys/proc.h>
74#include <sys/mount.h>
75#include <sys/vnode.h>
76#include <sys/stat.h>
77#include <sys/unistd.h>
78#include <sys/filedesc.h>
79#include <sys/file.h>
80#include <sys/fcntl.h>
81#include <sys/bio.h>
82#include <sys/buf.h>
83#include <sys/sysctl.h>
84
85#include <vm/vm.h>
86#include <vm/vm_extern.h>
87#include <vm/pmap.h>
88#include <vm/vm_map.h>
89#include <vm/vm_page.h>
90#include <vm/vm_object.h>
91
92#include "fuse.h"
93#include "fuse_file.h"
94#include "fuse_node.h"
95#include "fuse_internal.h"
96#include "fuse_ipc.h"
97#include "fuse_io.h"
98
99#define FUSE_DEBUG_MODULE IO
100#include "fuse_debug.h"
101
102
103static int
104fuse_read_directbackend(struct vnode *vp, struct uio *uio,
105    struct ucred *cred, struct fuse_filehandle *fufh);
106static int
107fuse_read_biobackend(struct vnode *vp, struct uio *uio,
108    struct ucred *cred, struct fuse_filehandle *fufh);
109static int
110fuse_write_directbackend(struct vnode *vp, struct uio *uio,
111    struct ucred *cred, struct fuse_filehandle *fufh, int ioflag);
112static int
113fuse_write_biobackend(struct vnode *vp, struct uio *uio,
114    struct ucred *cred, struct fuse_filehandle *fufh, int ioflag);
115
116int
117fuse_io_dispatch(struct vnode *vp, struct uio *uio, int ioflag,
118    struct ucred *cred)
119{
120	struct fuse_filehandle *fufh;
121	int err, directio;
122
123	MPASS(vp->v_type == VREG || vp->v_type == VDIR);
124
125	err = fuse_filehandle_getrw(vp,
126	    (uio->uio_rw == UIO_READ) ? FUFH_RDONLY : FUFH_WRONLY, &fufh);
127	if (err) {
128		printf("FUSE: io dispatch: filehandles are closed\n");
129		return err;
130	}
131	/*
132         * Ideally, when the daemon asks for direct io at open time, the
133         * standard file flag should be set according to this, so that would
134         * just change the default mode, which later on could be changed via
135         * fcntl(2).
136         * But this doesn't work, the O_DIRECT flag gets cleared at some point
137         * (don't know where). So to make any use of the Fuse direct_io option,
138         * we hardwire it into the file's private data (similarly to Linux,
139         * btw.).
140         */
141	directio = (ioflag & IO_DIRECT) || !fsess_opt_datacache(vnode_mount(vp));
142
143	switch (uio->uio_rw) {
144	case UIO_READ:
145		if (directio) {
146			FS_DEBUG("direct read of vnode %ju via file handle %ju\n",
147			    (uintmax_t)VTOILLU(vp), (uintmax_t)fufh->fh_id);
148			err = fuse_read_directbackend(vp, uio, cred, fufh);
149		} else {
150			FS_DEBUG("buffered read of vnode %ju\n",
151			      (uintmax_t)VTOILLU(vp));
152			err = fuse_read_biobackend(vp, uio, cred, fufh);
153		}
154		break;
155	case UIO_WRITE:
156		if (directio) {
157			FS_DEBUG("direct write of vnode %ju via file handle %ju\n",
158			    (uintmax_t)VTOILLU(vp), (uintmax_t)fufh->fh_id);
159			err = fuse_write_directbackend(vp, uio, cred, fufh, ioflag);
160		} else {
161			FS_DEBUG("buffered write of vnode %ju\n",
162			      (uintmax_t)VTOILLU(vp));
163			err = fuse_write_biobackend(vp, uio, cred, fufh, ioflag);
164		}
165		break;
166	default:
167		panic("uninterpreted mode passed to fuse_io_dispatch");
168	}
169
170	return (err);
171}
172
173static int
174fuse_read_biobackend(struct vnode *vp, struct uio *uio,
175    struct ucred *cred, struct fuse_filehandle *fufh)
176{
177	struct buf *bp;
178	daddr_t lbn;
179	int bcount;
180	int err = 0, n = 0, on = 0;
181	off_t filesize;
182
183	const int biosize = fuse_iosize(vp);
184
185	FS_DEBUG("resid=%zx offset=%jx fsize=%jx\n",
186	    uio->uio_resid, uio->uio_offset, VTOFUD(vp)->filesize);
187
188	if (uio->uio_resid == 0)
189		return (0);
190	if (uio->uio_offset < 0)
191		return (EINVAL);
192
193	bcount = MIN(MAXBSIZE, biosize);
194	filesize = VTOFUD(vp)->filesize;
195
196	do {
197		if (fuse_isdeadfs(vp)) {
198			err = ENXIO;
199			break;
200		}
201		lbn = uio->uio_offset / biosize;
202		on = uio->uio_offset & (biosize - 1);
203
204		FS_DEBUG2G("biosize %d, lbn %d, on %d\n", biosize, (int)lbn, on);
205
206		/*
207	         * Obtain the buffer cache block.  Figure out the buffer size
208	         * when we are at EOF.  If we are modifying the size of the
209	         * buffer based on an EOF condition we need to hold
210	         * nfs_rslock() through obtaining the buffer to prevent
211	         * a potential writer-appender from messing with n_size.
212	         * Otherwise we may accidentally truncate the buffer and
213	         * lose dirty data.
214	         *
215	         * Note that bcount is *not* DEV_BSIZE aligned.
216	         */
217		if ((off_t)lbn * biosize >= filesize) {
218			bcount = 0;
219		} else if ((off_t)(lbn + 1) * biosize > filesize) {
220			bcount = filesize - (off_t)lbn *biosize;
221		}
222		bp = getblk(vp, lbn, bcount, PCATCH, 0, 0);
223
224		if (!bp)
225			return (EINTR);
226
227		/*
228	         * If B_CACHE is not set, we must issue the read.  If this
229	         * fails, we return an error.
230	         */
231
232		if ((bp->b_flags & B_CACHE) == 0) {
233			bp->b_iocmd = BIO_READ;
234			vfs_busy_pages(bp, 0);
235			err = fuse_io_strategy(vp, bp);
236			if (err) {
237				brelse(bp);
238				return (err);
239			}
240		}
241		/*
242	         * on is the offset into the current bp.  Figure out how many
243	         * bytes we can copy out of the bp.  Note that bcount is
244	         * NOT DEV_BSIZE aligned.
245	         *
246	         * Then figure out how many bytes we can copy into the uio.
247	         */
248
249		n = 0;
250		if (on < bcount)
251			n = MIN((unsigned)(bcount - on), uio->uio_resid);
252		if (n > 0) {
253			FS_DEBUG2G("feeding buffeater with %d bytes of buffer %p,"
254				" saying %d was asked for\n",
255				n, bp->b_data + on, n + (int)bp->b_resid);
256			err = uiomove(bp->b_data + on, n, uio);
257		}
258		brelse(bp);
259		FS_DEBUG2G("end of turn, err %d, uio->uio_resid %zd, n %d\n",
260		    err, uio->uio_resid, n);
261	} while (err == 0 && uio->uio_resid > 0 && n > 0);
262
263	return (err);
264}
265
266static int
267fuse_read_directbackend(struct vnode *vp, struct uio *uio,
268    struct ucred *cred, struct fuse_filehandle *fufh)
269{
270	struct fuse_dispatcher fdi;
271	struct fuse_read_in *fri;
272	int err = 0;
273
274	if (uio->uio_resid == 0)
275		return (0);
276
277	fdisp_init(&fdi, 0);
278
279	/*
280         * XXX In "normal" case we use an intermediate kernel buffer for
281         * transmitting data from daemon's context to ours. Eventually, we should
282         * get rid of this. Anyway, if the target uio lives in sysspace (we are
283         * called from pageops), and the input data doesn't need kernel-side
284         * processing (we are not called from readdir) we can already invoke
285         * an optimized, "peer-to-peer" I/O routine.
286         */
287	while (uio->uio_resid > 0) {
288		fdi.iosize = sizeof(*fri);
289		fdisp_make_vp(&fdi, FUSE_READ, vp, uio->uio_td, cred);
290		fri = fdi.indata;
291		fri->fh = fufh->fh_id;
292		fri->offset = uio->uio_offset;
293		fri->size = MIN(uio->uio_resid,
294		    fuse_get_mpdata(vp->v_mount)->max_read);
295
296		FS_DEBUG2G("fri->fh %ju, fri->offset %ju, fri->size %ju\n",
297			(uintmax_t)fri->fh, (uintmax_t)fri->offset,
298			(uintmax_t)fri->size);
299
300		if ((err = fdisp_wait_answ(&fdi)))
301			goto out;
302
303		FS_DEBUG2G("complete: got iosize=%d, requested fri.size=%zd; "
304			"resid=%zd offset=%ju\n",
305			fri->size, fdi.iosize, uio->uio_resid,
306			(uintmax_t)uio->uio_offset);
307
308		if ((err = uiomove(fdi.answ, MIN(fri->size, fdi.iosize), uio)))
309			break;
310		if (fdi.iosize < fri->size)
311			break;
312	}
313
314out:
315	fdisp_destroy(&fdi);
316	return (err);
317}
318
319static int
320fuse_write_directbackend(struct vnode *vp, struct uio *uio,
321    struct ucred *cred, struct fuse_filehandle *fufh, int ioflag)
322{
323	struct fuse_vnode_data *fvdat = VTOFUD(vp);
324	struct fuse_write_in *fwi;
325	struct fuse_dispatcher fdi;
326	size_t chunksize;
327	int diff;
328	int err = 0;
329
330	if (uio->uio_resid == 0)
331		return (0);
332	if (ioflag & IO_APPEND)
333		uio_setoffset(uio, fvdat->filesize);
334
335	fdisp_init(&fdi, 0);
336
337	while (uio->uio_resid > 0) {
338		chunksize = MIN(uio->uio_resid,
339		    fuse_get_mpdata(vp->v_mount)->max_write);
340
341		fdi.iosize = sizeof(*fwi) + chunksize;
342		fdisp_make_vp(&fdi, FUSE_WRITE, vp, uio->uio_td, cred);
343
344		fwi = fdi.indata;
345		fwi->fh = fufh->fh_id;
346		fwi->offset = uio->uio_offset;
347		fwi->size = chunksize;
348
349		if ((err = uiomove((char *)fdi.indata + sizeof(*fwi),
350		    chunksize, uio)))
351			break;
352
353		if ((err = fdisp_wait_answ(&fdi)))
354			break;
355
356		diff = chunksize - ((struct fuse_write_out *)fdi.answ)->size;
357		if (diff < 0) {
358			err = EINVAL;
359			break;
360		}
361		uio->uio_resid += diff;
362		uio->uio_offset -= diff;
363		if (uio->uio_offset > fvdat->filesize)
364			fuse_vnode_setsize(vp, cred, uio->uio_offset);
365	}
366
367	fdisp_destroy(&fdi);
368
369	return (err);
370}
371
372static int
373fuse_write_biobackend(struct vnode *vp, struct uio *uio,
374    struct ucred *cred, struct fuse_filehandle *fufh, int ioflag)
375{
376	struct fuse_vnode_data *fvdat = VTOFUD(vp);
377	struct buf *bp;
378	daddr_t lbn;
379	int bcount;
380	int n, on, err = 0;
381
382	const int biosize = fuse_iosize(vp);
383
384	KASSERT(uio->uio_rw == UIO_WRITE, ("ncl_write mode"));
385	FS_DEBUG("resid=%zx offset=%jx fsize=%jx\n",
386	    uio->uio_resid, uio->uio_offset, fvdat->filesize);
387	if (vp->v_type != VREG)
388		return (EIO);
389	if (uio->uio_offset < 0)
390		return (EINVAL);
391	if (uio->uio_resid == 0)
392		return (0);
393	if (ioflag & IO_APPEND)
394		uio_setoffset(uio, fvdat->filesize);
395
396	/*
397         * Find all of this file's B_NEEDCOMMIT buffers.  If our writes
398         * would exceed the local maximum per-file write commit size when
399         * combined with those, we must decide whether to flush,
400         * go synchronous, or return err.  We don't bother checking
401         * IO_UNIT -- we just make all writes atomic anyway, as there's
402         * no point optimizing for something that really won't ever happen.
403         */
404	do {
405		if (fuse_isdeadfs(vp)) {
406			err = ENXIO;
407			break;
408		}
409		lbn = uio->uio_offset / biosize;
410		on = uio->uio_offset & (biosize - 1);
411		n = MIN((unsigned)(biosize - on), uio->uio_resid);
412
413		FS_DEBUG2G("lbn %ju, on %d, n %d, uio offset %ju, uio resid %zd\n",
414			(uintmax_t)lbn, on, n,
415			(uintmax_t)uio->uio_offset, uio->uio_resid);
416
417again:
418		/*
419	         * Handle direct append and file extension cases, calculate
420	         * unaligned buffer size.
421	         */
422		if (uio->uio_offset == fvdat->filesize && n) {
423			/*
424	                 * Get the buffer (in its pre-append state to maintain
425	                 * B_CACHE if it was previously set).  Resize the
426	                 * nfsnode after we have locked the buffer to prevent
427	                 * readers from reading garbage.
428	                 */
429			bcount = on;
430			FS_DEBUG("getting block from OS, bcount %d\n", bcount);
431			bp = getblk(vp, lbn, bcount, PCATCH, 0, 0);
432
433			if (bp != NULL) {
434				long save;
435
436				err = fuse_vnode_setsize(vp, cred,
437							 uio->uio_offset + n);
438				if (err) {
439					brelse(bp);
440					break;
441				}
442				save = bp->b_flags & B_CACHE;
443				bcount += n;
444				allocbuf(bp, bcount);
445				bp->b_flags |= save;
446			}
447		} else {
448			/*
449	                 * Obtain the locked cache block first, and then
450	                 * adjust the file's size as appropriate.
451	                 */
452			bcount = on + n;
453			if ((off_t)lbn * biosize + bcount < fvdat->filesize) {
454				if ((off_t)(lbn + 1) * biosize < fvdat->filesize)
455					bcount = biosize;
456				else
457					bcount = fvdat->filesize -
458					  (off_t)lbn *biosize;
459			}
460			FS_DEBUG("getting block from OS, bcount %d\n", bcount);
461			bp = getblk(vp, lbn, bcount, PCATCH, 0, 0);
462			if (bp && uio->uio_offset + n > fvdat->filesize) {
463				err = fuse_vnode_setsize(vp, cred,
464							 uio->uio_offset + n);
465				if (err) {
466					brelse(bp);
467					break;
468				}
469			}
470		}
471
472		if (!bp) {
473			err = EINTR;
474			break;
475		}
476		/*
477	         * Issue a READ if B_CACHE is not set.  In special-append
478	         * mode, B_CACHE is based on the buffer prior to the write
479	         * op and is typically set, avoiding the read.  If a read
480	         * is required in special append mode, the server will
481	         * probably send us a short-read since we extended the file
482	         * on our end, resulting in b_resid == 0 and, thusly,
483	         * B_CACHE getting set.
484	         *
485	         * We can also avoid issuing the read if the write covers
486	         * the entire buffer.  We have to make sure the buffer state
487	         * is reasonable in this case since we will not be initiating
488	         * I/O.  See the comments in kern/vfs_bio.c's getblk() for
489	         * more information.
490	         *
491	         * B_CACHE may also be set due to the buffer being cached
492	         * normally.
493	         */
494
495		if (on == 0 && n == bcount) {
496			bp->b_flags |= B_CACHE;
497			bp->b_flags &= ~B_INVAL;
498			bp->b_ioflags &= ~BIO_ERROR;
499		}
500		if ((bp->b_flags & B_CACHE) == 0) {
501			bp->b_iocmd = BIO_READ;
502			vfs_busy_pages(bp, 0);
503			fuse_io_strategy(vp, bp);
504			if ((err = bp->b_error)) {
505				brelse(bp);
506				break;
507			}
508		}
509		if (bp->b_wcred == NOCRED)
510			bp->b_wcred = crhold(cred);
511
512		/*
513	         * If dirtyend exceeds file size, chop it down.  This should
514	         * not normally occur but there is an append race where it
515	         * might occur XXX, so we log it.
516	         *
517	         * If the chopping creates a reverse-indexed or degenerate
518	         * situation with dirtyoff/end, we 0 both of them.
519	         */
520
521		if (bp->b_dirtyend > bcount) {
522			FS_DEBUG("FUSE append race @%lx:%d\n",
523			    (long)bp->b_blkno * biosize,
524			    bp->b_dirtyend - bcount);
525			bp->b_dirtyend = bcount;
526		}
527		if (bp->b_dirtyoff >= bp->b_dirtyend)
528			bp->b_dirtyoff = bp->b_dirtyend = 0;
529
530		/*
531	         * If the new write will leave a contiguous dirty
532	         * area, just update the b_dirtyoff and b_dirtyend,
533	         * otherwise force a write rpc of the old dirty area.
534	         *
535	         * While it is possible to merge discontiguous writes due to
536	         * our having a B_CACHE buffer ( and thus valid read data
537	         * for the hole), we don't because it could lead to
538	         * significant cache coherency problems with multiple clients,
539	         * especially if locking is implemented later on.
540	         *
541	         * as an optimization we could theoretically maintain
542	         * a linked list of discontinuous areas, but we would still
543	         * have to commit them separately so there isn't much
544	         * advantage to it except perhaps a bit of asynchronization.
545	         */
546
547		if (bp->b_dirtyend > 0 &&
548		    (on > bp->b_dirtyend || (on + n) < bp->b_dirtyoff)) {
549			/*
550	                 * Yes, we mean it. Write out everything to "storage"
551	                 * immediately, without hesitation. (Apart from other
552	                 * reasons: the only way to know if a write is valid
553	                 * if its actually written out.)
554	                 */
555			bwrite(bp);
556			if (bp->b_error == EINTR) {
557				err = EINTR;
558				break;
559			}
560			goto again;
561		}
562		err = uiomove((char *)bp->b_data + on, n, uio);
563
564		/*
565	         * Since this block is being modified, it must be written
566	         * again and not just committed.  Since write clustering does
567	         * not work for the stage 1 data write, only the stage 2
568	         * commit rpc, we have to clear B_CLUSTEROK as well.
569	         */
570		bp->b_flags &= ~(B_NEEDCOMMIT | B_CLUSTEROK);
571
572		if (err) {
573			bp->b_ioflags |= BIO_ERROR;
574			bp->b_error = err;
575			brelse(bp);
576			break;
577		}
578		/*
579	         * Only update dirtyoff/dirtyend if not a degenerate
580	         * condition.
581	         */
582		if (n) {
583			if (bp->b_dirtyend > 0) {
584				bp->b_dirtyoff = MIN(on, bp->b_dirtyoff);
585				bp->b_dirtyend = MAX((on + n), bp->b_dirtyend);
586			} else {
587				bp->b_dirtyoff = on;
588				bp->b_dirtyend = on + n;
589			}
590			vfs_bio_set_valid(bp, on, n);
591		}
592		err = bwrite(bp);
593		if (err)
594			break;
595	} while (uio->uio_resid > 0 && n > 0);
596
597	if (fuse_sync_resize && (fvdat->flag & FN_SIZECHANGE) != 0)
598		fuse_vnode_savesize(vp, cred);
599
600	return (err);
601}
602
603int
604fuse_io_strategy(struct vnode *vp, struct buf *bp)
605{
606	struct fuse_filehandle *fufh;
607	struct fuse_vnode_data *fvdat = VTOFUD(vp);
608	struct ucred *cred;
609	struct uio *uiop;
610	struct uio uio;
611	struct iovec io;
612	int error = 0;
613
614	const int biosize = fuse_iosize(vp);
615
616	MPASS(vp->v_type == VREG || vp->v_type == VDIR);
617	MPASS(bp->b_iocmd == BIO_READ || bp->b_iocmd == BIO_WRITE);
618	FS_DEBUG("inode=%ju offset=%jd resid=%ld\n",
619	    (uintmax_t)VTOI(vp), (intmax_t)(((off_t)bp->b_blkno) * biosize),
620	    bp->b_bcount);
621
622	error = fuse_filehandle_getrw(vp,
623	    (bp->b_iocmd == BIO_READ) ? FUFH_RDONLY : FUFH_WRONLY, &fufh);
624	if (error) {
625		printf("FUSE: strategy: filehandles are closed\n");
626		bp->b_ioflags |= BIO_ERROR;
627		bp->b_error = error;
628		return (error);
629	}
630	cred = bp->b_iocmd == BIO_READ ? bp->b_rcred : bp->b_wcred;
631
632	uiop = &uio;
633	uiop->uio_iov = &io;
634	uiop->uio_iovcnt = 1;
635	uiop->uio_segflg = UIO_SYSSPACE;
636	uiop->uio_td = curthread;
637
638	/*
639         * clear BIO_ERROR and B_INVAL state prior to initiating the I/O.  We
640         * do this here so we do not have to do it in all the code that
641         * calls us.
642         */
643	bp->b_flags &= ~B_INVAL;
644	bp->b_ioflags &= ~BIO_ERROR;
645
646	KASSERT(!(bp->b_flags & B_DONE),
647	    ("fuse_io_strategy: bp %p already marked done", bp));
648	if (bp->b_iocmd == BIO_READ) {
649		io.iov_len = uiop->uio_resid = bp->b_bcount;
650		io.iov_base = bp->b_data;
651		uiop->uio_rw = UIO_READ;
652
653		uiop->uio_offset = ((off_t)bp->b_blkno) * biosize;
654		error = fuse_read_directbackend(vp, uiop, cred, fufh);
655
656		if ((!error && uiop->uio_resid) ||
657		    (fsess_opt_brokenio(vnode_mount(vp)) && error == EIO &&
658		    uiop->uio_offset < fvdat->filesize && fvdat->filesize > 0 &&
659		    uiop->uio_offset >= fvdat->cached_attrs.va_size)) {
660			/*
661	                 * If we had a short read with no error, we must have
662	                 * hit a file hole.  We should zero-fill the remainder.
663	                 * This can also occur if the server hits the file EOF.
664	                 *
665	                 * Holes used to be able to occur due to pending
666	                 * writes, but that is not possible any longer.
667	                 */
668			int nread = bp->b_bcount - uiop->uio_resid;
669			int left = uiop->uio_resid;
670
671			if (error != 0) {
672				printf("FUSE: Fix broken io: offset %ju, "
673				       " resid %zd, file size %ju/%ju\n",
674				       (uintmax_t)uiop->uio_offset,
675				    uiop->uio_resid, fvdat->filesize,
676				    fvdat->cached_attrs.va_size);
677				error = 0;
678			}
679			if (left > 0)
680				bzero((char *)bp->b_data + nread, left);
681			uiop->uio_resid = 0;
682		}
683		if (error) {
684			bp->b_ioflags |= BIO_ERROR;
685			bp->b_error = error;
686		}
687	} else {
688		/*
689	         * If we only need to commit, try to commit
690	         */
691		if (bp->b_flags & B_NEEDCOMMIT) {
692			FS_DEBUG("write: B_NEEDCOMMIT flags set\n");
693		}
694		/*
695	         * Setup for actual write
696	         */
697		if ((off_t)bp->b_blkno * biosize + bp->b_dirtyend >
698		    fvdat->filesize)
699			bp->b_dirtyend = fvdat->filesize -
700				(off_t)bp->b_blkno * biosize;
701
702		if (bp->b_dirtyend > bp->b_dirtyoff) {
703			io.iov_len = uiop->uio_resid = bp->b_dirtyend
704			    - bp->b_dirtyoff;
705			uiop->uio_offset = (off_t)bp->b_blkno * biosize
706			    + bp->b_dirtyoff;
707			io.iov_base = (char *)bp->b_data + bp->b_dirtyoff;
708			uiop->uio_rw = UIO_WRITE;
709
710			error = fuse_write_directbackend(vp, uiop, cred, fufh, 0);
711
712			if (error == EINTR || error == ETIMEDOUT
713			    || (!error && (bp->b_flags & B_NEEDCOMMIT))) {
714
715				bp->b_flags &= ~(B_INVAL | B_NOCACHE);
716				if ((bp->b_flags & B_PAGING) == 0) {
717					bdirty(bp);
718					bp->b_flags &= ~B_DONE;
719				}
720				if ((error == EINTR || error == ETIMEDOUT) &&
721				    (bp->b_flags & B_ASYNC) == 0)
722					bp->b_flags |= B_EINTR;
723			} else {
724				if (error) {
725					bp->b_ioflags |= BIO_ERROR;
726					bp->b_flags |= B_INVAL;
727					bp->b_error = error;
728				}
729				bp->b_dirtyoff = bp->b_dirtyend = 0;
730			}
731		} else {
732			bp->b_resid = 0;
733			bufdone(bp);
734			return (0);
735		}
736	}
737	bp->b_resid = uiop->uio_resid;
738	bufdone(bp);
739	return (error);
740}
741
742int
743fuse_io_flushbuf(struct vnode *vp, int waitfor, struct thread *td)
744{
745	struct vop_fsync_args a = {
746		.a_vp = vp,
747		.a_waitfor = waitfor,
748		.a_td = td,
749	};
750
751	return (vop_stdfsync(&a));
752}
753
754/*
755 * Flush and invalidate all dirty buffers. If another process is already
756 * doing the flush, just wait for completion.
757 */
758int
759fuse_io_invalbuf(struct vnode *vp, struct thread *td)
760{
761	struct fuse_vnode_data *fvdat = VTOFUD(vp);
762	int error = 0;
763
764	if (vp->v_iflag & VI_DOOMED)
765		return 0;
766
767	ASSERT_VOP_ELOCKED(vp, "fuse_io_invalbuf");
768
769	while (fvdat->flag & FN_FLUSHINPROG) {
770		struct proc *p = td->td_proc;
771
772		if (vp->v_mount->mnt_kern_flag & MNTK_UNMOUNTF)
773			return EIO;
774		fvdat->flag |= FN_FLUSHWANT;
775		tsleep(&fvdat->flag, PRIBIO + 2, "fusevinv", 2 * hz);
776		error = 0;
777		if (p != NULL) {
778			PROC_LOCK(p);
779			if (SIGNOTEMPTY(p->p_siglist) ||
780			    SIGNOTEMPTY(td->td_siglist))
781				error = EINTR;
782			PROC_UNLOCK(p);
783		}
784		if (error == EINTR)
785			return EINTR;
786	}
787	fvdat->flag |= FN_FLUSHINPROG;
788
789	if (vp->v_bufobj.bo_object != NULL) {
790		VM_OBJECT_WLOCK(vp->v_bufobj.bo_object);
791		vm_object_page_clean(vp->v_bufobj.bo_object, 0, 0, OBJPC_SYNC);
792		VM_OBJECT_WUNLOCK(vp->v_bufobj.bo_object);
793	}
794	error = vinvalbuf(vp, V_SAVE, PCATCH, 0);
795	while (error) {
796		if (error == ERESTART || error == EINTR) {
797			fvdat->flag &= ~FN_FLUSHINPROG;
798			if (fvdat->flag & FN_FLUSHWANT) {
799				fvdat->flag &= ~FN_FLUSHWANT;
800				wakeup(&fvdat->flag);
801			}
802			return EINTR;
803		}
804		error = vinvalbuf(vp, V_SAVE, PCATCH, 0);
805	}
806	fvdat->flag &= ~FN_FLUSHINPROG;
807	if (fvdat->flag & FN_FLUSHWANT) {
808		fvdat->flag &= ~FN_FLUSHWANT;
809		wakeup(&fvdat->flag);
810	}
811	return (error);
812}
813