]> CyberLeo.Net >> Repos - FreeBSD/FreeBSD.git/blob - sys/fs/fuse/fuse_io.c
fusefs: set the flags fields of fuse_write_in and fuse_read_in
[FreeBSD/FreeBSD.git] / sys / fs / fuse / fuse_io.c
1 /*-
2  * SPDX-License-Identifier: BSD-3-Clause
3  *
4  * Copyright (c) 2007-2009 Google Inc.
5  * All rights reserved.
6  *
7  * Redistribution and use in source and binary forms, with or without
8  * modification, are permitted provided that the following conditions are
9  * met:
10  *
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
16  *   distribution.
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.
20  *
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.
32  *
33  * Copyright (C) 2005 Csaba Henk.
34  * All rights reserved.
35  *
36  * Redistribution and use in source and binary forms, with or without
37  * modification, are permitted provided that the following conditions
38  * are met:
39  * 1. Redistributions of source code must retain the above copyright
40  *    notice, this list of conditions and the following disclaimer.
41  * 2. Redistributions in binary form must reproduce the above copyright
42  *    notice, this list of conditions and the following disclaimer in the
43  *    documentation and/or other materials provided with the distribution.
44  *
45  * THIS SOFTWARE IS PROVIDED BY AUTHOR AND CONTRIBUTORS ``AS IS'' AND
46  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
47  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
48  * ARE DISCLAIMED.  IN NO EVENT SHALL AUTHOR OR CONTRIBUTORS BE LIABLE
49  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
50  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
51  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
52  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
53  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
54  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
55  * SUCH DAMAGE.
56  */
57
58 #include <sys/cdefs.h>
59 __FBSDID("$FreeBSD$");
60
61 #include <sys/types.h>
62 #include <sys/module.h>
63 #include <sys/systm.h>
64 #include <sys/errno.h>
65 #include <sys/param.h>
66 #include <sys/kernel.h>
67 #include <sys/conf.h>
68 #include <sys/uio.h>
69 #include <sys/malloc.h>
70 #include <sys/queue.h>
71 #include <sys/lock.h>
72 #include <sys/sx.h>
73 #include <sys/mutex.h>
74 #include <sys/rwlock.h>
75 #include <sys/priv.h>
76 #include <sys/proc.h>
77 #include <sys/mount.h>
78 #include <sys/vnode.h>
79 #include <sys/stat.h>
80 #include <sys/unistd.h>
81 #include <sys/filedesc.h>
82 #include <sys/file.h>
83 #include <sys/fcntl.h>
84 #include <sys/bio.h>
85 #include <sys/buf.h>
86 #include <sys/sysctl.h>
87
88 #include <vm/vm.h>
89 #include <vm/vm_extern.h>
90 #include <vm/pmap.h>
91 #include <vm/vm_map.h>
92 #include <vm/vm_page.h>
93 #include <vm/vm_object.h>
94
95 #include "fuse.h"
96 #include "fuse_file.h"
97 #include "fuse_node.h"
98 #include "fuse_internal.h"
99 #include "fuse_ipc.h"
100 #include "fuse_io.h"
101
102 SDT_PROVIDER_DECLARE(fusefs);
103 /* 
104  * Fuse trace probe:
105  * arg0: verbosity.  Higher numbers give more verbose messages
106  * arg1: Textual message
107  */
108 SDT_PROBE_DEFINE2(fusefs, , io, trace, "int", "char*");
109
110 static void
111 fuse_io_clear_suid_on_write(struct vnode *vp, struct ucred *cred,
112         struct thread *td);
113 static int 
114 fuse_read_directbackend(struct vnode *vp, struct uio *uio,
115     struct ucred *cred, struct fuse_filehandle *fufh);
116 static int 
117 fuse_read_biobackend(struct vnode *vp, struct uio *uio, int ioflag,
118     struct ucred *cred, struct fuse_filehandle *fufh, pid_t pid);
119 static int 
120 fuse_write_directbackend(struct vnode *vp, struct uio *uio,
121     struct ucred *cred, struct fuse_filehandle *fufh, off_t filesize,
122     int ioflag, bool pages);
123 static int 
124 fuse_write_biobackend(struct vnode *vp, struct uio *uio,
125     struct ucred *cred, struct fuse_filehandle *fufh, int ioflag, pid_t pid);
126
127 /*
128  * FreeBSD clears the SUID and SGID bits on any write by a non-root user.
129  */
130 static void
131 fuse_io_clear_suid_on_write(struct vnode *vp, struct ucred *cred,
132         struct thread *td)
133 {
134         struct fuse_data *data;
135         struct mount *mp;
136         struct vattr va;
137         int dataflags;
138
139         mp = vnode_mount(vp);
140         data = fuse_get_mpdata(mp);
141         dataflags = data->dataflags;
142
143         if (dataflags & FSESS_DEFAULT_PERMISSIONS) {
144                 if (priv_check_cred(cred, PRIV_VFS_RETAINSUGID)) {
145                         fuse_internal_getattr(vp, &va, cred, td);
146                         if (va.va_mode & (S_ISUID | S_ISGID)) {
147                                 mode_t mode = va.va_mode & ~(S_ISUID | S_ISGID);
148                                 /* Clear all vattr fields except mode */
149                                 vattr_null(&va);
150                                 va.va_mode = mode;
151
152                                 /*
153                                  * Ignore fuse_internal_setattr's return value,
154                                  * because at this point the write operation has
155                                  * already succeeded and we don't want to return
156                                  * failing status for that.
157                                  */
158                                 (void)fuse_internal_setattr(vp, &va, td, NULL);
159                         }
160                 }
161         }
162 }
163
164 SDT_PROBE_DEFINE5(fusefs, , io, io_dispatch, "struct vnode*", "struct uio*",
165                 "int", "struct ucred*", "struct fuse_filehandle*");
166 int
167 fuse_io_dispatch(struct vnode *vp, struct uio *uio, int ioflag, bool pages,
168     struct ucred *cred, pid_t pid)
169 {
170         struct fuse_filehandle *fufh;
171         int err, directio;
172         int fflag;
173         bool closefufh = false;
174
175         MPASS(vp->v_type == VREG || vp->v_type == VDIR);
176
177         fflag = (uio->uio_rw == UIO_READ) ? FREAD : FWRITE;
178         err = fuse_filehandle_getrw(vp, fflag, &fufh, cred, pid);
179         if (err == EBADF && vnode_mount(vp)->mnt_flag & MNT_EXPORTED) {
180                 /* 
181                  * nfsd will do I/O without first doing VOP_OPEN.  We
182                  * must implicitly open the file here
183                  */
184                 err = fuse_filehandle_open(vp, fflag, &fufh, curthread, cred);
185                 closefufh = true;
186         }
187         else if (err) {
188                 printf("FUSE: io dispatch: filehandles are closed\n");
189                 return err;
190         }
191         if (err)
192                 goto out;
193         SDT_PROBE5(fusefs, , io, io_dispatch, vp, uio, ioflag, cred, fufh);
194
195         /*
196          * Ideally, when the daemon asks for direct io at open time, the
197          * standard file flag should be set according to this, so that would
198          * just change the default mode, which later on could be changed via
199          * fcntl(2).
200          * But this doesn't work, the O_DIRECT flag gets cleared at some point
201          * (don't know where). So to make any use of the Fuse direct_io option,
202          * we hardwire it into the file's private data (similarly to Linux,
203          * btw.).
204          */
205         directio = (ioflag & IO_DIRECT) || !fsess_opt_datacache(vnode_mount(vp));
206
207         switch (uio->uio_rw) {
208         case UIO_READ:
209                 if (directio) {
210                         SDT_PROBE2(fusefs, , io, trace, 1,
211                                 "direct read of vnode");
212                         err = fuse_read_directbackend(vp, uio, cred, fufh);
213                 } else {
214                         SDT_PROBE2(fusefs, , io, trace, 1,
215                                 "buffered read of vnode");
216                         err = fuse_read_biobackend(vp, uio, ioflag, cred, fufh,
217                                 pid);
218                 }
219                 break;
220         case UIO_WRITE:
221                 /*
222                  * Kludge: simulate write-through caching via write-around
223                  * caching.  Same effect, as far as never caching dirty data,
224                  * but slightly pessimal in that newly written data is not
225                  * cached.
226                  */
227                 if (directio || fuse_data_cache_mode == FUSE_CACHE_WT) {
228                         const int iosize = fuse_iosize(vp);
229                         off_t start, end, filesize;
230
231                         SDT_PROBE2(fusefs, , io, trace, 1,
232                                 "direct write of vnode");
233
234                         err = fuse_vnode_size(vp, &filesize, cred, curthread);
235                         if (err)
236                                 goto out;
237
238                         start = uio->uio_offset;
239                         end = start + uio->uio_resid;
240                         /* 
241                          * Invalidate the write cache unless we're coming from
242                          * VOP_PUTPAGES, in which case we're writing _from_ the
243                          * write cache
244                          */
245                         if (!pages )
246                                 v_inval_buf_range(vp, start, end, iosize);
247                         err = fuse_write_directbackend(vp, uio, cred, fufh,
248                                 filesize, ioflag, pages);
249                 } else {
250                         SDT_PROBE2(fusefs, , io, trace, 1,
251                                 "buffered write of vnode");
252                         err = fuse_write_biobackend(vp, uio, cred, fufh, ioflag,
253                                 pid);
254                 }
255                 fuse_io_clear_suid_on_write(vp, cred, uio->uio_td);
256                 break;
257         default:
258                 panic("uninterpreted mode passed to fuse_io_dispatch");
259         }
260
261 out:
262         if (closefufh)
263                 fuse_filehandle_close(vp, fufh, curthread, cred);
264
265         return (err);
266 }
267
268 SDT_PROBE_DEFINE3(fusefs, , io, read_bio_backend_start, "int", "int", "int");
269 SDT_PROBE_DEFINE2(fusefs, , io, read_bio_backend_feed, "int", "int");
270 SDT_PROBE_DEFINE3(fusefs, , io, read_bio_backend_end, "int", "ssize_t", "int");
271 static int
272 fuse_read_biobackend(struct vnode *vp, struct uio *uio, int ioflag,
273     struct ucred *cred, struct fuse_filehandle *fufh, pid_t pid)
274 {
275         struct buf *bp;
276         daddr_t lbn;
277         int bcount;
278         int err, n = 0, on = 0;
279         off_t filesize;
280
281         const int biosize = fuse_iosize(vp);
282
283         if (uio->uio_offset < 0)
284                 return (EINVAL);
285
286         err = fuse_vnode_size(vp, &filesize, cred, curthread);
287         if (err)
288                 return err;
289
290         for (err = 0, bp = NULL; uio->uio_resid > 0; bp = NULL) {
291                 if (fuse_isdeadfs(vp)) {
292                         err = ENXIO;
293                         break;
294                 }
295                 if (filesize - uio->uio_offset <= 0)
296                         break;
297                 lbn = uio->uio_offset / biosize;
298                 on = uio->uio_offset & (biosize - 1);
299
300                 SDT_PROBE3(fusefs, , io, read_bio_backend_start,
301                         biosize, (int)lbn, on);
302
303                 if ((off_t)lbn * biosize >= filesize) {
304                         bcount = 0;
305                 } else if ((off_t)(lbn + 1) * biosize > filesize) {
306                         bcount = filesize - (off_t)lbn *biosize;
307                 } else {
308                         bcount = biosize;
309                 }
310
311                 /* TODO: readahead.  See ext2_read for an example */
312                 err = bread(vp, lbn, bcount, NOCRED, &bp);
313                 if (err) {
314                         brelse(bp);
315                         bp = NULL;
316                         break;
317                 }
318
319                 /*
320                  * on is the offset into the current bp.  Figure out how many
321                  * bytes we can copy out of the bp.  Note that bcount is
322                  * NOT DEV_BSIZE aligned.
323                  *
324                  * Then figure out how many bytes we can copy into the uio.
325                  */
326
327                 n = 0;
328                 if (on < bcount)
329                         n = MIN((unsigned)(bcount - on), uio->uio_resid);
330                 if (n > 0) {
331                         SDT_PROBE2(fusefs, , io, read_bio_backend_feed,
332                                 n, n + (int)bp->b_resid);
333                         err = uiomove(bp->b_data + on, n, uio);
334                 }
335                 vfs_bio_brelse(bp, ioflag);
336                 SDT_PROBE3(fusefs, , io, read_bio_backend_end, err,
337                         uio->uio_resid, n);
338         }
339
340         return (err);
341 }
342
343 SDT_PROBE_DEFINE1(fusefs, , io, read_directbackend_start,
344         "struct fuse_read_in*");
345 SDT_PROBE_DEFINE2(fusefs, , io, read_directbackend_complete,
346         "struct fuse_dispatcher*", "struct uio*");
347
348 static int
349 fuse_read_directbackend(struct vnode *vp, struct uio *uio,
350     struct ucred *cred, struct fuse_filehandle *fufh)
351 {
352         struct fuse_data *data;
353         struct fuse_dispatcher fdi;
354         struct fuse_read_in *fri;
355         int err = 0;
356
357         data = fuse_get_mpdata(vp->v_mount);
358
359         if (uio->uio_resid == 0)
360                 return (0);
361
362         fdisp_init(&fdi, 0);
363
364         /*
365          * XXX In "normal" case we use an intermediate kernel buffer for
366          * transmitting data from daemon's context to ours. Eventually, we should
367          * get rid of this. Anyway, if the target uio lives in sysspace (we are
368          * called from pageops), and the input data doesn't need kernel-side
369          * processing (we are not called from readdir) we can already invoke
370          * an optimized, "peer-to-peer" I/O routine.
371          */
372         while (uio->uio_resid > 0) {
373                 fdi.iosize = sizeof(*fri);
374                 fdisp_make_vp(&fdi, FUSE_READ, vp, uio->uio_td, cred);
375                 fri = fdi.indata;
376                 fri->fh = fufh->fh_id;
377                 fri->offset = uio->uio_offset;
378                 fri->size = MIN(uio->uio_resid,
379                     fuse_get_mpdata(vp->v_mount)->max_read);
380                 if (fuse_libabi_geq(data, 7, 9)) {
381                         /* See comment regarding FUSE_WRITE_LOCKOWNER */
382                         fri->read_flags = 0;
383                         fri->flags = fufh_type_2_fflags(fufh->fufh_type);
384                 }
385
386                 SDT_PROBE1(fusefs, , io, read_directbackend_start, fri);
387
388                 if ((err = fdisp_wait_answ(&fdi)))
389                         goto out;
390
391                 SDT_PROBE2(fusefs, , io, read_directbackend_complete,
392                         fdi.iosize, uio);
393
394                 if ((err = uiomove(fdi.answ, MIN(fri->size, fdi.iosize), uio)))
395                         break;
396                 if (fdi.iosize < fri->size)
397                         break;
398         }
399
400 out:
401         fdisp_destroy(&fdi);
402         return (err);
403 }
404
405 static int
406 fuse_write_directbackend(struct vnode *vp, struct uio *uio,
407     struct ucred *cred, struct fuse_filehandle *fufh, off_t filesize,
408     int ioflag, bool pages)
409 {
410         struct fuse_vnode_data *fvdat = VTOFUD(vp);
411         struct fuse_data *data;
412         struct fuse_write_in *fwi;
413         struct fuse_write_out *fwo;
414         struct fuse_dispatcher fdi;
415         size_t chunksize;
416         void *fwi_data;
417         off_t as_written_offset;
418         int diff;
419         int err = 0;
420         bool direct_io = fufh->fuse_open_flags & FOPEN_DIRECT_IO;
421         uint32_t write_flags;
422
423         data = fuse_get_mpdata(vp->v_mount);
424
425         /* 
426          * Don't set FUSE_WRITE_LOCKOWNER in write_flags.  It can't be set
427          * accurately when using POSIX AIO, libfuse doesn't use it, and I'm not
428          * aware of any file systems that do.  It was an attempt to add
429          * Linux-style mandatory locking to the FUSE protocol, but mandatory
430          * locking is deprecated even on Linux.  See Linux commit
431          * f33321141b273d60cbb3a8f56a5489baad82ba5e .
432          */
433         /*
434          * Set FUSE_WRITE_CACHE whenever we don't know the uid, gid, and/or pid
435          * that originated a write.  For example when writing from the
436          * writeback cache.  I don't know of a single file system that cares,
437          * but the protocol says we're supposed to do this.
438          */
439         write_flags = !pages && (
440                 (ioflag & IO_DIRECT) ||
441                 !fsess_opt_datacache(vnode_mount(vp)) ||
442                 fuse_data_cache_mode != FUSE_CACHE_WB) ? 0 : FUSE_WRITE_CACHE;
443
444         if (uio->uio_resid == 0)
445                 return (0);
446
447         if (ioflag & IO_APPEND)
448                 uio_setoffset(uio, filesize);
449
450         fdisp_init(&fdi, 0);
451
452         while (uio->uio_resid > 0) {
453                 chunksize = MIN(uio->uio_resid, data->max_write);
454
455                 fdi.iosize = sizeof(*fwi) + chunksize;
456                 fdisp_make_vp(&fdi, FUSE_WRITE, vp, uio->uio_td, cred);
457
458                 fwi = fdi.indata;
459                 fwi->fh = fufh->fh_id;
460                 fwi->offset = uio->uio_offset;
461                 fwi->size = chunksize;
462                 fwi->write_flags = write_flags;
463                 if (fuse_libabi_geq(data, 7, 9)) {
464                         fwi->flags = fufh_type_2_fflags(fufh->fufh_type);
465                         fwi_data = (char *)fdi.indata + sizeof(*fwi);
466                 } else {
467                         fwi_data = (char *)fdi.indata +
468                                 FUSE_COMPAT_WRITE_IN_SIZE;
469                 }
470
471                 if ((err = uiomove(fwi_data, chunksize, uio)))
472                         break;
473
474 retry:
475                 err = fdisp_wait_answ(&fdi);
476                 if (err == ERESTART || err == EINTR || err == EWOULDBLOCK) {
477                         /*
478                          * Rewind the uio so dofilewrite will know it's
479                          * incomplete
480                          */
481                         uio->uio_resid += fwi->size;
482                         uio->uio_offset -= fwi->size;
483                         /* 
484                          * Change ERESTART into EINTR because we can't rewind
485                          * uio->uio_iov.  Basically, once uiomove(9) has been
486                          * called, it's impossible to restart a syscall.
487                          */
488                         if (err == ERESTART)
489                                 err = EINTR;
490                         break;
491                 } else if (err) {
492                         break;
493                 }
494
495                 fwo = ((struct fuse_write_out *)fdi.answ);
496
497                 /* Adjust the uio in the case of short writes */
498                 diff = fwi->size - fwo->size;
499                 as_written_offset = uio->uio_offset - diff;
500
501                 if (as_written_offset - diff > filesize &&
502                     fuse_data_cache_mode != FUSE_CACHE_UC)
503                         fuse_vnode_setsize(vp, cred, as_written_offset);
504                 if (as_written_offset - diff >= filesize)
505                         fvdat->flag &= ~FN_SIZECHANGE;
506
507                 if (diff < 0) {
508                         printf("WARNING: misbehaving FUSE filesystem "
509                                 "wrote more data than we provided it\n");
510                         err = EINVAL;
511                         break;
512                 } else if (diff > 0) {
513                         /* Short write */
514                         if (!direct_io) {
515                                 printf("WARNING: misbehaving FUSE filesystem: "
516                                         "short writes are only allowed with "
517                                         "direct_io\n");
518                         }
519                         if (ioflag & IO_DIRECT) {
520                                 /* Return early */
521                                 uio->uio_resid += diff;
522                                 uio->uio_offset -= diff;
523                                 break;
524                         } else {
525                                 /* Resend the unwritten portion of data */
526                                 fdi.iosize = sizeof(*fwi) + diff;
527                                 /* Refresh fdi without clearing data buffer */
528                                 fdisp_refresh_vp(&fdi, FUSE_WRITE, vp,
529                                         uio->uio_td, cred);
530                                 fwi = fdi.indata;
531                                 MPASS2(fwi == fdi.indata, "FUSE dispatcher "
532                                         "reallocated despite no increase in "
533                                         "size?");
534                                 void *src = (char*)fwi_data + fwo->size;
535                                 memmove(fwi_data, src, diff);
536                                 fwi->fh = fufh->fh_id;
537                                 fwi->offset = as_written_offset;
538                                 fwi->size = diff;
539                                 fwi->write_flags = write_flags;
540                                 goto retry;
541                         }
542                 }
543         }
544
545         fdisp_destroy(&fdi);
546
547         return (err);
548 }
549
550 SDT_PROBE_DEFINE6(fusefs, , io, write_biobackend_start, "int64_t", "int", "int",
551                 "struct uio*", "int", "bool");
552 SDT_PROBE_DEFINE2(fusefs, , io, write_biobackend_append_race, "long", "int");
553
554 static int
555 fuse_write_biobackend(struct vnode *vp, struct uio *uio,
556     struct ucred *cred, struct fuse_filehandle *fufh, int ioflag, pid_t pid)
557 {
558         struct fuse_vnode_data *fvdat = VTOFUD(vp);
559         struct buf *bp;
560         daddr_t lbn;
561         off_t filesize;
562         int bcount;
563         int n, on, err = 0;
564
565         const int biosize = fuse_iosize(vp);
566
567         KASSERT(uio->uio_rw == UIO_WRITE, ("ncl_write mode"));
568         if (vp->v_type != VREG)
569                 return (EIO);
570         if (uio->uio_offset < 0)
571                 return (EINVAL);
572         if (uio->uio_resid == 0)
573                 return (0);
574
575         err = fuse_vnode_size(vp, &filesize, cred, curthread);
576         if (err)
577                 return err;
578
579         if (ioflag & IO_APPEND)
580                 uio_setoffset(uio, filesize);
581
582         /*
583          * Find all of this file's B_NEEDCOMMIT buffers.  If our writes
584          * would exceed the local maximum per-file write commit size when
585          * combined with those, we must decide whether to flush,
586          * go synchronous, or return err.  We don't bother checking
587          * IO_UNIT -- we just make all writes atomic anyway, as there's
588          * no point optimizing for something that really won't ever happen.
589          */
590         do {
591                 if (fuse_isdeadfs(vp)) {
592                         err = ENXIO;
593                         break;
594                 }
595                 lbn = uio->uio_offset / biosize;
596                 on = uio->uio_offset & (biosize - 1);
597                 n = MIN((unsigned)(biosize - on), uio->uio_resid);
598
599 again:
600                 /*
601                  * Handle direct append and file extension cases, calculate
602                  * unaligned buffer size.
603                  */
604                 if (uio->uio_offset == filesize && n) {
605                         /*
606                          * Get the buffer (in its pre-append state to maintain
607                          * B_CACHE if it was previously set).  Resize the
608                          * nfsnode after we have locked the buffer to prevent
609                          * readers from reading garbage.
610                          */
611                         bcount = on;
612                         SDT_PROBE6(fusefs, , io, write_biobackend_start,
613                                 lbn, on, n, uio, bcount, true);
614                         bp = getblk(vp, lbn, bcount, PCATCH, 0, 0);
615
616                         if (bp != NULL) {
617                                 long save;
618
619                                 err = fuse_vnode_setsize(vp, cred, 
620                                                          uio->uio_offset + n);
621                                 fvdat->flag |= FN_SIZECHANGE;
622
623                                 if (err) {
624                                         brelse(bp);
625                                         break;
626                                 }
627                                 save = bp->b_flags & B_CACHE;
628                                 bcount += n;
629                                 allocbuf(bp, bcount);
630                                 bp->b_flags |= save;
631                         }
632                 } else {
633                         /*
634                          * Obtain the locked cache block first, and then
635                          * adjust the file's size as appropriate.
636                          */
637                         bcount = on + n;
638                         if ((off_t)lbn * biosize + bcount < filesize) {
639                                 if ((off_t)(lbn + 1) * biosize < filesize)
640                                         bcount = biosize;
641                                 else
642                                         bcount = filesize - (off_t)lbn *biosize;
643                         }
644                         SDT_PROBE6(fusefs, , io, write_biobackend_start,
645                                 lbn, on, n, uio, bcount, false);
646                         bp = getblk(vp, lbn, bcount, PCATCH, 0, 0);
647                         if (bp && uio->uio_offset + n > filesize) {
648                                 err = fuse_vnode_setsize(vp, cred, 
649                                                          uio->uio_offset + n);
650                                 fvdat->flag |= FN_SIZECHANGE;
651                                 if (err) {
652                                         brelse(bp);
653                                         break;
654                                 } 
655                         }
656                 }
657
658                 if (!bp) {
659                         err = EINTR;
660                         break;
661                 }
662                 /*
663                  * Issue a READ if B_CACHE is not set.  In special-append
664                  * mode, B_CACHE is based on the buffer prior to the write
665                  * op and is typically set, avoiding the read.  If a read
666                  * is required in special append mode, the server will
667                  * probably send us a short-read since we extended the file
668                  * on our end, resulting in b_resid == 0 and, thusly,
669                  * B_CACHE getting set.
670                  *
671                  * We can also avoid issuing the read if the write covers
672                  * the entire buffer.  We have to make sure the buffer state
673                  * is reasonable in this case since we will not be initiating
674                  * I/O.  See the comments in kern/vfs_bio.c's getblk() for
675                  * more information.
676                  *
677                  * B_CACHE may also be set due to the buffer being cached
678                  * normally.
679                  */
680
681                 if (on == 0 && n == bcount) {
682                         bp->b_flags |= B_CACHE;
683                         bp->b_flags &= ~B_INVAL;
684                         bp->b_ioflags &= ~BIO_ERROR;
685                 }
686                 if ((bp->b_flags & B_CACHE) == 0) {
687                         bp->b_iocmd = BIO_READ;
688                         vfs_busy_pages(bp, 0);
689                         fuse_io_strategy(vp, bp);
690                         if ((err = bp->b_error)) {
691                                 brelse(bp);
692                                 break;
693                         }
694                 }
695                 if (bp->b_wcred == NOCRED)
696                         bp->b_wcred = crhold(cred);
697
698                 /*
699                  * If dirtyend exceeds file size, chop it down.  This should
700                  * not normally occur but there is an append race where it
701                  * might occur XXX, so we log it.
702                  *
703                  * If the chopping creates a reverse-indexed or degenerate
704                  * situation with dirtyoff/end, we 0 both of them.
705                  */
706
707                 if (bp->b_dirtyend > bcount) {
708                         SDT_PROBE2(fusefs, , io, write_biobackend_append_race,
709                             (long)bp->b_blkno * biosize,
710                             bp->b_dirtyend - bcount);
711                         bp->b_dirtyend = bcount;
712                 }
713                 if (bp->b_dirtyoff >= bp->b_dirtyend)
714                         bp->b_dirtyoff = bp->b_dirtyend = 0;
715
716                 /*
717                  * If the new write will leave a contiguous dirty
718                  * area, just update the b_dirtyoff and b_dirtyend,
719                  * otherwise force a write rpc of the old dirty area.
720                  *
721                  * While it is possible to merge discontiguous writes due to
722                  * our having a B_CACHE buffer ( and thus valid read data
723                  * for the hole), we don't because it could lead to
724                  * significant cache coherency problems with multiple clients,
725                  * especially if locking is implemented later on.
726                  *
727                  * as an optimization we could theoretically maintain
728                  * a linked list of discontinuous areas, but we would still
729                  * have to commit them separately so there isn't much
730                  * advantage to it except perhaps a bit of asynchronization.
731                  */
732
733                 if (bp->b_dirtyend > 0 &&
734                     (on > bp->b_dirtyend || (on + n) < bp->b_dirtyoff)) {
735                         /*
736                          * Yes, we mean it. Write out everything to "storage"
737                          * immediately, without hesitation. (Apart from other
738                          * reasons: the only way to know if a write is valid
739                          * if its actually written out.)
740                          */
741                         bwrite(bp);
742                         if (bp->b_error == EINTR) {
743                                 err = EINTR;
744                                 break;
745                         }
746                         goto again;
747                 }
748                 err = uiomove((char *)bp->b_data + on, n, uio);
749
750                 /*
751                  * Since this block is being modified, it must be written
752                  * again and not just committed.  Since write clustering does
753                  * not work for the stage 1 data write, only the stage 2
754                  * commit rpc, we have to clear B_CLUSTEROK as well.
755                  */
756                 bp->b_flags &= ~(B_NEEDCOMMIT | B_CLUSTEROK);
757
758                 if (err) {
759                         bp->b_ioflags |= BIO_ERROR;
760                         bp->b_error = err;
761                         brelse(bp);
762                         break;
763                 }
764                 /*
765                  * Only update dirtyoff/dirtyend if not a degenerate
766                  * condition.
767                  */
768                 if (n) {
769                         if (bp->b_dirtyend > 0) {
770                                 bp->b_dirtyoff = MIN(on, bp->b_dirtyoff);
771                                 bp->b_dirtyend = MAX((on + n), bp->b_dirtyend);
772                         } else {
773                                 bp->b_dirtyoff = on;
774                                 bp->b_dirtyend = on + n;
775                         }
776                         vfs_bio_set_valid(bp, on, n);
777                 }
778                 err = bwrite(bp);
779                 if (err)
780                         break;
781         } while (uio->uio_resid > 0 && n > 0);
782
783         return (err);
784 }
785
786 int
787 fuse_io_strategy(struct vnode *vp, struct buf *bp)
788 {
789         struct fuse_filehandle *fufh;
790         struct ucred *cred;
791         struct uio *uiop;
792         struct uio uio;
793         struct iovec io;
794         off_t filesize;
795         int error = 0;
796         int fflag;
797         /* We don't know the true pid when we're dealing with the cache */
798         pid_t pid = 0;
799
800         const int biosize = fuse_iosize(vp);
801
802         MPASS(vp->v_type == VREG || vp->v_type == VDIR);
803         MPASS(bp->b_iocmd == BIO_READ || bp->b_iocmd == BIO_WRITE);
804
805         fflag = bp->b_iocmd == BIO_READ ? FREAD : FWRITE;
806         cred = bp->b_iocmd == BIO_READ ? bp->b_rcred : bp->b_wcred;
807         error = fuse_filehandle_getrw(vp, fflag, &fufh, cred, pid);
808         if (bp->b_iocmd == BIO_READ && error == EBADF) {
809                 /* 
810                  * This may be a read-modify-write operation on a cached file
811                  * opened O_WRONLY.  The FUSE protocol allows this.
812                  */
813                 error = fuse_filehandle_get(vp, FWRITE, &fufh, cred, pid);
814         }
815         if (error) {
816                 printf("FUSE: strategy: filehandles are closed\n");
817                 bp->b_ioflags |= BIO_ERROR;
818                 bp->b_error = error;
819                 bufdone(bp);
820                 return (error);
821         }
822
823         uiop = &uio;
824         uiop->uio_iov = &io;
825         uiop->uio_iovcnt = 1;
826         uiop->uio_segflg = UIO_SYSSPACE;
827         uiop->uio_td = curthread;
828
829         /*
830          * clear BIO_ERROR and B_INVAL state prior to initiating the I/O.  We
831          * do this here so we do not have to do it in all the code that
832          * calls us.
833          */
834         bp->b_flags &= ~B_INVAL;
835         bp->b_ioflags &= ~BIO_ERROR;
836
837         KASSERT(!(bp->b_flags & B_DONE),
838             ("fuse_io_strategy: bp %p already marked done", bp));
839         if (bp->b_iocmd == BIO_READ) {
840                 io.iov_len = uiop->uio_resid = bp->b_bcount;
841                 io.iov_base = bp->b_data;
842                 uiop->uio_rw = UIO_READ;
843
844                 uiop->uio_offset = ((off_t)bp->b_blkno) * biosize;
845                 error = fuse_read_directbackend(vp, uiop, cred, fufh);
846
847                 if (!error && uiop->uio_resid) {
848                         /*
849                          * If we had a short read with no error, we must have
850                          * hit a file hole.  We should zero-fill the remainder.
851                          * This can also occur if the server hits the file EOF.
852                          *
853                          * Holes used to be able to occur due to pending
854                          * writes, but that is not possible any longer.
855                          */
856                         int nread = bp->b_bcount - uiop->uio_resid;
857                         int left = uiop->uio_resid;
858
859                         if (left > 0)
860                                 bzero((char *)bp->b_data + nread, left);
861                         uiop->uio_resid = 0;
862                 }
863                 if (error) {
864                         bp->b_ioflags |= BIO_ERROR;
865                         bp->b_error = error;
866                 }
867         } else {
868                 /*
869                  * If we only need to commit, try to commit
870                  */
871                 if (bp->b_flags & B_NEEDCOMMIT) {
872                         SDT_PROBE2(fusefs, , io, trace, 1,
873                                 "write: B_NEEDCOMMIT flags set");
874                 }
875                 /*
876                  * Setup for actual write
877                  */
878                 error = fuse_vnode_size(vp, &filesize, cred, curthread);
879                 if (error) {
880                         bp->b_ioflags |= BIO_ERROR;
881                         bp->b_error = error;
882                         bufdone(bp);
883                         return (error);
884                 }
885
886                 if ((off_t)bp->b_blkno * biosize + bp->b_dirtyend > filesize)
887                         bp->b_dirtyend = filesize - 
888                                 (off_t)bp->b_blkno * biosize;
889
890                 if (bp->b_dirtyend > bp->b_dirtyoff) {
891                         io.iov_len = uiop->uio_resid = bp->b_dirtyend
892                             - bp->b_dirtyoff;
893                         uiop->uio_offset = (off_t)bp->b_blkno * biosize
894                             + bp->b_dirtyoff;
895                         io.iov_base = (char *)bp->b_data + bp->b_dirtyoff;
896                         uiop->uio_rw = UIO_WRITE;
897
898                         error = fuse_write_directbackend(vp, uiop, cred, fufh,
899                                 filesize, 0, false);
900
901                         if (error == EINTR || error == ETIMEDOUT
902                             || (!error && (bp->b_flags & B_NEEDCOMMIT))) {
903
904                                 bp->b_flags &= ~(B_INVAL | B_NOCACHE);
905                                 if ((bp->b_flags & B_PAGING) == 0) {
906                                         bdirty(bp);
907                                         bp->b_flags &= ~B_DONE;
908                                 }
909                                 if ((error == EINTR || error == ETIMEDOUT) &&
910                                     (bp->b_flags & B_ASYNC) == 0)
911                                         bp->b_flags |= B_EINTR;
912                         } else {
913                                 if (error) {
914                                         bp->b_ioflags |= BIO_ERROR;
915                                         bp->b_flags |= B_INVAL;
916                                         bp->b_error = error;
917                                 }
918                                 bp->b_dirtyoff = bp->b_dirtyend = 0;
919                         }
920                 } else {
921                         bp->b_resid = 0;
922                         bufdone(bp);
923                         return (0);
924                 }
925         }
926         bp->b_resid = uiop->uio_resid;
927         bufdone(bp);
928         return (error);
929 }
930
931 int
932 fuse_io_flushbuf(struct vnode *vp, int waitfor, struct thread *td)
933 {
934
935         return (vn_fsync_buf(vp, waitfor));
936 }
937
938 /*
939  * Flush and invalidate all dirty buffers. If another process is already
940  * doing the flush, just wait for completion.
941  */
942 int
943 fuse_io_invalbuf(struct vnode *vp, struct thread *td)
944 {
945         struct fuse_vnode_data *fvdat = VTOFUD(vp);
946         int error = 0;
947
948         if (vp->v_iflag & VI_DOOMED)
949                 return 0;
950
951         ASSERT_VOP_ELOCKED(vp, "fuse_io_invalbuf");
952
953         while (fvdat->flag & FN_FLUSHINPROG) {
954                 struct proc *p = td->td_proc;
955
956                 if (vp->v_mount->mnt_kern_flag & MNTK_UNMOUNTF)
957                         return EIO;
958                 fvdat->flag |= FN_FLUSHWANT;
959                 tsleep(&fvdat->flag, PRIBIO + 2, "fusevinv", 2 * hz);
960                 error = 0;
961                 if (p != NULL) {
962                         PROC_LOCK(p);
963                         if (SIGNOTEMPTY(p->p_siglist) ||
964                             SIGNOTEMPTY(td->td_siglist))
965                                 error = EINTR;
966                         PROC_UNLOCK(p);
967                 }
968                 if (error == EINTR)
969                         return EINTR;
970         }
971         fvdat->flag |= FN_FLUSHINPROG;
972
973         if (vp->v_bufobj.bo_object != NULL) {
974                 VM_OBJECT_WLOCK(vp->v_bufobj.bo_object);
975                 vm_object_page_clean(vp->v_bufobj.bo_object, 0, 0, OBJPC_SYNC);
976                 VM_OBJECT_WUNLOCK(vp->v_bufobj.bo_object);
977         }
978         error = vinvalbuf(vp, V_SAVE, PCATCH, 0);
979         while (error) {
980                 if (error == ERESTART || error == EINTR) {
981                         fvdat->flag &= ~FN_FLUSHINPROG;
982                         if (fvdat->flag & FN_FLUSHWANT) {
983                                 fvdat->flag &= ~FN_FLUSHWANT;
984                                 wakeup(&fvdat->flag);
985                         }
986                         return EINTR;
987                 }
988                 error = vinvalbuf(vp, V_SAVE, PCATCH, 0);
989         }
990         fvdat->flag &= ~FN_FLUSHINPROG;
991         if (fvdat->flag & FN_FLUSHWANT) {
992                 fvdat->flag &= ~FN_FLUSHWANT;
993                 wakeup(&fvdat->flag);
994         }
995         return (error);
996 }