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