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