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>
72 #include <sys/rwlock.h>
75 #include <sys/mount.h>
76 #include <sys/vnode.h>
77 #include <sys/namei.h>
78 #include <sys/extattr.h>
80 #include <sys/unistd.h>
81 #include <sys/filedesc.h>
83 #include <sys/fcntl.h>
84 #include <sys/dirent.h>
87 #include <sys/sysctl.h>
88 #include <sys/vmmeter.h>
91 #include <vm/vm_extern.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>
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"
109 #include <sys/priv.h>
111 #define FUSE_DEBUG_MODULE VNOPS
112 #include "fuse_debug.h"
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;
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,
180 static u_long fuse_lookup_cache_hits = 0;
182 SYSCTL_ULONG(_vfs_fuse, OID_AUTO, lookup_cache_hits, CTLFLAG_RD,
183 &fuse_lookup_cache_hits, 0, "");
185 static u_long fuse_lookup_cache_misses = 0;
187 SYSCTL_ULONG(_vfs_fuse, OID_AUTO, lookup_cache_misses, CTLFLAG_RD,
188 &fuse_lookup_cache_misses, 0, "");
190 int fuse_lookup_cache_enable = 1;
192 SYSCTL_INT(_vfs_fuse, OID_AUTO, lookup_cache_enable, CTLFLAG_RW,
193 &fuse_lookup_cache_enable, 0, "");
196 * XXX: This feature is highly experimental and can bring to instabilities,
197 * needs revisiting before to be enabled by default.
199 static int fuse_reclaim_revoked = 0;
201 SYSCTL_INT(_vfs_fuse, OID_AUTO, reclaim_revoked, CTLFLAG_RW,
202 &fuse_reclaim_revoked, 0, "");
204 int fuse_pbuf_freecnt = -1;
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)
212 struct vnop_access_args {
214 #if VOP_ACCESS_TAKES_ACCMODE_T
219 struct ucred *a_cred;
224 fuse_vnop_access(struct vop_access_args *ap)
226 struct vnode *vp = ap->a_vp;
227 int accmode = ap->a_accmode;
228 struct ucred *cred = ap->a_cred;
230 struct fuse_access_param facp;
231 struct fuse_data *data = fuse_get_mpdata(vnode_mount(vp));
235 FS_DEBUG2G("inode=%ju\n", (uintmax_t)VTOI(vp));
237 if (fuse_isdeadfs(vp)) {
238 if (vnode_isvroot(vp)) {
243 if (!(data->dataflags & FSESS_INITED)) {
244 if (vnode_isvroot(vp)) {
245 if (priv_check_cred(cred, PRIV_VFS_ADMIN, 0) ||
246 (fuse_match_cred(data->daemoncred, cred) == 0)) {
252 if (vnode_islnk(vp)) {
255 bzero(&facp, sizeof(facp));
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);
263 struct vnop_close_args {
266 struct ucred *a_cred;
271 fuse_vnop_close(struct vop_close_args *ap)
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;
278 fuse_trace_printf_vnop();
280 if (fuse_isdeadfs(vp)) {
283 if (vnode_isdir(vp)) {
284 if (fuse_filehandle_valid(vp, FUFH_RDONLY)) {
285 fuse_filehandle_close(vp, FUFH_RDONLY, NULL, cred);
289 if (fflag & IO_NDELAY) {
292 fufh_type = fuse_filehandle_xlate_from_fflags(fflag);
294 if (!fuse_filehandle_valid(vp, fufh_type)) {
297 for (i = 0; i < FUFH_MAXTYPE; i++)
298 if (fuse_filehandle_valid(vp, i))
300 if (i == FUFH_MAXTYPE)
301 panic("FUSE: fufh type %d found to be invalid in close"
305 if ((VTOFUD(vp)->flag & FN_SIZECHANGE) != 0) {
306 fuse_vnode_savesize(vp, cred);
312 struct vnop_create_args {
314 struct vnode **a_vpp;
315 struct componentname *a_cnp;
320 fuse_vnop_create(struct vop_create_args *ap)
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;
329 struct fuse_open_in *foi;
330 struct fuse_entry_out *feo;
331 struct fuse_dispatcher fdi;
332 struct fuse_dispatcher *fdip = &fdi;
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);
340 uint32_t x_open_flags;
342 fuse_trace_printf_vnop();
344 if (fuse_isdeadfs(dvp)) {
347 bzero(&fdi, sizeof(fdi));
349 /* XXX: Will we ever want devices ? */
350 if ((vap->va_type != VREG)) {
351 printf("fuse_vnop_create: unsupported va_type %d\n",
355 debug_printf("parent nid = %ju, mode = %x\n", (uintmax_t)parentnid,
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");
363 fdisp_make(fdip, FUSE_CREATE, vnode_mount(dvp), parentnid, td, cred);
367 foi->flags = O_CREAT | O_RDWR;
369 memcpy((char *)fdip->indata + sizeof(*foi), cnp->cn_nameptr,
371 ((char *)fdip->indata)[sizeof(*foi) + cnp->cn_namelen] = '\0';
373 err = fdisp_wait_answ(fdip);
377 fsess_set_notimpl(mp, FUSE_CREATE);
378 debug_printf("create: got err=%d from daemon\n", err);
384 if ((err = fuse_internal_checkentry(feo, VREG))) {
387 err = fuse_vnode_get(mp, feo->nodeid, dvp, vpp, cnp, VREG);
389 struct fuse_release_in *fri;
390 uint64_t nodeid = feo->nodeid;
391 uint64_t fh_id = ((struct fuse_open_out *)(feo + 1))->fh;
393 fdisp_init(fdip, sizeof(*fri));
394 fdisp_make(fdip, FUSE_RELEASE, mp, nodeid, td, cred);
397 fri->flags = OFLAGS(mode);
398 fuse_insert_callback(fdip->tick, fuse_internal_forget_callback);
399 fuse_insert_message(fdip->tick);
402 ASSERT_VOP_ELOCKED(*vpp, "fuse_vnop_create");
404 fdip->answ = feo + 1;
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);
418 * Our vnop_fsync roughly corresponds to the FUSE_FSYNC method. The Linux
419 * version of FUSE also has a FUSE_FLUSH method.
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.
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.
430 struct vnop_fsync_args {
431 struct vnodeop_desc *a_desc;
433 struct ucred * a_cred;
435 struct thread * a_td;
439 fuse_vnop_fsync(struct vop_fsync_args *ap)
441 struct vnode *vp = ap->a_vp;
442 struct thread *td = ap->a_td;
444 struct fuse_filehandle *fufh;
445 struct fuse_vnode_data *fvdat = VTOFUD(vp);
449 fuse_trace_printf_vnop();
451 if (fuse_isdeadfs(vp)) {
454 if ((err = vop_stdfsync(ap)))
457 if (!fsess_isimpl(vnode_mount(vp),
458 (vnode_vtype(vp) == VDIR ? FUSE_FSYNCDIR : FUSE_FSYNC))) {
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);
473 struct vnop_getattr_args {
476 struct ucred *a_cred;
481 fuse_vnop_getattr(struct vop_getattr_args *ap)
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);
491 struct fuse_dispatcher fdi;
493 FS_DEBUG2G("inode=%ju\n", (uintmax_t)VTOI(vp));
495 dataflags = fuse_get_mpdata(vnode_mount(vp))->dataflags;
497 /* Note that we are not bailing out on a dead file system just yet. */
499 if (!(dataflags & FSESS_INITED)) {
500 if (!vnode_isvroot(vp)) {
501 fdata_set_dead(fuse_get_mpdata(vnode_mount(vp)));
503 debug_printf("fuse_getattr b: returning ENOTCONN\n");
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() */
517 fuse_internal_vnode_disappear(vp);
521 cache_attrs(vp, (struct fuse_attr_out *)fdi.answ);
522 if (vap != VTOVA(vp)) {
523 memcpy(vap, VTOVA(vp), sizeof(*vap));
525 if (vap->va_type != vnode_vtype(vp)) {
526 fuse_internal_vnode_disappear(vp);
530 if ((fvdat->flag & FN_SIZECHANGE) != 0)
531 vap->va_size = fvdat->filesize;
533 if (vnode_isreg(vp) && (fvdat->flag & FN_SIZECHANGE) == 0) {
535 * This is for those cases when the file size changed without us
536 * knowing, and we want to catch up.
538 off_t new_filesize = ((struct fuse_attr_out *)
539 fdi.answ)->attr.size;
541 if (fvdat->filesize != new_filesize) {
542 fuse_vnode_setsize(vp, cred, new_filesize);
545 debug_printf("fuse_getattr e: returning 0\n");
552 bzero(vap, sizeof(*vap));
553 vap->va_type = vnode_vtype(vp);
559 struct vnop_inactive_args {
565 fuse_vnop_inactive(struct vop_inactive_args *ap)
567 struct vnode *vp = ap->a_vp;
568 struct thread *td = ap->a_td;
570 struct fuse_vnode_data *fvdat = VTOFUD(vp);
571 struct fuse_filehandle *fufh = NULL;
573 int type, need_flush = 1;
575 FS_DEBUG("inode=%ju\n", (uintmax_t)VTOI(vp));
577 for (type = 0; type < FUFH_MAXTYPE; type++) {
578 fufh = &(fvdat->fufh[type]);
579 if (FUFH_IS_VALID(fufh)) {
580 if (need_flush && vp->v_type == VREG) {
581 if ((VTOFUD(vp)->flag & FN_SIZECHANGE) != 0) {
582 fuse_vnode_savesize(vp, NULL);
584 if (fuse_data_cache_invalidate ||
585 (fvdat->flag & FN_REVOKED) != 0)
586 fuse_io_invalbuf(vp, td);
588 fuse_io_flushbuf(vp, MNT_WAIT, td);
591 fuse_filehandle_close(vp, type, td, NULL);
595 if ((fvdat->flag & FN_REVOKED) != 0 && fuse_reclaim_revoked) {
602 struct vnop_link_args {
603 struct vnode *a_tdvp;
605 struct componentname *a_cnp;
609 fuse_vnop_link(struct vop_link_args *ap)
611 struct vnode *vp = ap->a_vp;
612 struct vnode *tdvp = ap->a_tdvp;
613 struct componentname *cnp = ap->a_cnp;
615 struct vattr *vap = VTOVA(vp);
617 struct fuse_dispatcher fdi;
618 struct fuse_entry_out *feo;
619 struct fuse_link_in fli;
623 fuse_trace_printf_vnop();
625 if (fuse_isdeadfs(vp)) {
628 if (vnode_mount(tdvp) != vnode_mount(vp)) {
631 if (vap->va_nlink >= FUSE_LINK_MAX) {
634 fli.oldnodeid = VTOI(vp);
637 fuse_internal_newentry_makerequest(vnode_mount(tdvp), VTOI(tdvp), cnp,
638 FUSE_LINK, &fli, sizeof(fli), &fdi);
639 if ((err = fdisp_wait_answ(&fdi))) {
644 err = fuse_internal_checkentry(feo, vnode_vtype(vp));
651 struct vnop_lookup_args {
652 struct vnodeop_desc *a_desc;
654 struct vnode **a_vpp;
655 struct componentname *a_cnp;
659 fuse_vnop_lookup(struct vop_lookup_args *ap)
661 struct vnode *dvp = ap->a_dvp;
662 struct vnode **vpp = ap->a_vpp;
663 struct componentname *cnp = ap->a_cnp;
664 struct thread *td = cnp->cn_thread;
665 struct ucred *cred = cnp->cn_cred;
667 int nameiop = cnp->cn_nameiop;
668 int flags = cnp->cn_flags;
669 int wantparent = flags & (LOCKPARENT | WANTPARENT);
670 int islastcn = flags & ISLASTCN;
671 struct mount *mp = vnode_mount(dvp);
675 struct vnode *vp = NULL;
677 struct fuse_dispatcher fdi;
681 struct fuse_access_param facp;
683 FS_DEBUG2G("parent_inode=%ju - %*s\n",
684 (uintmax_t)VTOI(dvp), (int)cnp->cn_namelen, cnp->cn_nameptr);
686 if (fuse_isdeadfs(dvp)) {
690 if (!vnode_isdir(dvp)) {
693 if (islastcn && vfs_isrdonly(mp) && (nameiop != LOOKUP)) {
697 * We do access check prior to doing anything else only in the case
698 * when we are at fs root (we'd like to say, "we are at the first
699 * component", but that's not exactly the same... nevermind).
700 * See further comments at further access checks.
703 bzero(&facp, sizeof(facp));
704 if (vnode_isvroot(dvp)) { /* early permission check hack */
705 if ((err = fuse_internal_access(dvp, VEXEC, &facp, td, cred))) {
709 if (flags & ISDOTDOT) {
710 nid = VTOFUD(dvp)->parent_nid;
717 } else if (cnp->cn_namelen == 1 && *(cnp->cn_nameptr) == '.') {
722 } else if (fuse_lookup_cache_enable) {
723 err = cache_lookup(dvp, vpp, cnp, NULL, NULL);
726 case -1: /* positive match */
727 atomic_add_acq_long(&fuse_lookup_cache_hits, 1);
730 case 0: /* no match in cache */
731 atomic_add_acq_long(&fuse_lookup_cache_misses, 1);
734 case ENOENT: /* negative match */
741 fdisp_init(&fdi, cnp->cn_namelen + 1);
745 fdisp_make(&fdi, op, mp, nid, td, cred);
747 if (op == FUSE_LOOKUP) {
748 memcpy(fdi.indata, cnp->cn_nameptr, cnp->cn_namelen);
749 ((char *)fdi.indata)[cnp->cn_namelen] = '\0';
751 lookup_err = fdisp_wait_answ(&fdi);
753 if ((op == FUSE_LOOKUP) && !lookup_err) { /* lookup call succeeded */
754 nid = ((struct fuse_entry_out *)fdi.answ)->nodeid;
757 * zero nodeid is the same as "not found",
758 * but it's also cacheable (which we keep
759 * keep on doing not as of writing this)
762 } else if (nid == FUSE_ROOT_ID) {
767 (!fdi.answ_stat || lookup_err != ENOENT || op != FUSE_LOOKUP)) {
771 /* lookup_err, if non-zero, must be ENOENT at this point */
775 if ((nameiop == CREATE || nameiop == RENAME) && islastcn
776 /* && directory dvp has not been removed */ ) {
778 if (vfs_isrdonly(mp)) {
782 #if 0 /* THINK_ABOUT_THIS */
783 if ((err = fuse_internal_access(dvp, VWRITE, cred, td, &facp))) {
789 * Possibly record the position of a slot in the
790 * directory large enough for the new component name.
791 * This can be recorded in the vnode private data for
792 * dvp. Set the SAVENAME flag to hold onto the
793 * pathname for use later in VOP_CREATE or VOP_RENAME.
795 cnp->cn_flags |= SAVENAME;
800 /* Consider inserting name into cache. */
803 * No we can't use negative caching, as the fs
804 * changes are out of our control.
805 * False positives' falseness turns out just as things
806 * go by, but false negatives' falseness doesn't.
807 * (and aiding the caching mechanism with extra control
808 * mechanisms comes quite close to beating the whole purpose
812 if ((cnp->cn_flags & MAKEENTRY) != 0) {
813 FS_DEBUG("inserting NULL into cache\n");
814 cache_enter(dvp, NULL, cnp);
824 struct fuse_entry_out *feo = NULL;
825 struct fuse_attr *fattr = NULL;
827 if (op == FUSE_GETATTR) {
828 fattr = &((struct fuse_attr_out *)fdi.answ)->attr;
830 feo = (struct fuse_entry_out *)fdi.answ;
831 fattr = &(feo->attr);
835 * If deleting, and at end of pathname, return parameters
836 * which can be used to remove file. If the wantparent flag
837 * isn't set, we return only the directory, otherwise we go on
838 * and lock the inode, being careful with ".".
840 if (nameiop == DELETE && islastcn) {
842 * Check for write access on directory.
844 facp.xuid = fattr->uid;
845 facp.facc_flags |= FACCESS_STICKY;
846 err = fuse_internal_access(dvp, VWRITE, &facp, td, cred);
847 facp.facc_flags &= ~FACCESS_XQUERIES;
852 if (nid == VTOI(dvp)) {
856 err = fuse_vnode_get(dvp->v_mount, nid, dvp,
857 &vp, cnp, IFTOVT(fattr->mode));
864 * Save the name for use in VOP_RMDIR and VOP_REMOVE
867 cnp->cn_flags |= SAVENAME;
872 * If rewriting (RENAME), return the inode and the
873 * information required to rewrite the present directory
874 * Must get inode of directory entry to verify it's a
875 * regular file, or empty directory.
877 if (nameiop == RENAME && wantparent && islastcn) {
879 #if 0 /* THINK_ABOUT_THIS */
880 if ((err = fuse_internal_access(dvp, VWRITE, cred, td, &facp))) {
888 if (nid == VTOI(dvp)) {
892 err = fuse_vnode_get(vnode_mount(dvp),
897 IFTOVT(fattr->mode));
903 * Save the name for use in VOP_RENAME later.
905 cnp->cn_flags |= SAVENAME;
909 if (flags & ISDOTDOT) {
914 * Expanded copy of vn_vget_ino() so that
915 * fuse_vnode_get() can be used.
918 ltype = VOP_ISLOCKED(dvp);
919 err = vfs_busy(mp, MBF_NOWAIT);
923 err = vfs_busy(mp, 0);
924 vn_lock(dvp, ltype | LK_RETRY);
928 if ((dvp->v_iflag & VI_DOOMED) != 0) {
935 err = fuse_vnode_get(vnode_mount(dvp),
940 IFTOVT(fattr->mode));
942 vn_lock(dvp, ltype | LK_RETRY);
943 if ((dvp->v_iflag & VI_DOOMED) != 0) {
951 } else if (nid == VTOI(dvp)) {
955 err = fuse_vnode_get(vnode_mount(dvp),
960 IFTOVT(fattr->mode));
964 fuse_vnode_setparent(vp, dvp);
968 if (op == FUSE_GETATTR) {
969 cache_attrs(*vpp, (struct fuse_attr_out *)fdi.answ);
971 cache_attrs(*vpp, (struct fuse_entry_out *)fdi.answ);
974 /* Insert name into cache if appropriate. */
977 * Nooo, caching is evil. With caching, we can't avoid stale
978 * information taking over the playground (cached info is not
979 * just positive/negative, it does have qualitative aspects,
980 * too). And a (VOP/FUSE)_GETATTR is always thrown anyway, when
981 * walking down along cached path components, and that's not
982 * any cheaper than FUSE_LOOKUP. This might change with
983 * implementing kernel side attr caching, but... In Linux,
984 * lookup results are not cached, and the daemon is bombarded
985 * with FUSE_LOOKUPS on and on. This shows that by design, the
986 * daemon is expected to handle frequent lookup queries
987 * efficiently, do its caching in userspace, and so on.
989 * So just leave the name cache alone.
993 * Well, now I know, Linux caches lookups, but with a
994 * timeout... So it's the same thing as attribute caching:
995 * we can deal with it when implement timeouts.
998 if (cnp->cn_flags & MAKEENTRY) {
999 cache_enter(dvp, *vpp, cnp);
1006 /* No lookup error; need to clean up. */
1008 if (err) { /* Found inode; exit with no vnode. */
1009 if (op == FUSE_LOOKUP) {
1010 fuse_internal_forget_send(vnode_mount(dvp), td, cred,
1013 fdisp_destroy(&fdi);
1016 #ifndef NO_EARLY_PERM_CHECK_HACK
1019 * We have the attributes of the next item
1020 * *now*, and it's a fact, and we do not
1021 * have to do extra work for it (ie, beg the
1022 * daemon), and it neither depends on such
1023 * accidental things like attr caching. So
1024 * the big idea: check credentials *now*,
1025 * not at the beginning of the next call to
1028 * The first item of the lookup chain (fs root)
1029 * won't be checked then here, of course, as
1030 * its never "the next". But go and see that
1031 * the root is taken care about at the very
1032 * beginning of this function.
1034 * Now, given we want to do the access check
1035 * this way, one might ask: so then why not
1036 * do the access check just after fetching
1037 * the inode and its attributes from the
1038 * daemon? Why bother with producing the
1039 * corresponding vnode at all if something
1040 * is not OK? We know what's the deal as
1041 * soon as we get those attrs... There is
1042 * one bit of info though not given us by
1043 * the daemon: whether his response is
1044 * authoritative or not... His response should
1045 * be ignored if something is mounted over
1046 * the dir in question. But that can be
1047 * known only by having the vnode...
1049 int tmpvtype = vnode_vtype(*vpp);
1051 bzero(&facp, sizeof(facp));
1052 /*the early perm check hack */
1053 facp.facc_flags |= FACCESS_VA_VALID;
1055 if ((tmpvtype != VDIR) && (tmpvtype != VLNK)) {
1058 if (!err && !vnode_mountedhere(*vpp)) {
1059 err = fuse_internal_access(*vpp, VEXEC, &facp, td, cred);
1062 if (tmpvtype == VLNK)
1063 FS_DEBUG("weird, permission error with a symlink?\n");
1071 fdisp_destroy(&fdi);
1077 struct vnop_mkdir_args {
1078 struct vnode *a_dvp;
1079 struct vnode **a_vpp;
1080 struct componentname *a_cnp;
1081 struct vattr *a_vap;
1085 fuse_vnop_mkdir(struct vop_mkdir_args *ap)
1087 struct vnode *dvp = ap->a_dvp;
1088 struct vnode **vpp = ap->a_vpp;
1089 struct componentname *cnp = ap->a_cnp;
1090 struct vattr *vap = ap->a_vap;
1092 struct fuse_mkdir_in fmdi;
1094 fuse_trace_printf_vnop();
1096 if (fuse_isdeadfs(dvp)) {
1099 fmdi.mode = MAKEIMODE(vap->va_type, vap->va_mode);
1101 return (fuse_internal_newentry(dvp, vpp, cnp, FUSE_MKDIR, &fmdi,
1102 sizeof(fmdi), VDIR));
1106 struct vnop_mknod_args {
1107 struct vnode *a_dvp;
1108 struct vnode **a_vpp;
1109 struct componentname *a_cnp;
1110 struct vattr *a_vap;
1114 fuse_vnop_mknod(struct vop_mknod_args *ap)
1122 struct vnop_open_args {
1125 struct ucred *a_cred;
1126 struct thread *a_td;
1127 int a_fdidx; / struct file *a_fp;
1131 fuse_vnop_open(struct vop_open_args *ap)
1133 struct vnode *vp = ap->a_vp;
1134 int mode = ap->a_mode;
1135 struct thread *td = ap->a_td;
1136 struct ucred *cred = ap->a_cred;
1138 fufh_type_t fufh_type;
1139 struct fuse_vnode_data *fvdat;
1141 int error, isdir = 0;
1142 int32_t fuse_open_flags;
1144 FS_DEBUG2G("inode=%ju mode=0x%x\n", (uintmax_t)VTOI(vp), mode);
1146 if (fuse_isdeadfs(vp)) {
1151 if (vnode_isdir(vp)) {
1154 fuse_open_flags = 0;
1156 fufh_type = FUFH_RDONLY;
1158 fufh_type = fuse_filehandle_xlate_from_fflags(mode);
1160 * For WRONLY opens, force DIRECT_IO. This is necessary
1161 * since writing a partial block through the buffer cache
1162 * will result in a read of the block and that read won't
1163 * be allowed by the WRONLY open.
1165 if (fufh_type == FUFH_WRONLY ||
1166 (fvdat->flag & FN_DIRECTIO) != 0)
1167 fuse_open_flags = FOPEN_DIRECT_IO;
1170 if (fuse_filehandle_validrw(vp, fufh_type) != FUFH_INVALID) {
1171 fuse_vnode_open(vp, fuse_open_flags, td);
1174 error = fuse_filehandle_open(vp, fufh_type, NULL, td, cred);
1180 fuse_vnop_pathconf(struct vop_pathconf_args *ap)
1183 switch (ap->a_name) {
1184 case _PC_FILESIZEBITS:
1188 *ap->a_retval = NAME_MAX;
1191 *ap->a_retval = MIN(LONG_MAX, FUSE_LINK_MAX);
1193 case _PC_SYMLINK_MAX:
1194 *ap->a_retval = MAXPATHLEN;
1200 return (vop_stdpathconf(ap));
1205 struct vnop_read_args {
1209 struct ucred *a_cred;
1213 fuse_vnop_read(struct vop_read_args *ap)
1215 struct vnode *vp = ap->a_vp;
1216 struct uio *uio = ap->a_uio;
1217 int ioflag = ap->a_ioflag;
1218 struct ucred *cred = ap->a_cred;
1220 FS_DEBUG2G("inode=%ju offset=%jd resid=%zd\n",
1221 (uintmax_t)VTOI(vp), uio->uio_offset, uio->uio_resid);
1223 if (fuse_isdeadfs(vp)) {
1227 if (VTOFUD(vp)->flag & FN_DIRECTIO) {
1228 ioflag |= IO_DIRECT;
1231 return fuse_io_dispatch(vp, uio, ioflag, cred);
1235 struct vnop_readdir_args {
1238 struct ucred *a_cred;
1245 fuse_vnop_readdir(struct vop_readdir_args *ap)
1247 struct vnode *vp = ap->a_vp;
1248 struct uio *uio = ap->a_uio;
1249 struct ucred *cred = ap->a_cred;
1251 struct fuse_filehandle *fufh = NULL;
1252 struct fuse_iov cookediov;
1257 FS_DEBUG2G("inode=%ju\n", (uintmax_t)VTOI(vp));
1259 if (fuse_isdeadfs(vp)) {
1262 if ( /* XXXIP ((uio_iovcnt(uio) > 1)) || */
1263 (uio_resid(uio) < sizeof(struct dirent))) {
1267 if (!fuse_filehandle_valid(vp, FUFH_RDONLY)) {
1268 FS_DEBUG("calling readdir() before open()");
1269 err = fuse_filehandle_open(vp, FUFH_RDONLY, &fufh, NULL, cred);
1272 err = fuse_filehandle_get(vp, FUFH_RDONLY, &fufh);
1277 #define DIRCOOKEDSIZE FUSE_DIRENT_ALIGN(FUSE_NAME_OFFSET + MAXNAMLEN + 1)
1278 fiov_init(&cookediov, DIRCOOKEDSIZE);
1280 err = fuse_internal_readdir(vp, uio, fufh, &cookediov);
1282 fiov_teardown(&cookediov);
1284 fuse_filehandle_close(vp, FUFH_RDONLY, NULL, cred);
1290 struct vnop_readlink_args {
1293 struct ucred *a_cred;
1297 fuse_vnop_readlink(struct vop_readlink_args *ap)
1299 struct vnode *vp = ap->a_vp;
1300 struct uio *uio = ap->a_uio;
1301 struct ucred *cred = ap->a_cred;
1303 struct fuse_dispatcher fdi;
1306 FS_DEBUG2G("inode=%ju\n", (uintmax_t)VTOI(vp));
1308 if (fuse_isdeadfs(vp)) {
1311 if (!vnode_islnk(vp)) {
1314 fdisp_init(&fdi, 0);
1315 err = fdisp_simple_putget_vp(&fdi, FUSE_READLINK, vp, curthread, cred);
1319 if (((char *)fdi.answ)[0] == '/' &&
1320 fuse_get_mpdata(vnode_mount(vp))->dataflags & FSESS_PUSH_SYMLINKS_IN) {
1321 char *mpth = vnode_mount(vp)->mnt_stat.f_mntonname;
1323 err = uiomove(mpth, strlen(mpth), uio);
1326 err = uiomove(fdi.answ, fdi.iosize, uio);
1329 fdisp_destroy(&fdi);
1334 struct vnop_reclaim_args {
1336 struct thread *a_td;
1340 fuse_vnop_reclaim(struct vop_reclaim_args *ap)
1342 struct vnode *vp = ap->a_vp;
1343 struct thread *td = ap->a_td;
1345 struct fuse_vnode_data *fvdat = VTOFUD(vp);
1346 struct fuse_filehandle *fufh = NULL;
1351 panic("FUSE: no vnode data during recycling");
1353 FS_DEBUG("inode=%ju\n", (uintmax_t)VTOI(vp));
1355 for (type = 0; type < FUFH_MAXTYPE; type++) {
1356 fufh = &(fvdat->fufh[type]);
1357 if (FUFH_IS_VALID(fufh)) {
1358 printf("FUSE: vnode being reclaimed but fufh (type=%d) is valid",
1360 fuse_filehandle_close(vp, type, td, NULL);
1364 if ((!fuse_isdeadfs(vp)) && (fvdat->nlookup)) {
1365 fuse_internal_forget_send(vnode_mount(vp), td, NULL, VTOI(vp),
1368 fuse_vnode_setparent(vp, NULL);
1370 vfs_hash_remove(vp);
1371 vnode_destroy_vobject(vp);
1372 fuse_vnode_destroy(vp);
1378 struct vnop_remove_args {
1379 struct vnode *a_dvp;
1381 struct componentname *a_cnp;
1385 fuse_vnop_remove(struct vop_remove_args *ap)
1387 struct vnode *dvp = ap->a_dvp;
1388 struct vnode *vp = ap->a_vp;
1389 struct componentname *cnp = ap->a_cnp;
1393 FS_DEBUG2G("inode=%ju name=%*s\n",
1394 (uintmax_t)VTOI(vp), (int)cnp->cn_namelen, cnp->cn_nameptr);
1396 if (fuse_isdeadfs(vp)) {
1399 if (vnode_isdir(vp)) {
1404 err = fuse_internal_remove(dvp, vp, cnp, FUSE_UNLINK);
1407 fuse_internal_vnode_disappear(vp);
1412 struct vnop_rename_args {
1413 struct vnode *a_fdvp;
1414 struct vnode *a_fvp;
1415 struct componentname *a_fcnp;
1416 struct vnode *a_tdvp;
1417 struct vnode *a_tvp;
1418 struct componentname *a_tcnp;
1422 fuse_vnop_rename(struct vop_rename_args *ap)
1424 struct vnode *fdvp = ap->a_fdvp;
1425 struct vnode *fvp = ap->a_fvp;
1426 struct componentname *fcnp = ap->a_fcnp;
1427 struct vnode *tdvp = ap->a_tdvp;
1428 struct vnode *tvp = ap->a_tvp;
1429 struct componentname *tcnp = ap->a_tcnp;
1430 struct fuse_data *data;
1434 FS_DEBUG2G("from: inode=%ju name=%*s -> to: inode=%ju name=%*s\n",
1435 (uintmax_t)VTOI(fvp), (int)fcnp->cn_namelen, fcnp->cn_nameptr,
1436 (uintmax_t)(tvp == NULL ? -1 : VTOI(tvp)),
1437 (int)tcnp->cn_namelen, tcnp->cn_nameptr);
1439 if (fuse_isdeadfs(fdvp)) {
1442 if (fvp->v_mount != tdvp->v_mount ||
1443 (tvp && fvp->v_mount != tvp->v_mount)) {
1444 FS_DEBUG("cross-device rename: %s -> %s\n",
1445 fcnp->cn_nameptr, (tcnp != NULL ? tcnp->cn_nameptr : "(NULL)"));
1452 * FUSE library is expected to check if target directory is not
1453 * under the source directory in the file system tree.
1454 * Linux performs this check at VFS level.
1456 data = fuse_get_mpdata(vnode_mount(tdvp));
1457 sx_xlock(&data->rename_lock);
1458 err = fuse_internal_rename(fdvp, fcnp, tdvp, tcnp);
1461 fuse_vnode_setparent(fvp, tdvp);
1463 fuse_vnode_setparent(tvp, NULL);
1465 sx_unlock(&data->rename_lock);
1467 if (tvp != NULL && tvp != fvp) {
1470 if (vnode_isdir(fvp)) {
1471 if ((tvp != NULL) && vnode_isdir(tvp)) {
1492 struct vnop_rmdir_args {
1493 struct vnode *a_dvp;
1495 struct componentname *a_cnp;
1499 fuse_vnop_rmdir(struct vop_rmdir_args *ap)
1501 struct vnode *dvp = ap->a_dvp;
1502 struct vnode *vp = ap->a_vp;
1506 FS_DEBUG2G("inode=%ju\n", (uintmax_t)VTOI(vp));
1508 if (fuse_isdeadfs(vp)) {
1511 if (VTOFUD(vp) == VTOFUD(dvp)) {
1514 err = fuse_internal_remove(dvp, vp, ap->a_cnp, FUSE_RMDIR);
1517 fuse_internal_vnode_disappear(vp);
1522 struct vnop_setattr_args {
1524 struct vattr *a_vap;
1525 struct ucred *a_cred;
1526 struct thread *a_td;
1530 fuse_vnop_setattr(struct vop_setattr_args *ap)
1532 struct vnode *vp = ap->a_vp;
1533 struct vattr *vap = ap->a_vap;
1534 struct ucred *cred = ap->a_cred;
1535 struct thread *td = curthread;
1537 struct fuse_dispatcher fdi;
1538 struct fuse_setattr_in *fsai;
1539 struct fuse_access_param facp;
1543 int sizechanged = 0;
1544 uint64_t newsize = 0;
1546 FS_DEBUG2G("inode=%ju\n", (uintmax_t)VTOI(vp));
1548 if (fuse_isdeadfs(vp)) {
1551 fdisp_init(&fdi, sizeof(*fsai));
1552 fdisp_make_vp(&fdi, FUSE_SETATTR, vp, td, cred);
1556 bzero(&facp, sizeof(facp));
1558 facp.xuid = vap->va_uid;
1559 facp.xgid = vap->va_gid;
1561 if (vap->va_uid != (uid_t)VNOVAL) {
1562 facp.facc_flags |= FACCESS_CHOWN;
1563 fsai->uid = vap->va_uid;
1564 fsai->valid |= FATTR_UID;
1566 if (vap->va_gid != (gid_t)VNOVAL) {
1567 facp.facc_flags |= FACCESS_CHOWN;
1568 fsai->gid = vap->va_gid;
1569 fsai->valid |= FATTR_GID;
1571 if (vap->va_size != VNOVAL) {
1573 struct fuse_filehandle *fufh = NULL;
1575 /*Truncate to a new value. */
1576 fsai->size = vap->va_size;
1578 newsize = vap->va_size;
1579 fsai->valid |= FATTR_SIZE;
1581 fuse_filehandle_getrw(vp, FUFH_WRONLY, &fufh);
1583 fsai->fh = fufh->fh_id;
1584 fsai->valid |= FATTR_FH;
1587 if (vap->va_atime.tv_sec != VNOVAL) {
1588 fsai->atime = vap->va_atime.tv_sec;
1589 fsai->atimensec = vap->va_atime.tv_nsec;
1590 fsai->valid |= FATTR_ATIME;
1592 if (vap->va_mtime.tv_sec != VNOVAL) {
1593 fsai->mtime = vap->va_mtime.tv_sec;
1594 fsai->mtimensec = vap->va_mtime.tv_nsec;
1595 fsai->valid |= FATTR_MTIME;
1597 if (vap->va_mode != (mode_t)VNOVAL) {
1598 fsai->mode = vap->va_mode & ALLPERMS;
1599 fsai->valid |= FATTR_MODE;
1604 vtyp = vnode_vtype(vp);
1606 if (fsai->valid & FATTR_SIZE && vtyp == VDIR) {
1610 if (vfs_isrdonly(vnode_mount(vp)) && (fsai->valid & ~FATTR_SIZE || vtyp == VREG)) {
1614 if (fsai->valid & ~FATTR_SIZE) {
1615 /*err = fuse_internal_access(vp, VADMIN, context, &facp); */
1619 facp.facc_flags &= ~FACCESS_XQUERIES;
1621 if (err && !(fsai->valid & ~(FATTR_ATIME | FATTR_MTIME)) &&
1622 vap->va_vaflags & VA_UTIMES_NULL) {
1623 err = fuse_internal_access(vp, VWRITE, &facp, td, cred);
1627 if ((err = fdisp_wait_answ(&fdi)))
1629 vtyp = IFTOVT(((struct fuse_attr_out *)fdi.answ)->attr.mode);
1631 if (vnode_vtype(vp) != vtyp) {
1632 if (vnode_vtype(vp) == VNON && vtyp != VNON) {
1633 debug_printf("FUSE: Dang! vnode_vtype is VNON and vtype isn't.\n");
1636 * STALE vnode, ditch
1638 * The vnode has changed its type "behind our back". There's
1639 * nothing really we can do, so let us just force an internal
1640 * revocation and tell the caller to try again, if interested.
1642 fuse_internal_vnode_disappear(vp);
1646 if (!err && !sizechanged) {
1647 cache_attrs(vp, (struct fuse_attr_out *)fdi.answ);
1650 fdisp_destroy(&fdi);
1651 if (!err && sizechanged) {
1652 fuse_vnode_setsize(vp, cred, newsize);
1653 VTOFUD(vp)->flag &= ~FN_SIZECHANGE;
1659 struct vnop_strategy_args {
1665 fuse_vnop_strategy(struct vop_strategy_args *ap)
1667 struct vnode *vp = ap->a_vp;
1668 struct buf *bp = ap->a_bp;
1670 fuse_trace_printf_vnop();
1672 if (!vp || fuse_isdeadfs(vp)) {
1673 bp->b_ioflags |= BIO_ERROR;
1674 bp->b_error = ENXIO;
1678 if (bp->b_iocmd == BIO_WRITE)
1679 fuse_vnode_refreshsize(vp, NOCRED);
1681 (void)fuse_io_strategy(vp, bp);
1684 * This is a dangerous function. If returns error, that might mean a
1685 * panic. We prefer pretty much anything over being forced to panic
1686 * by a malicious daemon (a demon?). So we just return 0 anyway. You
1687 * should never mind this: this function has its own error
1688 * propagation mechanism via the argument buffer, so
1689 * not-that-melodramatic residents of the call chain still will be
1690 * able to know what to do.
1697 struct vnop_symlink_args {
1698 struct vnode *a_dvp;
1699 struct vnode **a_vpp;
1700 struct componentname *a_cnp;
1701 struct vattr *a_vap;
1706 fuse_vnop_symlink(struct vop_symlink_args *ap)
1708 struct vnode *dvp = ap->a_dvp;
1709 struct vnode **vpp = ap->a_vpp;
1710 struct componentname *cnp = ap->a_cnp;
1711 char *target = ap->a_target;
1713 struct fuse_dispatcher fdi;
1718 FS_DEBUG2G("inode=%ju name=%*s\n",
1719 (uintmax_t)VTOI(dvp), (int)cnp->cn_namelen, cnp->cn_nameptr);
1721 if (fuse_isdeadfs(dvp)) {
1725 * Unlike the other creator type calls, here we have to create a message
1726 * where the name of the new entry comes first, and the data describing
1727 * the entry comes second.
1728 * Hence we can't rely on our handy fuse_internal_newentry() routine,
1729 * but put together the message manually and just call the core part.
1732 len = strlen(target) + 1;
1733 fdisp_init(&fdi, len + cnp->cn_namelen + 1);
1734 fdisp_make_vp(&fdi, FUSE_SYMLINK, dvp, curthread, NULL);
1736 memcpy(fdi.indata, cnp->cn_nameptr, cnp->cn_namelen);
1737 ((char *)fdi.indata)[cnp->cn_namelen] = '\0';
1738 memcpy((char *)fdi.indata + cnp->cn_namelen + 1, target, len);
1740 err = fuse_internal_newentry_core(dvp, vpp, cnp, VLNK, &fdi);
1741 fdisp_destroy(&fdi);
1746 struct vnop_write_args {
1750 struct ucred *a_cred;
1754 fuse_vnop_write(struct vop_write_args *ap)
1756 struct vnode *vp = ap->a_vp;
1757 struct uio *uio = ap->a_uio;
1758 int ioflag = ap->a_ioflag;
1759 struct ucred *cred = ap->a_cred;
1761 fuse_trace_printf_vnop();
1763 if (fuse_isdeadfs(vp)) {
1766 fuse_vnode_refreshsize(vp, cred);
1768 if (VTOFUD(vp)->flag & FN_DIRECTIO) {
1769 ioflag |= IO_DIRECT;
1772 return fuse_io_dispatch(vp, uio, ioflag, cred);
1776 struct vnop_getpages_args {
1784 fuse_vnop_getpages(struct vop_getpages_args *ap)
1786 int i, error, nextoff, size, toff, count, npages;
1796 FS_DEBUG2G("heh\n");
1799 KASSERT(vp->v_object, ("objectless vp passed to getpages"));
1800 td = curthread; /* XXX */
1801 cred = curthread->td_ucred; /* XXX */
1803 npages = ap->a_count;
1805 if (!fsess_opt_mmap(vnode_mount(vp))) {
1806 FS_DEBUG("called on non-cacheable vnode??\n");
1807 return (VM_PAGER_ERROR);
1811 * If the last page is partially valid, just return it and allow
1812 * the pager to zero-out the blanks. Partially valid pages can
1813 * only occur at the file EOF.
1815 * XXXGL: is that true for FUSE, which is a local filesystem,
1816 * but still somewhat disconnected from the kernel?
1818 VM_OBJECT_WLOCK(vp->v_object);
1819 if (pages[npages - 1]->valid != 0 && --npages == 0)
1821 VM_OBJECT_WUNLOCK(vp->v_object);
1824 * We use only the kva address for the buffer, but this is extremely
1825 * convenient and fast.
1827 bp = getpbuf(&fuse_pbuf_freecnt);
1829 kva = (vm_offset_t)bp->b_data;
1830 pmap_qenter(kva, pages, npages);
1831 VM_CNT_INC(v_vnodein);
1832 VM_CNT_ADD(v_vnodepgsin, npages);
1834 count = npages << PAGE_SHIFT;
1835 iov.iov_base = (caddr_t)kva;
1836 iov.iov_len = count;
1839 uio.uio_offset = IDX_TO_OFF(pages[0]->pindex);
1840 uio.uio_resid = count;
1841 uio.uio_segflg = UIO_SYSSPACE;
1842 uio.uio_rw = UIO_READ;
1845 error = fuse_io_dispatch(vp, &uio, IO_DIRECT, cred);
1846 pmap_qremove(kva, npages);
1848 relpbuf(bp, &fuse_pbuf_freecnt);
1850 if (error && (uio.uio_resid == count)) {
1851 FS_DEBUG("error %d\n", error);
1852 return VM_PAGER_ERROR;
1855 * Calculate the number of bytes read and validate only that number
1856 * of bytes. Note that due to pending writes, size may be 0. This
1857 * does not mean that the remaining data is invalid!
1860 size = count - uio.uio_resid;
1861 VM_OBJECT_WLOCK(vp->v_object);
1862 fuse_vm_page_lock_queues();
1863 for (i = 0, toff = 0; i < npages; i++, toff = nextoff) {
1866 nextoff = toff + PAGE_SIZE;
1869 if (nextoff <= size) {
1871 * Read operation filled an entire page
1873 m->valid = VM_PAGE_BITS_ALL;
1874 KASSERT(m->dirty == 0,
1875 ("fuse_getpages: page %p is dirty", m));
1876 } else if (size > toff) {
1878 * Read operation filled a partial page.
1881 vm_page_set_valid_range(m, 0, size - toff);
1882 KASSERT(m->dirty == 0,
1883 ("fuse_getpages: page %p is dirty", m));
1886 * Read operation was short. If no error occurred
1887 * we may have hit a zero-fill section. We simply
1888 * leave valid set to 0.
1893 fuse_vm_page_unlock_queues();
1895 VM_OBJECT_WUNLOCK(vp->v_object);
1900 return (VM_PAGER_OK);
1904 struct vnop_putpages_args {
1910 vm_ooffset_t a_offset;
1914 fuse_vnop_putpages(struct vop_putpages_args *ap)
1920 int i, error, npages, count;
1929 FS_DEBUG2G("heh\n");
1932 KASSERT(vp->v_object, ("objectless vp passed to putpages"));
1933 fsize = vp->v_object->un_pager.vnp.vnp_size;
1934 td = curthread; /* XXX */
1935 cred = curthread->td_ucred; /* XXX */
1937 count = ap->a_count;
1938 rtvals = ap->a_rtvals;
1939 npages = btoc(count);
1940 offset = IDX_TO_OFF(pages[0]->pindex);
1942 if (!fsess_opt_mmap(vnode_mount(vp))) {
1943 FS_DEBUG("called on non-cacheable vnode??\n");
1945 for (i = 0; i < npages; i++)
1946 rtvals[i] = VM_PAGER_AGAIN;
1949 * When putting pages, do not extend file past EOF.
1952 if (offset + count > fsize) {
1953 count = fsize - offset;
1958 * We use only the kva address for the buffer, but this is extremely
1959 * convenient and fast.
1961 bp = getpbuf(&fuse_pbuf_freecnt);
1963 kva = (vm_offset_t)bp->b_data;
1964 pmap_qenter(kva, pages, npages);
1965 VM_CNT_INC(v_vnodeout);
1966 VM_CNT_ADD(v_vnodepgsout, count);
1968 iov.iov_base = (caddr_t)kva;
1969 iov.iov_len = count;
1972 uio.uio_offset = offset;
1973 uio.uio_resid = count;
1974 uio.uio_segflg = UIO_SYSSPACE;
1975 uio.uio_rw = UIO_WRITE;
1978 error = fuse_io_dispatch(vp, &uio, IO_DIRECT, cred);
1980 pmap_qremove(kva, npages);
1981 relpbuf(bp, &fuse_pbuf_freecnt);
1984 int nwritten = round_page(count - uio.uio_resid) / PAGE_SIZE;
1986 for (i = 0; i < nwritten; i++) {
1987 rtvals[i] = VM_PAGER_OK;
1988 VM_OBJECT_WLOCK(pages[i]->object);
1989 vm_page_undirty(pages[i]);
1990 VM_OBJECT_WUNLOCK(pages[i]->object);
1996 static const char extattr_namespace_separator = '.';
1999 struct vop_getextattr_args {
2000 struct vop_generic_args a_gen;
2002 int a_attrnamespace;
2006 struct ucred *a_cred;
2007 struct thread *a_td;
2011 fuse_vnop_getextattr(struct vop_getextattr_args *ap)
2013 struct vnode *vp = ap->a_vp;
2014 struct uio *uio = ap->a_uio;
2015 struct fuse_dispatcher fdi;
2016 struct fuse_getxattr_in *get_xattr_in;
2017 struct fuse_getxattr_out *get_xattr_out;
2018 struct mount *mp = vnode_mount(vp);
2019 struct thread *td = ap->a_td;
2020 struct ucred *cred = ap->a_cred;
2026 fuse_trace_printf_vnop();
2028 if (fuse_isdeadfs(vp))
2031 /* Default to looking for user attributes. */
2032 if (ap->a_attrnamespace == EXTATTR_NAMESPACE_SYSTEM)
2033 prefix = EXTATTR_NAMESPACE_SYSTEM_STRING;
2035 prefix = EXTATTR_NAMESPACE_USER_STRING;
2037 len = strlen(prefix) + sizeof(extattr_namespace_separator) +
2038 strlen(ap->a_name) + 1;
2040 fdisp_init(&fdi, len + sizeof(*get_xattr_in));
2041 fdisp_make_vp(&fdi, FUSE_GETXATTR, vp, td, cred);
2043 get_xattr_in = fdi.indata;
2045 * Check to see whether we're querying the available size or
2046 * issuing the actual request. If we pass in 0, we get back struct
2047 * fuse_getxattr_out. If we pass in a non-zero size, we get back
2048 * that much data, without the struct fuse_getxattr_out header.
2051 get_xattr_in->size = 0;
2053 get_xattr_in->size = uio->uio_resid;
2055 attr_str = (char *)fdi.indata + sizeof(*get_xattr_in);
2056 snprintf(attr_str, len, "%s%c%s", prefix, extattr_namespace_separator,
2059 err = fdisp_wait_answ(&fdi);
2062 fsess_set_notimpl(mp, FUSE_GETXATTR);
2063 debug_printf("getxattr: got err=%d from daemon\n", err);
2067 get_xattr_out = fdi.answ;
2069 if (ap->a_size != NULL)
2070 *ap->a_size = get_xattr_out->size;
2073 err = uiomove(fdi.answ, fdi.iosize, uio);
2076 fdisp_destroy(&fdi);
2081 struct vop_setextattr_args {
2082 struct vop_generic_args a_gen;
2084 int a_attrnamespace;
2087 struct ucred *a_cred;
2088 struct thread *a_td;
2092 fuse_vnop_setextattr(struct vop_setextattr_args *ap)
2094 struct vnode *vp = ap->a_vp;
2095 struct uio *uio = ap->a_uio;
2096 struct fuse_dispatcher fdi;
2097 struct fuse_setxattr_in *set_xattr_in;
2098 struct mount *mp = vnode_mount(vp);
2099 struct thread *td = ap->a_td;
2100 struct ucred *cred = ap->a_cred;
2106 fuse_trace_printf_vnop();
2108 if (fuse_isdeadfs(vp))
2111 /* Default to looking for user attributes. */
2112 if (ap->a_attrnamespace == EXTATTR_NAMESPACE_SYSTEM)
2113 prefix = EXTATTR_NAMESPACE_SYSTEM_STRING;
2115 prefix = EXTATTR_NAMESPACE_USER_STRING;
2117 len = strlen(prefix) + sizeof(extattr_namespace_separator) +
2118 strlen(ap->a_name) + 1;
2120 fdisp_init(&fdi, len + sizeof(*set_xattr_in) + uio->uio_resid);
2121 fdisp_make_vp(&fdi, FUSE_SETXATTR, vp, td, cred);
2123 set_xattr_in = fdi.indata;
2124 set_xattr_in->size = uio->uio_resid;
2126 attr_str = (char *)fdi.indata + sizeof(*set_xattr_in);
2127 snprintf(attr_str, len, "%s%c%s", prefix, extattr_namespace_separator,
2130 err = uiomove((char *)fdi.indata + sizeof(*set_xattr_in) + len,
2131 uio->uio_resid, uio);
2133 debug_printf("setxattr: got error %d from uiomove\n", err);
2137 err = fdisp_wait_answ(&fdi);
2141 fsess_set_notimpl(mp, FUSE_SETXATTR);
2142 debug_printf("setxattr: got err=%d from daemon\n", err);
2147 fdisp_destroy(&fdi);
2152 * The Linux / FUSE extended attribute list is simply a collection of
2153 * NUL-terminated strings. The FreeBSD extended attribute list is a single
2154 * byte length followed by a non-NUL terminated string. So, this allows
2155 * conversion of the Linux / FUSE format to the FreeBSD format in place.
2156 * Linux attribute names are reported with the namespace as a prefix (e.g.
2157 * "user.attribute_name"), but in FreeBSD they are reported without the
2158 * namespace prefix (e.g. "attribute_name"). So, we're going from:
2160 * user.attr_name1\0user.attr_name2\0
2164 * <num>attr_name1<num>attr_name2
2166 * Where "<num>" is a single byte number of characters in the attribute name.
2169 * prefix - exattr namespace prefix string
2170 * list, list_len - input list with namespace prefixes
2171 * bsd_list, bsd_list_len - output list compatible with bsd vfs
2174 fuse_xattrlist_convert(char *prefix, const char *list, int list_len,
2175 char *bsd_list, int *bsd_list_len)
2177 int len, pos, dist_to_next, prefix_len;
2181 prefix_len = strlen(prefix);
2183 while (pos < list_len && list[pos] != '\0') {
2184 dist_to_next = strlen(&list[pos]) + 1;
2185 if (bcmp(&list[pos], prefix, prefix_len) == 0 &&
2186 list[pos + prefix_len] == extattr_namespace_separator) {
2187 len = dist_to_next -
2188 (prefix_len + sizeof(extattr_namespace_separator)) - 1;
2189 if (len >= EXTATTR_MAXNAMELEN)
2190 return (ENAMETOOLONG);
2192 bsd_list[*bsd_list_len] = len;
2193 memcpy(&bsd_list[*bsd_list_len + 1],
2194 &list[pos + prefix_len +
2195 sizeof(extattr_namespace_separator)], len);
2197 *bsd_list_len += len + 1;
2200 pos += dist_to_next;
2207 struct vop_listextattr_args {
2208 struct vop_generic_args a_gen;
2210 int a_attrnamespace;
2213 struct ucred *a_cred;
2214 struct thread *a_td;
2218 fuse_vnop_listextattr(struct vop_listextattr_args *ap)
2220 struct vnode *vp = ap->a_vp;
2221 struct uio *uio = ap->a_uio;
2222 struct fuse_dispatcher fdi;
2223 struct fuse_listxattr_in *list_xattr_in;
2224 struct fuse_listxattr_out *list_xattr_out;
2225 struct mount *mp = vnode_mount(vp);
2226 struct thread *td = ap->a_td;
2227 struct ucred *cred = ap->a_cred;
2231 char *bsd_list = NULL;
2237 fuse_trace_printf_vnop();
2239 if (fuse_isdeadfs(vp))
2243 * Add space for a NUL and the period separator if enabled.
2244 * Default to looking for user attributes.
2246 if (ap->a_attrnamespace == EXTATTR_NAMESPACE_SYSTEM)
2247 prefix = EXTATTR_NAMESPACE_SYSTEM_STRING;
2249 prefix = EXTATTR_NAMESPACE_USER_STRING;
2251 len = strlen(prefix) + sizeof(extattr_namespace_separator) + 1;
2253 fdisp_init(&fdi, sizeof(*list_xattr_in) + len);
2254 fdisp_make_vp(&fdi, FUSE_LISTXATTR, vp, td, cred);
2257 * Retrieve Linux / FUSE compatible list size.
2259 list_xattr_in = fdi.indata;
2260 list_xattr_in->size = 0;
2261 attr_str = (char *)fdi.indata + sizeof(*list_xattr_in);
2262 snprintf(attr_str, len, "%s%c", prefix, extattr_namespace_separator);
2264 err = fdisp_wait_answ(&fdi);
2267 fsess_set_notimpl(mp, FUSE_LISTXATTR);
2268 debug_printf("listextattr: got err=%d from daemon\n", err);
2272 list_xattr_out = fdi.answ;
2273 linux_list_len = list_xattr_out->size;
2274 if (linux_list_len == 0) {
2275 if (ap->a_size != NULL)
2276 *ap->a_size = linux_list_len;
2281 * Retrieve Linux / FUSE compatible list values.
2283 fdisp_make_vp(&fdi, FUSE_LISTXATTR, vp, td, cred);
2284 list_xattr_in = fdi.indata;
2285 list_xattr_in->size = linux_list_len + sizeof(*list_xattr_out);
2286 attr_str = (char *)fdi.indata + sizeof(*list_xattr_in);
2287 snprintf(attr_str, len, "%s%c", prefix, extattr_namespace_separator);
2289 err = fdisp_wait_answ(&fdi);
2293 linux_list = fdi.answ;
2294 linux_list_len = fdi.iosize;
2297 * Retrieve the BSD compatible list values.
2298 * The Linux / FUSE attribute list format isn't the same
2299 * as FreeBSD's format. So we need to transform it into
2300 * FreeBSD's format before giving it to the user.
2302 bsd_list = malloc(linux_list_len, M_TEMP, M_WAITOK);
2303 err = fuse_xattrlist_convert(prefix, linux_list, linux_list_len,
2304 bsd_list, &bsd_list_len);
2308 if (ap->a_size != NULL)
2309 *ap->a_size = bsd_list_len;
2312 err = uiomove(bsd_list, bsd_list_len, uio);
2315 free(bsd_list, M_TEMP);
2316 fdisp_destroy(&fdi);
2321 struct vop_deleteextattr_args {
2322 struct vop_generic_args a_gen;
2324 int a_attrnamespace;
2326 struct ucred *a_cred;
2327 struct thread *a_td;
2331 fuse_vnop_deleteextattr(struct vop_deleteextattr_args *ap)
2333 struct vnode *vp = ap->a_vp;
2334 struct fuse_dispatcher fdi;
2335 struct mount *mp = vnode_mount(vp);
2336 struct thread *td = ap->a_td;
2337 struct ucred *cred = ap->a_cred;
2343 fuse_trace_printf_vnop();
2345 if (fuse_isdeadfs(vp))
2348 /* Default to looking for user attributes. */
2349 if (ap->a_attrnamespace == EXTATTR_NAMESPACE_SYSTEM)
2350 prefix = EXTATTR_NAMESPACE_SYSTEM_STRING;
2352 prefix = EXTATTR_NAMESPACE_USER_STRING;
2354 len = strlen(prefix) + sizeof(extattr_namespace_separator) +
2355 strlen(ap->a_name) + 1;
2357 fdisp_init(&fdi, len);
2358 fdisp_make_vp(&fdi, FUSE_REMOVEXATTR, vp, td, cred);
2360 attr_str = fdi.indata;
2361 snprintf(attr_str, len, "%s%c%s", prefix, extattr_namespace_separator,
2364 err = fdisp_wait_answ(&fdi);
2367 fsess_set_notimpl(mp, FUSE_REMOVEXATTR);
2368 debug_printf("removexattr: got err=%d from daemon\n", err);
2371 fdisp_destroy(&fdi);
2376 struct vnop_print_args {
2381 fuse_vnop_print(struct vop_print_args *ap)
2383 struct fuse_vnode_data *fvdat = VTOFUD(ap->a_vp);
2385 printf("nodeid: %ju, parent nodeid: %ju, nlookup: %ju, flag: %#x\n",
2386 (uintmax_t)VTOILLU(ap->a_vp), (uintmax_t)fvdat->parent_nid,
2387 (uintmax_t)fvdat->nlookup,