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