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