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