]> CyberLeo.Net >> Repos - FreeBSD/FreeBSD.git/blob - sys/fs/fuse/fuse_vnops.c
fusefs: eliminate a superfluous fuse_node_setparent
[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_allocate = VOP_EINVAL,
172         .vop_default = &default_vnodeops,
173         .vop_access = fuse_vnop_access,
174         .vop_advlock = fuse_vnop_advlock,
175         .vop_close = fuse_vnop_close,
176         .vop_create = fuse_vnop_create,
177         .vop_deleteextattr = fuse_vnop_deleteextattr,
178         .vop_fsync = fuse_vnop_fsync,
179         .vop_fdatasync = fuse_vnop_fdatasync,
180         .vop_getattr = fuse_vnop_getattr,
181         .vop_getextattr = fuse_vnop_getextattr,
182         .vop_inactive = fuse_vnop_inactive,
183         .vop_link = fuse_vnop_link,
184         .vop_listextattr = fuse_vnop_listextattr,
185         .vop_lookup = fuse_vnop_lookup,
186         .vop_mkdir = fuse_vnop_mkdir,
187         .vop_mknod = fuse_vnop_mknod,
188         .vop_open = fuse_vnop_open,
189         .vop_pathconf = fuse_vnop_pathconf,
190         .vop_read = fuse_vnop_read,
191         .vop_readdir = fuse_vnop_readdir,
192         .vop_readlink = fuse_vnop_readlink,
193         .vop_reclaim = fuse_vnop_reclaim,
194         .vop_remove = fuse_vnop_remove,
195         .vop_rename = fuse_vnop_rename,
196         .vop_rmdir = fuse_vnop_rmdir,
197         .vop_setattr = fuse_vnop_setattr,
198         .vop_setextattr = fuse_vnop_setextattr,
199         .vop_strategy = fuse_vnop_strategy,
200         .vop_symlink = fuse_vnop_symlink,
201         .vop_write = fuse_vnop_write,
202         .vop_getpages = fuse_vnop_getpages,
203         .vop_putpages = fuse_vnop_putpages,
204         .vop_print = fuse_vnop_print,
205 };
206
207 static u_long fuse_lookup_cache_hits = 0;
208
209 SYSCTL_ULONG(_vfs_fusefs, OID_AUTO, lookup_cache_hits, CTLFLAG_RD,
210     &fuse_lookup_cache_hits, 0, "number of positive cache hits in lookup");
211
212 static u_long fuse_lookup_cache_misses = 0;
213
214 SYSCTL_ULONG(_vfs_fusefs, OID_AUTO, lookup_cache_misses, CTLFLAG_RD,
215     &fuse_lookup_cache_misses, 0, "number of cache misses in lookup");
216
217 /*
218  * XXX: This feature is highly experimental and can bring to instabilities,
219  * needs revisiting before to be enabled by default.
220  */
221 static int fuse_reclaim_revoked = 0;
222
223 SYSCTL_INT(_vfs_fusefs, OID_AUTO, reclaim_revoked, CTLFLAG_RW,
224     &fuse_reclaim_revoked, 0, "");
225
226 uma_zone_t fuse_pbuf_zone;
227
228 #define fuse_vm_page_lock(m)            vm_page_lock((m));
229 #define fuse_vm_page_unlock(m)          vm_page_unlock((m));
230 #define fuse_vm_page_lock_queues()      ((void)0)
231 #define fuse_vm_page_unlock_queues()    ((void)0)
232
233 /* Check permission for extattr operations, much like extattr_check_cred */
234 static int
235 fuse_extattr_check_cred(struct vnode *vp, int ns, struct ucred *cred,
236         struct thread *td, accmode_t accmode)
237 {
238         struct mount *mp = vnode_mount(vp);
239         struct fuse_data *data = fuse_get_mpdata(mp);
240
241         /*
242          * Kernel-invoked always succeeds.
243          */
244         if (cred == NOCRED)
245                 return (0);
246
247         /*
248          * Do not allow privileged processes in jail to directly manipulate
249          * system attributes.
250          */
251         switch (ns) {
252         case EXTATTR_NAMESPACE_SYSTEM:
253                 if (data->dataflags & FSESS_DEFAULT_PERMISSIONS) {
254                         return (priv_check_cred(cred, PRIV_VFS_EXTATTR_SYSTEM));
255                 }
256                 /* FALLTHROUGH */
257         case EXTATTR_NAMESPACE_USER:
258                 return (fuse_internal_access(vp, accmode, td, cred));
259         default:
260                 return (EPERM);
261         }
262 }
263
264 /* Get a filehandle for a directory */
265 static int
266 fuse_filehandle_get_dir(struct vnode *vp, struct fuse_filehandle **fufhp,
267         struct ucred *cred, pid_t pid)
268 {
269         if (fuse_filehandle_get(vp, FREAD, fufhp, cred, pid) == 0)
270                 return 0;
271         return fuse_filehandle_get(vp, FEXEC, fufhp, cred, pid);
272 }
273
274 /* Send FUSE_FLUSH for this vnode */
275 static int
276 fuse_flush(struct vnode *vp, struct ucred *cred, pid_t pid, int fflag)
277 {
278         struct fuse_flush_in *ffi;
279         struct fuse_filehandle *fufh;
280         struct fuse_dispatcher fdi;
281         struct thread *td = curthread;
282         struct mount *mp = vnode_mount(vp);
283         int err;
284
285         if (!fsess_isimpl(vnode_mount(vp), FUSE_FLUSH))
286                 return 0;
287
288         err = fuse_filehandle_getrw(vp, fflag, &fufh, cred, pid);
289         if (err)
290                 return err;
291
292         fdisp_init(&fdi, sizeof(*ffi));
293         fdisp_make_vp(&fdi, FUSE_FLUSH, vp, td, cred);
294         ffi = fdi.indata;
295         ffi->fh = fufh->fh_id;
296         /* 
297          * If the file has a POSIX lock then we're supposed to set lock_owner.
298          * If not, then lock_owner is undefined.  So we may as well always set
299          * it.
300          */
301         ffi->lock_owner = td->td_proc->p_pid;
302
303         err = fdisp_wait_answ(&fdi);
304         if (err == ENOSYS) {
305                 fsess_set_notimpl(mp, FUSE_FLUSH);
306                 err = 0;
307         }
308         fdisp_destroy(&fdi);
309         return err;
310 }
311
312 /* Close wrapper for fifos.  */
313 static int
314 fuse_fifo_close(struct vop_close_args *ap)
315 {
316         return (fifo_specops.vop_close(ap));
317 }
318
319 /*
320     struct vnop_access_args {
321         struct vnode *a_vp;
322 #if VOP_ACCESS_TAKES_ACCMODE_T
323         accmode_t a_accmode;
324 #else
325         int a_mode;
326 #endif
327         struct ucred *a_cred;
328         struct thread *a_td;
329     };
330 */
331 static int
332 fuse_vnop_access(struct vop_access_args *ap)
333 {
334         struct vnode *vp = ap->a_vp;
335         int accmode = ap->a_accmode;
336         struct ucred *cred = ap->a_cred;
337
338         struct fuse_data *data = fuse_get_mpdata(vnode_mount(vp));
339
340         int err;
341
342         if (fuse_isdeadfs(vp)) {
343                 if (vnode_isvroot(vp)) {
344                         return 0;
345                 }
346                 return ENXIO;
347         }
348         if (!(data->dataflags & FSESS_INITED)) {
349                 if (vnode_isvroot(vp)) {
350                         if (priv_check_cred(cred, PRIV_VFS_ADMIN) ||
351                             (fuse_match_cred(data->daemoncred, cred) == 0)) {
352                                 return 0;
353                         }
354                 }
355                 return EBADF;
356         }
357         if (vnode_islnk(vp)) {
358                 return 0;
359         }
360
361         err = fuse_internal_access(vp, accmode, ap->a_td, ap->a_cred);
362         return err;
363 }
364
365 /*
366  * struct vop_advlock_args {
367  *      struct vop_generic_args a_gen;
368  *      struct vnode *a_vp;
369  *      void *a_id;
370  *      int a_op;
371  *      struct flock *a_fl;
372  *      int a_flags;
373  * }
374  */
375 static int
376 fuse_vnop_advlock(struct vop_advlock_args *ap)
377 {
378         struct vnode *vp = ap->a_vp;
379         struct flock *fl = ap->a_fl;
380         struct thread *td = curthread;
381         struct ucred *cred = td->td_ucred;
382         pid_t pid = td->td_proc->p_pid;
383         struct fuse_filehandle *fufh;
384         struct fuse_dispatcher fdi;
385         struct fuse_lk_in *fli;
386         struct fuse_lk_out *flo;
387         enum fuse_opcode op;
388         int dataflags, err;
389
390         dataflags = fuse_get_mpdata(vnode_mount(vp))->dataflags;
391
392         if (fuse_isdeadfs(vp)) {
393                 return ENXIO;
394         }
395
396         if (!(dataflags & FSESS_POSIX_LOCKS))
397                 return vop_stdadvlock(ap);
398
399         err = fuse_filehandle_get_anyflags(vp, &fufh, cred, pid);
400         if (err)
401                 return err;
402
403         fdisp_init(&fdi, sizeof(*fli));
404
405         switch(ap->a_op) {
406         case F_GETLK:
407                 op = FUSE_GETLK;
408                 break;
409         case F_SETLK:
410                 op = FUSE_SETLK;
411                 break;
412         case F_SETLKW:
413                 op = FUSE_SETLKW;
414                 break;
415         default:
416                 return EINVAL;
417         }
418
419         fdisp_make_vp(&fdi, op, vp, td, cred);
420         fli = fdi.indata;
421         fli->fh = fufh->fh_id;
422         fli->owner = fl->l_pid;
423         fli->lk.start = fl->l_start;
424         if (fl->l_len != 0)
425                 fli->lk.end = fl->l_start + fl->l_len - 1;
426         else
427                 fli->lk.end = INT64_MAX;
428         fli->lk.type = fl->l_type;
429         fli->lk.pid = fl->l_pid;
430
431         err = fdisp_wait_answ(&fdi);
432         fdisp_destroy(&fdi);
433
434         if (err == 0 && op == FUSE_GETLK) {
435                 flo = fdi.answ;
436                 fl->l_type = flo->lk.type;
437                 fl->l_pid = flo->lk.pid;
438                 if (flo->lk.type != F_UNLCK) {
439                         fl->l_start = flo->lk.start;
440                         if (flo->lk.end == INT64_MAX)
441                                 fl->l_len = 0;
442                         else
443                                 fl->l_len = flo->lk.end - flo->lk.start + 1;
444                         fl->l_start = flo->lk.start;
445                 }
446         }
447
448         return err;
449 }
450
451 /*
452     struct vop_close_args {
453         struct vnode *a_vp;
454         int  a_fflag;
455         struct ucred *a_cred;
456         struct thread *a_td;
457     };
458 */
459 static int
460 fuse_vnop_close(struct vop_close_args *ap)
461 {
462         struct vnode *vp = ap->a_vp;
463         struct ucred *cred = ap->a_cred;
464         int fflag = ap->a_fflag;
465         struct thread *td = ap->a_td;
466         pid_t pid = td->td_proc->p_pid;
467         int err = 0;
468
469         if (fuse_isdeadfs(vp))
470                 return 0;
471         if (vnode_isdir(vp))
472                 return 0;
473         if (fflag & IO_NDELAY)
474                 return 0;
475
476         err = fuse_flush(vp, cred, pid, fflag);
477         /* TODO: close the file handle, if we're sure it's no longer used */
478         if ((VTOFUD(vp)->flag & FN_SIZECHANGE) != 0) {
479                 fuse_vnode_savesize(vp, cred, td->td_proc->p_pid);
480         }
481         return err;
482 }
483
484 static void
485 fdisp_make_mknod_for_fallback(
486         struct fuse_dispatcher *fdip,
487         struct componentname *cnp,
488         struct vnode *dvp,
489         uint64_t parentnid,
490         struct thread *td,
491         struct ucred *cred,
492         mode_t mode,
493         enum fuse_opcode *op)
494 {
495         struct fuse_mknod_in *fmni;
496
497         fdisp_init(fdip, sizeof(*fmni) + cnp->cn_namelen + 1);
498         *op = FUSE_MKNOD;
499         fdisp_make(fdip, *op, vnode_mount(dvp), parentnid, td, cred);
500         fmni = fdip->indata;
501         fmni->mode = mode;
502         fmni->rdev = 0;
503         memcpy((char *)fdip->indata + sizeof(*fmni), cnp->cn_nameptr,
504             cnp->cn_namelen);
505         ((char *)fdip->indata)[sizeof(*fmni) + cnp->cn_namelen] = '\0';
506 }
507 /*
508     struct vnop_create_args {
509         struct vnode *a_dvp;
510         struct vnode **a_vpp;
511         struct componentname *a_cnp;
512         struct vattr *a_vap;
513     };
514 */
515 static int
516 fuse_vnop_create(struct vop_create_args *ap)
517 {
518         struct vnode *dvp = ap->a_dvp;
519         struct vnode **vpp = ap->a_vpp;
520         struct componentname *cnp = ap->a_cnp;
521         struct vattr *vap = ap->a_vap;
522         struct thread *td = cnp->cn_thread;
523         struct ucred *cred = cnp->cn_cred;
524
525         struct fuse_open_in *foi;
526         struct fuse_entry_out *feo;
527         struct fuse_open_out *foo;
528         struct fuse_dispatcher fdi, fdi2;
529         struct fuse_dispatcher *fdip = &fdi;
530         struct fuse_dispatcher *fdip2 = NULL;
531
532         int err;
533
534         struct mount *mp = vnode_mount(dvp);
535         uint64_t parentnid = VTOFUD(dvp)->nid;
536         mode_t mode = MAKEIMODE(vap->va_type, vap->va_mode);
537         enum fuse_opcode op;
538         int flags;
539
540         if (fuse_isdeadfs(dvp))
541                 return ENXIO;
542
543         /* FUSE expects sockets to be created with FUSE_MKNOD */
544         if (vap->va_type == VSOCK)
545                 return fuse_internal_mknod(dvp, vpp, cnp, vap);
546
547         /* 
548          * VOP_CREATE doesn't tell us the open(2) flags, so we guess.  Only a
549          * writable mode makes sense, and we might as well include readability
550          * too.
551          */
552         flags = O_RDWR;
553
554         bzero(&fdi, sizeof(fdi));
555
556         if (vap->va_type != VREG)
557                 return (EINVAL);
558
559         if (!fsess_isimpl(mp, FUSE_CREATE) || vap->va_type == VSOCK) {
560                 /* Fallback to FUSE_MKNOD/FUSE_OPEN */
561                 fdisp_make_mknod_for_fallback(fdip, cnp, dvp, parentnid, td,
562                         cred, mode, &op);
563         } else {
564                 /* Use FUSE_CREATE */
565                 op = FUSE_CREATE;
566                 fdisp_init(fdip, sizeof(*foi) + cnp->cn_namelen + 1);
567                 fdisp_make(fdip, op, vnode_mount(dvp), parentnid, td, cred);
568                 foi = fdip->indata;
569                 foi->mode = mode;
570                 foi->flags = O_CREAT | flags;
571                 memcpy((char *)fdip->indata + sizeof(*foi), cnp->cn_nameptr,
572                     cnp->cn_namelen);
573                 ((char *)fdip->indata)[sizeof(*foi) + cnp->cn_namelen] = '\0';
574         }
575
576         err = fdisp_wait_answ(fdip);
577
578         if (err) {
579                 if (err == ENOSYS && op == FUSE_CREATE) {
580                         fsess_set_notimpl(mp, FUSE_CREATE);
581                         fdisp_make_mknod_for_fallback(fdip, cnp, dvp,
582                                 parentnid, td, cred, mode, &op);
583                         err = fdisp_wait_answ(fdip);
584                 }
585                 if (err)
586                         goto out;
587         }
588
589         feo = fdip->answ;
590
591         if ((err = fuse_internal_checkentry(feo, vap->va_type))) {
592                 goto out;
593         }
594
595         if (op == FUSE_CREATE) {
596                 foo = (struct fuse_open_out*)(feo + 1);
597         } else {
598                 /* Issue a separate FUSE_OPEN */
599                 fdip2 = &fdi2;
600                 fdisp_init(fdip2, sizeof(*foi));
601                 fdisp_make(fdip2, FUSE_OPEN, vnode_mount(dvp), feo->nodeid, td,
602                         cred);
603                 foi = fdip2->indata;
604                 foi->mode = mode;
605                 foi->flags = flags;
606                 err = fdisp_wait_answ(fdip2);
607                 if (err)
608                         goto out;
609                 foo = fdip2->answ;
610         }
611         err = fuse_vnode_get(mp, feo, feo->nodeid, dvp, vpp, cnp, vap->va_type);
612         if (err) {
613                 struct fuse_release_in *fri;
614                 uint64_t nodeid = feo->nodeid;
615                 uint64_t fh_id = foo->fh;
616
617                 fdisp_init(fdip, sizeof(*fri));
618                 fdisp_make(fdip, FUSE_RELEASE, mp, nodeid, td, cred);
619                 fri = fdip->indata;
620                 fri->fh = fh_id;
621                 fri->flags = flags;
622                 fuse_insert_callback(fdip->tick, fuse_internal_forget_callback);
623                 fuse_insert_message(fdip->tick, false);
624                 goto out;
625         }
626         ASSERT_VOP_ELOCKED(*vpp, "fuse_vnop_create");
627         fuse_internal_cache_attrs(*vpp, &feo->attr, feo->attr_valid,
628                 feo->attr_valid_nsec, NULL);
629
630         fuse_filehandle_init(*vpp, FUFH_RDWR, NULL, td, cred, foo);
631         fuse_vnode_open(*vpp, foo->open_flags, td);
632         /* 
633          * Purge the parent's attribute cache because the daemon should've
634          * updated its mtime and ctime
635          */
636         fuse_vnode_clear_attr_cache(dvp);
637         cache_purge_negative(dvp);
638
639 out:
640         if (fdip2)
641                 fdisp_destroy(fdip2);
642         fdisp_destroy(fdip);
643         return err;
644 }
645
646 /*
647     struct vnop_fdatasync_args {
648         struct vop_generic_args a_gen;
649         struct vnode * a_vp;
650         struct thread * a_td;
651     };
652 */
653 static int
654 fuse_vnop_fdatasync(struct vop_fdatasync_args *ap)
655 {
656         struct vnode *vp = ap->a_vp;
657         struct thread *td = ap->a_td;
658         int waitfor = MNT_WAIT;
659
660         int err = 0;
661
662         if (fuse_isdeadfs(vp)) {
663                 return 0;
664         }
665         if ((err = vop_stdfdatasync_buf(ap)))
666                 return err;
667
668         return fuse_internal_fsync(vp, td, waitfor, true);
669 }
670
671 /*
672     struct vnop_fsync_args {
673         struct vop_generic_args a_gen;
674         struct vnode * a_vp;
675         int  a_waitfor;
676         struct thread * a_td;
677     };
678 */
679 static int
680 fuse_vnop_fsync(struct vop_fsync_args *ap)
681 {
682         struct vnode *vp = ap->a_vp;
683         struct thread *td = ap->a_td;
684         int waitfor = ap->a_waitfor;
685         int err = 0;
686
687         if (fuse_isdeadfs(vp)) {
688                 return 0;
689         }
690         if ((err = vop_stdfsync(ap)))
691                 return err;
692
693         return fuse_internal_fsync(vp, td, waitfor, false);
694 }
695
696 /*
697     struct vnop_getattr_args {
698         struct vnode *a_vp;
699         struct vattr *a_vap;
700         struct ucred *a_cred;
701         struct thread *a_td;
702     };
703 */
704 static int
705 fuse_vnop_getattr(struct vop_getattr_args *ap)
706 {
707         struct vnode *vp = ap->a_vp;
708         struct vattr *vap = ap->a_vap;
709         struct ucred *cred = ap->a_cred;
710         struct thread *td = curthread;
711
712         int err = 0;
713         int dataflags;
714
715         dataflags = fuse_get_mpdata(vnode_mount(vp))->dataflags;
716
717         /* Note that we are not bailing out on a dead file system just yet. */
718
719         if (!(dataflags & FSESS_INITED)) {
720                 if (!vnode_isvroot(vp)) {
721                         fdata_set_dead(fuse_get_mpdata(vnode_mount(vp)));
722                         err = ENOTCONN;
723                         return err;
724                 } else {
725                         goto fake;
726                 }
727         }
728         err = fuse_internal_getattr(vp, vap, cred, td);
729         if (err == ENOTCONN && vnode_isvroot(vp)) {
730                 /* see comment in fuse_vfsop_statfs() */
731                 goto fake;
732         } else {
733                 return err;
734         }
735
736 fake:
737         bzero(vap, sizeof(*vap));
738         vap->va_type = vnode_vtype(vp);
739
740         return 0;
741 }
742
743 /*
744     struct vnop_inactive_args {
745         struct vnode *a_vp;
746         struct thread *a_td;
747     };
748 */
749 static int
750 fuse_vnop_inactive(struct vop_inactive_args *ap)
751 {
752         struct vnode *vp = ap->a_vp;
753         struct thread *td = ap->a_td;
754
755         struct fuse_vnode_data *fvdat = VTOFUD(vp);
756         struct fuse_filehandle *fufh, *fufh_tmp;
757
758         int need_flush = 1;
759
760         LIST_FOREACH_SAFE(fufh, &fvdat->handles, next, fufh_tmp) {
761                 if (need_flush && vp->v_type == VREG) {
762                         if ((VTOFUD(vp)->flag & FN_SIZECHANGE) != 0) {
763                                 fuse_vnode_savesize(vp, NULL, 0);
764                         }
765                         if ((fvdat->flag & FN_REVOKED) != 0)
766                                 fuse_io_invalbuf(vp, td);
767                         else
768                                 fuse_io_flushbuf(vp, MNT_WAIT, td);
769                         need_flush = 0;
770                 }
771                 fuse_filehandle_close(vp, fufh, td, NULL);
772         }
773
774         if ((fvdat->flag & FN_REVOKED) != 0 && fuse_reclaim_revoked) {
775                 vrecycle(vp);
776         }
777         return 0;
778 }
779
780 /*
781     struct vnop_link_args {
782         struct vnode *a_tdvp;
783         struct vnode *a_vp;
784         struct componentname *a_cnp;
785     };
786 */
787 static int
788 fuse_vnop_link(struct vop_link_args *ap)
789 {
790         struct vnode *vp = ap->a_vp;
791         struct vnode *tdvp = ap->a_tdvp;
792         struct componentname *cnp = ap->a_cnp;
793
794         struct vattr *vap = VTOVA(vp);
795
796         struct fuse_dispatcher fdi;
797         struct fuse_entry_out *feo;
798         struct fuse_link_in fli;
799
800         int err;
801
802         if (fuse_isdeadfs(vp)) {
803                 return ENXIO;
804         }
805         if (vnode_mount(tdvp) != vnode_mount(vp)) {
806                 return EXDEV;
807         }
808
809         /*
810          * This is a seatbelt check to protect naive userspace filesystems from
811          * themselves and the limitations of the FUSE IPC protocol.  If a
812          * filesystem does not allow attribute caching, assume it is capable of
813          * validating that nlink does not overflow.
814          */
815         if (vap != NULL && vap->va_nlink >= FUSE_LINK_MAX)
816                 return EMLINK;
817         fli.oldnodeid = VTOI(vp);
818
819         fdisp_init(&fdi, 0);
820         fuse_internal_newentry_makerequest(vnode_mount(tdvp), VTOI(tdvp), cnp,
821             FUSE_LINK, &fli, sizeof(fli), &fdi);
822         if ((err = fdisp_wait_answ(&fdi))) {
823                 goto out;
824         }
825         feo = fdi.answ;
826
827         err = fuse_internal_checkentry(feo, vnode_vtype(vp));
828         if (!err) {
829                 /* 
830                  * Purge the parent's attribute cache because the daemon
831                  * should've updated its mtime and ctime
832                  */
833                 fuse_vnode_clear_attr_cache(tdvp);
834                 fuse_internal_cache_attrs(vp, &feo->attr, feo->attr_valid,
835                         feo->attr_valid_nsec, NULL);
836         }
837 out:
838         fdisp_destroy(&fdi);
839         return err;
840 }
841
842 struct fuse_lookup_alloc_arg {
843         struct fuse_entry_out *feo;
844         struct componentname *cnp;
845         uint64_t nid;
846         enum vtype vtyp;
847 };
848
849 /* Callback for vn_get_ino */
850 static int
851 fuse_lookup_alloc(struct mount *mp, void *arg, int lkflags, struct vnode **vpp)
852 {
853         struct fuse_lookup_alloc_arg *flaa = arg;
854
855         return fuse_vnode_get(mp, flaa->feo, flaa->nid, NULL, vpp, flaa->cnp,
856                 flaa->vtyp);
857 }
858
859 SDT_PROBE_DEFINE3(fusefs, , vnops, cache_lookup,
860         "int", "struct timespec*", "struct timespec*");
861 /*
862     struct vnop_lookup_args {
863         struct vnodeop_desc *a_desc;
864         struct vnode *a_dvp;
865         struct vnode **a_vpp;
866         struct componentname *a_cnp;
867     };
868 */
869 int
870 fuse_vnop_lookup(struct vop_lookup_args *ap)
871 {
872         struct vnode *dvp = ap->a_dvp;
873         struct vnode **vpp = ap->a_vpp;
874         struct componentname *cnp = ap->a_cnp;
875         struct thread *td = cnp->cn_thread;
876         struct ucred *cred = cnp->cn_cred;
877
878         int nameiop = cnp->cn_nameiop;
879         int flags = cnp->cn_flags;
880         int wantparent = flags & (LOCKPARENT | WANTPARENT);
881         int islastcn = flags & ISLASTCN;
882         struct mount *mp = vnode_mount(dvp);
883
884         int err = 0;
885         int lookup_err = 0;
886         struct vnode *vp = NULL;
887
888         struct fuse_dispatcher fdi;
889         bool did_lookup = false;
890         struct fuse_entry_out *feo = NULL;
891         enum vtype vtyp;        /* vnode type of target */
892         off_t filesize;         /* filesize of target */
893
894         uint64_t nid;
895
896         if (fuse_isdeadfs(dvp)) {
897                 *vpp = NULL;
898                 return ENXIO;
899         }
900         if (!vnode_isdir(dvp))
901                 return ENOTDIR;
902
903         if (islastcn && vfs_isrdonly(mp) && (nameiop != LOOKUP))
904                 return EROFS;
905
906         if ((err = fuse_internal_access(dvp, VEXEC, td, cred)))
907                 return err;
908
909         if (flags & ISDOTDOT) {
910                 nid = VTOFUD(dvp)->parent_nid;
911                 if (nid == 0)
912                         return ENOENT;
913                 /* .. is obviously a directory */
914                 vtyp = VDIR;
915                 filesize = 0;
916         } else if (cnp->cn_namelen == 1 && *(cnp->cn_nameptr) == '.') {
917                 nid = VTOI(dvp);
918                 /* . is obviously a directory */
919                 vtyp = VDIR;
920                 filesize = 0;
921         } else {
922                 struct timespec now, timeout;
923
924                 err = cache_lookup(dvp, vpp, cnp, &timeout, NULL);
925                 getnanouptime(&now);
926                 SDT_PROBE3(fusefs, , vnops, cache_lookup, err, &timeout, &now);
927                 switch (err) {
928                 case -1:                /* positive match */
929                         if (timespeccmp(&timeout, &now, >)) {
930                                 atomic_add_acq_long(&fuse_lookup_cache_hits, 1);
931                         } else {
932                                 /* Cache timeout */
933                                 atomic_add_acq_long(&fuse_lookup_cache_misses,
934                                         1);
935                                 cache_purge(*vpp);
936                                 if (dvp != *vpp)
937                                         vput(*vpp);
938                                 else 
939                                         vrele(*vpp);
940                                 *vpp = NULL;
941                                 break;
942                         }
943                         return 0;
944
945                 case 0:         /* no match in cache */
946                         atomic_add_acq_long(&fuse_lookup_cache_misses, 1);
947                         break;
948
949                 case ENOENT:            /* negative match */
950                         getnanouptime(&now);
951                         if (timespeccmp(&timeout, &now, <=)) {
952                                 /* Cache timeout */
953                                 cache_purge_negative(dvp);
954                                 break;
955                         }
956                         /* fall through */
957                 default:
958                         return err;
959                 }
960
961                 nid = VTOI(dvp);
962                 fdisp_init(&fdi, cnp->cn_namelen + 1);
963                 fdisp_make(&fdi, FUSE_LOOKUP, mp, nid, td, cred);
964
965                 memcpy(fdi.indata, cnp->cn_nameptr, cnp->cn_namelen);
966                 ((char *)fdi.indata)[cnp->cn_namelen] = '\0';
967                 lookup_err = fdisp_wait_answ(&fdi);
968                 did_lookup = true;
969
970                 if (!lookup_err) {
971                         /* lookup call succeeded */
972                         nid = ((struct fuse_entry_out *)fdi.answ)->nodeid;
973                         feo = (struct fuse_entry_out *)fdi.answ;
974                         if (nid == 0) {
975                                 /* zero nodeid means ENOENT and cache it */
976                                 struct timespec timeout;
977
978                                 fdi.answ_stat = ENOENT;
979                                 lookup_err = ENOENT;
980                                 if (cnp->cn_flags & MAKEENTRY) {
981                                         fuse_validity_2_timespec(feo, &timeout);
982                                         cache_enter_time(dvp, *vpp, cnp,
983                                                 &timeout, NULL);
984                                 }
985                         } else if (nid == FUSE_ROOT_ID) {
986                                 lookup_err = EINVAL;
987                         }
988                         vtyp = IFTOVT(feo->attr.mode);
989                         filesize = feo->attr.size;
990                 }
991                 if (lookup_err && (!fdi.answ_stat || lookup_err != ENOENT)) {
992                         fdisp_destroy(&fdi);
993                         return lookup_err;
994                 }
995         }
996         /* lookup_err, if non-zero, must be ENOENT at this point */
997
998         if (lookup_err) {
999                 /* Entry not found */
1000                 if ((nameiop == CREATE || nameiop == RENAME) && islastcn) {
1001                         err = fuse_internal_access(dvp, VWRITE, td, cred);
1002                         if (!err) {
1003                                 /*
1004                                  * Set the SAVENAME flag to hold onto the
1005                                  * pathname for use later in VOP_CREATE or
1006                                  * VOP_RENAME.
1007                                  */
1008                                 cnp->cn_flags |= SAVENAME;
1009
1010                                 err = EJUSTRETURN;
1011                         }
1012                 } else {
1013                         err = ENOENT;
1014                 }
1015         } else {
1016                 /* Entry was found */
1017                 if (flags & ISDOTDOT) {
1018                         struct fuse_lookup_alloc_arg flaa;
1019
1020                         flaa.nid = nid;
1021                         flaa.feo = feo;
1022                         flaa.cnp = cnp;
1023                         flaa.vtyp = vtyp;
1024                         err = vn_vget_ino_gen(dvp, fuse_lookup_alloc, &flaa, 0,
1025                                 &vp);
1026                         *vpp = vp;
1027                 } else if (nid == VTOI(dvp)) {
1028                         vref(dvp);
1029                         *vpp = dvp;
1030                 } else {
1031                         struct fuse_vnode_data *fvdat;
1032
1033                         err = fuse_vnode_get(vnode_mount(dvp), feo, nid, dvp,
1034                             &vp, cnp, vtyp);
1035                         if (err)
1036                                 goto out;
1037                         *vpp = vp;
1038
1039                         /*
1040                          * In the case where we are looking up a FUSE node
1041                          * represented by an existing cached vnode, and the
1042                          * true size reported by FUSE_LOOKUP doesn't match
1043                          * the vnode's cached size, fix the vnode cache to
1044                          * match the real object size.
1045                          *
1046                          * We can get here:
1047                          * * following attribute cache expiration, or
1048                          * * due a bug in the daemon, or
1049                          * * the first time that we looked up the file.
1050                          */
1051                         fvdat = VTOFUD(vp);
1052                         if (vnode_isreg(vp) &&
1053                             filesize != fvdat->cached_attrs.va_size) {
1054                                 /*
1055                                  * The FN_SIZECHANGE flag reflects a dirty
1056                                  * append.  If userspace lets us know our cache
1057                                  * is invalid, that write was lost.  (Dirty
1058                                  * writes that do not cause append are also
1059                                  * lost, but we don't detect them here.)
1060                                  *
1061                                  * XXX: Maybe disable WB caching on this mount.
1062                                  */
1063                                 if (fvdat->flag & FN_SIZECHANGE)
1064                                         printf("%s: WB cache incoherent on "
1065                                             "%s!\n", __func__,
1066                                             vnode_mount(vp)->mnt_stat.f_mntonname);
1067
1068                                 (void)fuse_vnode_setsize(vp, cred, filesize);
1069                                 fvdat->flag &= ~FN_SIZECHANGE;
1070                         }
1071
1072                         MPASS(feo != NULL);
1073                         fuse_internal_cache_attrs(*vpp, &feo->attr,
1074                                 feo->attr_valid, feo->attr_valid_nsec, NULL);
1075
1076                         if ((nameiop == DELETE || nameiop == RENAME) &&
1077                                 islastcn)
1078                         {
1079                                 struct vattr dvattr;
1080
1081                                 err = fuse_internal_access(dvp, VWRITE, td,
1082                                         cred);
1083                                 if (err != 0)
1084                                         goto out;
1085                                 /* 
1086                                  * if the parent's sticky bit is set, check
1087                                  * whether we're allowed to remove the file.
1088                                  * Need to figure out the vnode locking to make
1089                                  * this work.
1090                                  */
1091                                 fuse_internal_getattr(dvp, &dvattr, cred, td);
1092                                 if ((dvattr.va_mode & S_ISTXT) &&
1093                                         fuse_internal_access(dvp, VADMIN, td,
1094                                                 cred) &&
1095                                         fuse_internal_access(*vpp, VADMIN, td,
1096                                                 cred)) {
1097                                         err = EPERM;
1098                                         goto out;
1099                                 }
1100                         }
1101
1102                         if (islastcn && (
1103                                 (nameiop == DELETE) ||
1104                                 (nameiop == RENAME && wantparent))) {
1105                                 cnp->cn_flags |= SAVENAME;
1106                         }
1107
1108                 }
1109         }
1110 out:
1111         if (err) {
1112                 if (vp != NULL && dvp != vp)
1113                         vput(vp);
1114                 else if (vp != NULL)
1115                         vrele(vp);
1116                 *vpp = NULL;
1117         }
1118         if (did_lookup)
1119                 fdisp_destroy(&fdi);
1120
1121         return err;
1122 }
1123
1124 /*
1125     struct vnop_mkdir_args {
1126         struct vnode *a_dvp;
1127         struct vnode **a_vpp;
1128         struct componentname *a_cnp;
1129         struct vattr *a_vap;
1130     };
1131 */
1132 static int
1133 fuse_vnop_mkdir(struct vop_mkdir_args *ap)
1134 {
1135         struct vnode *dvp = ap->a_dvp;
1136         struct vnode **vpp = ap->a_vpp;
1137         struct componentname *cnp = ap->a_cnp;
1138         struct vattr *vap = ap->a_vap;
1139
1140         struct fuse_mkdir_in fmdi;
1141
1142         if (fuse_isdeadfs(dvp)) {
1143                 return ENXIO;
1144         }
1145         fmdi.mode = MAKEIMODE(vap->va_type, vap->va_mode);
1146
1147         return (fuse_internal_newentry(dvp, vpp, cnp, FUSE_MKDIR, &fmdi,
1148             sizeof(fmdi), VDIR));
1149 }
1150
1151 /*
1152     struct vnop_mknod_args {
1153         struct vnode *a_dvp;
1154         struct vnode **a_vpp;
1155         struct componentname *a_cnp;
1156         struct vattr *a_vap;
1157     };
1158 */
1159 static int
1160 fuse_vnop_mknod(struct vop_mknod_args *ap)
1161 {
1162
1163         struct vnode *dvp = ap->a_dvp;
1164         struct vnode **vpp = ap->a_vpp;
1165         struct componentname *cnp = ap->a_cnp;
1166         struct vattr *vap = ap->a_vap;
1167
1168         if (fuse_isdeadfs(dvp))
1169                 return ENXIO;
1170
1171         return fuse_internal_mknod(dvp, vpp, cnp, vap);
1172 }
1173
1174 /*
1175     struct vnop_open_args {
1176         struct vnode *a_vp;
1177         int  a_mode;
1178         struct ucred *a_cred;
1179         struct thread *a_td;
1180         int a_fdidx; / struct file *a_fp;
1181     };
1182 */
1183 static int
1184 fuse_vnop_open(struct vop_open_args *ap)
1185 {
1186         struct vnode *vp = ap->a_vp;
1187         int a_mode = ap->a_mode;
1188         struct thread *td = ap->a_td;
1189         struct ucred *cred = ap->a_cred;
1190         pid_t pid = td->td_proc->p_pid;
1191         struct fuse_vnode_data *fvdat;
1192
1193         if (fuse_isdeadfs(vp))
1194                 return ENXIO;
1195         if (vp->v_type == VCHR || vp->v_type == VBLK || vp->v_type == VFIFO)
1196                 return (EOPNOTSUPP);
1197         if ((a_mode & (FREAD | FWRITE | FEXEC)) == 0)
1198                 return EINVAL;
1199
1200         fvdat = VTOFUD(vp);
1201
1202         if (fuse_filehandle_validrw(vp, a_mode, cred, pid)) {
1203                 fuse_vnode_open(vp, 0, td);
1204                 return 0;
1205         }
1206
1207         return fuse_filehandle_open(vp, a_mode, NULL, td, cred);
1208 }
1209
1210 static int
1211 fuse_vnop_pathconf(struct vop_pathconf_args *ap)
1212 {
1213
1214         switch (ap->a_name) {
1215         case _PC_FILESIZEBITS:
1216                 *ap->a_retval = 64;
1217                 return (0);
1218         case _PC_NAME_MAX:
1219                 *ap->a_retval = NAME_MAX;
1220                 return (0);
1221         case _PC_LINK_MAX:
1222                 *ap->a_retval = MIN(LONG_MAX, FUSE_LINK_MAX);
1223                 return (0);
1224         case _PC_SYMLINK_MAX:
1225                 *ap->a_retval = MAXPATHLEN;
1226                 return (0);
1227         case _PC_NO_TRUNC:
1228                 *ap->a_retval = 1;
1229                 return (0);
1230         default:
1231                 return (vop_stdpathconf(ap));
1232         }
1233 }
1234
1235 /*
1236     struct vnop_read_args {
1237         struct vnode *a_vp;
1238         struct uio *a_uio;
1239         int  a_ioflag;
1240         struct ucred *a_cred;
1241     };
1242 */
1243 static int
1244 fuse_vnop_read(struct vop_read_args *ap)
1245 {
1246         struct vnode *vp = ap->a_vp;
1247         struct uio *uio = ap->a_uio;
1248         int ioflag = ap->a_ioflag;
1249         struct ucred *cred = ap->a_cred;
1250         pid_t pid = curthread->td_proc->p_pid;
1251
1252         if (fuse_isdeadfs(vp)) {
1253                 return ENXIO;
1254         }
1255
1256         if (VTOFUD(vp)->flag & FN_DIRECTIO) {
1257                 ioflag |= IO_DIRECT;
1258         }
1259
1260         return fuse_io_dispatch(vp, uio, ioflag, false, cred, pid);
1261 }
1262
1263 /*
1264     struct vnop_readdir_args {
1265         struct vnode *a_vp;
1266         struct uio *a_uio;
1267         struct ucred *a_cred;
1268         int *a_eofflag;
1269         int *ncookies;
1270         u_long **a_cookies;
1271     };
1272 */
1273 static int
1274 fuse_vnop_readdir(struct vop_readdir_args *ap)
1275 {
1276         struct vnode *vp = ap->a_vp;
1277         struct uio *uio = ap->a_uio;
1278         struct ucred *cred = ap->a_cred;
1279         struct fuse_filehandle *fufh = NULL;
1280         struct fuse_iov cookediov;
1281         int err = 0;
1282         pid_t pid = curthread->td_proc->p_pid;
1283
1284         if (fuse_isdeadfs(vp)) {
1285                 return ENXIO;
1286         }
1287         if (                            /* XXXIP ((uio_iovcnt(uio) > 1)) || */
1288             (uio_resid(uio) < sizeof(struct dirent))) {
1289                 return EINVAL;
1290         }
1291
1292         err = fuse_filehandle_get_dir(vp, &fufh, cred, pid);
1293         if (err)
1294                 return (err);
1295 #define DIRCOOKEDSIZE FUSE_DIRENT_ALIGN(FUSE_NAME_OFFSET + MAXNAMLEN + 1)
1296         fiov_init(&cookediov, DIRCOOKEDSIZE);
1297
1298         err = fuse_internal_readdir(vp, uio, fufh, &cookediov);
1299
1300         fiov_teardown(&cookediov);
1301
1302         return err;
1303 }
1304
1305 /*
1306     struct vnop_readlink_args {
1307         struct vnode *a_vp;
1308         struct uio *a_uio;
1309         struct ucred *a_cred;
1310     };
1311 */
1312 static int
1313 fuse_vnop_readlink(struct vop_readlink_args *ap)
1314 {
1315         struct vnode *vp = ap->a_vp;
1316         struct uio *uio = ap->a_uio;
1317         struct ucred *cred = ap->a_cred;
1318
1319         struct fuse_dispatcher fdi;
1320         int err;
1321
1322         if (fuse_isdeadfs(vp)) {
1323                 return ENXIO;
1324         }
1325         if (!vnode_islnk(vp)) {
1326                 return EINVAL;
1327         }
1328         fdisp_init(&fdi, 0);
1329         err = fdisp_simple_putget_vp(&fdi, FUSE_READLINK, vp, curthread, cred);
1330         if (err) {
1331                 goto out;
1332         }
1333         if (((char *)fdi.answ)[0] == '/' &&
1334             fuse_get_mpdata(vnode_mount(vp))->dataflags & FSESS_PUSH_SYMLINKS_IN) {
1335                 char *mpth = vnode_mount(vp)->mnt_stat.f_mntonname;
1336
1337                 err = uiomove(mpth, strlen(mpth), uio);
1338         }
1339         if (!err) {
1340                 err = uiomove(fdi.answ, fdi.iosize, uio);
1341         }
1342 out:
1343         fdisp_destroy(&fdi);
1344         return err;
1345 }
1346
1347 /*
1348     struct vnop_reclaim_args {
1349         struct vnode *a_vp;
1350         struct thread *a_td;
1351     };
1352 */
1353 static int
1354 fuse_vnop_reclaim(struct vop_reclaim_args *ap)
1355 {
1356         struct vnode *vp = ap->a_vp;
1357         struct thread *td = ap->a_td;
1358         struct fuse_vnode_data *fvdat = VTOFUD(vp);
1359         struct fuse_filehandle *fufh, *fufh_tmp;
1360
1361         if (!fvdat) {
1362                 panic("FUSE: no vnode data during recycling");
1363         }
1364         LIST_FOREACH_SAFE(fufh, &fvdat->handles, next, fufh_tmp) {
1365                 printf("FUSE: vnode being reclaimed with open fufh "
1366                         "(type=%#x)", fufh->fufh_type);
1367                 fuse_filehandle_close(vp, fufh, td, NULL);
1368         }
1369
1370         if ((!fuse_isdeadfs(vp)) && (fvdat->nlookup)) {
1371                 fuse_internal_forget_send(vnode_mount(vp), td, NULL, VTOI(vp),
1372                     fvdat->nlookup);
1373         }
1374         fuse_vnode_setparent(vp, NULL);
1375         cache_purge(vp);
1376         vfs_hash_remove(vp);
1377         vnode_destroy_vobject(vp);
1378         fuse_vnode_destroy(vp);
1379
1380         return 0;
1381 }
1382
1383 /*
1384     struct vnop_remove_args {
1385         struct vnode *a_dvp;
1386         struct vnode *a_vp;
1387         struct componentname *a_cnp;
1388     };
1389 */
1390 static int
1391 fuse_vnop_remove(struct vop_remove_args *ap)
1392 {
1393         struct vnode *dvp = ap->a_dvp;
1394         struct vnode *vp = ap->a_vp;
1395         struct componentname *cnp = ap->a_cnp;
1396
1397         int err;
1398
1399         if (fuse_isdeadfs(vp)) {
1400                 return ENXIO;
1401         }
1402         if (vnode_isdir(vp)) {
1403                 return EPERM;
1404         }
1405         cache_purge(vp);
1406
1407         err = fuse_internal_remove(dvp, vp, cnp, FUSE_UNLINK);
1408
1409         if (err == 0) {
1410                 fuse_internal_vnode_disappear(vp);
1411                 /* 
1412                  * Purge the parent's attribute cache because the daemon
1413                  * should've updated its mtime and ctime
1414                  */
1415                 fuse_vnode_clear_attr_cache(dvp);
1416         }
1417         return err;
1418 }
1419
1420 /*
1421     struct vnop_rename_args {
1422         struct vnode *a_fdvp;
1423         struct vnode *a_fvp;
1424         struct componentname *a_fcnp;
1425         struct vnode *a_tdvp;
1426         struct vnode *a_tvp;
1427         struct componentname *a_tcnp;
1428     };
1429 */
1430 static int
1431 fuse_vnop_rename(struct vop_rename_args *ap)
1432 {
1433         struct vnode *fdvp = ap->a_fdvp;
1434         struct vnode *fvp = ap->a_fvp;
1435         struct componentname *fcnp = ap->a_fcnp;
1436         struct vnode *tdvp = ap->a_tdvp;
1437         struct vnode *tvp = ap->a_tvp;
1438         struct componentname *tcnp = ap->a_tcnp;
1439         struct fuse_data *data;
1440         bool newparent = fdvp != tdvp;
1441         bool isdir = fvp->v_type == VDIR;
1442         int err = 0;
1443
1444         if (fuse_isdeadfs(fdvp)) {
1445                 return ENXIO;
1446         }
1447         if (fvp->v_mount != tdvp->v_mount ||
1448             (tvp && fvp->v_mount != tvp->v_mount)) {
1449                 SDT_PROBE2(fusefs, , vnops, trace, 1, "cross-device rename");
1450                 err = EXDEV;
1451                 goto out;
1452         }
1453         cache_purge(fvp);
1454
1455         /*
1456          * FUSE library is expected to check if target directory is not
1457          * under the source directory in the file system tree.
1458          * Linux performs this check at VFS level.
1459          */
1460         /* 
1461          * If source is a directory, and it will get a new parent, user must
1462          * have write permission to it, so ".." can be modified.
1463          */
1464         data = fuse_get_mpdata(vnode_mount(tdvp));
1465         if (data->dataflags & FSESS_DEFAULT_PERMISSIONS && isdir && newparent) {
1466                 err = fuse_internal_access(fvp, VWRITE,
1467                         tcnp->cn_thread, tcnp->cn_cred);
1468                 if (err)
1469                         goto out;
1470         }
1471         sx_xlock(&data->rename_lock);
1472         err = fuse_internal_rename(fdvp, fcnp, tdvp, tcnp);
1473         if (err == 0) {
1474                 if (tdvp != fdvp)
1475                         fuse_vnode_setparent(fvp, tdvp);
1476                 if (tvp != NULL)
1477                         fuse_vnode_setparent(tvp, NULL);
1478         }
1479         sx_unlock(&data->rename_lock);
1480
1481         if (tvp != NULL && tvp != fvp) {
1482                 cache_purge(tvp);
1483         }
1484         if (vnode_isdir(fvp)) {
1485                 if ((tvp != NULL) && vnode_isdir(tvp)) {
1486                         cache_purge(tdvp);
1487                 }
1488                 cache_purge(fdvp);
1489         }
1490 out:
1491         if (tdvp == tvp) {
1492                 vrele(tdvp);
1493         } else {
1494                 vput(tdvp);
1495         }
1496         if (tvp != NULL) {
1497                 vput(tvp);
1498         }
1499         vrele(fdvp);
1500         vrele(fvp);
1501
1502         return err;
1503 }
1504
1505 /*
1506     struct vnop_rmdir_args {
1507             struct vnode *a_dvp;
1508             struct vnode *a_vp;
1509             struct componentname *a_cnp;
1510     } *ap;
1511 */
1512 static int
1513 fuse_vnop_rmdir(struct vop_rmdir_args *ap)
1514 {
1515         struct vnode *dvp = ap->a_dvp;
1516         struct vnode *vp = ap->a_vp;
1517
1518         int err;
1519
1520         if (fuse_isdeadfs(vp)) {
1521                 return ENXIO;
1522         }
1523         if (VTOFUD(vp) == VTOFUD(dvp)) {
1524                 return EINVAL;
1525         }
1526         err = fuse_internal_remove(dvp, vp, ap->a_cnp, FUSE_RMDIR);
1527
1528         if (err == 0) {
1529                 fuse_internal_vnode_disappear(vp);
1530                 /* 
1531                  * Purge the parent's attribute cache because the daemon
1532                  * should've updated its mtime and ctime
1533                  */
1534                 fuse_vnode_clear_attr_cache(dvp);
1535         }
1536         return err;
1537 }
1538
1539 /*
1540     struct vnop_setattr_args {
1541         struct vnode *a_vp;
1542         struct vattr *a_vap;
1543         struct ucred *a_cred;
1544         struct thread *a_td;
1545     };
1546 */
1547 static int
1548 fuse_vnop_setattr(struct vop_setattr_args *ap)
1549 {
1550         struct vnode *vp = ap->a_vp;
1551         struct vattr *vap = ap->a_vap;
1552         struct ucred *cred = ap->a_cred;
1553         struct thread *td = curthread;
1554         struct mount *mp;
1555         struct fuse_data *data;
1556         struct vattr old_va;
1557         int dataflags;
1558         int err = 0, err2;
1559         accmode_t accmode = 0;
1560         bool checkperm;
1561         bool drop_suid = false;
1562         gid_t cr_gid;
1563
1564         mp = vnode_mount(vp);
1565         data = fuse_get_mpdata(mp);
1566         dataflags = data->dataflags;
1567         checkperm = dataflags & FSESS_DEFAULT_PERMISSIONS;
1568         if (cred->cr_ngroups > 0)
1569                 cr_gid = cred->cr_groups[0];
1570         else
1571                 cr_gid = 0;
1572
1573         if (fuse_isdeadfs(vp)) {
1574                 return ENXIO;
1575         }
1576
1577         if (vap->va_uid != (uid_t)VNOVAL) {
1578                 if (checkperm) {
1579                         /* Only root may change a file's owner */
1580                         err = priv_check_cred(cred, PRIV_VFS_CHOWN);
1581                         if (err) {
1582                                 /* As a special case, allow the null chown */
1583                                 err2 = fuse_internal_getattr(vp, &old_va, cred,
1584                                         td);
1585                                 if (err2)
1586                                         return (err2);
1587                                 if (vap->va_uid != old_va.va_uid)
1588                                         return err;
1589                                 else
1590                                         accmode |= VADMIN;
1591                                 drop_suid = true;
1592                         } else
1593                                 accmode |= VADMIN;
1594                 } else
1595                         accmode |= VADMIN;
1596         }
1597         if (vap->va_gid != (gid_t)VNOVAL) {
1598                 if (checkperm && priv_check_cred(cred, PRIV_VFS_CHOWN))
1599                         drop_suid = true;
1600                 if (checkperm && !groupmember(vap->va_gid, cred))
1601                 {
1602                         /*
1603                          * Non-root users may only chgrp to one of their own
1604                          * groups 
1605                          */
1606                         err = priv_check_cred(cred, PRIV_VFS_CHOWN);
1607                         if (err) {
1608                                 /* As a special case, allow the null chgrp */
1609                                 err2 = fuse_internal_getattr(vp, &old_va, cred,
1610                                         td);
1611                                 if (err2)
1612                                         return (err2);
1613                                 if (vap->va_gid != old_va.va_gid)
1614                                         return err;
1615                                 accmode |= VADMIN;
1616                         } else
1617                                 accmode |= VADMIN;
1618                 } else
1619                         accmode |= VADMIN;
1620         }
1621         if (vap->va_size != VNOVAL) {
1622                 switch (vp->v_type) {
1623                 case VDIR:
1624                         return (EISDIR);
1625                 case VLNK:
1626                 case VREG:
1627                         if (vfs_isrdonly(mp))
1628                                 return (EROFS);
1629                         break;
1630                 default:
1631                         /*
1632                          * According to POSIX, the result is unspecified
1633                          * for file types other than regular files,
1634                          * directories and shared memory objects.  We
1635                          * don't support shared memory objects in the file
1636                          * system, and have dubious support for truncating
1637                          * symlinks.  Just ignore the request in other cases.
1638                          */
1639                         return (0);
1640                 }
1641                 /* Don't set accmode.  Permission to trunc is checked upstack */
1642         }
1643         if (vap->va_atime.tv_sec != VNOVAL || vap->va_mtime.tv_sec != VNOVAL) {
1644                 if (vap->va_vaflags & VA_UTIMES_NULL)
1645                         accmode |= VWRITE;
1646                 else
1647                         accmode |= VADMIN;
1648         }
1649         if (drop_suid) {
1650                 if (vap->va_mode != (mode_t)VNOVAL)
1651                         vap->va_mode &= ~(S_ISUID | S_ISGID);
1652                 else {
1653                         err = fuse_internal_getattr(vp, &old_va, cred, td);
1654                         if (err)
1655                                 return (err);
1656                         vap->va_mode = old_va.va_mode & ~(S_ISUID | S_ISGID);
1657                 }
1658         }
1659         if (vap->va_mode != (mode_t)VNOVAL) {
1660                 /* Only root may set the sticky bit on non-directories */
1661                 if (checkperm && vp->v_type != VDIR && (vap->va_mode & S_ISTXT)
1662                     && priv_check_cred(cred, PRIV_VFS_STICKYFILE))
1663                         return EFTYPE;
1664                 if (checkperm && (vap->va_mode & S_ISGID)) {
1665                         err = fuse_internal_getattr(vp, &old_va, cred, td);
1666                         if (err)
1667                                 return (err);
1668                         if (!groupmember(old_va.va_gid, cred)) {
1669                                 err = priv_check_cred(cred, PRIV_VFS_SETGID);
1670                                 if (err)
1671                                         return (err);
1672                         }
1673                 }
1674                 accmode |= VADMIN;
1675         }
1676
1677         if (vfs_isrdonly(mp))
1678                 return EROFS;
1679
1680         err = fuse_internal_access(vp, accmode, td, cred);
1681         if (err)
1682                 return err;
1683         else
1684                 return fuse_internal_setattr(vp, vap, td, cred);
1685 }
1686
1687 /*
1688     struct vnop_strategy_args {
1689         struct vnode *a_vp;
1690         struct buf *a_bp;
1691     };
1692 */
1693 static int
1694 fuse_vnop_strategy(struct vop_strategy_args *ap)
1695 {
1696         struct vnode *vp = ap->a_vp;
1697         struct buf *bp = ap->a_bp;
1698
1699         if (!vp || fuse_isdeadfs(vp)) {
1700                 bp->b_ioflags |= BIO_ERROR;
1701                 bp->b_error = ENXIO;
1702                 bufdone(bp);
1703                 return 0;
1704         }
1705
1706         /*
1707          * VOP_STRATEGY always returns zero and signals error via bp->b_ioflags.
1708          * fuse_io_strategy sets bp's error fields
1709          */
1710         (void)fuse_io_strategy(vp, bp);
1711
1712         return 0;
1713 }
1714
1715
1716 /*
1717     struct vnop_symlink_args {
1718         struct vnode *a_dvp;
1719         struct vnode **a_vpp;
1720         struct componentname *a_cnp;
1721         struct vattr *a_vap;
1722         char *a_target;
1723     };
1724 */
1725 static int
1726 fuse_vnop_symlink(struct vop_symlink_args *ap)
1727 {
1728         struct vnode *dvp = ap->a_dvp;
1729         struct vnode **vpp = ap->a_vpp;
1730         struct componentname *cnp = ap->a_cnp;
1731         const char *target = ap->a_target;
1732
1733         struct fuse_dispatcher fdi;
1734
1735         int err;
1736         size_t len;
1737
1738         if (fuse_isdeadfs(dvp)) {
1739                 return ENXIO;
1740         }
1741         /*
1742          * Unlike the other creator type calls, here we have to create a message
1743          * where the name of the new entry comes first, and the data describing
1744          * the entry comes second.
1745          * Hence we can't rely on our handy fuse_internal_newentry() routine,
1746          * but put together the message manually and just call the core part.
1747          */
1748
1749         len = strlen(target) + 1;
1750         fdisp_init(&fdi, len + cnp->cn_namelen + 1);
1751         fdisp_make_vp(&fdi, FUSE_SYMLINK, dvp, curthread, NULL);
1752
1753         memcpy(fdi.indata, cnp->cn_nameptr, cnp->cn_namelen);
1754         ((char *)fdi.indata)[cnp->cn_namelen] = '\0';
1755         memcpy((char *)fdi.indata + cnp->cn_namelen + 1, target, len);
1756
1757         err = fuse_internal_newentry_core(dvp, vpp, cnp, VLNK, &fdi);
1758         fdisp_destroy(&fdi);
1759         return err;
1760 }
1761
1762 /*
1763     struct vnop_write_args {
1764         struct vnode *a_vp;
1765         struct uio *a_uio;
1766         int  a_ioflag;
1767         struct ucred *a_cred;
1768     };
1769 */
1770 static int
1771 fuse_vnop_write(struct vop_write_args *ap)
1772 {
1773         struct vnode *vp = ap->a_vp;
1774         struct uio *uio = ap->a_uio;
1775         int ioflag = ap->a_ioflag;
1776         struct ucred *cred = ap->a_cred;
1777         pid_t pid = curthread->td_proc->p_pid;
1778
1779         if (fuse_isdeadfs(vp)) {
1780                 return ENXIO;
1781         }
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         attr_str = (char *)fdi.indata + sizeof(*list_xattr_in);
2337         snprintf(attr_str, len, "%s%c", prefix, extattr_namespace_separator);
2338
2339         err = fdisp_wait_answ(&fdi);
2340         if (err != 0)
2341                 goto out;
2342
2343         linux_list = fdi.answ;
2344         linux_list_len = fdi.iosize;
2345
2346         /*
2347          * Retrieve the BSD compatible list values.
2348          * The Linux / FUSE attribute list format isn't the same
2349          * as FreeBSD's format. So we need to transform it into
2350          * FreeBSD's format before giving it to the user.
2351          */
2352         bsd_list = malloc(linux_list_len, M_TEMP, M_WAITOK);
2353         err = fuse_xattrlist_convert(prefix, linux_list, linux_list_len,
2354             bsd_list, &bsd_list_len);
2355         if (err != 0)
2356                 goto out;
2357
2358         if (ap->a_size != NULL)
2359                 *ap->a_size = bsd_list_len;
2360
2361         if (uio != NULL)
2362                 err = uiomove(bsd_list, bsd_list_len, uio);
2363
2364 out:
2365         free(bsd_list, M_TEMP);
2366         fdisp_destroy(&fdi);
2367         return (err);
2368 }
2369
2370 /*
2371     struct vop_deleteextattr_args {
2372         struct vop_generic_args a_gen;
2373         struct vnode *a_vp;
2374         int a_attrnamespace;
2375         const char *a_name;
2376         struct ucred *a_cred;
2377         struct thread *a_td;
2378     };
2379 */
2380 static int
2381 fuse_vnop_deleteextattr(struct vop_deleteextattr_args *ap)
2382 {
2383         struct vnode *vp = ap->a_vp;
2384         struct fuse_dispatcher fdi;
2385         struct mount *mp = vnode_mount(vp);
2386         struct thread *td = ap->a_td;
2387         struct ucred *cred = ap->a_cred;
2388         char *prefix;
2389         size_t len;
2390         char *attr_str;
2391         int err;
2392
2393         if (fuse_isdeadfs(vp))
2394                 return (ENXIO);
2395
2396         if (!fsess_isimpl(mp, FUSE_REMOVEXATTR))
2397                 return EOPNOTSUPP;
2398
2399         if (vfs_isrdonly(mp))
2400                 return EROFS;
2401
2402         err = fuse_extattr_check_cred(vp, ap->a_attrnamespace, cred, td,
2403                 VWRITE);
2404         if (err)
2405                 return err;
2406
2407         /* Default to looking for user attributes. */
2408         if (ap->a_attrnamespace == EXTATTR_NAMESPACE_SYSTEM)
2409                 prefix = EXTATTR_NAMESPACE_SYSTEM_STRING;
2410         else
2411                 prefix = EXTATTR_NAMESPACE_USER_STRING;
2412
2413         len = strlen(prefix) + sizeof(extattr_namespace_separator) +
2414             strlen(ap->a_name) + 1;
2415
2416         fdisp_init(&fdi, len);
2417         fdisp_make_vp(&fdi, FUSE_REMOVEXATTR, vp, td, cred);
2418
2419         attr_str = fdi.indata;
2420         snprintf(attr_str, len, "%s%c%s", prefix, extattr_namespace_separator,
2421             ap->a_name);
2422
2423         err = fdisp_wait_answ(&fdi);
2424         if (err == ENOSYS) {
2425                 fsess_set_notimpl(mp, FUSE_REMOVEXATTR);
2426                 err = EOPNOTSUPP;
2427         }
2428
2429         fdisp_destroy(&fdi);
2430         return (err);
2431 }
2432
2433 /*
2434     struct vnop_print_args {
2435         struct vnode *a_vp;
2436     };
2437 */
2438 static int
2439 fuse_vnop_print(struct vop_print_args *ap)
2440 {
2441         struct fuse_vnode_data *fvdat = VTOFUD(ap->a_vp);
2442
2443         printf("nodeid: %ju, parent nodeid: %ju, nlookup: %ju, flag: %#x\n",
2444             (uintmax_t)VTOILLU(ap->a_vp), (uintmax_t)fvdat->parent_nid,
2445             (uintmax_t)fvdat->nlookup,
2446             fvdat->flag);
2447
2448         return 0;
2449 }