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