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