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