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