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