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