]> CyberLeo.Net >> Repos - FreeBSD/FreeBSD.git/blob - sys/fs/fuse/fuse_io.c
fusefs: send FUSE_FLUSH during VOP_CLOSE
[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/proc.h>
76 #include <sys/mount.h>
77 #include <sys/vnode.h>
78 #include <sys/stat.h>
79 #include <sys/unistd.h>
80 #include <sys/filedesc.h>
81 #include <sys/file.h>
82 #include <sys/fcntl.h>
83 #include <sys/bio.h>
84 #include <sys/buf.h>
85 #include <sys/sysctl.h>
86
87 #include <vm/vm.h>
88 #include <vm/vm_extern.h>
89 #include <vm/pmap.h>
90 #include <vm/vm_map.h>
91 #include <vm/vm_page.h>
92 #include <vm/vm_object.h>
93
94 #include "fuse.h"
95 #include "fuse_file.h"
96 #include "fuse_node.h"
97 #include "fuse_internal.h"
98 #include "fuse_ipc.h"
99 #include "fuse_io.h"
100
101 SDT_PROVIDER_DECLARE(fuse);
102 /* 
103  * Fuse trace probe:
104  * arg0: verbosity.  Higher numbers give more verbose messages
105  * arg1: Textual message
106  */
107 SDT_PROBE_DEFINE2(fuse, , io, trace, "int", "char*");
108
109 static int 
110 fuse_read_directbackend(struct vnode *vp, struct uio *uio,
111     struct ucred *cred, struct fuse_filehandle *fufh);
112 static int 
113 fuse_read_biobackend(struct vnode *vp, struct uio *uio,
114     struct ucred *cred, struct fuse_filehandle *fufh, pid_t pid);
115 static int 
116 fuse_write_directbackend(struct vnode *vp, struct uio *uio,
117     struct ucred *cred, struct fuse_filehandle *fufh, int ioflag);
118 static int 
119 fuse_write_biobackend(struct vnode *vp, struct uio *uio,
120     struct ucred *cred, struct fuse_filehandle *fufh, int ioflag, pid_t pid);
121
122 SDT_PROBE_DEFINE5(fuse, , io, io_dispatch, "struct vnode*", "struct uio*",
123                 "int", "struct ucred*", "struct fuse_filehandle*");
124 int
125 fuse_io_dispatch(struct vnode *vp, struct uio *uio, int ioflag,
126     struct ucred *cred, pid_t pid)
127 {
128         struct fuse_filehandle *fufh;
129         int err, directio;
130         int fflag;
131
132         MPASS(vp->v_type == VREG || vp->v_type == VDIR);
133
134         fflag = (uio->uio_rw == UIO_READ) ? FREAD : FWRITE;
135         err = fuse_filehandle_getrw(vp, fflag, &fufh, cred, pid);
136         if (err) {
137                 printf("FUSE: io dispatch: filehandles are closed\n");
138                 return err;
139         }
140         SDT_PROBE5(fuse, , io, io_dispatch, vp, uio, ioflag, cred, fufh);
141
142         /*
143          * Ideally, when the daemon asks for direct io at open time, the
144          * standard file flag should be set according to this, so that would
145          * just change the default mode, which later on could be changed via
146          * fcntl(2).
147          * But this doesn't work, the O_DIRECT flag gets cleared at some point
148          * (don't know where). So to make any use of the Fuse direct_io option,
149          * we hardwire it into the file's private data (similarly to Linux,
150          * btw.).
151          */
152         directio = (ioflag & IO_DIRECT) || !fsess_opt_datacache(vnode_mount(vp));
153
154         switch (uio->uio_rw) {
155         case UIO_READ:
156                 if (directio) {
157                         SDT_PROBE2(fuse, , io, trace, 1,
158                                 "direct read of vnode");
159                         err = fuse_read_directbackend(vp, uio, cred, fufh);
160                 } else {
161                         SDT_PROBE2(fuse, , io, trace, 1,
162                                 "buffered read of vnode");
163                         err = fuse_read_biobackend(vp, uio, cred, fufh, pid);
164                 }
165                 break;
166         case UIO_WRITE:
167                 /*
168                  * Kludge: simulate write-through caching via write-around
169                  * caching.  Same effect, as far as never caching dirty data,
170                  * but slightly pessimal in that newly written data is not
171                  * cached.
172                  */
173                 if (directio || fuse_data_cache_mode == FUSE_CACHE_WT) {
174                         SDT_PROBE2(fuse, , io, trace, 1,
175                                 "direct write of vnode");
176                         err = fuse_write_directbackend(vp, uio, cred, fufh,
177                                 ioflag);
178                 } else {
179                         SDT_PROBE2(fuse, , io, trace, 1,
180                                 "buffered write of vnode");
181                         err = fuse_write_biobackend(vp, uio, cred, fufh, ioflag,
182                                 pid);
183                 }
184                 break;
185         default:
186                 panic("uninterpreted mode passed to fuse_io_dispatch");
187         }
188
189         return (err);
190 }
191
192 SDT_PROBE_DEFINE3(fuse, , io, read_bio_backend_start, "int", "int", "int");
193 SDT_PROBE_DEFINE2(fuse, , io, read_bio_backend_feed, "int", "int");
194 SDT_PROBE_DEFINE3(fuse, , io, read_bio_backend_end, "int", "ssize_t", "int");
195 static int
196 fuse_read_biobackend(struct vnode *vp, struct uio *uio,
197     struct ucred *cred, struct fuse_filehandle *fufh, pid_t pid)
198 {
199         struct buf *bp;
200         daddr_t lbn;
201         int bcount;
202         int err = 0, n = 0, on = 0;
203         off_t filesize;
204
205         const int biosize = fuse_iosize(vp);
206
207         if (uio->uio_resid == 0)
208                 return (0);
209         if (uio->uio_offset < 0)
210                 return (EINVAL);
211
212         bcount = biosize;
213         filesize = VTOFUD(vp)->filesize;
214
215         do {
216                 if (fuse_isdeadfs(vp)) {
217                         err = ENXIO;
218                         break;
219                 }
220                 lbn = uio->uio_offset / biosize;
221                 on = uio->uio_offset & (biosize - 1);
222
223                 SDT_PROBE3(fuse, , io, read_bio_backend_start,
224                         biosize, (int)lbn, on);
225
226                 /*
227                  * Obtain the buffer cache block.  Figure out the buffer size
228                  * when we are at EOF.  If we are modifying the size of the
229                  * buffer based on an EOF condition we need to hold
230                  * nfs_rslock() through obtaining the buffer to prevent
231                  * a potential writer-appender from messing with n_size.
232                  * Otherwise we may accidentally truncate the buffer and
233                  * lose dirty data.
234                  *
235                  * Note that bcount is *not* DEV_BSIZE aligned.
236                  */
237                 if ((off_t)lbn * biosize >= filesize) {
238                         bcount = 0;
239                 } else if ((off_t)(lbn + 1) * biosize > filesize) {
240                         bcount = filesize - (off_t)lbn *biosize;
241                 }
242                 bp = getblk(vp, lbn, bcount, PCATCH, 0, 0);
243
244                 if (!bp)
245                         return (EINTR);
246
247                 /*
248                  * If B_CACHE is not set, we must issue the read.  If this
249                  * fails, we return an error.
250                  */
251
252                 if ((bp->b_flags & B_CACHE) == 0) {
253                         bp->b_iocmd = BIO_READ;
254                         vfs_busy_pages(bp, 0);
255                         err = fuse_io_strategy(vp, bp);
256                         if (err) {
257                                 brelse(bp);
258                                 return (err);
259                         }
260                 }
261                 /*
262                  * on is the offset into the current bp.  Figure out how many
263                  * bytes we can copy out of the bp.  Note that bcount is
264                  * NOT DEV_BSIZE aligned.
265                  *
266                  * Then figure out how many bytes we can copy into the uio.
267                  */
268
269                 n = 0;
270                 if (on < bcount)
271                         n = MIN((unsigned)(bcount - on), uio->uio_resid);
272                 if (n > 0) {
273                         SDT_PROBE2(fuse, , io, read_bio_backend_feed,
274                                 n, n + (int)bp->b_resid);
275                         err = uiomove(bp->b_data + on, n, uio);
276                 }
277                 brelse(bp);
278                 SDT_PROBE3(fuse, , io, read_bio_backend_end, err,
279                         uio->uio_resid, n);
280         } while (err == 0 && uio->uio_resid > 0 && n > 0);
281
282         return (err);
283 }
284
285 SDT_PROBE_DEFINE1(fuse, , io, read_directbackend_start, "struct fuse_read_in*");
286 SDT_PROBE_DEFINE2(fuse, , io, read_directbackend_complete,
287         "struct fuse_dispatcher*", "struct uio*");
288
289 static int
290 fuse_read_directbackend(struct vnode *vp, struct uio *uio,
291     struct ucred *cred, struct fuse_filehandle *fufh)
292 {
293         struct fuse_dispatcher fdi;
294         struct fuse_read_in *fri;
295         int err = 0;
296
297         if (uio->uio_resid == 0)
298                 return (0);
299
300         fdisp_init(&fdi, 0);
301
302         /*
303          * XXX In "normal" case we use an intermediate kernel buffer for
304          * transmitting data from daemon's context to ours. Eventually, we should
305          * get rid of this. Anyway, if the target uio lives in sysspace (we are
306          * called from pageops), and the input data doesn't need kernel-side
307          * processing (we are not called from readdir) we can already invoke
308          * an optimized, "peer-to-peer" I/O routine.
309          */
310         while (uio->uio_resid > 0) {
311                 fdi.iosize = sizeof(*fri);
312                 fdisp_make_vp(&fdi, FUSE_READ, vp, uio->uio_td, cred);
313                 fri = fdi.indata;
314                 fri->fh = fufh->fh_id;
315                 fri->offset = uio->uio_offset;
316                 fri->size = MIN(uio->uio_resid,
317                     fuse_get_mpdata(vp->v_mount)->max_read);
318
319                 SDT_PROBE1(fuse, , io, read_directbackend_start, fri);
320
321                 if ((err = fdisp_wait_answ(&fdi)))
322                         goto out;
323
324                 SDT_PROBE2(fuse, , io, read_directbackend_complete,
325                         fdi.iosize, uio);
326
327                 if ((err = uiomove(fdi.answ, MIN(fri->size, fdi.iosize), uio)))
328                         break;
329                 if (fdi.iosize < fri->size)
330                         break;
331         }
332
333 out:
334         fdisp_destroy(&fdi);
335         return (err);
336 }
337
338 static int
339 fuse_write_directbackend(struct vnode *vp, struct uio *uio,
340     struct ucred *cred, struct fuse_filehandle *fufh, int ioflag)
341 {
342         struct fuse_vnode_data *fvdat = VTOFUD(vp);
343         struct fuse_write_in *fwi;
344         struct fuse_dispatcher fdi;
345         size_t chunksize;
346         int diff;
347         int err = 0;
348
349         if (uio->uio_resid == 0)
350                 return (0);
351         if (ioflag & IO_APPEND)
352                 uio_setoffset(uio, fvdat->filesize);
353
354         fdisp_init(&fdi, 0);
355
356         while (uio->uio_resid > 0) {
357                 chunksize = MIN(uio->uio_resid,
358                     fuse_get_mpdata(vp->v_mount)->max_write);
359
360                 fdi.iosize = sizeof(*fwi) + chunksize;
361                 fdisp_make_vp(&fdi, FUSE_WRITE, vp, uio->uio_td, cred);
362
363                 fwi = fdi.indata;
364                 fwi->fh = fufh->fh_id;
365                 fwi->offset = uio->uio_offset;
366                 fwi->size = chunksize;
367
368                 if ((err = uiomove((char *)fdi.indata + sizeof(*fwi),
369                     chunksize, uio)))
370                         break;
371
372                 if ((err = fdisp_wait_answ(&fdi)))
373                         break;
374
375                 /* Adjust the uio in the case of short writes */
376                 diff = chunksize - ((struct fuse_write_out *)fdi.answ)->size;
377                 if (diff < 0) {
378                         err = EINVAL;
379                         break;
380                 } else if (diff > 0 && !(ioflag & IO_DIRECT)) {
381                         /* 
382                          * XXX We really should be directly checking whether
383                          * the file was opened with FOPEN_DIRECT_IO, not
384                          * IO_DIRECT.  IO_DIRECT can be set in multiple ways.
385                          */
386                         SDT_PROBE2(fuse, , io, trace, 1,
387                                 "misbehaving filesystem: short writes are only "
388                                 "allowed with direct_io");
389                 }
390                 uio->uio_resid += diff;
391                 uio->uio_offset -= diff;
392
393                 if (uio->uio_offset > fvdat->filesize &&
394                     fuse_data_cache_mode != FUSE_CACHE_UC) {
395                         fuse_vnode_setsize(vp, cred, uio->uio_offset);
396                         fvdat->flag &= ~FN_SIZECHANGE;
397                 }
398         }
399
400         fdisp_destroy(&fdi);
401
402         return (err);
403 }
404
405 SDT_PROBE_DEFINE6(fuse, , io, write_biobackend_start, "int64_t", "int", "int",
406                 "struct uio*", "int", "bool");
407 SDT_PROBE_DEFINE2(fuse, , io, write_biobackend_append_race, "long", "int");
408
409 static int
410 fuse_write_biobackend(struct vnode *vp, struct uio *uio,
411     struct ucred *cred, struct fuse_filehandle *fufh, int ioflag, pid_t pid)
412 {
413         struct fuse_vnode_data *fvdat = VTOFUD(vp);
414         struct buf *bp;
415         daddr_t lbn;
416         int bcount;
417         int n, on, err = 0;
418
419         const int biosize = fuse_iosize(vp);
420
421         KASSERT(uio->uio_rw == UIO_WRITE, ("ncl_write mode"));
422         if (vp->v_type != VREG)
423                 return (EIO);
424         if (uio->uio_offset < 0)
425                 return (EINVAL);
426         if (uio->uio_resid == 0)
427                 return (0);
428         if (ioflag & IO_APPEND)
429                 uio_setoffset(uio, fvdat->filesize);
430
431         /*
432          * Find all of this file's B_NEEDCOMMIT buffers.  If our writes
433          * would exceed the local maximum per-file write commit size when
434          * combined with those, we must decide whether to flush,
435          * go synchronous, or return err.  We don't bother checking
436          * IO_UNIT -- we just make all writes atomic anyway, as there's
437          * no point optimizing for something that really won't ever happen.
438          */
439         do {
440                 if (fuse_isdeadfs(vp)) {
441                         err = ENXIO;
442                         break;
443                 }
444                 lbn = uio->uio_offset / biosize;
445                 on = uio->uio_offset & (biosize - 1);
446                 n = MIN((unsigned)(biosize - on), uio->uio_resid);
447
448 again:
449                 /*
450                  * Handle direct append and file extension cases, calculate
451                  * unaligned buffer size.
452                  */
453                 if (uio->uio_offset == fvdat->filesize && n) {
454                         /*
455                          * Get the buffer (in its pre-append state to maintain
456                          * B_CACHE if it was previously set).  Resize the
457                          * nfsnode after we have locked the buffer to prevent
458                          * readers from reading garbage.
459                          */
460                         bcount = on;
461                         SDT_PROBE6(fuse, , io, write_biobackend_start,
462                                 lbn, on, n, uio, bcount, true);
463                         bp = getblk(vp, lbn, bcount, PCATCH, 0, 0);
464
465                         if (bp != NULL) {
466                                 long save;
467
468                                 err = fuse_vnode_setsize(vp, cred, 
469                                                          uio->uio_offset + n);
470                                 if (err) {
471                                         brelse(bp);
472                                         break;
473                                 }
474                                 save = bp->b_flags & B_CACHE;
475                                 bcount += n;
476                                 allocbuf(bp, bcount);
477                                 bp->b_flags |= save;
478                         }
479                 } else {
480                         /*
481                          * Obtain the locked cache block first, and then
482                          * adjust the file's size as appropriate.
483                          */
484                         bcount = on + n;
485                         if ((off_t)lbn * biosize + bcount < fvdat->filesize) {
486                                 if ((off_t)(lbn + 1) * biosize < fvdat->filesize)
487                                         bcount = biosize;
488                                 else
489                                         bcount = fvdat->filesize - 
490                                           (off_t)lbn *biosize;
491                         }
492                         SDT_PROBE6(fuse, , io, write_biobackend_start,
493                                 lbn, on, n, uio, bcount, false);
494                         bp = getblk(vp, lbn, bcount, PCATCH, 0, 0);
495                         if (bp && uio->uio_offset + n > fvdat->filesize) {
496                                 err = fuse_vnode_setsize(vp, cred, 
497                                                          uio->uio_offset + n);
498                                 if (err) {
499                                         brelse(bp);
500                                         break;
501                                 }
502                         }
503                 }
504
505                 if (!bp) {
506                         err = EINTR;
507                         break;
508                 }
509                 /*
510                  * Issue a READ if B_CACHE is not set.  In special-append
511                  * mode, B_CACHE is based on the buffer prior to the write
512                  * op and is typically set, avoiding the read.  If a read
513                  * is required in special append mode, the server will
514                  * probably send us a short-read since we extended the file
515                  * on our end, resulting in b_resid == 0 and, thusly,
516                  * B_CACHE getting set.
517                  *
518                  * We can also avoid issuing the read if the write covers
519                  * the entire buffer.  We have to make sure the buffer state
520                  * is reasonable in this case since we will not be initiating
521                  * I/O.  See the comments in kern/vfs_bio.c's getblk() for
522                  * more information.
523                  *
524                  * B_CACHE may also be set due to the buffer being cached
525                  * normally.
526                  */
527
528                 if (on == 0 && n == bcount) {
529                         bp->b_flags |= B_CACHE;
530                         bp->b_flags &= ~B_INVAL;
531                         bp->b_ioflags &= ~BIO_ERROR;
532                 }
533                 if ((bp->b_flags & B_CACHE) == 0) {
534                         bp->b_iocmd = BIO_READ;
535                         vfs_busy_pages(bp, 0);
536                         fuse_io_strategy(vp, bp);
537                         if ((err = bp->b_error)) {
538                                 brelse(bp);
539                                 break;
540                         }
541                 }
542                 if (bp->b_wcred == NOCRED)
543                         bp->b_wcred = crhold(cred);
544
545                 /*
546                  * If dirtyend exceeds file size, chop it down.  This should
547                  * not normally occur but there is an append race where it
548                  * might occur XXX, so we log it.
549                  *
550                  * If the chopping creates a reverse-indexed or degenerate
551                  * situation with dirtyoff/end, we 0 both of them.
552                  */
553
554                 if (bp->b_dirtyend > bcount) {
555                         SDT_PROBE2(fuse, , io, write_biobackend_append_race,
556                             (long)bp->b_blkno * biosize,
557                             bp->b_dirtyend - bcount);
558                         bp->b_dirtyend = bcount;
559                 }
560                 if (bp->b_dirtyoff >= bp->b_dirtyend)
561                         bp->b_dirtyoff = bp->b_dirtyend = 0;
562
563                 /*
564                  * If the new write will leave a contiguous dirty
565                  * area, just update the b_dirtyoff and b_dirtyend,
566                  * otherwise force a write rpc of the old dirty area.
567                  *
568                  * While it is possible to merge discontiguous writes due to
569                  * our having a B_CACHE buffer ( and thus valid read data
570                  * for the hole), we don't because it could lead to
571                  * significant cache coherency problems with multiple clients,
572                  * especially if locking is implemented later on.
573                  *
574                  * as an optimization we could theoretically maintain
575                  * a linked list of discontinuous areas, but we would still
576                  * have to commit them separately so there isn't much
577                  * advantage to it except perhaps a bit of asynchronization.
578                  */
579
580                 if (bp->b_dirtyend > 0 &&
581                     (on > bp->b_dirtyend || (on + n) < bp->b_dirtyoff)) {
582                         /*
583                          * Yes, we mean it. Write out everything to "storage"
584                          * immediately, without hesitation. (Apart from other
585                          * reasons: the only way to know if a write is valid
586                          * if its actually written out.)
587                          */
588                         bwrite(bp);
589                         if (bp->b_error == EINTR) {
590                                 err = EINTR;
591                                 break;
592                         }
593                         goto again;
594                 }
595                 err = uiomove((char *)bp->b_data + on, n, uio);
596
597                 /*
598                  * Since this block is being modified, it must be written
599                  * again and not just committed.  Since write clustering does
600                  * not work for the stage 1 data write, only the stage 2
601                  * commit rpc, we have to clear B_CLUSTEROK as well.
602                  */
603                 bp->b_flags &= ~(B_NEEDCOMMIT | B_CLUSTEROK);
604
605                 if (err) {
606                         bp->b_ioflags |= BIO_ERROR;
607                         bp->b_error = err;
608                         brelse(bp);
609                         break;
610                 }
611                 /*
612                  * Only update dirtyoff/dirtyend if not a degenerate
613                  * condition.
614                  */
615                 if (n) {
616                         if (bp->b_dirtyend > 0) {
617                                 bp->b_dirtyoff = MIN(on, bp->b_dirtyoff);
618                                 bp->b_dirtyend = MAX((on + n), bp->b_dirtyend);
619                         } else {
620                                 bp->b_dirtyoff = on;
621                                 bp->b_dirtyend = on + n;
622                         }
623                         vfs_bio_set_valid(bp, on, n);
624                 }
625                 err = bwrite(bp);
626                 if (err)
627                         break;
628         } while (uio->uio_resid > 0 && n > 0);
629
630         if (fuse_sync_resize && (fvdat->flag & FN_SIZECHANGE) != 0)
631                 fuse_vnode_savesize(vp, cred, pid);
632
633         return (err);
634 }
635
636 int
637 fuse_io_strategy(struct vnode *vp, struct buf *bp)
638 {
639         struct fuse_filehandle *fufh;
640         struct fuse_vnode_data *fvdat = VTOFUD(vp);
641         struct ucred *cred;
642         struct uio *uiop;
643         struct uio uio;
644         struct iovec io;
645         int error = 0;
646         int fflag;
647         /* We don't know the true pid when we're dealing with the cache */
648         pid_t pid = 0;
649
650         const int biosize = fuse_iosize(vp);
651
652         MPASS(vp->v_type == VREG || vp->v_type == VDIR);
653         MPASS(bp->b_iocmd == BIO_READ || bp->b_iocmd == BIO_WRITE);
654
655         fflag = bp->b_iocmd == BIO_READ ? FREAD : FWRITE;
656         cred = bp->b_iocmd == BIO_READ ? bp->b_rcred : bp->b_wcred;
657         error = fuse_filehandle_getrw(vp, fflag, &fufh, cred, pid);
658         if (bp->b_iocmd == BIO_READ && error == EBADF) {
659                 /* 
660                  * This may be a read-modify-write operation on a cached file
661                  * opened O_WRONLY.  The FUSE protocol allows this.
662                  *
663                  * TODO: eliminate this hacky check once the FUFH table is gone
664                  */
665                 error = fuse_filehandle_get(vp, FWRITE, &fufh, cred, pid);
666         }
667         if (error) {
668                 printf("FUSE: strategy: filehandles are closed\n");
669                 bp->b_ioflags |= BIO_ERROR;
670                 bp->b_error = error;
671                 bufdone(bp);
672                 return (error);
673         }
674
675         uiop = &uio;
676         uiop->uio_iov = &io;
677         uiop->uio_iovcnt = 1;
678         uiop->uio_segflg = UIO_SYSSPACE;
679         uiop->uio_td = curthread;
680
681         /*
682          * clear BIO_ERROR and B_INVAL state prior to initiating the I/O.  We
683          * do this here so we do not have to do it in all the code that
684          * calls us.
685          */
686         bp->b_flags &= ~B_INVAL;
687         bp->b_ioflags &= ~BIO_ERROR;
688
689         KASSERT(!(bp->b_flags & B_DONE),
690             ("fuse_io_strategy: bp %p already marked done", bp));
691         if (bp->b_iocmd == BIO_READ) {
692                 io.iov_len = uiop->uio_resid = bp->b_bcount;
693                 io.iov_base = bp->b_data;
694                 uiop->uio_rw = UIO_READ;
695
696                 uiop->uio_offset = ((off_t)bp->b_blkno) * biosize;
697                 error = fuse_read_directbackend(vp, uiop, cred, fufh);
698
699                 /* XXXCEM: Potentially invalid access to cached_attrs here */
700                 if ((!error && uiop->uio_resid) ||
701                     (fsess_opt_brokenio(vnode_mount(vp)) && error == EIO &&
702                     uiop->uio_offset < fvdat->filesize && fvdat->filesize > 0 &&
703                     uiop->uio_offset >= fvdat->cached_attrs.va_size)) {
704                         /*
705                          * If we had a short read with no error, we must have
706                          * hit a file hole.  We should zero-fill the remainder.
707                          * This can also occur if the server hits the file EOF.
708                          *
709                          * Holes used to be able to occur due to pending
710                          * writes, but that is not possible any longer.
711                          */
712                         int nread = bp->b_bcount - uiop->uio_resid;
713                         int left = uiop->uio_resid;
714
715                         if (error != 0) {
716                                 printf("FUSE: Fix broken io: offset %ju, "
717                                        " resid %zd, file size %ju/%ju\n", 
718                                        (uintmax_t)uiop->uio_offset,
719                                     uiop->uio_resid, fvdat->filesize,
720                                     fvdat->cached_attrs.va_size);
721                                 error = 0;
722                         }
723                         if (left > 0)
724                                 bzero((char *)bp->b_data + nread, left);
725                         uiop->uio_resid = 0;
726                 }
727                 if (error) {
728                         bp->b_ioflags |= BIO_ERROR;
729                         bp->b_error = error;
730                 }
731         } else {
732                 /*
733                  * If we only need to commit, try to commit
734                  */
735                 if (bp->b_flags & B_NEEDCOMMIT) {
736                         SDT_PROBE2(fuse, , io, trace, 1,
737                                 "write: B_NEEDCOMMIT flags set");
738                 }
739                 /*
740                  * Setup for actual write
741                  */
742                 if ((off_t)bp->b_blkno * biosize + bp->b_dirtyend > 
743                     fvdat->filesize)
744                         bp->b_dirtyend = fvdat->filesize - 
745                                 (off_t)bp->b_blkno * biosize;
746
747                 if (bp->b_dirtyend > bp->b_dirtyoff) {
748                         io.iov_len = uiop->uio_resid = bp->b_dirtyend
749                             - bp->b_dirtyoff;
750                         uiop->uio_offset = (off_t)bp->b_blkno * biosize
751                             + bp->b_dirtyoff;
752                         io.iov_base = (char *)bp->b_data + bp->b_dirtyoff;
753                         uiop->uio_rw = UIO_WRITE;
754
755                         error = fuse_write_directbackend(vp, uiop, cred, fufh, 0);
756
757                         if (error == EINTR || error == ETIMEDOUT
758                             || (!error && (bp->b_flags & B_NEEDCOMMIT))) {
759
760                                 bp->b_flags &= ~(B_INVAL | B_NOCACHE);
761                                 if ((bp->b_flags & B_PAGING) == 0) {
762                                         bdirty(bp);
763                                         bp->b_flags &= ~B_DONE;
764                                 }
765                                 if ((error == EINTR || error == ETIMEDOUT) &&
766                                     (bp->b_flags & B_ASYNC) == 0)
767                                         bp->b_flags |= B_EINTR;
768                         } else {
769                                 if (error) {
770                                         bp->b_ioflags |= BIO_ERROR;
771                                         bp->b_flags |= B_INVAL;
772                                         bp->b_error = error;
773                                 }
774                                 bp->b_dirtyoff = bp->b_dirtyend = 0;
775                         }
776                 } else {
777                         bp->b_resid = 0;
778                         bufdone(bp);
779                         return (0);
780                 }
781         }
782         bp->b_resid = uiop->uio_resid;
783         bufdone(bp);
784         return (error);
785 }
786
787 int
788 fuse_io_flushbuf(struct vnode *vp, int waitfor, struct thread *td)
789 {
790         struct vop_fsync_args a = {
791                 .a_vp = vp,
792                 .a_waitfor = waitfor,
793                 .a_td = td,
794         };
795
796         return (vop_stdfsync(&a));
797 }
798
799 /*
800  * Flush and invalidate all dirty buffers. If another process is already
801  * doing the flush, just wait for completion.
802  */
803 int
804 fuse_io_invalbuf(struct vnode *vp, struct thread *td)
805 {
806         struct fuse_vnode_data *fvdat = VTOFUD(vp);
807         int error = 0;
808
809         if (vp->v_iflag & VI_DOOMED)
810                 return 0;
811
812         ASSERT_VOP_ELOCKED(vp, "fuse_io_invalbuf");
813
814         while (fvdat->flag & FN_FLUSHINPROG) {
815                 struct proc *p = td->td_proc;
816
817                 if (vp->v_mount->mnt_kern_flag & MNTK_UNMOUNTF)
818                         return EIO;
819                 fvdat->flag |= FN_FLUSHWANT;
820                 tsleep(&fvdat->flag, PRIBIO + 2, "fusevinv", 2 * hz);
821                 error = 0;
822                 if (p != NULL) {
823                         PROC_LOCK(p);
824                         if (SIGNOTEMPTY(p->p_siglist) ||
825                             SIGNOTEMPTY(td->td_siglist))
826                                 error = EINTR;
827                         PROC_UNLOCK(p);
828                 }
829                 if (error == EINTR)
830                         return EINTR;
831         }
832         fvdat->flag |= FN_FLUSHINPROG;
833
834         if (vp->v_bufobj.bo_object != NULL) {
835                 VM_OBJECT_WLOCK(vp->v_bufobj.bo_object);
836                 vm_object_page_clean(vp->v_bufobj.bo_object, 0, 0, OBJPC_SYNC);
837                 VM_OBJECT_WUNLOCK(vp->v_bufobj.bo_object);
838         }
839         error = vinvalbuf(vp, V_SAVE, PCATCH, 0);
840         while (error) {
841                 if (error == ERESTART || error == EINTR) {
842                         fvdat->flag &= ~FN_FLUSHINPROG;
843                         if (fvdat->flag & FN_FLUSHWANT) {
844                                 fvdat->flag &= ~FN_FLUSHWANT;
845                                 wakeup(&fvdat->flag);
846                         }
847                         return EINTR;
848                 }
849                 error = vinvalbuf(vp, V_SAVE, PCATCH, 0);
850         }
851         fvdat->flag &= ~FN_FLUSHINPROG;
852         if (fvdat->flag & FN_FLUSHWANT) {
853                 fvdat->flag &= ~FN_FLUSHWANT;
854                 wakeup(&fvdat->flag);
855         }
856         return (error);
857 }