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