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