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