2 * SPDX-License-Identifier: BSD-3-Clause
4 * Copyright (c) 2007-2009 Google Inc. and Amit Singh
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions are
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
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.
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.
33 * Copyright (C) 2005 Csaba Henk.
34 * All rights reserved.
36 * Redistribution and use in source and binary forms, with or without
37 * modification, are permitted provided that the following conditions
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.
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
58 #include <sys/cdefs.h>
59 __FBSDID("$FreeBSD$");
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>
69 #include <sys/malloc.h>
70 #include <sys/queue.h>
71 #include <sys/limits.h>
73 #include <sys/rwlock.h>
76 #include <sys/mount.h>
77 #include <sys/vnode.h>
78 #include <sys/namei.h>
79 #include <sys/extattr.h>
81 #include <sys/unistd.h>
82 #include <sys/filedesc.h>
84 #include <sys/fcntl.h>
85 #include <sys/dirent.h>
88 #include <sys/sysctl.h>
89 #include <sys/vmmeter.h>
92 #include <vm/vm_extern.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>
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"
110 #include <sys/priv.h>
112 #define FUSE_DEBUG_MODULE VNOPS
113 #include "fuse_debug.h"
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;
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,
181 static u_long fuse_lookup_cache_hits = 0;
183 SYSCTL_ULONG(_vfs_fusefs, OID_AUTO, lookup_cache_hits, CTLFLAG_RD,
184 &fuse_lookup_cache_hits, 0, "");
186 static u_long fuse_lookup_cache_misses = 0;
188 SYSCTL_ULONG(_vfs_fusefs, OID_AUTO, lookup_cache_misses, CTLFLAG_RD,
189 &fuse_lookup_cache_misses, 0, "");
191 int fuse_lookup_cache_enable = 1;
193 SYSCTL_INT(_vfs_fusefs, OID_AUTO, lookup_cache_enable, CTLFLAG_RW,
194 &fuse_lookup_cache_enable, 0, "");
197 * XXX: This feature is highly experimental and can bring to instabilities,
198 * needs revisiting before to be enabled by default.
200 static int fuse_reclaim_revoked = 0;
202 SYSCTL_INT(_vfs_fusefs, OID_AUTO, reclaim_revoked, CTLFLAG_RW,
203 &fuse_reclaim_revoked, 0, "");
205 int fuse_pbuf_freecnt = -1;
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)
213 struct vnop_access_args {
215 #if VOP_ACCESS_TAKES_ACCMODE_T
220 struct ucred *a_cred;
225 fuse_vnop_access(struct vop_access_args *ap)
227 struct vnode *vp = ap->a_vp;
228 int accmode = ap->a_accmode;
229 struct ucred *cred = ap->a_cred;
231 struct fuse_access_param facp;
232 struct fuse_data *data = fuse_get_mpdata(vnode_mount(vp));
236 FS_DEBUG2G("inode=%ju\n", (uintmax_t)VTOI(vp));
238 if (fuse_isdeadfs(vp)) {
239 if (vnode_isvroot(vp)) {
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)) {
253 if (vnode_islnk(vp)) {
256 bzero(&facp, sizeof(facp));
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);
264 struct vnop_close_args {
267 struct ucred *a_cred;
272 fuse_vnop_close(struct vop_close_args *ap)
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;
279 fuse_trace_printf_vnop();
281 if (fuse_isdeadfs(vp)) {
284 if (vnode_isdir(vp)) {
285 if (fuse_filehandle_valid(vp, FUFH_RDONLY)) {
286 fuse_filehandle_close(vp, FUFH_RDONLY, NULL, cred);
290 if (fflag & IO_NDELAY) {
293 fufh_type = fuse_filehandle_xlate_from_fflags(fflag);
295 if (!fuse_filehandle_valid(vp, fufh_type)) {
298 for (i = 0; i < FUFH_MAXTYPE; i++)
299 if (fuse_filehandle_valid(vp, i))
301 if (i == FUFH_MAXTYPE)
302 panic("FUSE: fufh type %d found to be invalid in close"
306 if ((VTOFUD(vp)->flag & FN_SIZECHANGE) != 0) {
307 fuse_vnode_savesize(vp, cred);
313 struct vnop_create_args {
315 struct vnode **a_vpp;
316 struct componentname *a_cnp;
321 fuse_vnop_create(struct vop_create_args *ap)
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;
330 struct fuse_open_in *foi;
331 struct fuse_entry_out *feo;
332 struct fuse_dispatcher fdi;
333 struct fuse_dispatcher *fdip = &fdi;
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);
341 uint32_t x_open_flags;
343 fuse_trace_printf_vnop();
345 if (fuse_isdeadfs(dvp)) {
348 bzero(&fdi, sizeof(fdi));
350 /* XXX: Will we ever want devices ? */
351 if ((vap->va_type != VREG)) {
352 printf("fuse_vnop_create: unsupported va_type %d\n",
356 debug_printf("parent nid = %ju, mode = %x\n", (uintmax_t)parentnid,
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");
364 fdisp_make(fdip, FUSE_CREATE, vnode_mount(dvp), parentnid, td, cred);
368 foi->flags = O_CREAT | O_RDWR;
370 memcpy((char *)fdip->indata + sizeof(*foi), cnp->cn_nameptr,
372 ((char *)fdip->indata)[sizeof(*foi) + cnp->cn_namelen] = '\0';
374 err = fdisp_wait_answ(fdip);
378 fsess_set_notimpl(mp, FUSE_CREATE);
379 debug_printf("create: got err=%d from daemon\n", err);
385 if ((err = fuse_internal_checkentry(feo, VREG))) {
388 err = fuse_vnode_get(mp, feo->nodeid, dvp, vpp, cnp, VREG);
390 struct fuse_release_in *fri;
391 uint64_t nodeid = feo->nodeid;
392 uint64_t fh_id = ((struct fuse_open_out *)(feo + 1))->fh;
394 fdisp_init(fdip, sizeof(*fri));
395 fdisp_make(fdip, FUSE_RELEASE, mp, nodeid, td, cred);
398 fri->flags = OFLAGS(mode);
399 fuse_insert_callback(fdip->tick, fuse_internal_forget_callback);
400 fuse_insert_message(fdip->tick);
403 ASSERT_VOP_ELOCKED(*vpp, "fuse_vnop_create");
405 fdip->answ = feo + 1;
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);
419 * Our vnop_fsync roughly corresponds to the FUSE_FSYNC method. The Linux
420 * version of FUSE also has a FUSE_FLUSH method.
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.
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.
431 struct vnop_fsync_args {
432 struct vnodeop_desc *a_desc;
434 struct ucred * a_cred;
436 struct thread * a_td;
440 fuse_vnop_fsync(struct vop_fsync_args *ap)
442 struct vnode *vp = ap->a_vp;
443 struct thread *td = ap->a_td;
445 struct fuse_filehandle *fufh;
446 struct fuse_vnode_data *fvdat = VTOFUD(vp);
450 fuse_trace_printf_vnop();
452 if (fuse_isdeadfs(vp)) {
455 if ((err = vop_stdfsync(ap)))
458 if (!fsess_isimpl(vnode_mount(vp),
459 (vnode_vtype(vp) == VDIR ? FUSE_FSYNCDIR : FUSE_FSYNC))) {
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);
474 struct vnop_getattr_args {
477 struct ucred *a_cred;
482 fuse_vnop_getattr(struct vop_getattr_args *ap)
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);
492 struct fuse_dispatcher fdi;
494 FS_DEBUG2G("inode=%ju\n", (uintmax_t)VTOI(vp));
496 dataflags = fuse_get_mpdata(vnode_mount(vp))->dataflags;
498 /* Note that we are not bailing out on a dead file system just yet. */
500 if (!(dataflags & FSESS_INITED)) {
501 if (!vnode_isvroot(vp)) {
502 fdata_set_dead(fuse_get_mpdata(vnode_mount(vp)));
504 debug_printf("fuse_getattr b: returning ENOTCONN\n");
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() */
518 fuse_internal_vnode_disappear(vp);
522 cache_attrs(vp, (struct fuse_attr_out *)fdi.answ);
523 if (vap != VTOVA(vp)) {
524 memcpy(vap, VTOVA(vp), sizeof(*vap));
526 if (vap->va_type != vnode_vtype(vp)) {
527 fuse_internal_vnode_disappear(vp);
531 if ((fvdat->flag & FN_SIZECHANGE) != 0)
532 vap->va_size = fvdat->filesize;
534 if (vnode_isreg(vp) && (fvdat->flag & FN_SIZECHANGE) == 0) {
536 * This is for those cases when the file size changed without us
537 * knowing, and we want to catch up.
539 off_t new_filesize = ((struct fuse_attr_out *)
540 fdi.answ)->attr.size;
542 if (fvdat->filesize != new_filesize) {
543 fuse_vnode_setsize(vp, new_filesize);
546 debug_printf("fuse_getattr e: returning 0\n");
553 bzero(vap, sizeof(*vap));
554 vap->va_type = vnode_vtype(vp);
560 struct vnop_inactive_args {
566 fuse_vnop_inactive(struct vop_inactive_args *ap)
568 struct vnode *vp = ap->a_vp;
569 struct thread *td = ap->a_td;
571 struct fuse_vnode_data *fvdat = VTOFUD(vp);
572 struct fuse_filehandle *fufh = NULL;
574 int type, need_flush = 1;
576 FS_DEBUG("inode=%ju\n", (uintmax_t)VTOI(vp));
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);
585 if (fuse_data_cache_invalidate ||
586 (fvdat->flag & FN_REVOKED) != 0)
587 fuse_io_invalbuf(vp, td);
589 fuse_io_flushbuf(vp, MNT_WAIT, td);
592 fuse_filehandle_close(vp, type, td, NULL);
596 if ((fvdat->flag & FN_REVOKED) != 0 && fuse_reclaim_revoked) {
603 struct vnop_link_args {
604 struct vnode *a_tdvp;
606 struct componentname *a_cnp;
610 fuse_vnop_link(struct vop_link_args *ap)
612 struct vnode *vp = ap->a_vp;
613 struct vnode *tdvp = ap->a_tdvp;
614 struct componentname *cnp = ap->a_cnp;
616 struct vattr *vap = VTOVA(vp);
618 struct fuse_dispatcher fdi;
619 struct fuse_entry_out *feo;
620 struct fuse_link_in fli;
624 fuse_trace_printf_vnop();
626 if (fuse_isdeadfs(vp)) {
629 if (vnode_mount(tdvp) != vnode_mount(vp)) {
632 if (vap->va_nlink >= FUSE_LINK_MAX) {
635 fli.oldnodeid = VTOI(vp);
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))) {
645 err = fuse_internal_checkentry(feo, vnode_vtype(vp));
652 struct vnop_lookup_args {
653 struct vnodeop_desc *a_desc;
655 struct vnode **a_vpp;
656 struct componentname *a_cnp;
660 fuse_vnop_lookup(struct vop_lookup_args *ap)
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;
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);
676 struct vnode *vp = NULL;
678 struct fuse_dispatcher fdi;
682 struct fuse_access_param facp;
684 FS_DEBUG2G("parent_inode=%ju - %*s\n",
685 (uintmax_t)VTOI(dvp), (int)cnp->cn_namelen, cnp->cn_nameptr);
687 if (fuse_isdeadfs(dvp)) {
691 if (!vnode_isdir(dvp)) {
694 if (islastcn && vfs_isrdonly(mp) && (nameiop != LOOKUP)) {
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.
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))) {
710 if (flags & ISDOTDOT) {
711 nid = VTOFUD(dvp)->parent_nid;
718 } else if (cnp->cn_namelen == 1 && *(cnp->cn_nameptr) == '.') {
723 } else if (fuse_lookup_cache_enable) {
724 err = cache_lookup(dvp, vpp, cnp, NULL, NULL);
727 case -1: /* positive match */
728 atomic_add_acq_long(&fuse_lookup_cache_hits, 1);
731 case 0: /* no match in cache */
732 atomic_add_acq_long(&fuse_lookup_cache_misses, 1);
735 case ENOENT: /* negative match */
742 fdisp_init(&fdi, cnp->cn_namelen + 1);
746 fdisp_make(&fdi, op, mp, nid, td, cred);
748 if (op == FUSE_LOOKUP) {
749 memcpy(fdi.indata, cnp->cn_nameptr, cnp->cn_namelen);
750 ((char *)fdi.indata)[cnp->cn_namelen] = '\0';
752 lookup_err = fdisp_wait_answ(&fdi);
754 if ((op == FUSE_LOOKUP) && !lookup_err) { /* lookup call succeeded */
755 nid = ((struct fuse_entry_out *)fdi.answ)->nodeid;
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)
763 } else if (nid == FUSE_ROOT_ID) {
768 (!fdi.answ_stat || lookup_err != ENOENT || op != FUSE_LOOKUP)) {
772 /* lookup_err, if non-zero, must be ENOENT at this point */
776 if ((nameiop == CREATE || nameiop == RENAME) && islastcn
777 /* && directory dvp has not been removed */ ) {
779 if (vfs_isrdonly(mp)) {
783 #if 0 /* THINK_ABOUT_THIS */
784 if ((err = fuse_internal_access(dvp, VWRITE, cred, td, &facp))) {
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.
796 cnp->cn_flags |= SAVENAME;
801 /* Consider inserting name into cache. */
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
813 if ((cnp->cn_flags & MAKEENTRY) != 0) {
814 FS_DEBUG("inserting NULL into cache\n");
815 cache_enter(dvp, NULL, cnp);
825 struct fuse_entry_out *feo = NULL;
826 struct fuse_attr *fattr = NULL;
828 if (op == FUSE_GETATTR) {
829 fattr = &((struct fuse_attr_out *)fdi.answ)->attr;
831 feo = (struct fuse_entry_out *)fdi.answ;
832 fattr = &(feo->attr);
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 ".".
841 if (nameiop == DELETE && islastcn) {
843 * Check for write access on directory.
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;
853 if (nid == VTOI(dvp)) {
857 err = fuse_vnode_get(dvp->v_mount, nid, dvp,
858 &vp, cnp, IFTOVT(fattr->mode));
865 * Save the name for use in VOP_RMDIR and VOP_REMOVE
868 cnp->cn_flags |= SAVENAME;
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.
878 if (nameiop == RENAME && wantparent && islastcn) {
880 #if 0 /* THINK_ABOUT_THIS */
881 if ((err = fuse_internal_access(dvp, VWRITE, cred, td, &facp))) {
889 if (nid == VTOI(dvp)) {
893 err = fuse_vnode_get(vnode_mount(dvp),
898 IFTOVT(fattr->mode));
904 * Save the name for use in VOP_RENAME later.
906 cnp->cn_flags |= SAVENAME;
910 if (flags & ISDOTDOT) {
915 * Expanded copy of vn_vget_ino() so that
916 * fuse_vnode_get() can be used.
919 ltype = VOP_ISLOCKED(dvp);
920 err = vfs_busy(mp, MBF_NOWAIT);
924 err = vfs_busy(mp, 0);
925 vn_lock(dvp, ltype | LK_RETRY);
929 if ((dvp->v_iflag & VI_DOOMED) != 0) {
936 err = fuse_vnode_get(vnode_mount(dvp),
941 IFTOVT(fattr->mode));
943 vn_lock(dvp, ltype | LK_RETRY);
944 if ((dvp->v_iflag & VI_DOOMED) != 0) {
952 } else if (nid == VTOI(dvp)) {
956 err = fuse_vnode_get(vnode_mount(dvp),
961 IFTOVT(fattr->mode));
965 fuse_vnode_setparent(vp, dvp);
969 if (op == FUSE_GETATTR) {
970 cache_attrs(*vpp, (struct fuse_attr_out *)fdi.answ);
972 cache_attrs(*vpp, (struct fuse_entry_out *)fdi.answ);
975 /* Insert name into cache if appropriate. */
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.
990 * So just leave the name cache alone.
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.
999 if (cnp->cn_flags & MAKEENTRY) {
1000 cache_enter(dvp, *vpp, cnp);
1007 /* No lookup error; need to clean up. */
1009 if (err) { /* Found inode; exit with no vnode. */
1010 if (op == FUSE_LOOKUP) {
1011 fuse_internal_forget_send(vnode_mount(dvp), td, cred,
1014 fdisp_destroy(&fdi);
1017 #ifndef NO_EARLY_PERM_CHECK_HACK
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
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.
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...
1050 int tmpvtype = vnode_vtype(*vpp);
1052 bzero(&facp, sizeof(facp));
1053 /*the early perm check hack */
1054 facp.facc_flags |= FACCESS_VA_VALID;
1056 if ((tmpvtype != VDIR) && (tmpvtype != VLNK)) {
1059 if (!err && !vnode_mountedhere(*vpp)) {
1060 err = fuse_internal_access(*vpp, VEXEC, &facp, td, cred);
1063 if (tmpvtype == VLNK)
1064 FS_DEBUG("weird, permission error with a symlink?\n");
1072 fdisp_destroy(&fdi);
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;
1086 fuse_vnop_mkdir(struct vop_mkdir_args *ap)
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;
1093 struct fuse_mkdir_in fmdi;
1095 fuse_trace_printf_vnop();
1097 if (fuse_isdeadfs(dvp)) {
1100 fmdi.mode = MAKEIMODE(vap->va_type, vap->va_mode);
1102 return (fuse_internal_newentry(dvp, vpp, cnp, FUSE_MKDIR, &fmdi,
1103 sizeof(fmdi), VDIR));
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;
1115 fuse_vnop_mknod(struct vop_mknod_args *ap)
1123 struct vnop_open_args {
1126 struct ucred *a_cred;
1127 struct thread *a_td;
1128 int a_fdidx; / struct file *a_fp;
1132 fuse_vnop_open(struct vop_open_args *ap)
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;
1139 fufh_type_t fufh_type;
1140 struct fuse_vnode_data *fvdat;
1142 int error, isdir = 0;
1143 int32_t fuse_open_flags;
1145 FS_DEBUG2G("inode=%ju mode=0x%x\n", (uintmax_t)VTOI(vp), mode);
1147 if (fuse_isdeadfs(vp)) {
1152 if (vnode_isdir(vp)) {
1155 fuse_open_flags = 0;
1157 fufh_type = FUFH_RDONLY;
1159 fufh_type = fuse_filehandle_xlate_from_fflags(mode);
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.
1166 if (fufh_type == FUFH_WRONLY ||
1167 (fvdat->flag & FN_DIRECTIO) != 0)
1168 fuse_open_flags = FOPEN_DIRECT_IO;
1171 if (fuse_filehandle_validrw(vp, fufh_type) != FUFH_INVALID) {
1172 fuse_vnode_open(vp, fuse_open_flags, td);
1175 error = fuse_filehandle_open(vp, fufh_type, NULL, td, cred);
1181 fuse_vnop_pathconf(struct vop_pathconf_args *ap)
1184 switch (ap->a_name) {
1185 case _PC_FILESIZEBITS:
1189 *ap->a_retval = NAME_MAX;
1192 *ap->a_retval = MIN(LONG_MAX, FUSE_LINK_MAX);
1194 case _PC_SYMLINK_MAX:
1195 *ap->a_retval = MAXPATHLEN;
1201 return (vop_stdpathconf(ap));
1206 struct vnop_read_args {
1210 struct ucred *a_cred;
1214 fuse_vnop_read(struct vop_read_args *ap)
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;
1221 FS_DEBUG2G("inode=%ju offset=%jd resid=%zd\n",
1222 (uintmax_t)VTOI(vp), uio->uio_offset, uio->uio_resid);
1224 if (fuse_isdeadfs(vp)) {
1228 if (VTOFUD(vp)->flag & FN_DIRECTIO) {
1229 ioflag |= IO_DIRECT;
1232 return fuse_io_dispatch(vp, uio, ioflag, cred);
1236 struct vnop_readdir_args {
1239 struct ucred *a_cred;
1246 fuse_vnop_readdir(struct vop_readdir_args *ap)
1248 struct vnode *vp = ap->a_vp;
1249 struct uio *uio = ap->a_uio;
1250 struct ucred *cred = ap->a_cred;
1252 struct fuse_filehandle *fufh = NULL;
1253 struct fuse_iov cookediov;
1258 FS_DEBUG2G("inode=%ju\n", (uintmax_t)VTOI(vp));
1260 if (fuse_isdeadfs(vp)) {
1263 if ( /* XXXIP ((uio_iovcnt(uio) > 1)) || */
1264 (uio_resid(uio) < sizeof(struct dirent))) {
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);
1273 err = fuse_filehandle_get(vp, FUFH_RDONLY, &fufh);
1278 #define DIRCOOKEDSIZE FUSE_DIRENT_ALIGN(FUSE_NAME_OFFSET + MAXNAMLEN + 1)
1279 fiov_init(&cookediov, DIRCOOKEDSIZE);
1281 err = fuse_internal_readdir(vp, uio, fufh, &cookediov);
1283 fiov_teardown(&cookediov);
1285 fuse_filehandle_close(vp, FUFH_RDONLY, NULL, cred);
1291 struct vnop_readlink_args {
1294 struct ucred *a_cred;
1298 fuse_vnop_readlink(struct vop_readlink_args *ap)
1300 struct vnode *vp = ap->a_vp;
1301 struct uio *uio = ap->a_uio;
1302 struct ucred *cred = ap->a_cred;
1304 struct fuse_dispatcher fdi;
1307 FS_DEBUG2G("inode=%ju\n", (uintmax_t)VTOI(vp));
1309 if (fuse_isdeadfs(vp)) {
1312 if (!vnode_islnk(vp)) {
1315 fdisp_init(&fdi, 0);
1316 err = fdisp_simple_putget_vp(&fdi, FUSE_READLINK, vp, curthread, cred);
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;
1324 err = uiomove(mpth, strlen(mpth), uio);
1327 err = uiomove(fdi.answ, fdi.iosize, uio);
1330 fdisp_destroy(&fdi);
1335 struct vnop_reclaim_args {
1337 struct thread *a_td;
1341 fuse_vnop_reclaim(struct vop_reclaim_args *ap)
1343 struct vnode *vp = ap->a_vp;
1344 struct thread *td = ap->a_td;
1346 struct fuse_vnode_data *fvdat = VTOFUD(vp);
1347 struct fuse_filehandle *fufh = NULL;
1352 panic("FUSE: no vnode data during recycling");
1354 FS_DEBUG("inode=%ju\n", (uintmax_t)VTOI(vp));
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",
1361 fuse_filehandle_close(vp, type, td, NULL);
1365 if ((!fuse_isdeadfs(vp)) && (fvdat->nlookup)) {
1366 fuse_internal_forget_send(vnode_mount(vp), td, NULL, VTOI(vp),
1369 fuse_vnode_setparent(vp, NULL);
1371 vfs_hash_remove(vp);
1372 vnode_destroy_vobject(vp);
1373 fuse_vnode_destroy(vp);
1379 struct vnop_remove_args {
1380 struct vnode *a_dvp;
1382 struct componentname *a_cnp;
1386 fuse_vnop_remove(struct vop_remove_args *ap)
1388 struct vnode *dvp = ap->a_dvp;
1389 struct vnode *vp = ap->a_vp;
1390 struct componentname *cnp = ap->a_cnp;
1394 FS_DEBUG2G("inode=%ju name=%*s\n",
1395 (uintmax_t)VTOI(vp), (int)cnp->cn_namelen, cnp->cn_nameptr);
1397 if (fuse_isdeadfs(vp)) {
1400 if (vnode_isdir(vp)) {
1405 err = fuse_internal_remove(dvp, vp, cnp, FUSE_UNLINK);
1408 fuse_internal_vnode_disappear(vp);
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;
1423 fuse_vnop_rename(struct vop_rename_args *ap)
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;
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);
1440 if (fuse_isdeadfs(fdvp)) {
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)"));
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.
1457 data = fuse_get_mpdata(vnode_mount(tdvp));
1458 sx_xlock(&data->rename_lock);
1459 err = fuse_internal_rename(fdvp, fcnp, tdvp, tcnp);
1462 fuse_vnode_setparent(fvp, tdvp);
1464 fuse_vnode_setparent(tvp, NULL);
1466 sx_unlock(&data->rename_lock);
1468 if (tvp != NULL && tvp != fvp) {
1471 if (vnode_isdir(fvp)) {
1472 if ((tvp != NULL) && vnode_isdir(tvp)) {
1493 struct vnop_rmdir_args {
1494 struct vnode *a_dvp;
1496 struct componentname *a_cnp;
1500 fuse_vnop_rmdir(struct vop_rmdir_args *ap)
1502 struct vnode *dvp = ap->a_dvp;
1503 struct vnode *vp = ap->a_vp;
1507 FS_DEBUG2G("inode=%ju\n", (uintmax_t)VTOI(vp));
1509 if (fuse_isdeadfs(vp)) {
1512 if (VTOFUD(vp) == VTOFUD(dvp)) {
1515 err = fuse_internal_remove(dvp, vp, ap->a_cnp, FUSE_RMDIR);
1518 fuse_internal_vnode_disappear(vp);
1523 struct vnop_setattr_args {
1525 struct vattr *a_vap;
1526 struct ucred *a_cred;
1527 struct thread *a_td;
1531 fuse_vnop_setattr(struct vop_setattr_args *ap)
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;
1538 struct fuse_dispatcher fdi;
1539 struct fuse_setattr_in *fsai;
1540 struct fuse_access_param facp;
1544 int sizechanged = 0;
1545 uint64_t newsize = 0;
1547 FS_DEBUG2G("inode=%ju\n", (uintmax_t)VTOI(vp));
1549 if (fuse_isdeadfs(vp)) {
1552 fdisp_init(&fdi, sizeof(*fsai));
1553 fdisp_make_vp(&fdi, FUSE_SETATTR, vp, td, cred);
1557 bzero(&facp, sizeof(facp));
1559 facp.xuid = vap->va_uid;
1560 facp.xgid = vap->va_gid;
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;
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;
1572 if (vap->va_size != VNOVAL) {
1574 struct fuse_filehandle *fufh = NULL;
1576 /*Truncate to a new value. */
1577 fsai->size = vap->va_size;
1579 newsize = vap->va_size;
1580 fsai->valid |= FATTR_SIZE;
1582 fuse_filehandle_getrw(vp, FUFH_WRONLY, &fufh);
1584 fsai->fh = fufh->fh_id;
1585 fsai->valid |= FATTR_FH;
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;
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;
1598 if (vap->va_mode != (mode_t)VNOVAL) {
1599 fsai->mode = vap->va_mode & ALLPERMS;
1600 fsai->valid |= FATTR_MODE;
1605 vtyp = vnode_vtype(vp);
1607 if (fsai->valid & FATTR_SIZE && vtyp == VDIR) {
1611 if (vfs_isrdonly(vnode_mount(vp)) && (fsai->valid & ~FATTR_SIZE || vtyp == VREG)) {
1615 if (fsai->valid & ~FATTR_SIZE) {
1616 /*err = fuse_internal_access(vp, VADMIN, context, &facp); */
1620 facp.facc_flags &= ~FACCESS_XQUERIES;
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);
1628 if ((err = fdisp_wait_answ(&fdi)))
1630 vtyp = IFTOVT(((struct fuse_attr_out *)fdi.answ)->attr.mode);
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");
1637 * STALE vnode, ditch
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.
1643 fuse_internal_vnode_disappear(vp);
1647 if (!err && !sizechanged) {
1648 cache_attrs(vp, (struct fuse_attr_out *)fdi.answ);
1651 fdisp_destroy(&fdi);
1652 if (!err && sizechanged) {
1653 fuse_vnode_setsize(vp, newsize);
1654 VTOFUD(vp)->flag &= ~FN_SIZECHANGE;
1660 struct vnop_strategy_args {
1666 fuse_vnop_strategy(struct vop_strategy_args *ap)
1668 struct vnode *vp = ap->a_vp;
1669 struct buf *bp = ap->a_bp;
1671 fuse_trace_printf_vnop();
1673 if (!vp || fuse_isdeadfs(vp)) {
1674 bp->b_ioflags |= BIO_ERROR;
1675 bp->b_error = ENXIO;
1679 if (bp->b_iocmd == BIO_WRITE)
1680 fuse_vnode_refreshsize(vp, NOCRED);
1682 (void)fuse_io_strategy(vp, bp);
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.
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;
1707 fuse_vnop_symlink(struct vop_symlink_args *ap)
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;
1714 struct fuse_dispatcher fdi;
1719 FS_DEBUG2G("inode=%ju name=%*s\n",
1720 (uintmax_t)VTOI(dvp), (int)cnp->cn_namelen, cnp->cn_nameptr);
1722 if (fuse_isdeadfs(dvp)) {
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.
1733 len = strlen(target) + 1;
1734 fdisp_init(&fdi, len + cnp->cn_namelen + 1);
1735 fdisp_make_vp(&fdi, FUSE_SYMLINK, dvp, curthread, NULL);
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);
1741 err = fuse_internal_newentry_core(dvp, vpp, cnp, VLNK, &fdi);
1742 fdisp_destroy(&fdi);
1747 struct vnop_write_args {
1751 struct ucred *a_cred;
1755 fuse_vnop_write(struct vop_write_args *ap)
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;
1762 fuse_trace_printf_vnop();
1764 if (fuse_isdeadfs(vp)) {
1767 fuse_vnode_refreshsize(vp, cred);
1769 if (VTOFUD(vp)->flag & FN_DIRECTIO) {
1770 ioflag |= IO_DIRECT;
1773 return fuse_io_dispatch(vp, uio, ioflag, cred);
1777 struct vnop_getpages_args {
1785 fuse_vnop_getpages(struct vop_getpages_args *ap)
1787 int i, error, nextoff, size, toff, count, npages;
1797 FS_DEBUG2G("heh\n");
1800 KASSERT(vp->v_object, ("objectless vp passed to getpages"));
1801 td = curthread; /* XXX */
1802 cred = curthread->td_ucred; /* XXX */
1804 npages = ap->a_count;
1806 if (!fsess_opt_mmap(vnode_mount(vp))) {
1807 FS_DEBUG("called on non-cacheable vnode??\n");
1808 return (VM_PAGER_ERROR);
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.
1816 * XXXGL: is that true for FUSE, which is a local filesystem,
1817 * but still somewhat disconnected from the kernel?
1819 VM_OBJECT_WLOCK(vp->v_object);
1820 if (pages[npages - 1]->valid != 0 && --npages == 0)
1822 VM_OBJECT_WUNLOCK(vp->v_object);
1825 * We use only the kva address for the buffer, but this is extremely
1826 * convenient and fast.
1828 bp = getpbuf(&fuse_pbuf_freecnt);
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);
1835 count = npages << PAGE_SHIFT;
1836 iov.iov_base = (caddr_t)kva;
1837 iov.iov_len = count;
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;
1846 error = fuse_io_dispatch(vp, &uio, IO_DIRECT, cred);
1847 pmap_qremove(kva, npages);
1849 relpbuf(bp, &fuse_pbuf_freecnt);
1851 if (error && (uio.uio_resid == count)) {
1852 FS_DEBUG("error %d\n", error);
1853 return VM_PAGER_ERROR;
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!
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) {
1867 nextoff = toff + PAGE_SIZE;
1870 if (nextoff <= size) {
1872 * Read operation filled an entire page
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) {
1879 * Read operation filled a partial page.
1882 vm_page_set_valid_range(m, 0, size - toff);
1883 KASSERT(m->dirty == 0,
1884 ("fuse_getpages: page %p is dirty", m));
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.
1894 fuse_vm_page_unlock_queues();
1896 VM_OBJECT_WUNLOCK(vp->v_object);
1901 return (VM_PAGER_OK);
1905 struct vnop_putpages_args {
1911 vm_ooffset_t a_offset;
1915 fuse_vnop_putpages(struct vop_putpages_args *ap)
1921 int i, error, npages, count;
1930 FS_DEBUG2G("heh\n");
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 */
1938 count = ap->a_count;
1939 rtvals = ap->a_rtvals;
1940 npages = btoc(count);
1941 offset = IDX_TO_OFF(pages[0]->pindex);
1943 if (!fsess_opt_mmap(vnode_mount(vp))) {
1944 FS_DEBUG("called on non-cacheable vnode??\n");
1946 for (i = 0; i < npages; i++)
1947 rtvals[i] = VM_PAGER_AGAIN;
1950 * When putting pages, do not extend file past EOF.
1953 if (offset + count > fsize) {
1954 count = fsize - offset;
1959 * We use only the kva address for the buffer, but this is extremely
1960 * convenient and fast.
1962 bp = getpbuf(&fuse_pbuf_freecnt);
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);
1969 iov.iov_base = (caddr_t)kva;
1970 iov.iov_len = count;
1973 uio.uio_offset = offset;
1974 uio.uio_resid = count;
1975 uio.uio_segflg = UIO_SYSSPACE;
1976 uio.uio_rw = UIO_WRITE;
1979 error = fuse_io_dispatch(vp, &uio, IO_DIRECT, cred);
1981 pmap_qremove(kva, npages);
1982 relpbuf(bp, &fuse_pbuf_freecnt);
1985 int nwritten = round_page(count - uio.uio_resid) / PAGE_SIZE;
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);
1997 static const char extattr_namespace_separator = '.';
2000 struct vop_getextattr_args {
2001 struct vop_generic_args a_gen;
2003 int a_attrnamespace;
2007 struct ucred *a_cred;
2008 struct thread *a_td;
2012 fuse_vnop_getextattr(struct vop_getextattr_args *ap)
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;
2027 fuse_trace_printf_vnop();
2029 if (fuse_isdeadfs(vp))
2032 /* Default to looking for user attributes. */
2033 if (ap->a_attrnamespace == EXTATTR_NAMESPACE_SYSTEM)
2034 prefix = EXTATTR_NAMESPACE_SYSTEM_STRING;
2036 prefix = EXTATTR_NAMESPACE_USER_STRING;
2038 len = strlen(prefix) + sizeof(extattr_namespace_separator) +
2039 strlen(ap->a_name) + 1;
2041 fdisp_init(&fdi, len + sizeof(*get_xattr_in));
2042 fdisp_make_vp(&fdi, FUSE_GETXATTR, vp, td, cred);
2044 get_xattr_in = fdi.indata;
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.
2052 get_xattr_in->size = 0;
2054 get_xattr_in->size = uio->uio_resid;
2056 attr_str = (char *)fdi.indata + sizeof(*get_xattr_in);
2057 snprintf(attr_str, len, "%s%c%s", prefix, extattr_namespace_separator,
2060 err = fdisp_wait_answ(&fdi);
2063 fsess_set_notimpl(mp, FUSE_GETXATTR);
2064 debug_printf("getxattr: got err=%d from daemon\n", err);
2068 get_xattr_out = fdi.answ;
2070 if (ap->a_size != NULL)
2071 *ap->a_size = get_xattr_out->size;
2074 err = uiomove(fdi.answ, fdi.iosize, uio);
2077 fdisp_destroy(&fdi);
2082 struct vop_setextattr_args {
2083 struct vop_generic_args a_gen;
2085 int a_attrnamespace;
2088 struct ucred *a_cred;
2089 struct thread *a_td;
2093 fuse_vnop_setextattr(struct vop_setextattr_args *ap)
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;
2107 fuse_trace_printf_vnop();
2109 if (fuse_isdeadfs(vp))
2112 /* Default to looking for user attributes. */
2113 if (ap->a_attrnamespace == EXTATTR_NAMESPACE_SYSTEM)
2114 prefix = EXTATTR_NAMESPACE_SYSTEM_STRING;
2116 prefix = EXTATTR_NAMESPACE_USER_STRING;
2118 len = strlen(prefix) + sizeof(extattr_namespace_separator) +
2119 strlen(ap->a_name) + 1;
2121 fdisp_init(&fdi, len + sizeof(*set_xattr_in) + uio->uio_resid);
2122 fdisp_make_vp(&fdi, FUSE_SETXATTR, vp, td, cred);
2124 set_xattr_in = fdi.indata;
2125 set_xattr_in->size = uio->uio_resid;
2127 attr_str = (char *)fdi.indata + sizeof(*set_xattr_in);
2128 snprintf(attr_str, len, "%s%c%s", prefix, extattr_namespace_separator,
2131 err = uiomove((char *)fdi.indata + sizeof(*set_xattr_in) + len,
2132 uio->uio_resid, uio);
2134 debug_printf("setxattr: got error %d from uiomove\n", err);
2138 err = fdisp_wait_answ(&fdi);
2142 fsess_set_notimpl(mp, FUSE_SETXATTR);
2143 debug_printf("setxattr: got err=%d from daemon\n", err);
2148 fdisp_destroy(&fdi);
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:
2161 * user.attr_name1\0user.attr_name2\0
2165 * <num>attr_name1<num>attr_name2
2167 * Where "<num>" is a single byte number of characters in the attribute name.
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
2175 fuse_xattrlist_convert(char *prefix, const char *list, int list_len,
2176 char *bsd_list, int *bsd_list_len)
2178 int len, pos, dist_to_next, prefix_len;
2182 prefix_len = strlen(prefix);
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);
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);
2198 *bsd_list_len += len + 1;
2201 pos += dist_to_next;
2208 struct vop_listextattr_args {
2209 struct vop_generic_args a_gen;
2211 int a_attrnamespace;
2214 struct ucred *a_cred;
2215 struct thread *a_td;
2219 fuse_vnop_listextattr(struct vop_listextattr_args *ap)
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;
2232 char *bsd_list = NULL;
2238 fuse_trace_printf_vnop();
2240 if (fuse_isdeadfs(vp))
2244 * Add space for a NUL and the period separator if enabled.
2245 * Default to looking for user attributes.
2247 if (ap->a_attrnamespace == EXTATTR_NAMESPACE_SYSTEM)
2248 prefix = EXTATTR_NAMESPACE_SYSTEM_STRING;
2250 prefix = EXTATTR_NAMESPACE_USER_STRING;
2252 len = strlen(prefix) + sizeof(extattr_namespace_separator) + 1;
2254 fdisp_init(&fdi, sizeof(*list_xattr_in) + len);
2255 fdisp_make_vp(&fdi, FUSE_LISTXATTR, vp, td, cred);
2258 * Retrieve Linux / FUSE compatible list size.
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);
2265 err = fdisp_wait_answ(&fdi);
2268 fsess_set_notimpl(mp, FUSE_LISTXATTR);
2269 debug_printf("listextattr: got err=%d from daemon\n", err);
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;
2282 * Retrieve Linux / FUSE compatible list values.
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);
2290 err = fdisp_wait_answ(&fdi);
2294 linux_list = fdi.answ;
2295 linux_list_len = fdi.iosize;
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.
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);
2309 if (ap->a_size != NULL)
2310 *ap->a_size = bsd_list_len;
2313 err = uiomove(bsd_list, bsd_list_len, uio);
2316 free(bsd_list, M_TEMP);
2317 fdisp_destroy(&fdi);
2322 struct vop_deleteextattr_args {
2323 struct vop_generic_args a_gen;
2325 int a_attrnamespace;
2327 struct ucred *a_cred;
2328 struct thread *a_td;
2332 fuse_vnop_deleteextattr(struct vop_deleteextattr_args *ap)
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;
2344 fuse_trace_printf_vnop();
2346 if (fuse_isdeadfs(vp))
2349 /* Default to looking for user attributes. */
2350 if (ap->a_attrnamespace == EXTATTR_NAMESPACE_SYSTEM)
2351 prefix = EXTATTR_NAMESPACE_SYSTEM_STRING;
2353 prefix = EXTATTR_NAMESPACE_USER_STRING;
2355 len = strlen(prefix) + sizeof(extattr_namespace_separator) +
2356 strlen(ap->a_name) + 1;
2358 fdisp_init(&fdi, len);
2359 fdisp_make_vp(&fdi, FUSE_REMOVEXATTR, vp, td, cred);
2361 attr_str = fdi.indata;
2362 snprintf(attr_str, len, "%s%c%s", prefix, extattr_namespace_separator,
2365 err = fdisp_wait_answ(&fdi);
2368 fsess_set_notimpl(mp, FUSE_REMOVEXATTR);
2369 debug_printf("removexattr: got err=%d from daemon\n", err);
2372 fdisp_destroy(&fdi);
2377 struct vnop_print_args {
2382 fuse_vnop_print(struct vop_print_args *ap)
2384 struct fuse_vnode_data *fvdat = VTOFUD(ap->a_vp);
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,