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