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