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