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