]> CyberLeo.Net >> Repos - FreeBSD/FreeBSD.git/blob - sys/fs/fuse/fuse_vnops.c
fusefs: fix a memory leak from r346979
[FreeBSD/FreeBSD.git] / sys / fs / fuse / fuse_vnops.c
1 /*-
2  * SPDX-License-Identifier: BSD-3-Clause
3  *
4  * Copyright (c) 2007-2009 Google Inc. and Amit Singh
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/param.h>
62 #include <sys/module.h>
63 #include <sys/systm.h>
64 #include <sys/errno.h>
65 #include <sys/kernel.h>
66 #include <sys/conf.h>
67 #include <sys/uio.h>
68 #include <sys/malloc.h>
69 #include <sys/queue.h>
70 #include <sys/lock.h>
71 #include <sys/rwlock.h>
72 #include <sys/sx.h>
73 #include <sys/proc.h>
74 #include <sys/mount.h>
75 #include <sys/vnode.h>
76 #include <sys/namei.h>
77 #include <sys/extattr.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/dirent.h>
84 #include <sys/bio.h>
85 #include <sys/buf.h>
86 #include <sys/sysctl.h>
87 #include <sys/vmmeter.h>
88
89 #include <vm/vm.h>
90 #include <vm/vm_extern.h>
91 #include <vm/pmap.h>
92 #include <vm/vm_map.h>
93 #include <vm/vm_page.h>
94 #include <vm/vm_param.h>
95 #include <vm/vm_object.h>
96 #include <vm/vm_pager.h>
97 #include <vm/vnode_pager.h>
98 #include <vm/vm_object.h>
99
100 #include "fuse.h"
101 #include "fuse_file.h"
102 #include "fuse_internal.h"
103 #include "fuse_ipc.h"
104 #include "fuse_node.h"
105 #include "fuse_io.h"
106
107 #include <sys/priv.h>
108
109 /* Maximum number of hardlinks to a single FUSE file */
110 #define FUSE_LINK_MAX                      UINT32_MAX
111
112 SDT_PROVIDER_DECLARE(fusefs);
113 /* 
114  * Fuse trace probe:
115  * arg0: verbosity.  Higher numbers give more verbose messages
116  * arg1: Textual message
117  */
118 SDT_PROBE_DEFINE2(fusefs, , vnops, trace, "int", "char*");
119
120 /* vnode ops */
121 static vop_access_t fuse_vnop_access;
122 static vop_advlock_t fuse_vnop_advlock;
123 static vop_close_t fuse_fifo_close;
124 static vop_close_t fuse_vnop_close;
125 static vop_create_t fuse_vnop_create;
126 static vop_deleteextattr_t fuse_vnop_deleteextattr;
127 static vop_fdatasync_t fuse_vnop_fdatasync;
128 static vop_fsync_t fuse_vnop_fsync;
129 static vop_getattr_t fuse_vnop_getattr;
130 static vop_getextattr_t fuse_vnop_getextattr;
131 static vop_inactive_t fuse_vnop_inactive;
132 static vop_link_t fuse_vnop_link;
133 static vop_listextattr_t fuse_vnop_listextattr;
134 static vop_lookup_t fuse_vnop_lookup;
135 static vop_mkdir_t fuse_vnop_mkdir;
136 static vop_mknod_t fuse_vnop_mknod;
137 static vop_open_t fuse_vnop_open;
138 static vop_pathconf_t fuse_vnop_pathconf;
139 static vop_read_t fuse_vnop_read;
140 static vop_readdir_t fuse_vnop_readdir;
141 static vop_readlink_t fuse_vnop_readlink;
142 static vop_reclaim_t fuse_vnop_reclaim;
143 static vop_remove_t fuse_vnop_remove;
144 static vop_rename_t fuse_vnop_rename;
145 static vop_rmdir_t fuse_vnop_rmdir;
146 static vop_setattr_t fuse_vnop_setattr;
147 static vop_setextattr_t fuse_vnop_setextattr;
148 static vop_strategy_t fuse_vnop_strategy;
149 static vop_symlink_t fuse_vnop_symlink;
150 static vop_write_t fuse_vnop_write;
151 static vop_getpages_t fuse_vnop_getpages;
152 static vop_putpages_t fuse_vnop_putpages;
153 static vop_print_t fuse_vnop_print;
154
155 struct vop_vector fuse_fifoops = {
156         .vop_default =          &fifo_specops,
157         .vop_access =           fuse_vnop_access,
158         .vop_close =            fuse_fifo_close,
159         .vop_fsync =            fuse_vnop_fsync,
160         .vop_getattr =          fuse_vnop_getattr,
161         .vop_inactive =         fuse_vnop_inactive,
162         .vop_pathconf =         fuse_vnop_pathconf,
163         .vop_print =            fuse_vnop_print,
164         .vop_read =             VOP_PANIC,
165         .vop_reclaim =          fuse_vnop_reclaim,
166         .vop_setattr =          fuse_vnop_setattr,
167         .vop_write =            VOP_PANIC,
168 };
169
170 struct vop_vector fuse_vnops = {
171         .vop_default = &default_vnodeops,
172         .vop_access = fuse_vnop_access,
173         .vop_advlock = fuse_vnop_advlock,
174         .vop_close = fuse_vnop_close,
175         .vop_create = fuse_vnop_create,
176         .vop_deleteextattr = fuse_vnop_deleteextattr,
177         .vop_fsync = fuse_vnop_fsync,
178         .vop_fdatasync = fuse_vnop_fdatasync,
179         .vop_getattr = fuse_vnop_getattr,
180         .vop_getextattr = fuse_vnop_getextattr,
181         .vop_inactive = fuse_vnop_inactive,
182         .vop_link = fuse_vnop_link,
183         .vop_listextattr = fuse_vnop_listextattr,
184         .vop_lookup = fuse_vnop_lookup,
185         .vop_mkdir = fuse_vnop_mkdir,
186         .vop_mknod = fuse_vnop_mknod,
187         .vop_open = fuse_vnop_open,
188         .vop_pathconf = fuse_vnop_pathconf,
189         .vop_read = fuse_vnop_read,
190         .vop_readdir = fuse_vnop_readdir,
191         .vop_readlink = fuse_vnop_readlink,
192         .vop_reclaim = fuse_vnop_reclaim,
193         .vop_remove = fuse_vnop_remove,
194         .vop_rename = fuse_vnop_rename,
195         .vop_rmdir = fuse_vnop_rmdir,
196         .vop_setattr = fuse_vnop_setattr,
197         .vop_setextattr = fuse_vnop_setextattr,
198         .vop_strategy = fuse_vnop_strategy,
199         .vop_symlink = fuse_vnop_symlink,
200         .vop_write = fuse_vnop_write,
201         .vop_getpages = fuse_vnop_getpages,
202         .vop_putpages = fuse_vnop_putpages,
203         .vop_print = fuse_vnop_print,
204 };
205
206 static u_long fuse_lookup_cache_hits = 0;
207
208 SYSCTL_ULONG(_vfs_fusefs, OID_AUTO, lookup_cache_hits, CTLFLAG_RD,
209     &fuse_lookup_cache_hits, 0, "number of positive cache hits in lookup");
210
211 static u_long fuse_lookup_cache_misses = 0;
212
213 SYSCTL_ULONG(_vfs_fusefs, OID_AUTO, lookup_cache_misses, CTLFLAG_RD,
214     &fuse_lookup_cache_misses, 0, "number of cache misses in lookup");
215
216 /*
217  * XXX: This feature is highly experimental and can bring to instabilities,
218  * needs revisiting before to be enabled by default.
219  */
220 static int fuse_reclaim_revoked = 0;
221
222 SYSCTL_INT(_vfs_fusefs, OID_AUTO, reclaim_revoked, CTLFLAG_RW,
223     &fuse_reclaim_revoked, 0, "");
224
225 uma_zone_t fuse_pbuf_zone;
226
227 #define fuse_vm_page_lock(m)            vm_page_lock((m));
228 #define fuse_vm_page_unlock(m)          vm_page_unlock((m));
229 #define fuse_vm_page_lock_queues()      ((void)0)
230 #define fuse_vm_page_unlock_queues()    ((void)0)
231
232 /* Check permission for extattr operations, much like extattr_check_cred */
233 static int
234 fuse_extattr_check_cred(struct vnode *vp, int ns, struct ucred *cred,
235         struct thread *td, accmode_t accmode)
236 {
237         struct mount *mp = vnode_mount(vp);
238         struct fuse_data *data = fuse_get_mpdata(mp);
239
240         /*
241          * Kernel-invoked always succeeds.
242          */
243         if (cred == NOCRED)
244                 return (0);
245
246         /*
247          * Do not allow privileged processes in jail to directly manipulate
248          * system attributes.
249          */
250         switch (ns) {
251         case EXTATTR_NAMESPACE_SYSTEM:
252                 if (data->dataflags & FSESS_DEFAULT_PERMISSIONS) {
253                         return (priv_check_cred(cred, PRIV_VFS_EXTATTR_SYSTEM));
254                 }
255                 /* FALLTHROUGH */
256         case EXTATTR_NAMESPACE_USER:
257                 return (fuse_internal_access(vp, accmode, td, cred));
258         default:
259                 return (EPERM);
260         }
261 }
262
263 /* Get a filehandle for a directory */
264 static int
265 fuse_filehandle_get_dir(struct vnode *vp, struct fuse_filehandle **fufhp,
266         struct ucred *cred, pid_t pid)
267 {
268         if (fuse_filehandle_get(vp, FREAD, fufhp, cred, pid) == 0)
269                 return 0;
270         return fuse_filehandle_get(vp, FEXEC, fufhp, cred, pid);
271 }
272
273 /* Send FUSE_FLUSH for this vnode */
274 static int
275 fuse_flush(struct vnode *vp, struct ucred *cred, pid_t pid, int fflag)
276 {
277         struct fuse_flush_in *ffi;
278         struct fuse_filehandle *fufh;
279         struct fuse_dispatcher fdi;
280         struct thread *td = curthread;
281         struct mount *mp = vnode_mount(vp);
282         int err;
283
284         if (!fsess_isimpl(vnode_mount(vp), FUSE_FLUSH))
285                 return 0;
286
287         err = fuse_filehandle_getrw(vp, fflag, &fufh, cred, pid);
288         if (err)
289                 return err;
290
291         fdisp_init(&fdi, sizeof(*ffi));
292         fdisp_make_vp(&fdi, FUSE_FLUSH, vp, td, cred);
293         ffi = fdi.indata;
294         ffi->fh = fufh->fh_id;
295         /* 
296          * If the file has a POSIX lock then we're supposed to set lock_owner.
297          * If not, then lock_owner is undefined.  So we may as well always set
298          * it.
299          */
300         ffi->lock_owner = td->td_proc->p_pid;
301
302         err = fdisp_wait_answ(&fdi);
303         if (err == ENOSYS) {
304                 fsess_set_notimpl(mp, FUSE_FLUSH);
305                 err = 0;
306         }
307         fdisp_destroy(&fdi);
308         return err;
309 }
310
311 /* Close wrapper for fifos.  */
312 static int
313 fuse_fifo_close(struct vop_close_args *ap)
314 {
315         return (fifo_specops.vop_close(ap));
316 }
317
318 /*
319     struct vnop_access_args {
320         struct vnode *a_vp;
321 #if VOP_ACCESS_TAKES_ACCMODE_T
322         accmode_t a_accmode;
323 #else
324         int a_mode;
325 #endif
326         struct ucred *a_cred;
327         struct thread *a_td;
328     };
329 */
330 static int
331 fuse_vnop_access(struct vop_access_args *ap)
332 {
333         struct vnode *vp = ap->a_vp;
334         int accmode = ap->a_accmode;
335         struct ucred *cred = ap->a_cred;
336
337         struct fuse_data *data = fuse_get_mpdata(vnode_mount(vp));
338
339         int err;
340
341         if (fuse_isdeadfs(vp)) {
342                 if (vnode_isvroot(vp)) {
343                         return 0;
344                 }
345                 return ENXIO;
346         }
347         if (!(data->dataflags & FSESS_INITED)) {
348                 if (vnode_isvroot(vp)) {
349                         if (priv_check_cred(cred, PRIV_VFS_ADMIN) ||
350                             (fuse_match_cred(data->daemoncred, cred) == 0)) {
351                                 return 0;
352                         }
353                 }
354                 return EBADF;
355         }
356         if (vnode_islnk(vp)) {
357                 return 0;
358         }
359
360         err = fuse_internal_access(vp, accmode, ap->a_td, ap->a_cred);
361         return err;
362 }
363
364 /*
365  * struct vop_advlock_args {
366  *      struct vop_generic_args a_gen;
367  *      struct vnode *a_vp;
368  *      void *a_id;
369  *      int a_op;
370  *      struct flock *a_fl;
371  *      int a_flags;
372  * }
373  */
374 static int
375 fuse_vnop_advlock(struct vop_advlock_args *ap)
376 {
377         struct vnode *vp = ap->a_vp;
378         struct flock *fl = ap->a_fl;
379         struct thread *td = curthread;
380         struct ucred *cred = td->td_ucred;
381         pid_t pid = td->td_proc->p_pid;
382         struct fuse_filehandle *fufh;
383         struct fuse_dispatcher fdi;
384         struct fuse_lk_in *fli;
385         struct fuse_lk_out *flo;
386         enum fuse_opcode op;
387         int dataflags, err;
388
389         dataflags = fuse_get_mpdata(vnode_mount(vp))->dataflags;
390
391         if (fuse_isdeadfs(vp)) {
392                 return ENXIO;
393         }
394
395         if (!(dataflags & FSESS_POSIX_LOCKS))
396                 return vop_stdadvlock(ap);
397
398         err = fuse_filehandle_get_anyflags(vp, &fufh, cred, pid);
399         if (err)
400                 return err;
401
402         fdisp_init(&fdi, sizeof(*fli));
403
404         switch(ap->a_op) {
405         case F_GETLK:
406                 op = FUSE_GETLK;
407                 break;
408         case F_SETLK:
409                 op = FUSE_SETLK;
410                 break;
411         case F_SETLKW:
412                 op = FUSE_SETLKW;
413                 break;
414         default:
415                 return EINVAL;
416         }
417
418         fdisp_make_vp(&fdi, op, vp, td, cred);
419         fli = fdi.indata;
420         fli->fh = fufh->fh_id;
421         fli->owner = fl->l_pid;
422         fli->lk.start = fl->l_start;
423         if (fl->l_len != 0)
424                 fli->lk.end = fl->l_start + fl->l_len - 1;
425         else
426                 fli->lk.end = INT64_MAX;
427         fli->lk.type = fl->l_type;
428         fli->lk.pid = fl->l_pid;
429
430         err = fdisp_wait_answ(&fdi);
431         fdisp_destroy(&fdi);
432
433         if (err == 0 && op == FUSE_GETLK) {
434                 flo = fdi.answ;
435                 fl->l_type = flo->lk.type;
436                 fl->l_pid = flo->lk.pid;
437                 if (flo->lk.type != F_UNLCK) {
438                         fl->l_start = flo->lk.start;
439                         if (flo->lk.end == INT64_MAX)
440                                 fl->l_len = 0;
441                         else
442                                 fl->l_len = flo->lk.end - flo->lk.start + 1;
443                         fl->l_start = flo->lk.start;
444                 }
445         }
446
447         return err;
448 }
449
450 /*
451     struct vop_close_args {
452         struct vnode *a_vp;
453         int  a_fflag;
454         struct ucred *a_cred;
455         struct thread *a_td;
456     };
457 */
458 static int
459 fuse_vnop_close(struct vop_close_args *ap)
460 {
461         struct vnode *vp = ap->a_vp;
462         struct ucred *cred = ap->a_cred;
463         int fflag = ap->a_fflag;
464         struct thread *td = ap->a_td;
465         pid_t pid = td->td_proc->p_pid;
466         int err = 0;
467
468         if (fuse_isdeadfs(vp))
469                 return 0;
470         if (vnode_isdir(vp))
471                 return 0;
472         if (fflag & IO_NDELAY)
473                 return 0;
474
475         err = fuse_flush(vp, cred, pid, fflag);
476         /* TODO: close the file handle, if we're sure it's no longer used */
477         if ((VTOFUD(vp)->flag & FN_SIZECHANGE) != 0) {
478                 fuse_vnode_savesize(vp, cred, td->td_proc->p_pid);
479         }
480         return err;
481 }
482
483 static void
484 fdisp_make_mknod_for_fallback(
485         struct fuse_dispatcher *fdip,
486         struct componentname *cnp,
487         struct vnode *dvp,
488         uint64_t parentnid,
489         struct thread *td,
490         struct ucred *cred,
491         mode_t mode,
492         enum fuse_opcode *op)
493 {
494         struct fuse_mknod_in *fmni;
495
496         fdisp_init(fdip, sizeof(*fmni) + cnp->cn_namelen + 1);
497         *op = FUSE_MKNOD;
498         fdisp_make(fdip, *op, vnode_mount(dvp), parentnid, td, cred);
499         fmni = fdip->indata;
500         fmni->mode = mode;
501         fmni->rdev = 0;
502         memcpy((char *)fdip->indata + sizeof(*fmni), cnp->cn_nameptr,
503             cnp->cn_namelen);
504         ((char *)fdip->indata)[sizeof(*fmni) + cnp->cn_namelen] = '\0';
505 }
506 /*
507     struct vnop_create_args {
508         struct vnode *a_dvp;
509         struct vnode **a_vpp;
510         struct componentname *a_cnp;
511         struct vattr *a_vap;
512     };
513 */
514 static int
515 fuse_vnop_create(struct vop_create_args *ap)
516 {
517         struct vnode *dvp = ap->a_dvp;
518         struct vnode **vpp = ap->a_vpp;
519         struct componentname *cnp = ap->a_cnp;
520         struct vattr *vap = ap->a_vap;
521         struct thread *td = cnp->cn_thread;
522         struct ucred *cred = cnp->cn_cred;
523
524         struct fuse_open_in *foi;
525         struct fuse_entry_out *feo;
526         struct fuse_open_out *foo;
527         struct fuse_dispatcher fdi, fdi2;
528         struct fuse_dispatcher *fdip = &fdi;
529         struct fuse_dispatcher *fdip2 = NULL;
530
531         int err;
532
533         struct mount *mp = vnode_mount(dvp);
534         uint64_t parentnid = VTOFUD(dvp)->nid;
535         mode_t mode = MAKEIMODE(vap->va_type, vap->va_mode);
536         enum fuse_opcode op;
537         int flags;
538
539         /* 
540          * VOP_CREATE doesn't tell us the open(2) flags, so we guess.  Only a
541          * writable mode makes sense, and we might as well include readability
542          * too.
543          */
544         flags = O_RDWR;
545
546         if (fuse_isdeadfs(dvp)) {
547                 return ENXIO;
548         }
549         bzero(&fdi, sizeof(fdi));
550
551         if ((vap->va_type != VREG && vap->va_type != VSOCK))
552                 return (EINVAL);
553
554         if (!fsess_isimpl(mp, FUSE_CREATE)) {
555                 /* Fallback to FUSE_MKNOD/FUSE_OPEN */
556                 fdisp_make_mknod_for_fallback(fdip, cnp, dvp, parentnid, td,
557                         cred, mode, &op);
558         } else {
559                 /* Use FUSE_CREATE */
560                 op = FUSE_CREATE;
561                 fdisp_init(fdip, sizeof(*foi) + cnp->cn_namelen + 1);
562                 fdisp_make(fdip, op, vnode_mount(dvp), parentnid, td, cred);
563                 foi = fdip->indata;
564                 foi->mode = mode;
565                 foi->flags = O_CREAT | flags;
566                 memcpy((char *)fdip->indata + sizeof(*foi), cnp->cn_nameptr,
567                     cnp->cn_namelen);
568                 ((char *)fdip->indata)[sizeof(*foi) + cnp->cn_namelen] = '\0';
569         }
570
571         err = fdisp_wait_answ(fdip);
572
573         if (err) {
574                 if (err == ENOSYS && op == FUSE_CREATE) {
575                         fsess_set_notimpl(mp, FUSE_CREATE);
576                         fdisp_make_mknod_for_fallback(fdip, cnp, dvp,
577                                 parentnid, td, cred, mode, &op);
578                         err = fdisp_wait_answ(fdip);
579                 }
580                 if (err)
581                         goto out;
582         }
583
584         feo = fdip->answ;
585
586         if ((err = fuse_internal_checkentry(feo, vap->va_type))) {
587                 goto out;
588         }
589
590         if (op == FUSE_CREATE) {
591                 foo = (struct fuse_open_out*)(feo + 1);
592         } else {
593                 /* Issue a separate FUSE_OPEN */
594                 fdip2 = &fdi2;
595                 fdisp_init(fdip2, sizeof(*foi));
596                 fdisp_make(fdip2, FUSE_OPEN, vnode_mount(dvp), feo->nodeid, td,
597                         cred);
598                 foi = fdip2->indata;
599                 foi->mode = mode;
600                 foi->flags = flags;
601                 err = fdisp_wait_answ(fdip2);
602                 if (err)
603                         goto out;
604                 foo = fdip2->answ;
605         }
606         err = fuse_vnode_get(mp, feo, feo->nodeid, dvp, vpp, cnp, vap->va_type);
607         if (err) {
608                 struct fuse_release_in *fri;
609                 uint64_t nodeid = feo->nodeid;
610                 uint64_t fh_id = foo->fh;
611
612                 fdisp_init(fdip, sizeof(*fri));
613                 fdisp_make(fdip, FUSE_RELEASE, mp, nodeid, td, cred);
614                 fri = fdip->indata;
615                 fri->fh = fh_id;
616                 fri->flags = flags;
617                 fuse_insert_callback(fdip->tick, fuse_internal_forget_callback);
618                 fuse_insert_message(fdip->tick, false);
619                 goto out;
620         }
621         ASSERT_VOP_ELOCKED(*vpp, "fuse_vnop_create");
622         fuse_internal_cache_attrs(*vpp, &feo->attr, feo->attr_valid,
623                 feo->attr_valid_nsec, NULL);
624
625         fuse_filehandle_init(*vpp, FUFH_RDWR, NULL, td, cred, foo);
626         fuse_vnode_open(*vpp, foo->open_flags, td);
627         cache_purge_negative(dvp);
628
629 out:
630         if (fdip2)
631                 fdisp_destroy(fdip2);
632         fdisp_destroy(fdip);
633         return err;
634 }
635
636 /*
637     struct vnop_fdatasync_args {
638         struct vop_generic_args a_gen;
639         struct vnode * a_vp;
640         struct thread * a_td;
641     };
642 */
643 static int
644 fuse_vnop_fdatasync(struct vop_fdatasync_args *ap)
645 {
646         struct vnode *vp = ap->a_vp;
647         struct thread *td = ap->a_td;
648         int waitfor = MNT_WAIT;
649
650         int err = 0;
651
652         if (fuse_isdeadfs(vp)) {
653                 return 0;
654         }
655         if ((err = vop_stdfdatasync_buf(ap)))
656                 return err;
657
658         return fuse_internal_fsync(vp, td, waitfor, true);
659 }
660
661 /*
662     struct vnop_fsync_args {
663         struct vop_generic_args a_gen;
664         struct vnode * a_vp;
665         int  a_waitfor;
666         struct thread * a_td;
667     };
668 */
669 static int
670 fuse_vnop_fsync(struct vop_fsync_args *ap)
671 {
672         struct vnode *vp = ap->a_vp;
673         struct thread *td = ap->a_td;
674         int waitfor = ap->a_waitfor;
675         int err = 0;
676
677         if (fuse_isdeadfs(vp)) {
678                 return 0;
679         }
680         if ((err = vop_stdfsync(ap)))
681                 return err;
682
683         return fuse_internal_fsync(vp, td, waitfor, false);
684 }
685
686 /*
687     struct vnop_getattr_args {
688         struct vnode *a_vp;
689         struct vattr *a_vap;
690         struct ucred *a_cred;
691         struct thread *a_td;
692     };
693 */
694 static int
695 fuse_vnop_getattr(struct vop_getattr_args *ap)
696 {
697         struct vnode *vp = ap->a_vp;
698         struct vattr *vap = ap->a_vap;
699         struct ucred *cred = ap->a_cred;
700         struct thread *td = curthread;
701
702         int err = 0;
703         int dataflags;
704
705         dataflags = fuse_get_mpdata(vnode_mount(vp))->dataflags;
706
707         /* Note that we are not bailing out on a dead file system just yet. */
708
709         if (!(dataflags & FSESS_INITED)) {
710                 if (!vnode_isvroot(vp)) {
711                         fdata_set_dead(fuse_get_mpdata(vnode_mount(vp)));
712                         err = ENOTCONN;
713                         return err;
714                 } else {
715                         goto fake;
716                 }
717         }
718         err = fuse_internal_getattr(vp, vap, cred, td);
719         if (err == ENOTCONN && vnode_isvroot(vp)) {
720                 /* see comment in fuse_vfsop_statfs() */
721                 goto fake;
722         } else {
723                 return err;
724         }
725
726 fake:
727         bzero(vap, sizeof(*vap));
728         vap->va_type = vnode_vtype(vp);
729
730         return 0;
731 }
732
733 /*
734     struct vnop_inactive_args {
735         struct vnode *a_vp;
736         struct thread *a_td;
737     };
738 */
739 static int
740 fuse_vnop_inactive(struct vop_inactive_args *ap)
741 {
742         struct vnode *vp = ap->a_vp;
743         struct thread *td = ap->a_td;
744
745         struct fuse_vnode_data *fvdat = VTOFUD(vp);
746         struct fuse_filehandle *fufh, *fufh_tmp;
747
748         int need_flush = 1;
749
750         LIST_FOREACH_SAFE(fufh, &fvdat->handles, next, fufh_tmp) {
751                 if (need_flush && vp->v_type == VREG) {
752                         if ((VTOFUD(vp)->flag & FN_SIZECHANGE) != 0) {
753                                 fuse_vnode_savesize(vp, NULL, 0);
754                         }
755                         if (fuse_data_cache_invalidate ||
756                             (fvdat->flag & FN_REVOKED) != 0)
757                                 fuse_io_invalbuf(vp, td);
758                         else
759                                 fuse_io_flushbuf(vp, MNT_WAIT, td);
760                         need_flush = 0;
761                 }
762                 fuse_filehandle_close(vp, fufh, td, NULL);
763         }
764
765         if ((fvdat->flag & FN_REVOKED) != 0 && fuse_reclaim_revoked) {
766                 vrecycle(vp);
767         }
768         return 0;
769 }
770
771 /*
772     struct vnop_link_args {
773         struct vnode *a_tdvp;
774         struct vnode *a_vp;
775         struct componentname *a_cnp;
776     };
777 */
778 static int
779 fuse_vnop_link(struct vop_link_args *ap)
780 {
781         struct vnode *vp = ap->a_vp;
782         struct vnode *tdvp = ap->a_tdvp;
783         struct componentname *cnp = ap->a_cnp;
784
785         struct vattr *vap = VTOVA(vp);
786
787         struct fuse_dispatcher fdi;
788         struct fuse_entry_out *feo;
789         struct fuse_link_in fli;
790
791         int err;
792
793         if (fuse_isdeadfs(vp)) {
794                 return ENXIO;
795         }
796         if (vnode_mount(tdvp) != vnode_mount(vp)) {
797                 return EXDEV;
798         }
799
800         /*
801          * This is a seatbelt check to protect naive userspace filesystems from
802          * themselves and the limitations of the FUSE IPC protocol.  If a
803          * filesystem does not allow attribute caching, assume it is capable of
804          * validating that nlink does not overflow.
805          */
806         if (vap != NULL && vap->va_nlink >= FUSE_LINK_MAX)
807                 return EMLINK;
808         fli.oldnodeid = VTOI(vp);
809
810         fdisp_init(&fdi, 0);
811         fuse_internal_newentry_makerequest(vnode_mount(tdvp), VTOI(tdvp), cnp,
812             FUSE_LINK, &fli, sizeof(fli), &fdi);
813         if ((err = fdisp_wait_answ(&fdi))) {
814                 goto out;
815         }
816         feo = fdi.answ;
817
818         err = fuse_internal_checkentry(feo, vnode_vtype(vp));
819 out:
820         fdisp_destroy(&fdi);
821         return err;
822 }
823
824 struct fuse_lookup_alloc_arg {
825         struct fuse_entry_out *feo;
826         struct componentname *cnp;
827         uint64_t nid;
828         enum vtype vtyp;
829 };
830
831 /* Callback for vn_get_ino */
832 static int
833 fuse_lookup_alloc(struct mount *mp, void *arg, int lkflags, struct vnode **vpp)
834 {
835         struct fuse_lookup_alloc_arg *flaa = arg;
836
837         return fuse_vnode_get(mp, flaa->feo, flaa->nid, NULL, vpp, flaa->cnp,
838                 flaa->vtyp);
839 }
840
841 SDT_PROBE_DEFINE3(fusefs, , vnops, cache_lookup,
842         "int", "struct timespec*", "struct timespec*");
843 /*
844     struct vnop_lookup_args {
845         struct vnodeop_desc *a_desc;
846         struct vnode *a_dvp;
847         struct vnode **a_vpp;
848         struct componentname *a_cnp;
849     };
850 */
851 int
852 fuse_vnop_lookup(struct vop_lookup_args *ap)
853 {
854         struct vnode *dvp = ap->a_dvp;
855         struct vnode **vpp = ap->a_vpp;
856         struct componentname *cnp = ap->a_cnp;
857         struct thread *td = cnp->cn_thread;
858         struct ucred *cred = cnp->cn_cred;
859
860         int nameiop = cnp->cn_nameiop;
861         int flags = cnp->cn_flags;
862         int wantparent = flags & (LOCKPARENT | WANTPARENT);
863         int islastcn = flags & ISLASTCN;
864         struct mount *mp = vnode_mount(dvp);
865
866         int err = 0;
867         int lookup_err = 0;
868         struct vnode *vp = NULL;
869
870         struct fuse_dispatcher fdi;
871         bool did_lookup = false;
872         struct fuse_entry_out *feo = NULL;
873         enum vtype vtyp;        /* vnode type of target */
874         off_t filesize;         /* filesize of target */
875
876         uint64_t nid;
877
878         if (fuse_isdeadfs(dvp)) {
879                 *vpp = NULL;
880                 return ENXIO;
881         }
882         if (!vnode_isdir(dvp))
883                 return ENOTDIR;
884
885         if (islastcn && vfs_isrdonly(mp) && (nameiop != LOOKUP))
886                 return EROFS;
887
888         if ((err = fuse_internal_access(dvp, VEXEC, td, cred)))
889                 return err;
890
891         if (flags & ISDOTDOT) {
892                 nid = VTOFUD(dvp)->parent_nid;
893                 if (nid == 0)
894                         return ENOENT;
895                 /* .. is obviously a directory */
896                 vtyp = VDIR;
897                 filesize = 0;
898         } else if (cnp->cn_namelen == 1 && *(cnp->cn_nameptr) == '.') {
899                 nid = VTOI(dvp);
900                 /* . is obviously a directory */
901                 vtyp = VDIR;
902                 filesize = 0;
903         } else {
904                 struct timespec now, timeout;
905
906                 err = cache_lookup(dvp, vpp, cnp, &timeout, NULL);
907                 getnanouptime(&now);
908                 SDT_PROBE3(fusefs, , vnops, cache_lookup, err, &timeout, &now);
909                 switch (err) {
910                 case -1:                /* positive match */
911                         if (timespeccmp(&timeout, &now, >)) {
912                                 atomic_add_acq_long(&fuse_lookup_cache_hits, 1);
913                         } else {
914                                 /* Cache timeout */
915                                 atomic_add_acq_long(&fuse_lookup_cache_misses,
916                                         1);
917                                 cache_purge(*vpp);
918                                 if (dvp != *vpp)
919                                         vput(*vpp);
920                                 else 
921                                         vrele(*vpp);
922                                 *vpp = NULL;
923                                 break;
924                         }
925                         return 0;
926
927                 case 0:         /* no match in cache */
928                         atomic_add_acq_long(&fuse_lookup_cache_misses, 1);
929                         break;
930
931                 case ENOENT:            /* negative match */
932                         getnanouptime(&now);
933                         if (timespeccmp(&timeout, &now, <=)) {
934                                 /* Cache timeout */
935                                 cache_purge_negative(dvp);
936                                 break;
937                         }
938                         /* fall through */
939                 default:
940                         return err;
941                 }
942
943                 nid = VTOI(dvp);
944                 fdisp_init(&fdi, cnp->cn_namelen + 1);
945                 fdisp_make(&fdi, FUSE_LOOKUP, mp, nid, td, cred);
946
947                 memcpy(fdi.indata, cnp->cn_nameptr, cnp->cn_namelen);
948                 ((char *)fdi.indata)[cnp->cn_namelen] = '\0';
949                 lookup_err = fdisp_wait_answ(&fdi);
950                 did_lookup = true;
951
952                 if (!lookup_err) {
953                         /* lookup call succeeded */
954                         nid = ((struct fuse_entry_out *)fdi.answ)->nodeid;
955                         feo = (struct fuse_entry_out *)fdi.answ;
956                         if (nid == 0) {
957                                 /* zero nodeid means ENOENT and cache it */
958                                 struct timespec timeout;
959
960                                 fdi.answ_stat = ENOENT;
961                                 lookup_err = ENOENT;
962                                 if (cnp->cn_flags & MAKEENTRY) {
963                                         fuse_validity_2_timespec(feo, &timeout);
964                                         cache_enter_time(dvp, *vpp, cnp,
965                                                 &timeout, NULL);
966                                 }
967                         } else if (nid == FUSE_ROOT_ID) {
968                                 lookup_err = EINVAL;
969                         }
970                         vtyp = IFTOVT(feo->attr.mode);
971                         filesize = feo->attr.size;
972                 }
973                 if (lookup_err && (!fdi.answ_stat || lookup_err != ENOENT)) {
974                         fdisp_destroy(&fdi);
975                         return lookup_err;
976                 }
977         }
978         /* lookup_err, if non-zero, must be ENOENT at this point */
979
980         if (lookup_err) {
981                 /* Entry not found */
982                 if ((nameiop == CREATE || nameiop == RENAME) && islastcn) {
983                         err = fuse_internal_access(dvp, VWRITE, td, cred);
984                         if (!err) {
985                                 /*
986                                  * Set the SAVENAME flag to hold onto the
987                                  * pathname for use later in VOP_CREATE or
988                                  * VOP_RENAME.
989                                  */
990                                 cnp->cn_flags |= SAVENAME;
991
992                                 err = EJUSTRETURN;
993                         }
994                 } else {
995                         err = ENOENT;
996                 }
997         } else {
998                 /* Entry was found */
999                 if (flags & ISDOTDOT) {
1000                         struct fuse_lookup_alloc_arg flaa;
1001
1002                         flaa.nid = nid;
1003                         flaa.feo = feo;
1004                         flaa.cnp = cnp;
1005                         flaa.vtyp = vtyp;
1006                         err = vn_vget_ino_gen(dvp, fuse_lookup_alloc, &flaa, 0,
1007                                 &vp);
1008                         *vpp = vp;
1009                 } else if (nid == VTOI(dvp)) {
1010                         vref(dvp);
1011                         *vpp = dvp;
1012                 } else {
1013                         struct fuse_vnode_data *fvdat;
1014
1015                         err = fuse_vnode_get(vnode_mount(dvp), feo, nid, dvp,
1016                             &vp, cnp, vtyp);
1017                         if (err)
1018                                 goto out;
1019                         *vpp = vp;
1020
1021                         fuse_vnode_setparent(vp, dvp);
1022
1023                         /*
1024                          * In the case where we are looking up a FUSE node
1025                          * represented by an existing cached vnode, and the
1026                          * true size reported by FUSE_LOOKUP doesn't match
1027                          * the vnode's cached size, fix the vnode cache to
1028                          * match the real object size.
1029                          *
1030                          * We can get here:
1031                          * * following attribute cache expiration, or
1032                          * * due a bug in the daemon, or
1033                          * * the first time that we looked up the file.
1034                          */
1035                         fvdat = VTOFUD(vp);
1036                         if (vnode_isreg(vp) &&
1037                             filesize != fvdat->filesize) {
1038                                 /*
1039                                  * The FN_SIZECHANGE flag reflects a dirty
1040                                  * append.  If userspace lets us know our cache
1041                                  * is invalid, that write was lost.  (Dirty
1042                                  * writes that do not cause append are also
1043                                  * lost, but we don't detect them here.)
1044                                  *
1045                                  * XXX: Maybe disable WB caching on this mount.
1046                                  */
1047                                 if (fvdat->flag & FN_SIZECHANGE)
1048                                         printf("%s: WB cache incoherent on "
1049                                             "%s!\n", __func__,
1050                                             vnode_mount(vp)->mnt_stat.f_mntonname);
1051
1052                                 (void)fuse_vnode_setsize(vp, cred, filesize);
1053                                 fvdat->flag &= ~FN_SIZECHANGE;
1054                         }
1055
1056                         MPASS(feo != NULL);
1057                         fuse_internal_cache_attrs(*vpp, &feo->attr,
1058                                 feo->attr_valid, feo->attr_valid_nsec, NULL);
1059
1060                         if ((nameiop == DELETE || nameiop == RENAME) &&
1061                                 islastcn)
1062                         {
1063                                 struct vattr dvattr;
1064
1065                                 err = fuse_internal_access(dvp, VWRITE, td,
1066                                         cred);
1067                                 if (err != 0)
1068                                         goto out;
1069                                 /* 
1070                                  * if the parent's sticky bit is set, check
1071                                  * whether we're allowed to remove the file.
1072                                  * Need to figure out the vnode locking to make
1073                                  * this work.
1074                                  */
1075                                 fuse_internal_getattr(dvp, &dvattr, cred, td);
1076                                 if ((dvattr.va_mode & S_ISTXT) &&
1077                                         fuse_internal_access(dvp, VADMIN, td,
1078                                                 cred) &&
1079                                         fuse_internal_access(*vpp, VADMIN, td,
1080                                                 cred)) {
1081                                         err = EPERM;
1082                                         goto out;
1083                                 }
1084                         }
1085
1086                         if (islastcn && (
1087                                 (nameiop == DELETE) ||
1088                                 (nameiop == RENAME && wantparent))) {
1089                                 cnp->cn_flags |= SAVENAME;
1090                         }
1091
1092                 }
1093         }
1094 out:
1095         if (err) {
1096                 if (vp != NULL && dvp != vp)
1097                         vput(vp);
1098                 else if (vp != NULL)
1099                         vrele(vp);
1100                 *vpp = NULL;
1101         }
1102         if (did_lookup)
1103                 fdisp_destroy(&fdi);
1104
1105         return err;
1106 }
1107
1108 /*
1109     struct vnop_mkdir_args {
1110         struct vnode *a_dvp;
1111         struct vnode **a_vpp;
1112         struct componentname *a_cnp;
1113         struct vattr *a_vap;
1114     };
1115 */
1116 static int
1117 fuse_vnop_mkdir(struct vop_mkdir_args *ap)
1118 {
1119         struct vnode *dvp = ap->a_dvp;
1120         struct vnode **vpp = ap->a_vpp;
1121         struct componentname *cnp = ap->a_cnp;
1122         struct vattr *vap = ap->a_vap;
1123
1124         struct fuse_mkdir_in fmdi;
1125
1126         if (fuse_isdeadfs(dvp)) {
1127                 return ENXIO;
1128         }
1129         fmdi.mode = MAKEIMODE(vap->va_type, vap->va_mode);
1130
1131         return (fuse_internal_newentry(dvp, vpp, cnp, FUSE_MKDIR, &fmdi,
1132             sizeof(fmdi), VDIR));
1133 }
1134
1135 /*
1136     struct vnop_mknod_args {
1137         struct vnode *a_dvp;
1138         struct vnode **a_vpp;
1139         struct componentname *a_cnp;
1140         struct vattr *a_vap;
1141     };
1142 */
1143 static int
1144 fuse_vnop_mknod(struct vop_mknod_args *ap)
1145 {
1146
1147         struct vnode *dvp = ap->a_dvp;
1148         struct vnode **vpp = ap->a_vpp;
1149         struct componentname *cnp = ap->a_cnp;
1150         struct vattr *vap = ap->a_vap;
1151         struct fuse_mknod_in fmni;
1152
1153         if (fuse_isdeadfs(dvp))
1154                 return ENXIO;
1155
1156         fmni.mode = MAKEIMODE(vap->va_type, vap->va_mode);
1157         fmni.rdev = vap->va_rdev;
1158         return (fuse_internal_newentry(dvp, vpp, cnp, FUSE_MKNOD, &fmni,
1159             sizeof(fmni), vap->va_type));
1160 }
1161
1162 /*
1163     struct vnop_open_args {
1164         struct vnode *a_vp;
1165         int  a_mode;
1166         struct ucred *a_cred;
1167         struct thread *a_td;
1168         int a_fdidx; / struct file *a_fp;
1169     };
1170 */
1171 static int
1172 fuse_vnop_open(struct vop_open_args *ap)
1173 {
1174         struct vnode *vp = ap->a_vp;
1175         int a_mode = ap->a_mode;
1176         struct thread *td = ap->a_td;
1177         struct ucred *cred = ap->a_cred;
1178         pid_t pid = td->td_proc->p_pid;
1179         struct fuse_vnode_data *fvdat;
1180
1181         if (fuse_isdeadfs(vp))
1182                 return ENXIO;
1183         if (vp->v_type == VCHR || vp->v_type == VBLK || vp->v_type == VFIFO)
1184                 return (EOPNOTSUPP);
1185         if ((a_mode & (FREAD | FWRITE | FEXEC)) == 0)
1186                 return EINVAL;
1187
1188         fvdat = VTOFUD(vp);
1189
1190         if (fuse_filehandle_validrw(vp, a_mode, cred, pid)) {
1191                 fuse_vnode_open(vp, 0, td);
1192                 return 0;
1193         }
1194
1195         return fuse_filehandle_open(vp, a_mode, NULL, td, cred);
1196 }
1197
1198 static int
1199 fuse_vnop_pathconf(struct vop_pathconf_args *ap)
1200 {
1201
1202         switch (ap->a_name) {
1203         case _PC_FILESIZEBITS:
1204                 *ap->a_retval = 64;
1205                 return (0);
1206         case _PC_NAME_MAX:
1207                 *ap->a_retval = NAME_MAX;
1208                 return (0);
1209         case _PC_LINK_MAX:
1210                 *ap->a_retval = MIN(LONG_MAX, FUSE_LINK_MAX);
1211                 return (0);
1212         case _PC_SYMLINK_MAX:
1213                 *ap->a_retval = MAXPATHLEN;
1214                 return (0);
1215         case _PC_NO_TRUNC:
1216                 *ap->a_retval = 1;
1217                 return (0);
1218         default:
1219                 return (vop_stdpathconf(ap));
1220         }
1221 }
1222
1223 /*
1224     struct vnop_read_args {
1225         struct vnode *a_vp;
1226         struct uio *a_uio;
1227         int  a_ioflag;
1228         struct ucred *a_cred;
1229     };
1230 */
1231 static int
1232 fuse_vnop_read(struct vop_read_args *ap)
1233 {
1234         struct vnode *vp = ap->a_vp;
1235         struct uio *uio = ap->a_uio;
1236         int ioflag = ap->a_ioflag;
1237         struct ucred *cred = ap->a_cred;
1238         pid_t pid = curthread->td_proc->p_pid;
1239
1240         if (fuse_isdeadfs(vp)) {
1241                 return ENXIO;
1242         }
1243
1244         if (VTOFUD(vp)->flag & FN_DIRECTIO) {
1245                 ioflag |= IO_DIRECT;
1246         }
1247
1248         return fuse_io_dispatch(vp, uio, ioflag, false, cred, pid);
1249 }
1250
1251 /*
1252     struct vnop_readdir_args {
1253         struct vnode *a_vp;
1254         struct uio *a_uio;
1255         struct ucred *a_cred;
1256         int *a_eofflag;
1257         int *ncookies;
1258         u_long **a_cookies;
1259     };
1260 */
1261 static int
1262 fuse_vnop_readdir(struct vop_readdir_args *ap)
1263 {
1264         struct vnode *vp = ap->a_vp;
1265         struct uio *uio = ap->a_uio;
1266         struct ucred *cred = ap->a_cred;
1267         struct fuse_filehandle *fufh = NULL;
1268         struct fuse_iov cookediov;
1269         int err = 0;
1270         pid_t pid = curthread->td_proc->p_pid;
1271
1272         if (fuse_isdeadfs(vp)) {
1273                 return ENXIO;
1274         }
1275         if (                            /* XXXIP ((uio_iovcnt(uio) > 1)) || */
1276             (uio_resid(uio) < sizeof(struct dirent))) {
1277                 return EINVAL;
1278         }
1279
1280         err = fuse_filehandle_get_dir(vp, &fufh, cred, pid);
1281         if (err)
1282                 return (err);
1283 #define DIRCOOKEDSIZE FUSE_DIRENT_ALIGN(FUSE_NAME_OFFSET + MAXNAMLEN + 1)
1284         fiov_init(&cookediov, DIRCOOKEDSIZE);
1285
1286         err = fuse_internal_readdir(vp, uio, fufh, &cookediov);
1287
1288         fiov_teardown(&cookediov);
1289
1290         return err;
1291 }
1292
1293 /*
1294     struct vnop_readlink_args {
1295         struct vnode *a_vp;
1296         struct uio *a_uio;
1297         struct ucred *a_cred;
1298     };
1299 */
1300 static int
1301 fuse_vnop_readlink(struct vop_readlink_args *ap)
1302 {
1303         struct vnode *vp = ap->a_vp;
1304         struct uio *uio = ap->a_uio;
1305         struct ucred *cred = ap->a_cred;
1306
1307         struct fuse_dispatcher fdi;
1308         int err;
1309
1310         if (fuse_isdeadfs(vp)) {
1311                 return ENXIO;
1312         }
1313         if (!vnode_islnk(vp)) {
1314                 return EINVAL;
1315         }
1316         fdisp_init(&fdi, 0);
1317         err = fdisp_simple_putget_vp(&fdi, FUSE_READLINK, vp, curthread, cred);
1318         if (err) {
1319                 goto out;
1320         }
1321         if (((char *)fdi.answ)[0] == '/' &&
1322             fuse_get_mpdata(vnode_mount(vp))->dataflags & FSESS_PUSH_SYMLINKS_IN) {
1323                 char *mpth = vnode_mount(vp)->mnt_stat.f_mntonname;
1324
1325                 err = uiomove(mpth, strlen(mpth), uio);
1326         }
1327         if (!err) {
1328                 err = uiomove(fdi.answ, fdi.iosize, uio);
1329         }
1330 out:
1331         fdisp_destroy(&fdi);
1332         return err;
1333 }
1334
1335 /*
1336     struct vnop_reclaim_args {
1337         struct vnode *a_vp;
1338         struct thread *a_td;
1339     };
1340 */
1341 static int
1342 fuse_vnop_reclaim(struct vop_reclaim_args *ap)
1343 {
1344         struct vnode *vp = ap->a_vp;
1345         struct thread *td = ap->a_td;
1346         struct fuse_vnode_data *fvdat = VTOFUD(vp);
1347         struct fuse_filehandle *fufh, *fufh_tmp;
1348
1349         if (!fvdat) {
1350                 panic("FUSE: no vnode data during recycling");
1351         }
1352         LIST_FOREACH_SAFE(fufh, &fvdat->handles, next, fufh_tmp) {
1353                 printf("FUSE: vnode being reclaimed with open fufh "
1354                         "(type=%#x)", fufh->fufh_type);
1355                 fuse_filehandle_close(vp, fufh, td, NULL);
1356         }
1357
1358         if ((!fuse_isdeadfs(vp)) && (fvdat->nlookup)) {
1359                 fuse_internal_forget_send(vnode_mount(vp), td, NULL, VTOI(vp),
1360                     fvdat->nlookup);
1361         }
1362         fuse_vnode_setparent(vp, NULL);
1363         cache_purge(vp);
1364         vfs_hash_remove(vp);
1365         vnode_destroy_vobject(vp);
1366         fuse_vnode_destroy(vp);
1367
1368         return 0;
1369 }
1370
1371 /*
1372     struct vnop_remove_args {
1373         struct vnode *a_dvp;
1374         struct vnode *a_vp;
1375         struct componentname *a_cnp;
1376     };
1377 */
1378 static int
1379 fuse_vnop_remove(struct vop_remove_args *ap)
1380 {
1381         struct vnode *dvp = ap->a_dvp;
1382         struct vnode *vp = ap->a_vp;
1383         struct componentname *cnp = ap->a_cnp;
1384
1385         int err;
1386
1387         if (fuse_isdeadfs(vp)) {
1388                 return ENXIO;
1389         }
1390         if (vnode_isdir(vp)) {
1391                 return EPERM;
1392         }
1393         cache_purge(vp);
1394
1395         err = fuse_internal_remove(dvp, vp, cnp, FUSE_UNLINK);
1396
1397         if (err == 0)
1398                 fuse_internal_vnode_disappear(vp);
1399         return err;
1400 }
1401
1402 /*
1403     struct vnop_rename_args {
1404         struct vnode *a_fdvp;
1405         struct vnode *a_fvp;
1406         struct componentname *a_fcnp;
1407         struct vnode *a_tdvp;
1408         struct vnode *a_tvp;
1409         struct componentname *a_tcnp;
1410     };
1411 */
1412 static int
1413 fuse_vnop_rename(struct vop_rename_args *ap)
1414 {
1415         struct vnode *fdvp = ap->a_fdvp;
1416         struct vnode *fvp = ap->a_fvp;
1417         struct componentname *fcnp = ap->a_fcnp;
1418         struct vnode *tdvp = ap->a_tdvp;
1419         struct vnode *tvp = ap->a_tvp;
1420         struct componentname *tcnp = ap->a_tcnp;
1421         struct fuse_data *data;
1422
1423         int err = 0;
1424
1425         if (fuse_isdeadfs(fdvp)) {
1426                 return ENXIO;
1427         }
1428         if (fvp->v_mount != tdvp->v_mount ||
1429             (tvp && fvp->v_mount != tvp->v_mount)) {
1430                 SDT_PROBE2(fusefs, , vnops, trace, 1, "cross-device rename");
1431                 err = EXDEV;
1432                 goto out;
1433         }
1434         cache_purge(fvp);
1435
1436         /*
1437          * FUSE library is expected to check if target directory is not
1438          * under the source directory in the file system tree.
1439          * Linux performs this check at VFS level.
1440          */
1441         data = fuse_get_mpdata(vnode_mount(tdvp));
1442         sx_xlock(&data->rename_lock);
1443         err = fuse_internal_rename(fdvp, fcnp, tdvp, tcnp);
1444         if (err == 0) {
1445                 if (tdvp != fdvp)
1446                         fuse_vnode_setparent(fvp, tdvp);
1447                 if (tvp != NULL)
1448                         fuse_vnode_setparent(tvp, NULL);
1449         }
1450         sx_unlock(&data->rename_lock);
1451
1452         if (tvp != NULL && tvp != fvp) {
1453                 cache_purge(tvp);
1454         }
1455         if (vnode_isdir(fvp)) {
1456                 if ((tvp != NULL) && vnode_isdir(tvp)) {
1457                         cache_purge(tdvp);
1458                 }
1459                 cache_purge(fdvp);
1460         }
1461 out:
1462         if (tdvp == tvp) {
1463                 vrele(tdvp);
1464         } else {
1465                 vput(tdvp);
1466         }
1467         if (tvp != NULL) {
1468                 vput(tvp);
1469         }
1470         vrele(fdvp);
1471         vrele(fvp);
1472
1473         return err;
1474 }
1475
1476 /*
1477     struct vnop_rmdir_args {
1478             struct vnode *a_dvp;
1479             struct vnode *a_vp;
1480             struct componentname *a_cnp;
1481     } *ap;
1482 */
1483 static int
1484 fuse_vnop_rmdir(struct vop_rmdir_args *ap)
1485 {
1486         struct vnode *dvp = ap->a_dvp;
1487         struct vnode *vp = ap->a_vp;
1488
1489         int err;
1490
1491         if (fuse_isdeadfs(vp)) {
1492                 return ENXIO;
1493         }
1494         if (VTOFUD(vp) == VTOFUD(dvp)) {
1495                 return EINVAL;
1496         }
1497         err = fuse_internal_remove(dvp, vp, ap->a_cnp, FUSE_RMDIR);
1498
1499         if (err == 0)
1500                 fuse_internal_vnode_disappear(vp);
1501         return err;
1502 }
1503
1504 /*
1505     struct vnop_setattr_args {
1506         struct vnode *a_vp;
1507         struct vattr *a_vap;
1508         struct ucred *a_cred;
1509         struct thread *a_td;
1510     };
1511 */
1512 static int
1513 fuse_vnop_setattr(struct vop_setattr_args *ap)
1514 {
1515         struct vnode *vp = ap->a_vp;
1516         struct vattr *vap = ap->a_vap;
1517         struct ucred *cred = ap->a_cred;
1518         struct thread *td = curthread;
1519         struct fuse_dispatcher fdi;
1520         struct fuse_setattr_in *fsai;
1521         struct mount *mp;
1522         pid_t pid = td->td_proc->p_pid;
1523         struct fuse_data *data;
1524         int dataflags;
1525         int err = 0;
1526         enum vtype vtyp;
1527         int sizechanged = 0;
1528         uint64_t newsize = 0;
1529         accmode_t accmode = 0;
1530
1531         mp = vnode_mount(vp);
1532         data = fuse_get_mpdata(mp);
1533         dataflags = data->dataflags;
1534
1535         if (fuse_isdeadfs(vp)) {
1536                 return ENXIO;
1537         }
1538         fdisp_init(&fdi, sizeof(*fsai));
1539         fdisp_make_vp(&fdi, FUSE_SETATTR, vp, td, cred);
1540         fsai = fdi.indata;
1541         fsai->valid = 0;
1542
1543         if (vap->va_uid != (uid_t)VNOVAL) {
1544                 if (dataflags & FSESS_DEFAULT_PERMISSIONS) {
1545                         /* Only root may change a file's owner */
1546                         err = priv_check_cred(cred, PRIV_VFS_CHOWN);
1547                         if (err)
1548                                 goto out;
1549                 }
1550                 fsai->uid = vap->va_uid;
1551                 fsai->valid |= FATTR_UID;
1552                 accmode |= VADMIN;
1553         }
1554         if (vap->va_gid != (gid_t)VNOVAL) {
1555                 if (dataflags & FSESS_DEFAULT_PERMISSIONS &&
1556                     !groupmember(vap->va_gid, cred))
1557                 {
1558                         /*
1559                          * Non-root users may only chgrp to one of their own
1560                          * groups 
1561                          */
1562                         err = priv_check_cred(cred, PRIV_VFS_CHOWN);
1563                         if (err)
1564                                 goto out;
1565                 }
1566                 fsai->gid = vap->va_gid;
1567                 fsai->valid |= FATTR_GID;
1568                 accmode |= VADMIN;
1569         }
1570         if (vap->va_size != VNOVAL) {
1571
1572                 struct fuse_filehandle *fufh = NULL;
1573
1574                 /*Truncate to a new value. */
1575                 fsai->size = vap->va_size;
1576                 sizechanged = 1;
1577                 newsize = vap->va_size;
1578                 fsai->valid |= FATTR_SIZE;
1579                 accmode |= VWRITE;
1580
1581                 fuse_filehandle_getrw(vp, FWRITE, &fufh, cred, pid);
1582                 if (fufh) {
1583                         fsai->fh = fufh->fh_id;
1584                         fsai->valid |= FATTR_FH;
1585                 }
1586         }
1587         if (vap->va_atime.tv_sec != VNOVAL) {
1588                 fsai->atime = vap->va_atime.tv_sec;
1589                 fsai->atimensec = vap->va_atime.tv_nsec;
1590                 fsai->valid |= FATTR_ATIME;
1591                 accmode |= VADMIN;
1592                 /*
1593                  * TODO: only require VWRITE if UTIMENS_NULL is set. PR 237181
1594                  */
1595         }
1596         if (vap->va_mtime.tv_sec != VNOVAL) {
1597                 fsai->mtime = vap->va_mtime.tv_sec;
1598                 fsai->mtimensec = vap->va_mtime.tv_nsec;
1599                 fsai->valid |= FATTR_MTIME;
1600                 accmode |= VADMIN;
1601                 /*
1602                  * TODO: only require VWRITE if UTIMENS_NULL is set. PR 237181
1603                  */
1604         }
1605         if (vap->va_mode != (mode_t)VNOVAL) {
1606                 fsai->mode = vap->va_mode & ALLPERMS;
1607                 fsai->valid |= FATTR_MODE;
1608                 accmode |= VADMIN;
1609         }
1610         if (!fsai->valid) {
1611                 goto out;
1612         }
1613         vtyp = vnode_vtype(vp);
1614
1615         if (fsai->valid & FATTR_SIZE && vtyp == VDIR) {
1616                 err = EISDIR;
1617                 goto out;
1618         }
1619         if (vfs_isrdonly(vnode_mount(vp)) && (fsai->valid & ~FATTR_SIZE || vtyp == VREG)) {
1620                 err = EROFS;
1621                 goto out;
1622         }
1623         err = fuse_internal_access(vp, accmode, td, cred);
1624         if (err)
1625                 goto out;
1626
1627         if ((err = fdisp_wait_answ(&fdi)))
1628                 goto out;
1629         vtyp = IFTOVT(((struct fuse_attr_out *)fdi.answ)->attr.mode);
1630
1631         if (vnode_vtype(vp) != vtyp) {
1632                 if (vnode_vtype(vp) == VNON && vtyp != VNON) {
1633                         SDT_PROBE2(fusefs, , vnops, trace, 1, "FUSE: Dang! "
1634                                 "vnode_vtype is VNON and vtype isn't.");
1635                 } else {
1636                         /*
1637                          * STALE vnode, ditch
1638                          *
1639                          * The vnode has changed its type "behind our back".
1640                          * There's nothing really we can do, so let us just
1641                          * force an internal revocation and tell the caller to
1642                          * try again, if interested.
1643                          */
1644                         fuse_internal_vnode_disappear(vp);
1645                         err = EAGAIN;
1646                 }
1647         }
1648         if (err == 0) {
1649                 struct fuse_attr_out *fao = (struct fuse_attr_out*)fdi.answ;
1650                 fuse_internal_cache_attrs(vp, &fao->attr, fao->attr_valid,
1651                         fao->attr_valid_nsec, NULL);
1652         }
1653
1654 out:
1655         fdisp_destroy(&fdi);
1656         if (!err && sizechanged) {
1657                 fuse_vnode_setsize(vp, cred, newsize);
1658                 VTOFUD(vp)->flag &= ~FN_SIZECHANGE;
1659         }
1660         return err;
1661 }
1662
1663 /*
1664     struct vnop_strategy_args {
1665         struct vnode *a_vp;
1666         struct buf *a_bp;
1667     };
1668 */
1669 static int
1670 fuse_vnop_strategy(struct vop_strategy_args *ap)
1671 {
1672         struct vnode *vp = ap->a_vp;
1673         struct buf *bp = ap->a_bp;
1674
1675         if (!vp || fuse_isdeadfs(vp)) {
1676                 bp->b_ioflags |= BIO_ERROR;
1677                 bp->b_error = ENXIO;
1678                 bufdone(bp);
1679                 return 0;
1680         }
1681         if (bp->b_iocmd == BIO_WRITE) {
1682                 int err;
1683
1684                 err = fuse_vnode_refreshsize(vp, NOCRED);
1685                 if (err) {
1686                         bp->b_ioflags |= BIO_ERROR;
1687                         bp->b_error = err;
1688                         bufdone(bp);
1689                         return 0;
1690                 }
1691         }
1692
1693         /*
1694          * VOP_STRATEGY always returns zero and signals error via bp->b_ioflags.
1695          * fuse_io_strategy sets bp's error fields
1696          */
1697         (void)fuse_io_strategy(vp, bp);
1698
1699         return 0;
1700 }
1701
1702
1703 /*
1704     struct vnop_symlink_args {
1705         struct vnode *a_dvp;
1706         struct vnode **a_vpp;
1707         struct componentname *a_cnp;
1708         struct vattr *a_vap;
1709         char *a_target;
1710     };
1711 */
1712 static int
1713 fuse_vnop_symlink(struct vop_symlink_args *ap)
1714 {
1715         struct vnode *dvp = ap->a_dvp;
1716         struct vnode **vpp = ap->a_vpp;
1717         struct componentname *cnp = ap->a_cnp;
1718         const char *target = ap->a_target;
1719
1720         struct fuse_dispatcher fdi;
1721
1722         int err;
1723         size_t len;
1724
1725         if (fuse_isdeadfs(dvp)) {
1726                 return ENXIO;
1727         }
1728         /*
1729          * Unlike the other creator type calls, here we have to create a message
1730          * where the name of the new entry comes first, and the data describing
1731          * the entry comes second.
1732          * Hence we can't rely on our handy fuse_internal_newentry() routine,
1733          * but put together the message manually and just call the core part.
1734          */
1735
1736         len = strlen(target) + 1;
1737         fdisp_init(&fdi, len + cnp->cn_namelen + 1);
1738         fdisp_make_vp(&fdi, FUSE_SYMLINK, dvp, curthread, NULL);
1739
1740         memcpy(fdi.indata, cnp->cn_nameptr, cnp->cn_namelen);
1741         ((char *)fdi.indata)[cnp->cn_namelen] = '\0';
1742         memcpy((char *)fdi.indata + cnp->cn_namelen + 1, target, len);
1743
1744         err = fuse_internal_newentry_core(dvp, vpp, cnp, VLNK, &fdi);
1745         fdisp_destroy(&fdi);
1746         return err;
1747 }
1748
1749 /*
1750     struct vnop_write_args {
1751         struct vnode *a_vp;
1752         struct uio *a_uio;
1753         int  a_ioflag;
1754         struct ucred *a_cred;
1755     };
1756 */
1757 static int
1758 fuse_vnop_write(struct vop_write_args *ap)
1759 {
1760         struct vnode *vp = ap->a_vp;
1761         struct uio *uio = ap->a_uio;
1762         int ioflag = ap->a_ioflag;
1763         struct ucred *cred = ap->a_cred;
1764         pid_t pid = curthread->td_proc->p_pid;
1765         int err;
1766
1767         if (fuse_isdeadfs(vp)) {
1768                 return ENXIO;
1769         }
1770         err = fuse_vnode_refreshsize(vp, cred);
1771         if (err)
1772                 return err;
1773
1774         if (VTOFUD(vp)->flag & FN_DIRECTIO) {
1775                 ioflag |= IO_DIRECT;
1776         }
1777
1778         return fuse_io_dispatch(vp, uio, ioflag, false, cred, pid);
1779 }
1780
1781 SDT_PROBE_DEFINE1(fusefs, , vnops, vnop_getpages_error, "int");
1782 /*
1783     struct vnop_getpages_args {
1784         struct vnode *a_vp;
1785         vm_page_t *a_m;
1786         int a_count;
1787         int a_reqpage;
1788     };
1789 */
1790 static int
1791 fuse_vnop_getpages(struct vop_getpages_args *ap)
1792 {
1793         int i, error, nextoff, size, toff, count, npages;
1794         struct uio uio;
1795         struct iovec iov;
1796         vm_offset_t kva;
1797         struct buf *bp;
1798         struct vnode *vp;
1799         struct thread *td;
1800         struct ucred *cred;
1801         vm_page_t *pages;
1802         pid_t pid = curthread->td_proc->p_pid;
1803
1804         vp = ap->a_vp;
1805         KASSERT(vp->v_object, ("objectless vp passed to getpages"));
1806         td = curthread;                 /* XXX */
1807         cred = curthread->td_ucred;     /* XXX */
1808         pages = ap->a_m;
1809         npages = ap->a_count;
1810
1811         if (!fsess_opt_mmap(vnode_mount(vp))) {
1812                 SDT_PROBE2(fusefs, , vnops, trace, 1,
1813                         "called on non-cacheable vnode??\n");
1814                 return (VM_PAGER_ERROR);
1815         }
1816
1817         /*
1818          * If the last page is partially valid, just return it and allow
1819          * the pager to zero-out the blanks.  Partially valid pages can
1820          * only occur at the file EOF.
1821          *
1822          * XXXGL: is that true for FUSE, which is a local filesystem,
1823          * but still somewhat disconnected from the kernel?
1824          */
1825         VM_OBJECT_WLOCK(vp->v_object);
1826         if (pages[npages - 1]->valid != 0 && --npages == 0)
1827                 goto out;
1828         VM_OBJECT_WUNLOCK(vp->v_object);
1829
1830         /*
1831          * We use only the kva address for the buffer, but this is extremely
1832          * convenient and fast.
1833          */
1834         bp = uma_zalloc(fuse_pbuf_zone, M_WAITOK);
1835
1836         kva = (vm_offset_t)bp->b_data;
1837         pmap_qenter(kva, pages, npages);
1838         VM_CNT_INC(v_vnodein);
1839         VM_CNT_ADD(v_vnodepgsin, npages);
1840
1841         count = npages << PAGE_SHIFT;
1842         iov.iov_base = (caddr_t)kva;
1843         iov.iov_len = count;
1844         uio.uio_iov = &iov;
1845         uio.uio_iovcnt = 1;
1846         uio.uio_offset = IDX_TO_OFF(pages[0]->pindex);
1847         uio.uio_resid = count;
1848         uio.uio_segflg = UIO_SYSSPACE;
1849         uio.uio_rw = UIO_READ;
1850         uio.uio_td = td;
1851
1852         error = fuse_io_dispatch(vp, &uio, IO_DIRECT, true, cred, pid);
1853         pmap_qremove(kva, npages);
1854
1855         uma_zfree(fuse_pbuf_zone, bp);
1856
1857         if (error && (uio.uio_resid == count)) {
1858                 SDT_PROBE1(fusefs, , vnops, vnop_getpages_error, error);
1859                 return VM_PAGER_ERROR;
1860         }
1861         /*
1862          * Calculate the number of bytes read and validate only that number
1863          * of bytes.  Note that due to pending writes, size may be 0.  This
1864          * does not mean that the remaining data is invalid!
1865          */
1866
1867         size = count - uio.uio_resid;
1868         VM_OBJECT_WLOCK(vp->v_object);
1869         fuse_vm_page_lock_queues();
1870         for (i = 0, toff = 0; i < npages; i++, toff = nextoff) {
1871                 vm_page_t m;
1872
1873                 nextoff = toff + PAGE_SIZE;
1874                 m = pages[i];
1875
1876                 if (nextoff <= size) {
1877                         /*
1878                          * Read operation filled an entire page
1879                          */
1880                         m->valid = VM_PAGE_BITS_ALL;
1881                         KASSERT(m->dirty == 0,
1882                             ("fuse_getpages: page %p is dirty", m));
1883                 } else if (size > toff) {
1884                         /*
1885                          * Read operation filled a partial page.
1886                          */
1887                         m->valid = 0;
1888                         vm_page_set_valid_range(m, 0, size - toff);
1889                         KASSERT(m->dirty == 0,
1890                             ("fuse_getpages: page %p is dirty", m));
1891                 } else {
1892                         /*
1893                          * Read operation was short.  If no error occurred
1894                          * we may have hit a zero-fill section.   We simply
1895                          * leave valid set to 0.
1896                          */
1897                         ;
1898                 }
1899         }
1900         fuse_vm_page_unlock_queues();
1901 out:
1902         VM_OBJECT_WUNLOCK(vp->v_object);
1903         if (ap->a_rbehind)
1904                 *ap->a_rbehind = 0;
1905         if (ap->a_rahead)
1906                 *ap->a_rahead = 0;
1907         return (VM_PAGER_OK);
1908 }
1909
1910 /*
1911     struct vnop_putpages_args {
1912         struct vnode *a_vp;
1913         vm_page_t *a_m;
1914         int a_count;
1915         int a_sync;
1916         int *a_rtvals;
1917         vm_ooffset_t a_offset;
1918     };
1919 */
1920 static int
1921 fuse_vnop_putpages(struct vop_putpages_args *ap)
1922 {
1923         struct uio uio;
1924         struct iovec iov;
1925         vm_offset_t kva;
1926         struct buf *bp;
1927         int i, error, npages, count;
1928         off_t offset;
1929         int *rtvals;
1930         struct vnode *vp;
1931         struct thread *td;
1932         struct ucred *cred;
1933         vm_page_t *pages;
1934         vm_ooffset_t fsize;
1935         pid_t pid = curthread->td_proc->p_pid;
1936
1937         vp = ap->a_vp;
1938         KASSERT(vp->v_object, ("objectless vp passed to putpages"));
1939         fsize = vp->v_object->un_pager.vnp.vnp_size;
1940         td = curthread;                 /* XXX */
1941         cred = curthread->td_ucred;     /* XXX */
1942         pages = ap->a_m;
1943         count = ap->a_count;
1944         rtvals = ap->a_rtvals;
1945         npages = btoc(count);
1946         offset = IDX_TO_OFF(pages[0]->pindex);
1947
1948         if (!fsess_opt_mmap(vnode_mount(vp))) {
1949                 SDT_PROBE2(fusefs, , vnops, trace, 1,
1950                         "called on non-cacheable vnode??\n");
1951         }
1952         for (i = 0; i < npages; i++)
1953                 rtvals[i] = VM_PAGER_AGAIN;
1954
1955         /*
1956          * When putting pages, do not extend file past EOF.
1957          */
1958
1959         if (offset + count > fsize) {
1960                 count = fsize - offset;
1961                 if (count < 0)
1962                         count = 0;
1963         }
1964         /*
1965          * We use only the kva address for the buffer, but this is extremely
1966          * convenient and fast.
1967          */
1968         bp = uma_zalloc(fuse_pbuf_zone, M_WAITOK);
1969
1970         kva = (vm_offset_t)bp->b_data;
1971         pmap_qenter(kva, pages, npages);
1972         VM_CNT_INC(v_vnodeout);
1973         VM_CNT_ADD(v_vnodepgsout, count);
1974
1975         iov.iov_base = (caddr_t)kva;
1976         iov.iov_len = count;
1977         uio.uio_iov = &iov;
1978         uio.uio_iovcnt = 1;
1979         uio.uio_offset = offset;
1980         uio.uio_resid = count;
1981         uio.uio_segflg = UIO_SYSSPACE;
1982         uio.uio_rw = UIO_WRITE;
1983         uio.uio_td = td;
1984
1985         error = fuse_io_dispatch(vp, &uio, IO_DIRECT, true, cred, pid);
1986
1987         pmap_qremove(kva, npages);
1988         uma_zfree(fuse_pbuf_zone, bp);
1989
1990         if (!error) {
1991                 int nwritten = round_page(count - uio.uio_resid) / PAGE_SIZE;
1992
1993                 for (i = 0; i < nwritten; i++) {
1994                         rtvals[i] = VM_PAGER_OK;
1995                         VM_OBJECT_WLOCK(pages[i]->object);
1996                         vm_page_undirty(pages[i]);
1997                         VM_OBJECT_WUNLOCK(pages[i]->object);
1998                 }
1999         }
2000         return rtvals[0];
2001 }
2002
2003 static const char extattr_namespace_separator = '.';
2004
2005 /*
2006     struct vop_getextattr_args {
2007         struct vop_generic_args a_gen;
2008         struct vnode *a_vp;
2009         int a_attrnamespace;
2010         const char *a_name;
2011         struct uio *a_uio;
2012         size_t *a_size;
2013         struct ucred *a_cred;
2014         struct thread *a_td;
2015     };
2016 */
2017 static int
2018 fuse_vnop_getextattr(struct vop_getextattr_args *ap)
2019 {
2020         struct vnode *vp = ap->a_vp;
2021         struct uio *uio = ap->a_uio;
2022         struct fuse_dispatcher fdi;
2023         struct fuse_getxattr_in *get_xattr_in;
2024         struct fuse_getxattr_out *get_xattr_out;
2025         struct mount *mp = vnode_mount(vp);
2026         struct thread *td = ap->a_td;
2027         struct ucred *cred = ap->a_cred;
2028         char *prefix;
2029         char *attr_str;
2030         size_t len;
2031         int err;
2032
2033         if (fuse_isdeadfs(vp))
2034                 return (ENXIO);
2035
2036         if (!fsess_isimpl(mp, FUSE_GETXATTR))
2037                 return EOPNOTSUPP;
2038
2039         err = fuse_extattr_check_cred(vp, ap->a_attrnamespace, cred, td, VREAD);
2040         if (err)
2041                 return err;
2042
2043         /* Default to looking for user attributes. */
2044         if (ap->a_attrnamespace == EXTATTR_NAMESPACE_SYSTEM)
2045                 prefix = EXTATTR_NAMESPACE_SYSTEM_STRING;
2046         else
2047                 prefix = EXTATTR_NAMESPACE_USER_STRING;
2048
2049         len = strlen(prefix) + sizeof(extattr_namespace_separator) +
2050             strlen(ap->a_name) + 1;
2051
2052         fdisp_init(&fdi, len + sizeof(*get_xattr_in));
2053         fdisp_make_vp(&fdi, FUSE_GETXATTR, vp, td, cred);
2054
2055         get_xattr_in = fdi.indata;
2056         /*
2057          * Check to see whether we're querying the available size or
2058          * issuing the actual request.  If we pass in 0, we get back struct
2059          * fuse_getxattr_out.  If we pass in a non-zero size, we get back
2060          * that much data, without the struct fuse_getxattr_out header.
2061          */
2062         if (uio == NULL)
2063                 get_xattr_in->size = 0;
2064         else
2065                 get_xattr_in->size = uio->uio_resid;
2066
2067         attr_str = (char *)fdi.indata + sizeof(*get_xattr_in);
2068         snprintf(attr_str, len, "%s%c%s", prefix, extattr_namespace_separator,
2069             ap->a_name);
2070
2071         err = fdisp_wait_answ(&fdi);
2072         if (err != 0) {
2073                 if (err == ENOSYS) {
2074                         fsess_set_notimpl(mp, FUSE_GETXATTR);
2075                         err = EOPNOTSUPP;
2076                 }
2077                 goto out;
2078         }
2079
2080         get_xattr_out = fdi.answ;
2081
2082         if (ap->a_size != NULL)
2083                 *ap->a_size = get_xattr_out->size;
2084
2085         if (uio != NULL)
2086                 err = uiomove(fdi.answ, fdi.iosize, uio);
2087
2088 out:
2089         fdisp_destroy(&fdi);
2090         return (err);
2091 }
2092
2093 /*
2094     struct vop_setextattr_args {
2095         struct vop_generic_args a_gen;
2096         struct vnode *a_vp;
2097         int a_attrnamespace;
2098         const char *a_name;
2099         struct uio *a_uio;
2100         struct ucred *a_cred;
2101         struct thread *a_td;
2102     };
2103 */
2104 static int
2105 fuse_vnop_setextattr(struct vop_setextattr_args *ap)
2106 {
2107         struct vnode *vp = ap->a_vp;
2108         struct uio *uio = ap->a_uio;
2109         struct fuse_dispatcher fdi;
2110         struct fuse_setxattr_in *set_xattr_in;
2111         struct mount *mp = vnode_mount(vp);
2112         struct thread *td = ap->a_td;
2113         struct ucred *cred = ap->a_cred;
2114         char *prefix;
2115         size_t len;
2116         char *attr_str;
2117         int err;
2118         
2119         if (fuse_isdeadfs(vp))
2120                 return (ENXIO);
2121
2122         if (!fsess_isimpl(mp, FUSE_SETXATTR))
2123                 return EOPNOTSUPP;
2124
2125         if (vfs_isrdonly(mp))
2126                 return EROFS;
2127
2128         /* Deleting xattrs must use VOP_DELETEEXTATTR instead */
2129         if (ap->a_uio == NULL) {
2130                 /*
2131                  * If we got here as fallback from VOP_DELETEEXTATTR, then
2132                  * return EOPNOTSUPP.
2133                  */
2134                 if (!fsess_isimpl(mp, FUSE_REMOVEXATTR))
2135                         return (EOPNOTSUPP);
2136                 else
2137                         return (EINVAL);
2138         }
2139
2140         err = fuse_extattr_check_cred(vp, ap->a_attrnamespace, cred, td,
2141                 VWRITE);
2142         if (err)
2143                 return err;
2144
2145         /* Default to looking for user attributes. */
2146         if (ap->a_attrnamespace == EXTATTR_NAMESPACE_SYSTEM)
2147                 prefix = EXTATTR_NAMESPACE_SYSTEM_STRING;
2148         else
2149                 prefix = EXTATTR_NAMESPACE_USER_STRING;
2150
2151         len = strlen(prefix) + sizeof(extattr_namespace_separator) +
2152             strlen(ap->a_name) + 1;
2153
2154         fdisp_init(&fdi, len + sizeof(*set_xattr_in) + uio->uio_resid);
2155         fdisp_make_vp(&fdi, FUSE_SETXATTR, vp, td, cred);
2156
2157         set_xattr_in = fdi.indata;
2158         set_xattr_in->size = uio->uio_resid;
2159
2160         attr_str = (char *)fdi.indata + sizeof(*set_xattr_in);
2161         snprintf(attr_str, len, "%s%c%s", prefix, extattr_namespace_separator,
2162             ap->a_name);
2163
2164         err = uiomove((char *)fdi.indata + sizeof(*set_xattr_in) + len,
2165             uio->uio_resid, uio);
2166         if (err != 0) {
2167                 goto out;
2168         }
2169
2170         err = fdisp_wait_answ(&fdi);
2171
2172         if (err == ENOSYS) {
2173                 fsess_set_notimpl(mp, FUSE_SETXATTR);
2174                 err = EOPNOTSUPP;
2175         }
2176         if (err == ERESTART) {
2177                 /* Can't restart after calling uiomove */
2178                 err = EINTR;
2179         }
2180
2181 out:
2182         fdisp_destroy(&fdi);
2183         return (err);
2184 }
2185
2186 /*
2187  * The Linux / FUSE extended attribute list is simply a collection of
2188  * NUL-terminated strings.  The FreeBSD extended attribute list is a single
2189  * byte length followed by a non-NUL terminated string.  So, this allows
2190  * conversion of the Linux / FUSE format to the FreeBSD format in place.
2191  * Linux attribute names are reported with the namespace as a prefix (e.g.
2192  * "user.attribute_name"), but in FreeBSD they are reported without the
2193  * namespace prefix (e.g. "attribute_name").  So, we're going from:
2194  *
2195  * user.attr_name1\0user.attr_name2\0
2196  *
2197  * to:
2198  *
2199  * <num>attr_name1<num>attr_name2
2200  *
2201  * Where "<num>" is a single byte number of characters in the attribute name.
2202  * 
2203  * Args:
2204  * prefix - exattr namespace prefix string
2205  * list, list_len - input list with namespace prefixes
2206  * bsd_list, bsd_list_len - output list compatible with bsd vfs
2207  */
2208 static int
2209 fuse_xattrlist_convert(char *prefix, const char *list, int list_len,
2210     char *bsd_list, int *bsd_list_len)
2211 {
2212         int len, pos, dist_to_next, prefix_len;
2213
2214         pos = 0;
2215         *bsd_list_len = 0;
2216         prefix_len = strlen(prefix);
2217
2218         while (pos < list_len && list[pos] != '\0') {
2219                 dist_to_next = strlen(&list[pos]) + 1;
2220                 if (bcmp(&list[pos], prefix, prefix_len) == 0 &&
2221                     list[pos + prefix_len] == extattr_namespace_separator) {
2222                         len = dist_to_next -
2223                             (prefix_len + sizeof(extattr_namespace_separator)) - 1;
2224                         if (len >= EXTATTR_MAXNAMELEN)
2225                                 return (ENAMETOOLONG);
2226
2227                         bsd_list[*bsd_list_len] = len;
2228                         memcpy(&bsd_list[*bsd_list_len + 1],
2229                             &list[pos + prefix_len +
2230                             sizeof(extattr_namespace_separator)], len);
2231
2232                         *bsd_list_len += len + 1;
2233                 }
2234
2235                 pos += dist_to_next;
2236         }
2237
2238         return (0);
2239 }
2240
2241 /*
2242     struct vop_listextattr_args {
2243         struct vop_generic_args a_gen;
2244         struct vnode *a_vp;
2245         int a_attrnamespace;
2246         struct uio *a_uio;
2247         size_t *a_size;
2248         struct ucred *a_cred;
2249         struct thread *a_td;
2250     };
2251 */
2252 static int
2253 fuse_vnop_listextattr(struct vop_listextattr_args *ap)
2254 {
2255         struct vnode *vp = ap->a_vp;
2256         struct uio *uio = ap->a_uio;
2257         struct fuse_dispatcher fdi;
2258         struct fuse_listxattr_in *list_xattr_in;
2259         struct fuse_listxattr_out *list_xattr_out;
2260         struct mount *mp = vnode_mount(vp);
2261         struct thread *td = ap->a_td;
2262         struct ucred *cred = ap->a_cred;
2263         size_t len;
2264         char *prefix;
2265         char *attr_str;
2266         char *bsd_list = NULL;
2267         char *linux_list;
2268         int bsd_list_len;
2269         int linux_list_len;
2270         int err;
2271
2272         if (fuse_isdeadfs(vp))
2273                 return (ENXIO);
2274
2275         if (!fsess_isimpl(mp, FUSE_LISTXATTR))
2276                 return EOPNOTSUPP;
2277
2278         err = fuse_extattr_check_cred(vp, ap->a_attrnamespace, cred, td, VREAD);
2279         if (err)
2280                 return err;
2281
2282         /*
2283          * Add space for a NUL and the period separator if enabled.
2284          * Default to looking for user attributes.
2285          */
2286         if (ap->a_attrnamespace == EXTATTR_NAMESPACE_SYSTEM)
2287                 prefix = EXTATTR_NAMESPACE_SYSTEM_STRING;
2288         else
2289                 prefix = EXTATTR_NAMESPACE_USER_STRING;
2290
2291         len = strlen(prefix) + sizeof(extattr_namespace_separator) + 1;
2292
2293         fdisp_init(&fdi, sizeof(*list_xattr_in) + len);
2294         fdisp_make_vp(&fdi, FUSE_LISTXATTR, vp, td, cred);
2295
2296         /*
2297          * Retrieve Linux / FUSE compatible list size.
2298          */
2299         list_xattr_in = fdi.indata;
2300         list_xattr_in->size = 0;
2301         attr_str = (char *)fdi.indata + sizeof(*list_xattr_in);
2302         snprintf(attr_str, len, "%s%c", prefix, extattr_namespace_separator);
2303
2304         err = fdisp_wait_answ(&fdi);
2305         if (err != 0) {
2306                 if (err == ENOSYS) {
2307                         fsess_set_notimpl(mp, FUSE_LISTXATTR);
2308                         err = EOPNOTSUPP;
2309                 }
2310                 goto out;
2311         }
2312
2313         list_xattr_out = fdi.answ;
2314         linux_list_len = list_xattr_out->size;
2315         if (linux_list_len == 0) {
2316                 if (ap->a_size != NULL)
2317                         *ap->a_size = linux_list_len;
2318                 goto out;
2319         }
2320
2321         /*
2322          * Retrieve Linux / FUSE compatible list values.
2323          */
2324         fdisp_refresh_vp(&fdi, FUSE_LISTXATTR, vp, td, cred);
2325         list_xattr_in = fdi.indata;
2326         list_xattr_in->size = linux_list_len + sizeof(*list_xattr_out);
2327         list_xattr_in->flags = 0;
2328         attr_str = (char *)fdi.indata + sizeof(*list_xattr_in);
2329         snprintf(attr_str, len, "%s%c", prefix, extattr_namespace_separator);
2330
2331         err = fdisp_wait_answ(&fdi);
2332         if (err != 0)
2333                 goto out;
2334
2335         linux_list = fdi.answ;
2336         linux_list_len = fdi.iosize;
2337
2338         /*
2339          * Retrieve the BSD compatible list values.
2340          * The Linux / FUSE attribute list format isn't the same
2341          * as FreeBSD's format. So we need to transform it into
2342          * FreeBSD's format before giving it to the user.
2343          */
2344         bsd_list = malloc(linux_list_len, M_TEMP, M_WAITOK);
2345         err = fuse_xattrlist_convert(prefix, linux_list, linux_list_len,
2346             bsd_list, &bsd_list_len);
2347         if (err != 0)
2348                 goto out;
2349
2350         if (ap->a_size != NULL)
2351                 *ap->a_size = bsd_list_len;
2352
2353         if (uio != NULL)
2354                 err = uiomove(bsd_list, bsd_list_len, uio);
2355
2356 out:
2357         free(bsd_list, M_TEMP);
2358         fdisp_destroy(&fdi);
2359         return (err);
2360 }
2361
2362 /*
2363     struct vop_deleteextattr_args {
2364         struct vop_generic_args a_gen;
2365         struct vnode *a_vp;
2366         int a_attrnamespace;
2367         const char *a_name;
2368         struct ucred *a_cred;
2369         struct thread *a_td;
2370     };
2371 */
2372 static int
2373 fuse_vnop_deleteextattr(struct vop_deleteextattr_args *ap)
2374 {
2375         struct vnode *vp = ap->a_vp;
2376         struct fuse_dispatcher fdi;
2377         struct mount *mp = vnode_mount(vp);
2378         struct thread *td = ap->a_td;
2379         struct ucred *cred = ap->a_cred;
2380         char *prefix;
2381         size_t len;
2382         char *attr_str;
2383         int err;
2384
2385         if (fuse_isdeadfs(vp))
2386                 return (ENXIO);
2387
2388         if (!fsess_isimpl(mp, FUSE_REMOVEXATTR))
2389                 return EOPNOTSUPP;
2390
2391         if (vfs_isrdonly(mp))
2392                 return EROFS;
2393
2394         err = fuse_extattr_check_cred(vp, ap->a_attrnamespace, cred, td,
2395                 VWRITE);
2396         if (err)
2397                 return err;
2398
2399         /* Default to looking for user attributes. */
2400         if (ap->a_attrnamespace == EXTATTR_NAMESPACE_SYSTEM)
2401                 prefix = EXTATTR_NAMESPACE_SYSTEM_STRING;
2402         else
2403                 prefix = EXTATTR_NAMESPACE_USER_STRING;
2404
2405         len = strlen(prefix) + sizeof(extattr_namespace_separator) +
2406             strlen(ap->a_name) + 1;
2407
2408         fdisp_init(&fdi, len);
2409         fdisp_make_vp(&fdi, FUSE_REMOVEXATTR, vp, td, cred);
2410
2411         attr_str = fdi.indata;
2412         snprintf(attr_str, len, "%s%c%s", prefix, extattr_namespace_separator,
2413             ap->a_name);
2414
2415         err = fdisp_wait_answ(&fdi);
2416         if (err == ENOSYS) {
2417                 fsess_set_notimpl(mp, FUSE_REMOVEXATTR);
2418                 err = EOPNOTSUPP;
2419         }
2420
2421         fdisp_destroy(&fdi);
2422         return (err);
2423 }
2424
2425 /*
2426     struct vnop_print_args {
2427         struct vnode *a_vp;
2428     };
2429 */
2430 static int
2431 fuse_vnop_print(struct vop_print_args *ap)
2432 {
2433         struct fuse_vnode_data *fvdat = VTOFUD(ap->a_vp);
2434
2435         printf("nodeid: %ju, parent nodeid: %ju, nlookup: %ju, flag: %#x\n",
2436             (uintmax_t)VTOILLU(ap->a_vp), (uintmax_t)fvdat->parent_nid,
2437             (uintmax_t)fvdat->nlookup,
2438             fvdat->flag);
2439
2440         return 0;
2441 }