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