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