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