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/param.h>
62 #include <sys/module.h>
63 #include <sys/systm.h>
64 #include <sys/errno.h>
65 #include <sys/kernel.h>
68 #include <sys/malloc.h>
69 #include <sys/queue.h>
71 #include <sys/rwlock.h>
74 #include <sys/mount.h>
75 #include <sys/vnode.h>
76 #include <sys/namei.h>
77 #include <sys/extattr.h>
79 #include <sys/unistd.h>
80 #include <sys/filedesc.h>
82 #include <sys/fcntl.h>
83 #include <sys/dirent.h>
86 #include <sys/sysctl.h>
87 #include <sys/vmmeter.h>
90 #include <vm/vm_extern.h>
92 #include <vm/vm_map.h>
93 #include <vm/vm_page.h>
94 #include <vm/vm_param.h>
95 #include <vm/vm_object.h>
96 #include <vm/vm_pager.h>
97 #include <vm/vnode_pager.h>
98 #include <vm/vm_object.h>
101 #include "fuse_file.h"
102 #include "fuse_internal.h"
103 #include "fuse_ipc.h"
104 #include "fuse_node.h"
107 #include <sys/priv.h>
109 /* Maximum number of hardlinks to a single FUSE file */
110 #define FUSE_LINK_MAX UINT32_MAX
112 SDT_PROVIDER_DECLARE(fuse);
115 * arg0: verbosity. Higher numbers give more verbose messages
116 * arg1: Textual message
118 SDT_PROBE_DEFINE2(fuse, , vnops, trace, "int", "char*");
121 static vop_access_t fuse_vnop_access;
122 static vop_close_t fuse_vnop_close;
123 static vop_create_t fuse_vnop_create;
124 static vop_deleteextattr_t fuse_vnop_deleteextattr;
125 static vop_fdatasync_t fuse_vnop_fdatasync;
126 static vop_fsync_t fuse_vnop_fsync;
127 static vop_getattr_t fuse_vnop_getattr;
128 static vop_getextattr_t fuse_vnop_getextattr;
129 static vop_inactive_t fuse_vnop_inactive;
130 static vop_link_t fuse_vnop_link;
131 static vop_listextattr_t fuse_vnop_listextattr;
132 static vop_lookup_t fuse_vnop_lookup;
133 static vop_mkdir_t fuse_vnop_mkdir;
134 static vop_mknod_t fuse_vnop_mknod;
135 static vop_open_t fuse_vnop_open;
136 static vop_pathconf_t fuse_vnop_pathconf;
137 static vop_read_t fuse_vnop_read;
138 static vop_readdir_t fuse_vnop_readdir;
139 static vop_readlink_t fuse_vnop_readlink;
140 static vop_reclaim_t fuse_vnop_reclaim;
141 static vop_remove_t fuse_vnop_remove;
142 static vop_rename_t fuse_vnop_rename;
143 static vop_rmdir_t fuse_vnop_rmdir;
144 static vop_setattr_t fuse_vnop_setattr;
145 static vop_setextattr_t fuse_vnop_setextattr;
146 static vop_strategy_t fuse_vnop_strategy;
147 static vop_symlink_t fuse_vnop_symlink;
148 static vop_write_t fuse_vnop_write;
149 static vop_getpages_t fuse_vnop_getpages;
150 static vop_putpages_t fuse_vnop_putpages;
151 static vop_print_t fuse_vnop_print;
153 struct vop_vector fuse_vnops = {
154 .vop_default = &default_vnodeops,
155 .vop_access = fuse_vnop_access,
156 .vop_close = fuse_vnop_close,
157 .vop_create = fuse_vnop_create,
158 .vop_deleteextattr = fuse_vnop_deleteextattr,
159 .vop_fsync = fuse_vnop_fsync,
160 .vop_fdatasync = fuse_vnop_fdatasync,
161 .vop_getattr = fuse_vnop_getattr,
162 .vop_getextattr = fuse_vnop_getextattr,
163 .vop_inactive = fuse_vnop_inactive,
164 .vop_link = fuse_vnop_link,
165 .vop_listextattr = fuse_vnop_listextattr,
166 .vop_lookup = fuse_vnop_lookup,
167 .vop_mkdir = fuse_vnop_mkdir,
168 .vop_mknod = fuse_vnop_mknod,
169 .vop_open = fuse_vnop_open,
170 .vop_pathconf = fuse_vnop_pathconf,
171 .vop_read = fuse_vnop_read,
172 .vop_readdir = fuse_vnop_readdir,
173 .vop_readlink = fuse_vnop_readlink,
174 .vop_reclaim = fuse_vnop_reclaim,
175 .vop_remove = fuse_vnop_remove,
176 .vop_rename = fuse_vnop_rename,
177 .vop_rmdir = fuse_vnop_rmdir,
178 .vop_setattr = fuse_vnop_setattr,
179 .vop_setextattr = fuse_vnop_setextattr,
180 .vop_strategy = fuse_vnop_strategy,
181 .vop_symlink = fuse_vnop_symlink,
182 .vop_write = fuse_vnop_write,
183 .vop_getpages = fuse_vnop_getpages,
184 .vop_putpages = fuse_vnop_putpages,
185 .vop_print = fuse_vnop_print,
188 static u_long fuse_lookup_cache_hits = 0;
190 SYSCTL_ULONG(_vfs_fusefs, OID_AUTO, lookup_cache_hits, CTLFLAG_RD,
191 &fuse_lookup_cache_hits, 0, "number of positive cache hits in lookup");
193 static u_long fuse_lookup_cache_misses = 0;
195 SYSCTL_ULONG(_vfs_fusefs, OID_AUTO, lookup_cache_misses, CTLFLAG_RD,
196 &fuse_lookup_cache_misses, 0, "number of cache misses in lookup");
198 int fuse_lookup_cache_enable = 1;
200 SYSCTL_INT(_vfs_fusefs, OID_AUTO, lookup_cache_enable, CTLFLAG_RW,
201 &fuse_lookup_cache_enable, 0, "if non-zero, enable lookup cache");
204 * XXX: This feature is highly experimental and can bring to instabilities,
205 * needs revisiting before to be enabled by default.
207 static int fuse_reclaim_revoked = 0;
209 SYSCTL_INT(_vfs_fusefs, OID_AUTO, reclaim_revoked, CTLFLAG_RW,
210 &fuse_reclaim_revoked, 0, "");
212 uma_zone_t fuse_pbuf_zone;
214 #define fuse_vm_page_lock(m) vm_page_lock((m));
215 #define fuse_vm_page_unlock(m) vm_page_unlock((m));
216 #define fuse_vm_page_lock_queues() ((void)0)
217 #define fuse_vm_page_unlock_queues() ((void)0)
219 /* Get a filehandle for a directory */
221 fuse_filehandle_get_dir(struct vnode *vp, struct fuse_filehandle **fufhp,
222 struct ucred *cred, pid_t pid)
224 if (fuse_filehandle_get(vp, FUFH_RDONLY, fufhp, cred, pid) == 0)
226 return fuse_filehandle_get(vp, FUFH_EXEC, fufhp, cred, pid);
230 struct vnop_access_args {
232 #if VOP_ACCESS_TAKES_ACCMODE_T
237 struct ucred *a_cred;
242 fuse_vnop_access(struct vop_access_args *ap)
244 struct vnode *vp = ap->a_vp;
245 int accmode = ap->a_accmode;
246 struct ucred *cred = ap->a_cred;
248 struct fuse_access_param facp;
249 struct fuse_data *data = fuse_get_mpdata(vnode_mount(vp));
253 if (fuse_isdeadfs(vp)) {
254 if (vnode_isvroot(vp)) {
259 if (!(data->dataflags & FSESS_INITED)) {
260 if (vnode_isvroot(vp)) {
261 if (priv_check_cred(cred, PRIV_VFS_ADMIN) ||
262 (fuse_match_cred(data->daemoncred, cred) == 0)) {
268 if (vnode_islnk(vp)) {
271 bzero(&facp, sizeof(facp));
273 err = fuse_internal_access(vp, accmode, &facp, ap->a_td, ap->a_cred);
278 struct vnop_close_args {
281 struct ucred *a_cred;
286 fuse_vnop_close(struct vop_close_args *ap)
288 struct vnode *vp = ap->a_vp;
289 struct ucred *cred = ap->a_cred;
290 int fflag = ap->a_fflag;
291 struct thread *td = ap->a_td;
292 pid_t pid = td->td_proc->p_pid;
294 if (fuse_isdeadfs(vp)) {
297 if (vnode_isdir(vp)) {
298 struct fuse_filehandle *fufh;
300 if (fuse_filehandle_get_dir(vp, &fufh, cred, pid) == 0)
301 fuse_filehandle_close(vp, fufh, NULL, cred);
304 if (fflag & IO_NDELAY) {
307 /* TODO: close the file handle, if we're sure it's no longer used */
308 if ((VTOFUD(vp)->flag & FN_SIZECHANGE) != 0) {
309 fuse_vnode_savesize(vp, cred, td->td_proc->p_pid);
315 fdisp_make_mknod_for_fallback(
316 struct fuse_dispatcher *fdip,
317 struct componentname *cnp,
323 enum fuse_opcode *op)
325 struct fuse_mknod_in *fmni;
327 fdisp_init(fdip, sizeof(*fmni) + cnp->cn_namelen + 1);
329 fdisp_make(fdip, *op, vnode_mount(dvp), parentnid, td, cred);
333 memcpy((char *)fdip->indata + sizeof(*fmni), cnp->cn_nameptr,
335 ((char *)fdip->indata)[sizeof(*fmni) + cnp->cn_namelen] = '\0';
338 struct vnop_create_args {
340 struct vnode **a_vpp;
341 struct componentname *a_cnp;
346 fuse_vnop_create(struct vop_create_args *ap)
348 struct vnode *dvp = ap->a_dvp;
349 struct vnode **vpp = ap->a_vpp;
350 struct componentname *cnp = ap->a_cnp;
351 struct vattr *vap = ap->a_vap;
352 struct thread *td = cnp->cn_thread;
353 struct ucred *cred = cnp->cn_cred;
355 struct fuse_open_in *foi;
356 struct fuse_entry_out *feo;
357 struct fuse_open_out *foo;
358 struct fuse_dispatcher fdi, fdi2;
359 struct fuse_dispatcher *fdip = &fdi;
360 struct fuse_dispatcher *fdip2 = NULL;
364 struct mount *mp = vnode_mount(dvp);
365 uint64_t parentnid = VTOFUD(dvp)->nid;
366 mode_t mode = MAKEIMODE(vap->va_type, vap->va_mode);
369 if (fuse_isdeadfs(dvp)) {
372 bzero(&fdi, sizeof(fdi));
374 if ((vap->va_type != VREG))
377 if (!fsess_isimpl(mp, FUSE_CREATE)) {
378 /* Fallback to FUSE_MKNOD/FUSE_OPEN */
379 fdisp_make_mknod_for_fallback(fdip, cnp, dvp, parentnid, td,
382 /* Use FUSE_CREATE */
384 fdisp_init(fdip, sizeof(*foi) + cnp->cn_namelen + 1);
385 fdisp_make(fdip, op, vnode_mount(dvp), parentnid, td, cred);
388 foi->flags = O_CREAT | O_RDWR;
389 memcpy((char *)fdip->indata + sizeof(*foi), cnp->cn_nameptr,
391 ((char *)fdip->indata)[sizeof(*foi) + cnp->cn_namelen] = '\0';
394 err = fdisp_wait_answ(fdip);
397 if (err == ENOSYS && op == FUSE_CREATE) {
398 fsess_set_notimpl(mp, FUSE_CREATE);
399 fdisp_make_mknod_for_fallback(fdip, cnp, dvp,
400 parentnid, td, cred, mode, &op);
401 err = fdisp_wait_answ(fdip);
409 if ((err = fuse_internal_checkentry(feo, VREG))) {
413 if (op == FUSE_CREATE) {
414 foo = (struct fuse_open_out*)(feo + 1);
416 /* Issue a separate FUSE_OPEN */
418 fdisp_init(fdip2, sizeof(*foi));
419 fdisp_make(fdip2, FUSE_OPEN, vnode_mount(dvp), feo->nodeid, td,
424 err = fdisp_wait_answ(fdip2);
429 err = fuse_vnode_get(mp, feo, feo->nodeid, dvp, vpp, cnp, VREG);
431 struct fuse_release_in *fri;
432 uint64_t nodeid = feo->nodeid;
433 uint64_t fh_id = foo->fh;
435 fdisp_init(fdip, sizeof(*fri));
436 fdisp_make(fdip, FUSE_RELEASE, mp, nodeid, td, cred);
439 fri->flags = fuse_filehandle_xlate_to_oflags(FUFH_RDWR);
440 fuse_insert_callback(fdip->tick, fuse_internal_forget_callback);
441 fuse_insert_message(fdip->tick);
444 ASSERT_VOP_ELOCKED(*vpp, "fuse_vnop_create");
446 fuse_filehandle_init(*vpp, FUFH_RDWR, NULL, td->td_proc->p_pid, cred,
448 fuse_vnode_open(*vpp, foo->open_flags, td);
449 cache_purge_negative(dvp);
453 fdisp_destroy(fdip2);
459 struct vnop_fdatasync_args {
460 struct vop_generic_args a_gen;
462 struct thread * a_td;
466 fuse_vnop_fdatasync(struct vop_fdatasync_args *ap)
468 struct vnode *vp = ap->a_vp;
469 struct thread *td = ap->a_td;
470 int waitfor = MNT_WAIT;
474 if (fuse_isdeadfs(vp)) {
477 if ((err = vop_stdfdatasync_buf(ap)))
480 return fuse_internal_fsync(vp, td, waitfor, true);
484 struct vnop_fsync_args {
485 struct vop_generic_args a_gen;
488 struct thread * a_td;
492 fuse_vnop_fsync(struct vop_fsync_args *ap)
494 struct vnode *vp = ap->a_vp;
495 struct thread *td = ap->a_td;
496 int waitfor = ap->a_waitfor;
499 if (fuse_isdeadfs(vp)) {
502 if ((err = vop_stdfsync(ap)))
505 return fuse_internal_fsync(vp, td, waitfor, false);
509 struct vnop_getattr_args {
512 struct ucred *a_cred;
517 fuse_vnop_getattr(struct vop_getattr_args *ap)
519 struct vnode *vp = ap->a_vp;
520 struct vattr *vap = ap->a_vap;
521 struct ucred *cred = ap->a_cred;
522 struct thread *td = curthread;
523 struct fuse_vnode_data *fvdat = VTOFUD(vp);
524 struct fuse_attr_out *fao;
528 struct fuse_dispatcher fdi;
530 dataflags = fuse_get_mpdata(vnode_mount(vp))->dataflags;
532 /* Note that we are not bailing out on a dead file system just yet. */
534 if (!(dataflags & FSESS_INITED)) {
535 if (!vnode_isvroot(vp)) {
536 fdata_set_dead(fuse_get_mpdata(vnode_mount(vp)));
544 if ((err = fdisp_simple_putget_vp(&fdi, FUSE_GETATTR, vp, td, cred))) {
545 if ((err == ENOTCONN) && vnode_isvroot(vp)) {
546 /* see comment in fuse_vfsop_statfs() */
551 fuse_internal_vnode_disappear(vp);
556 fao = (struct fuse_attr_out *)fdi.answ;
557 fuse_internal_cache_attrs(vp, &fao->attr, fao->attr_valid,
558 fao->attr_valid_nsec, vap);
559 if (vap->va_type != vnode_vtype(vp)) {
560 fuse_internal_vnode_disappear(vp);
564 if ((fvdat->flag & FN_SIZECHANGE) != 0)
565 vap->va_size = fvdat->filesize;
567 if (vnode_isreg(vp) && (fvdat->flag & FN_SIZECHANGE) == 0) {
569 * This is for those cases when the file size changed without us
570 * knowing, and we want to catch up.
572 off_t new_filesize = ((struct fuse_attr_out *)
573 fdi.answ)->attr.size;
575 if (fvdat->filesize != new_filesize) {
576 fuse_vnode_setsize(vp, cred, new_filesize);
577 fvdat->flag &= ~FN_SIZECHANGE;
586 bzero(vap, sizeof(*vap));
587 vap->va_type = vnode_vtype(vp);
593 struct vnop_inactive_args {
599 fuse_vnop_inactive(struct vop_inactive_args *ap)
601 struct vnode *vp = ap->a_vp;
602 struct thread *td = ap->a_td;
604 struct fuse_vnode_data *fvdat = VTOFUD(vp);
605 struct fuse_filehandle *fufh, *fufh_tmp;
609 LIST_FOREACH_SAFE(fufh, &fvdat->handles, next, fufh_tmp) {
610 if (need_flush && vp->v_type == VREG) {
611 if ((VTOFUD(vp)->flag & FN_SIZECHANGE) != 0) {
612 fuse_vnode_savesize(vp, NULL, 0);
614 if (fuse_data_cache_invalidate ||
615 (fvdat->flag & FN_REVOKED) != 0)
616 fuse_io_invalbuf(vp, td);
618 fuse_io_flushbuf(vp, MNT_WAIT, td);
621 fuse_filehandle_close(vp, fufh, td, NULL);
624 if ((fvdat->flag & FN_REVOKED) != 0 && fuse_reclaim_revoked) {
631 struct vnop_link_args {
632 struct vnode *a_tdvp;
634 struct componentname *a_cnp;
638 fuse_vnop_link(struct vop_link_args *ap)
640 struct vnode *vp = ap->a_vp;
641 struct vnode *tdvp = ap->a_tdvp;
642 struct componentname *cnp = ap->a_cnp;
644 struct vattr *vap = VTOVA(vp);
646 struct fuse_dispatcher fdi;
647 struct fuse_entry_out *feo;
648 struct fuse_link_in fli;
652 if (fuse_isdeadfs(vp)) {
655 if (vnode_mount(tdvp) != vnode_mount(vp)) {
660 * This is a seatbelt check to protect naive userspace filesystems from
661 * themselves and the limitations of the FUSE IPC protocol. If a
662 * filesystem does not allow attribute caching, assume it is capable of
663 * validating that nlink does not overflow.
665 if (vap != NULL && vap->va_nlink >= FUSE_LINK_MAX)
667 fli.oldnodeid = VTOI(vp);
670 fuse_internal_newentry_makerequest(vnode_mount(tdvp), VTOI(tdvp), cnp,
671 FUSE_LINK, &fli, sizeof(fli), &fdi);
672 if ((err = fdisp_wait_answ(&fdi))) {
677 err = fuse_internal_checkentry(feo, vnode_vtype(vp));
684 struct vnop_lookup_args {
685 struct vnodeop_desc *a_desc;
687 struct vnode **a_vpp;
688 struct componentname *a_cnp;
692 fuse_vnop_lookup(struct vop_lookup_args *ap)
694 struct vnode *dvp = ap->a_dvp;
695 struct vnode **vpp = ap->a_vpp;
696 struct componentname *cnp = ap->a_cnp;
697 struct thread *td = cnp->cn_thread;
698 struct ucred *cred = cnp->cn_cred;
700 int nameiop = cnp->cn_nameiop;
701 int flags = cnp->cn_flags;
702 int wantparent = flags & (LOCKPARENT | WANTPARENT);
703 int islastcn = flags & ISLASTCN;
704 struct mount *mp = vnode_mount(dvp);
708 struct vnode *vp = NULL;
710 struct fuse_dispatcher fdi;
714 struct fuse_access_param facp;
716 if (fuse_isdeadfs(dvp)) {
720 if (!vnode_isdir(dvp)) {
723 if (islastcn && vfs_isrdonly(mp) && (nameiop != LOOKUP)) {
727 * We do access check prior to doing anything else only in the case
728 * when we are at fs root (we'd like to say, "we are at the first
729 * component", but that's not exactly the same... nevermind).
730 * See further comments at further access checks.
733 bzero(&facp, sizeof(facp));
734 if (vnode_isvroot(dvp)) { /* early permission check hack */
735 if ((err = fuse_internal_access(dvp, VEXEC, &facp, td, cred))) {
739 if (flags & ISDOTDOT) {
740 nid = VTOFUD(dvp)->parent_nid;
747 } else if (cnp->cn_namelen == 1 && *(cnp->cn_nameptr) == '.') {
752 } else if (fuse_lookup_cache_enable) {
753 err = cache_lookup(dvp, vpp, cnp, NULL, NULL);
756 case -1: /* positive match */
757 atomic_add_acq_long(&fuse_lookup_cache_hits, 1);
760 case 0: /* no match in cache */
761 atomic_add_acq_long(&fuse_lookup_cache_misses, 1);
764 case ENOENT: /* negative match */
771 fdisp_init(&fdi, cnp->cn_namelen + 1);
775 fdisp_make(&fdi, op, mp, nid, td, cred);
777 if (op == FUSE_LOOKUP) {
778 memcpy(fdi.indata, cnp->cn_nameptr, cnp->cn_namelen);
779 ((char *)fdi.indata)[cnp->cn_namelen] = '\0';
781 lookup_err = fdisp_wait_answ(&fdi);
783 if ((op == FUSE_LOOKUP) && !lookup_err) { /* lookup call succeeded */
784 nid = ((struct fuse_entry_out *)fdi.answ)->nodeid;
787 * zero nodeid is the same as "not found",
788 * but it's also cacheable (which we keep
789 * keep on doing not as of writing this)
791 fdi.answ_stat = ENOENT;
793 } else if (nid == FUSE_ROOT_ID) {
798 (!fdi.answ_stat || lookup_err != ENOENT || op != FUSE_LOOKUP)) {
802 /* lookup_err, if non-zero, must be ENOENT at this point */
806 if ((nameiop == CREATE || nameiop == RENAME) && islastcn
807 /* && directory dvp has not been removed */ ) {
809 if (vfs_isrdonly(mp)) {
813 #if 0 /* THINK_ABOUT_THIS */
814 if ((err = fuse_internal_access(dvp, VWRITE, cred, td, &facp))) {
820 * Possibly record the position of a slot in the
821 * directory large enough for the new component name.
822 * This can be recorded in the vnode private data for
823 * dvp. Set the SAVENAME flag to hold onto the
824 * pathname for use later in VOP_CREATE or VOP_RENAME.
826 cnp->cn_flags |= SAVENAME;
831 /* Consider inserting name into cache. */
834 * No we can't use negative caching, as the fs
835 * changes are out of our control.
836 * False positives' falseness turns out just as things
837 * go by, but false negatives' falseness doesn't.
838 * (and aiding the caching mechanism with extra control
839 * mechanisms comes quite close to beating the whole purpose
843 if ((cnp->cn_flags & MAKEENTRY) != 0) {
844 SDT_PROBE2(fuse, , vnops, trace, 1,
845 "inserting NULL into cache");
846 cache_enter(dvp, NULL, cnp);
856 struct fuse_entry_out *feo = NULL;
857 struct fuse_attr *fattr = NULL;
859 if (op == FUSE_GETATTR) {
860 fattr = &((struct fuse_attr_out *)fdi.answ)->attr;
862 feo = (struct fuse_entry_out *)fdi.answ;
863 fattr = &(feo->attr);
867 * If deleting, and at end of pathname, return parameters
868 * which can be used to remove file. If the wantparent flag
869 * isn't set, we return only the directory, otherwise we go on
870 * and lock the inode, being careful with ".".
872 if (nameiop == DELETE && islastcn) {
874 * Check for write access on directory.
876 facp.xuid = fattr->uid;
877 facp.facc_flags |= FACCESS_STICKY;
878 err = fuse_internal_access(dvp, VWRITE, &facp, td, cred);
879 facp.facc_flags &= ~FACCESS_XQUERIES;
884 if (nid == VTOI(dvp)) {
888 err = fuse_vnode_get(dvp->v_mount, feo, nid,
889 dvp, &vp, cnp, IFTOVT(fattr->mode));
896 * Save the name for use in VOP_RMDIR and VOP_REMOVE
899 cnp->cn_flags |= SAVENAME;
904 * If rewriting (RENAME), return the inode and the
905 * information required to rewrite the present directory
906 * Must get inode of directory entry to verify it's a
907 * regular file, or empty directory.
909 if (nameiop == RENAME && wantparent && islastcn) {
911 #if 0 /* THINK_ABOUT_THIS */
912 if ((err = fuse_internal_access(dvp, VWRITE, cred, td, &facp))) {
920 if (nid == VTOI(dvp)) {
924 err = fuse_vnode_get(vnode_mount(dvp), feo, nid, dvp,
925 &vp, cnp, IFTOVT(fattr->mode));
931 * Save the name for use in VOP_RENAME later.
933 cnp->cn_flags |= SAVENAME;
937 if (flags & ISDOTDOT) {
942 * Expanded copy of vn_vget_ino() so that
943 * fuse_vnode_get() can be used.
946 ltype = VOP_ISLOCKED(dvp);
947 err = vfs_busy(mp, MBF_NOWAIT);
951 err = vfs_busy(mp, 0);
952 vn_lock(dvp, ltype | LK_RETRY);
956 if ((dvp->v_iflag & VI_DOOMED) != 0) {
963 err = fuse_vnode_get(vnode_mount(dvp), feo, nid, NULL,
964 &vp, cnp, IFTOVT(fattr->mode));
966 vn_lock(dvp, ltype | LK_RETRY);
967 if ((dvp->v_iflag & VI_DOOMED) != 0) {
975 } else if (nid == VTOI(dvp)) {
979 struct fuse_vnode_data *fvdat;
981 err = fuse_vnode_get(vnode_mount(dvp), feo, nid, dvp,
982 &vp, cnp, IFTOVT(fattr->mode));
986 fuse_vnode_setparent(vp, dvp);
989 * In the case where we are looking up a FUSE node
990 * represented by an existing cached vnode, and the
991 * true size reported by FUSE_LOOKUP doesn't match
992 * the vnode's cached size, fix the vnode cache to
993 * match the real object size.
995 * This can occur via FUSE distributed filesystems,
996 * irregular files, etc.
999 if (vnode_isreg(vp) &&
1000 fattr->size != fvdat->filesize) {
1002 * The FN_SIZECHANGE flag reflects a dirty
1003 * append. If userspace lets us know our cache
1004 * is invalid, that write was lost. (Dirty
1005 * writes that do not cause append are also
1006 * lost, but we don't detect them here.)
1008 * XXX: Maybe disable WB caching on this mount.
1010 if (fvdat->flag & FN_SIZECHANGE)
1011 printf("%s: WB cache incoherent on "
1013 vnode_mount(vp)->mnt_stat.f_mntonname);
1015 (void)fuse_vnode_setsize(vp, cred, fattr->size);
1016 fvdat->flag &= ~FN_SIZECHANGE;
1021 if (op == FUSE_GETATTR) {
1022 struct fuse_attr_out *fao =
1023 (struct fuse_attr_out*)fdi.answ;
1024 fuse_internal_cache_attrs(*vpp,
1025 &fao->attr, fao->attr_valid,
1026 fao->attr_valid_nsec, NULL);
1028 struct fuse_entry_out *feo =
1029 (struct fuse_entry_out*)fdi.answ;
1030 fuse_internal_cache_attrs(*vpp,
1031 &feo->attr, feo->attr_valid,
1032 feo->attr_valid_nsec, NULL);
1035 /* Insert name into cache if appropriate. */
1038 * Nooo, caching is evil. With caching, we can't avoid stale
1039 * information taking over the playground (cached info is not
1040 * just positive/negative, it does have qualitative aspects,
1041 * too). And a (VOP/FUSE)_GETATTR is always thrown anyway, when
1042 * walking down along cached path components, and that's not
1043 * any cheaper than FUSE_LOOKUP. This might change with
1044 * implementing kernel side attr caching, but... In Linux,
1045 * lookup results are not cached, and the daemon is bombarded
1046 * with FUSE_LOOKUPS on and on. This shows that by design, the
1047 * daemon is expected to handle frequent lookup queries
1048 * efficiently, do its caching in userspace, and so on.
1050 * So just leave the name cache alone.
1054 * Well, now I know, Linux caches lookups, but with a
1055 * timeout... So it's the same thing as attribute caching:
1056 * we can deal with it when implement timeouts.
1059 if (cnp->cn_flags & MAKEENTRY) {
1060 cache_enter(dvp, *vpp, cnp);
1067 /* No lookup error; need to clean up. */
1069 if (err) { /* Found inode; exit with no vnode. */
1070 if (op == FUSE_LOOKUP) {
1071 fuse_internal_forget_send(vnode_mount(dvp), td, cred,
1074 fdisp_destroy(&fdi);
1077 #ifndef NO_EARLY_PERM_CHECK_HACK
1080 * We have the attributes of the next item
1081 * *now*, and it's a fact, and we do not
1082 * have to do extra work for it (ie, beg the
1083 * daemon), and it neither depends on such
1084 * accidental things like attr caching. So
1085 * the big idea: check credentials *now*,
1086 * not at the beginning of the next call to
1089 * The first item of the lookup chain (fs root)
1090 * won't be checked then here, of course, as
1091 * its never "the next". But go and see that
1092 * the root is taken care about at the very
1093 * beginning of this function.
1095 * Now, given we want to do the access check
1096 * this way, one might ask: so then why not
1097 * do the access check just after fetching
1098 * the inode and its attributes from the
1099 * daemon? Why bother with producing the
1100 * corresponding vnode at all if something
1101 * is not OK? We know what's the deal as
1102 * soon as we get those attrs... There is
1103 * one bit of info though not given us by
1104 * the daemon: whether his response is
1105 * authoritative or not... His response should
1106 * be ignored if something is mounted over
1107 * the dir in question. But that can be
1108 * known only by having the vnode...
1110 int tmpvtype = vnode_vtype(*vpp);
1112 bzero(&facp, sizeof(facp));
1113 /*the early perm check hack */
1114 facp.facc_flags |= FACCESS_VA_VALID;
1116 if ((tmpvtype != VDIR) && (tmpvtype != VLNK)) {
1119 if (!err && !vnode_mountedhere(*vpp)) {
1120 err = fuse_internal_access(*vpp, VEXEC, &facp, td, cred);
1123 if (tmpvtype == VLNK)
1124 SDT_PROBE2(fuse, , vnops, trace,
1125 1, "weird, permission "
1126 "error with a symlink?");
1134 fdisp_destroy(&fdi);
1140 struct vnop_mkdir_args {
1141 struct vnode *a_dvp;
1142 struct vnode **a_vpp;
1143 struct componentname *a_cnp;
1144 struct vattr *a_vap;
1148 fuse_vnop_mkdir(struct vop_mkdir_args *ap)
1150 struct vnode *dvp = ap->a_dvp;
1151 struct vnode **vpp = ap->a_vpp;
1152 struct componentname *cnp = ap->a_cnp;
1153 struct vattr *vap = ap->a_vap;
1155 struct fuse_mkdir_in fmdi;
1157 if (fuse_isdeadfs(dvp)) {
1160 fmdi.mode = MAKEIMODE(vap->va_type, vap->va_mode);
1162 return (fuse_internal_newentry(dvp, vpp, cnp, FUSE_MKDIR, &fmdi,
1163 sizeof(fmdi), VDIR));
1167 struct vnop_mknod_args {
1168 struct vnode *a_dvp;
1169 struct vnode **a_vpp;
1170 struct componentname *a_cnp;
1171 struct vattr *a_vap;
1175 fuse_vnop_mknod(struct vop_mknod_args *ap)
1178 struct vnode *dvp = ap->a_dvp;
1179 struct vnode **vpp = ap->a_vpp;
1180 struct componentname *cnp = ap->a_cnp;
1181 struct vattr *vap = ap->a_vap;
1182 struct fuse_mknod_in fmni;
1184 if (fuse_isdeadfs(dvp))
1187 fmni.mode = MAKEIMODE(vap->va_type, vap->va_mode);
1188 fmni.rdev = vap->va_rdev;
1189 return (fuse_internal_newentry(dvp, vpp, cnp, FUSE_MKNOD, &fmni,
1190 sizeof(fmni), vap->va_type));
1194 struct vnop_open_args {
1197 struct ucred *a_cred;
1198 struct thread *a_td;
1199 int a_fdidx; / struct file *a_fp;
1203 fuse_vnop_open(struct vop_open_args *ap)
1205 struct vnode *vp = ap->a_vp;
1206 int mode = ap->a_mode;
1207 struct thread *td = ap->a_td;
1208 struct ucred *cred = ap->a_cred;
1209 pid_t pid = td->td_proc->p_pid;
1211 fufh_type_t fufh_type;
1212 struct fuse_vnode_data *fvdat;
1214 if (fuse_isdeadfs(vp))
1216 if (vp->v_type == VCHR || vp->v_type == VBLK || vp->v_type == VFIFO)
1217 return (EOPNOTSUPP);
1218 if ((mode & (FREAD | FWRITE | FEXEC)) == 0)
1223 fufh_type = fuse_filehandle_xlate_from_fflags(mode);
1225 if (fuse_filehandle_validrw(vp, fufh_type, cred, pid)) {
1226 fuse_vnode_open(vp, 0, td);
1230 return fuse_filehandle_open(vp, fufh_type, NULL, td, cred);
1234 fuse_vnop_pathconf(struct vop_pathconf_args *ap)
1237 switch (ap->a_name) {
1238 case _PC_FILESIZEBITS:
1242 *ap->a_retval = NAME_MAX;
1245 *ap->a_retval = MIN(LONG_MAX, FUSE_LINK_MAX);
1247 case _PC_SYMLINK_MAX:
1248 *ap->a_retval = MAXPATHLEN;
1254 return (vop_stdpathconf(ap));
1259 struct vnop_read_args {
1263 struct ucred *a_cred;
1267 fuse_vnop_read(struct vop_read_args *ap)
1269 struct vnode *vp = ap->a_vp;
1270 struct uio *uio = ap->a_uio;
1271 int ioflag = ap->a_ioflag;
1272 struct ucred *cred = ap->a_cred;
1273 pid_t pid = curthread->td_proc->p_pid;
1275 if (fuse_isdeadfs(vp)) {
1279 if (VTOFUD(vp)->flag & FN_DIRECTIO) {
1280 ioflag |= IO_DIRECT;
1283 return fuse_io_dispatch(vp, uio, ioflag, cred, pid);
1287 struct vnop_readdir_args {
1290 struct ucred *a_cred;
1297 fuse_vnop_readdir(struct vop_readdir_args *ap)
1299 struct vnode *vp = ap->a_vp;
1300 struct uio *uio = ap->a_uio;
1301 struct ucred *cred = ap->a_cred;
1302 struct fuse_filehandle *fufh = NULL;
1303 struct fuse_iov cookediov;
1306 pid_t pid = curthread->td_proc->p_pid;
1308 if (fuse_isdeadfs(vp)) {
1311 if ( /* XXXIP ((uio_iovcnt(uio) > 1)) || */
1312 (uio_resid(uio) < sizeof(struct dirent))) {
1316 if ((err = fuse_filehandle_get_dir(vp, &fufh, cred, pid)) != 0) {
1317 SDT_PROBE2(fuse, , vnops, trace, 1,
1318 "calling readdir() before open()");
1319 err = fuse_filehandle_open(vp, O_RDONLY, &fufh, NULL, cred);
1325 #define DIRCOOKEDSIZE FUSE_DIRENT_ALIGN(FUSE_NAME_OFFSET + MAXNAMLEN + 1)
1326 fiov_init(&cookediov, DIRCOOKEDSIZE);
1328 err = fuse_internal_readdir(vp, uio, fufh, &cookediov);
1330 fiov_teardown(&cookediov);
1332 fuse_filehandle_close(vp, fufh, NULL, cred);
1338 struct vnop_readlink_args {
1341 struct ucred *a_cred;
1345 fuse_vnop_readlink(struct vop_readlink_args *ap)
1347 struct vnode *vp = ap->a_vp;
1348 struct uio *uio = ap->a_uio;
1349 struct ucred *cred = ap->a_cred;
1351 struct fuse_dispatcher fdi;
1354 if (fuse_isdeadfs(vp)) {
1357 if (!vnode_islnk(vp)) {
1360 fdisp_init(&fdi, 0);
1361 err = fdisp_simple_putget_vp(&fdi, FUSE_READLINK, vp, curthread, cred);
1365 if (((char *)fdi.answ)[0] == '/' &&
1366 fuse_get_mpdata(vnode_mount(vp))->dataflags & FSESS_PUSH_SYMLINKS_IN) {
1367 char *mpth = vnode_mount(vp)->mnt_stat.f_mntonname;
1369 err = uiomove(mpth, strlen(mpth), uio);
1372 err = uiomove(fdi.answ, fdi.iosize, uio);
1375 fdisp_destroy(&fdi);
1380 struct vnop_reclaim_args {
1382 struct thread *a_td;
1386 fuse_vnop_reclaim(struct vop_reclaim_args *ap)
1388 struct vnode *vp = ap->a_vp;
1389 struct thread *td = ap->a_td;
1390 struct fuse_vnode_data *fvdat = VTOFUD(vp);
1391 struct fuse_filehandle *fufh, *fufh_tmp;
1394 panic("FUSE: no vnode data during recycling");
1396 LIST_FOREACH_SAFE(fufh, &fvdat->handles, next, fufh_tmp) {
1397 printf("FUSE: vnode being reclaimed with open fufh "
1398 "(flags=%#x)", fufh->flags);
1399 fuse_filehandle_close(vp, fufh, td, NULL);
1402 if ((!fuse_isdeadfs(vp)) && (fvdat->nlookup)) {
1403 fuse_internal_forget_send(vnode_mount(vp), td, NULL, VTOI(vp),
1406 fuse_vnode_setparent(vp, NULL);
1408 vfs_hash_remove(vp);
1409 vnode_destroy_vobject(vp);
1410 fuse_vnode_destroy(vp);
1416 struct vnop_remove_args {
1417 struct vnode *a_dvp;
1419 struct componentname *a_cnp;
1423 fuse_vnop_remove(struct vop_remove_args *ap)
1425 struct vnode *dvp = ap->a_dvp;
1426 struct vnode *vp = ap->a_vp;
1427 struct componentname *cnp = ap->a_cnp;
1431 if (fuse_isdeadfs(vp)) {
1434 if (vnode_isdir(vp)) {
1439 err = fuse_internal_remove(dvp, vp, cnp, FUSE_UNLINK);
1442 fuse_internal_vnode_disappear(vp);
1447 struct vnop_rename_args {
1448 struct vnode *a_fdvp;
1449 struct vnode *a_fvp;
1450 struct componentname *a_fcnp;
1451 struct vnode *a_tdvp;
1452 struct vnode *a_tvp;
1453 struct componentname *a_tcnp;
1457 fuse_vnop_rename(struct vop_rename_args *ap)
1459 struct vnode *fdvp = ap->a_fdvp;
1460 struct vnode *fvp = ap->a_fvp;
1461 struct componentname *fcnp = ap->a_fcnp;
1462 struct vnode *tdvp = ap->a_tdvp;
1463 struct vnode *tvp = ap->a_tvp;
1464 struct componentname *tcnp = ap->a_tcnp;
1465 struct fuse_data *data;
1469 if (fuse_isdeadfs(fdvp)) {
1472 if (fvp->v_mount != tdvp->v_mount ||
1473 (tvp && fvp->v_mount != tvp->v_mount)) {
1474 SDT_PROBE2(fuse, , vnops, trace, 1, "cross-device rename");
1481 * FUSE library is expected to check if target directory is not
1482 * under the source directory in the file system tree.
1483 * Linux performs this check at VFS level.
1485 data = fuse_get_mpdata(vnode_mount(tdvp));
1486 sx_xlock(&data->rename_lock);
1487 err = fuse_internal_rename(fdvp, fcnp, tdvp, tcnp);
1490 fuse_vnode_setparent(fvp, tdvp);
1492 fuse_vnode_setparent(tvp, NULL);
1494 sx_unlock(&data->rename_lock);
1496 if (tvp != NULL && tvp != fvp) {
1499 if (vnode_isdir(fvp)) {
1500 if ((tvp != NULL) && vnode_isdir(tvp)) {
1521 struct vnop_rmdir_args {
1522 struct vnode *a_dvp;
1524 struct componentname *a_cnp;
1528 fuse_vnop_rmdir(struct vop_rmdir_args *ap)
1530 struct vnode *dvp = ap->a_dvp;
1531 struct vnode *vp = ap->a_vp;
1535 if (fuse_isdeadfs(vp)) {
1538 if (VTOFUD(vp) == VTOFUD(dvp)) {
1541 err = fuse_internal_remove(dvp, vp, ap->a_cnp, FUSE_RMDIR);
1544 fuse_internal_vnode_disappear(vp);
1549 struct vnop_setattr_args {
1551 struct vattr *a_vap;
1552 struct ucred *a_cred;
1553 struct thread *a_td;
1557 fuse_vnop_setattr(struct vop_setattr_args *ap)
1559 struct vnode *vp = ap->a_vp;
1560 struct vattr *vap = ap->a_vap;
1561 struct ucred *cred = ap->a_cred;
1562 struct thread *td = curthread;
1563 struct fuse_dispatcher fdi;
1564 struct fuse_setattr_in *fsai;
1565 struct fuse_access_param facp;
1566 pid_t pid = td->td_proc->p_pid;
1570 int sizechanged = 0;
1571 uint64_t newsize = 0;
1573 if (fuse_isdeadfs(vp)) {
1576 fdisp_init(&fdi, sizeof(*fsai));
1577 fdisp_make_vp(&fdi, FUSE_SETATTR, vp, td, cred);
1581 bzero(&facp, sizeof(facp));
1583 facp.xuid = vap->va_uid;
1584 facp.xgid = vap->va_gid;
1586 if (vap->va_uid != (uid_t)VNOVAL) {
1587 facp.facc_flags |= FACCESS_CHOWN;
1588 fsai->uid = vap->va_uid;
1589 fsai->valid |= FATTR_UID;
1591 if (vap->va_gid != (gid_t)VNOVAL) {
1592 facp.facc_flags |= FACCESS_CHOWN;
1593 fsai->gid = vap->va_gid;
1594 fsai->valid |= FATTR_GID;
1596 if (vap->va_size != VNOVAL) {
1598 struct fuse_filehandle *fufh = NULL;
1600 /*Truncate to a new value. */
1601 fsai->size = vap->va_size;
1603 newsize = vap->va_size;
1604 fsai->valid |= FATTR_SIZE;
1606 fuse_filehandle_getrw(vp, FUFH_WRONLY, &fufh, cred, pid);
1608 fsai->fh = fufh->fh_id;
1609 fsai->valid |= FATTR_FH;
1612 if (vap->va_atime.tv_sec != VNOVAL) {
1613 fsai->atime = vap->va_atime.tv_sec;
1614 fsai->atimensec = vap->va_atime.tv_nsec;
1615 fsai->valid |= FATTR_ATIME;
1617 if (vap->va_mtime.tv_sec != VNOVAL) {
1618 fsai->mtime = vap->va_mtime.tv_sec;
1619 fsai->mtimensec = vap->va_mtime.tv_nsec;
1620 fsai->valid |= FATTR_MTIME;
1622 if (vap->va_mode != (mode_t)VNOVAL) {
1623 fsai->mode = vap->va_mode & ALLPERMS;
1624 fsai->valid |= FATTR_MODE;
1629 vtyp = vnode_vtype(vp);
1631 if (fsai->valid & FATTR_SIZE && vtyp == VDIR) {
1635 if (vfs_isrdonly(vnode_mount(vp)) && (fsai->valid & ~FATTR_SIZE || vtyp == VREG)) {
1640 if ((err = fdisp_wait_answ(&fdi)))
1642 vtyp = IFTOVT(((struct fuse_attr_out *)fdi.answ)->attr.mode);
1644 if (vnode_vtype(vp) != vtyp) {
1645 if (vnode_vtype(vp) == VNON && vtyp != VNON) {
1646 SDT_PROBE2(fuse, , vnops, trace, 1, "FUSE: Dang! "
1647 "vnode_vtype is VNON and vtype isn't.");
1650 * STALE vnode, ditch
1652 * The vnode has changed its type "behind our back".
1653 * There's nothing really we can do, so let us just
1654 * force an internal revocation and tell the caller to
1655 * try again, if interested.
1657 fuse_internal_vnode_disappear(vp);
1662 struct fuse_attr_out *fao = (struct fuse_attr_out*)fdi.answ;
1663 fuse_internal_cache_attrs(vp, &fao->attr, fao->attr_valid,
1664 fao->attr_valid_nsec, NULL);
1668 fdisp_destroy(&fdi);
1669 if (!err && sizechanged) {
1670 fuse_vnode_setsize(vp, cred, newsize);
1671 VTOFUD(vp)->flag &= ~FN_SIZECHANGE;
1677 struct vnop_strategy_args {
1683 fuse_vnop_strategy(struct vop_strategy_args *ap)
1685 struct vnode *vp = ap->a_vp;
1686 struct buf *bp = ap->a_bp;
1688 if (!vp || fuse_isdeadfs(vp)) {
1689 bp->b_ioflags |= BIO_ERROR;
1690 bp->b_error = ENXIO;
1694 if (bp->b_iocmd == BIO_WRITE) {
1697 err = fuse_vnode_refreshsize(vp, NOCRED);
1699 bp->b_ioflags |= BIO_ERROR;
1707 * VOP_STRATEGY always returns zero and signals error via bp->b_ioflags.
1708 * fuse_io_strategy sets bp's error fields
1710 (void)fuse_io_strategy(vp, bp);
1717 struct vnop_symlink_args {
1718 struct vnode *a_dvp;
1719 struct vnode **a_vpp;
1720 struct componentname *a_cnp;
1721 struct vattr *a_vap;
1726 fuse_vnop_symlink(struct vop_symlink_args *ap)
1728 struct vnode *dvp = ap->a_dvp;
1729 struct vnode **vpp = ap->a_vpp;
1730 struct componentname *cnp = ap->a_cnp;
1731 const char *target = ap->a_target;
1733 struct fuse_dispatcher fdi;
1738 if (fuse_isdeadfs(dvp)) {
1742 * Unlike the other creator type calls, here we have to create a message
1743 * where the name of the new entry comes first, and the data describing
1744 * the entry comes second.
1745 * Hence we can't rely on our handy fuse_internal_newentry() routine,
1746 * but put together the message manually and just call the core part.
1749 len = strlen(target) + 1;
1750 fdisp_init(&fdi, len + cnp->cn_namelen + 1);
1751 fdisp_make_vp(&fdi, FUSE_SYMLINK, dvp, curthread, NULL);
1753 memcpy(fdi.indata, cnp->cn_nameptr, cnp->cn_namelen);
1754 ((char *)fdi.indata)[cnp->cn_namelen] = '\0';
1755 memcpy((char *)fdi.indata + cnp->cn_namelen + 1, target, len);
1757 err = fuse_internal_newentry_core(dvp, vpp, cnp, VLNK, &fdi);
1758 fdisp_destroy(&fdi);
1763 struct vnop_write_args {
1767 struct ucred *a_cred;
1771 fuse_vnop_write(struct vop_write_args *ap)
1773 struct vnode *vp = ap->a_vp;
1774 struct uio *uio = ap->a_uio;
1775 int ioflag = ap->a_ioflag;
1776 struct ucred *cred = ap->a_cred;
1777 pid_t pid = curthread->td_proc->p_pid;
1780 if (fuse_isdeadfs(vp)) {
1783 err = fuse_vnode_refreshsize(vp, cred);
1787 if (VTOFUD(vp)->flag & FN_DIRECTIO) {
1788 ioflag |= IO_DIRECT;
1791 return fuse_io_dispatch(vp, uio, ioflag, cred, pid);
1794 SDT_PROBE_DEFINE1(fuse, , vnops, vnop_getpages_error, "int");
1796 struct vnop_getpages_args {
1804 fuse_vnop_getpages(struct vop_getpages_args *ap)
1806 int i, error, nextoff, size, toff, count, npages;
1815 pid_t pid = curthread->td_proc->p_pid;
1818 KASSERT(vp->v_object, ("objectless vp passed to getpages"));
1819 td = curthread; /* XXX */
1820 cred = curthread->td_ucred; /* XXX */
1822 npages = ap->a_count;
1824 if (!fsess_opt_mmap(vnode_mount(vp))) {
1825 SDT_PROBE2(fuse, , vnops, trace, 1,
1826 "called on non-cacheable vnode??\n");
1827 return (VM_PAGER_ERROR);
1831 * If the last page is partially valid, just return it and allow
1832 * the pager to zero-out the blanks. Partially valid pages can
1833 * only occur at the file EOF.
1835 * XXXGL: is that true for FUSE, which is a local filesystem,
1836 * but still somewhat disconnected from the kernel?
1838 VM_OBJECT_WLOCK(vp->v_object);
1839 if (pages[npages - 1]->valid != 0 && --npages == 0)
1841 VM_OBJECT_WUNLOCK(vp->v_object);
1844 * We use only the kva address for the buffer, but this is extremely
1845 * convenient and fast.
1847 bp = uma_zalloc(fuse_pbuf_zone, M_WAITOK);
1849 kva = (vm_offset_t)bp->b_data;
1850 pmap_qenter(kva, pages, npages);
1851 VM_CNT_INC(v_vnodein);
1852 VM_CNT_ADD(v_vnodepgsin, npages);
1854 count = npages << PAGE_SHIFT;
1855 iov.iov_base = (caddr_t)kva;
1856 iov.iov_len = count;
1859 uio.uio_offset = IDX_TO_OFF(pages[0]->pindex);
1860 uio.uio_resid = count;
1861 uio.uio_segflg = UIO_SYSSPACE;
1862 uio.uio_rw = UIO_READ;
1865 error = fuse_io_dispatch(vp, &uio, IO_DIRECT, cred, pid);
1866 pmap_qremove(kva, npages);
1868 uma_zfree(fuse_pbuf_zone, bp);
1870 if (error && (uio.uio_resid == count)) {
1871 SDT_PROBE1(fuse, , vnops, vnop_getpages_error, error);
1872 return VM_PAGER_ERROR;
1875 * Calculate the number of bytes read and validate only that number
1876 * of bytes. Note that due to pending writes, size may be 0. This
1877 * does not mean that the remaining data is invalid!
1880 size = count - uio.uio_resid;
1881 VM_OBJECT_WLOCK(vp->v_object);
1882 fuse_vm_page_lock_queues();
1883 for (i = 0, toff = 0; i < npages; i++, toff = nextoff) {
1886 nextoff = toff + PAGE_SIZE;
1889 if (nextoff <= size) {
1891 * Read operation filled an entire page
1893 m->valid = VM_PAGE_BITS_ALL;
1894 KASSERT(m->dirty == 0,
1895 ("fuse_getpages: page %p is dirty", m));
1896 } else if (size > toff) {
1898 * Read operation filled a partial page.
1901 vm_page_set_valid_range(m, 0, size - toff);
1902 KASSERT(m->dirty == 0,
1903 ("fuse_getpages: page %p is dirty", m));
1906 * Read operation was short. If no error occurred
1907 * we may have hit a zero-fill section. We simply
1908 * leave valid set to 0.
1913 fuse_vm_page_unlock_queues();
1915 VM_OBJECT_WUNLOCK(vp->v_object);
1920 return (VM_PAGER_OK);
1924 struct vnop_putpages_args {
1930 vm_ooffset_t a_offset;
1934 fuse_vnop_putpages(struct vop_putpages_args *ap)
1940 int i, error, npages, count;
1948 pid_t pid = curthread->td_proc->p_pid;
1951 KASSERT(vp->v_object, ("objectless vp passed to putpages"));
1952 fsize = vp->v_object->un_pager.vnp.vnp_size;
1953 td = curthread; /* XXX */
1954 cred = curthread->td_ucred; /* XXX */
1956 count = ap->a_count;
1957 rtvals = ap->a_rtvals;
1958 npages = btoc(count);
1959 offset = IDX_TO_OFF(pages[0]->pindex);
1961 if (!fsess_opt_mmap(vnode_mount(vp))) {
1962 SDT_PROBE2(fuse, , vnops, trace, 1,
1963 "called on non-cacheable vnode??\n");
1965 for (i = 0; i < npages; i++)
1966 rtvals[i] = VM_PAGER_AGAIN;
1969 * When putting pages, do not extend file past EOF.
1972 if (offset + count > fsize) {
1973 count = fsize - offset;
1978 * We use only the kva address for the buffer, but this is extremely
1979 * convenient and fast.
1981 bp = uma_zalloc(fuse_pbuf_zone, M_WAITOK);
1983 kva = (vm_offset_t)bp->b_data;
1984 pmap_qenter(kva, pages, npages);
1985 VM_CNT_INC(v_vnodeout);
1986 VM_CNT_ADD(v_vnodepgsout, count);
1988 iov.iov_base = (caddr_t)kva;
1989 iov.iov_len = count;
1992 uio.uio_offset = offset;
1993 uio.uio_resid = count;
1994 uio.uio_segflg = UIO_SYSSPACE;
1995 uio.uio_rw = UIO_WRITE;
1998 error = fuse_io_dispatch(vp, &uio, IO_DIRECT, cred, pid);
2000 pmap_qremove(kva, npages);
2001 uma_zfree(fuse_pbuf_zone, bp);
2004 int nwritten = round_page(count - uio.uio_resid) / PAGE_SIZE;
2006 for (i = 0; i < nwritten; i++) {
2007 rtvals[i] = VM_PAGER_OK;
2008 VM_OBJECT_WLOCK(pages[i]->object);
2009 vm_page_undirty(pages[i]);
2010 VM_OBJECT_WUNLOCK(pages[i]->object);
2016 static const char extattr_namespace_separator = '.';
2019 struct vop_getextattr_args {
2020 struct vop_generic_args a_gen;
2022 int a_attrnamespace;
2026 struct ucred *a_cred;
2027 struct thread *a_td;
2031 fuse_vnop_getextattr(struct vop_getextattr_args *ap)
2033 struct vnode *vp = ap->a_vp;
2034 struct uio *uio = ap->a_uio;
2035 struct fuse_dispatcher fdi;
2036 struct fuse_getxattr_in *get_xattr_in;
2037 struct fuse_getxattr_out *get_xattr_out;
2038 struct mount *mp = vnode_mount(vp);
2039 struct thread *td = ap->a_td;
2040 struct ucred *cred = ap->a_cred;
2046 if (fuse_isdeadfs(vp))
2049 /* Default to looking for user attributes. */
2050 if (ap->a_attrnamespace == EXTATTR_NAMESPACE_SYSTEM)
2051 prefix = EXTATTR_NAMESPACE_SYSTEM_STRING;
2053 prefix = EXTATTR_NAMESPACE_USER_STRING;
2055 len = strlen(prefix) + sizeof(extattr_namespace_separator) +
2056 strlen(ap->a_name) + 1;
2058 fdisp_init(&fdi, len + sizeof(*get_xattr_in));
2059 fdisp_make_vp(&fdi, FUSE_GETXATTR, vp, td, cred);
2061 get_xattr_in = fdi.indata;
2063 * Check to see whether we're querying the available size or
2064 * issuing the actual request. If we pass in 0, we get back struct
2065 * fuse_getxattr_out. If we pass in a non-zero size, we get back
2066 * that much data, without the struct fuse_getxattr_out header.
2069 get_xattr_in->size = 0;
2071 get_xattr_in->size = uio->uio_resid;
2073 attr_str = (char *)fdi.indata + sizeof(*get_xattr_in);
2074 snprintf(attr_str, len, "%s%c%s", prefix, extattr_namespace_separator,
2077 err = fdisp_wait_answ(&fdi);
2080 fsess_set_notimpl(mp, FUSE_GETXATTR);
2084 get_xattr_out = fdi.answ;
2086 if (ap->a_size != NULL)
2087 *ap->a_size = get_xattr_out->size;
2090 err = uiomove(fdi.answ, fdi.iosize, uio);
2093 fdisp_destroy(&fdi);
2098 struct vop_setextattr_args {
2099 struct vop_generic_args a_gen;
2101 int a_attrnamespace;
2104 struct ucred *a_cred;
2105 struct thread *a_td;
2109 fuse_vnop_setextattr(struct vop_setextattr_args *ap)
2111 struct vnode *vp = ap->a_vp;
2112 struct uio *uio = ap->a_uio;
2113 struct fuse_dispatcher fdi;
2114 struct fuse_setxattr_in *set_xattr_in;
2115 struct mount *mp = vnode_mount(vp);
2116 struct thread *td = ap->a_td;
2117 struct ucred *cred = ap->a_cred;
2123 if (fuse_isdeadfs(vp))
2126 /* Default to looking for user attributes. */
2127 if (ap->a_attrnamespace == EXTATTR_NAMESPACE_SYSTEM)
2128 prefix = EXTATTR_NAMESPACE_SYSTEM_STRING;
2130 prefix = EXTATTR_NAMESPACE_USER_STRING;
2132 len = strlen(prefix) + sizeof(extattr_namespace_separator) +
2133 strlen(ap->a_name) + 1;
2135 fdisp_init(&fdi, len + sizeof(*set_xattr_in) + uio->uio_resid);
2136 fdisp_make_vp(&fdi, FUSE_SETXATTR, vp, td, cred);
2138 set_xattr_in = fdi.indata;
2139 set_xattr_in->size = uio->uio_resid;
2141 attr_str = (char *)fdi.indata + sizeof(*set_xattr_in);
2142 snprintf(attr_str, len, "%s%c%s", prefix, extattr_namespace_separator,
2145 err = uiomove((char *)fdi.indata + sizeof(*set_xattr_in) + len,
2146 uio->uio_resid, uio);
2151 err = fdisp_wait_answ(&fdi);
2155 fsess_set_notimpl(mp, FUSE_SETXATTR);
2160 fdisp_destroy(&fdi);
2165 * The Linux / FUSE extended attribute list is simply a collection of
2166 * NUL-terminated strings. The FreeBSD extended attribute list is a single
2167 * byte length followed by a non-NUL terminated string. So, this allows
2168 * conversion of the Linux / FUSE format to the FreeBSD format in place.
2169 * Linux attribute names are reported with the namespace as a prefix (e.g.
2170 * "user.attribute_name"), but in FreeBSD they are reported without the
2171 * namespace prefix (e.g. "attribute_name"). So, we're going from:
2173 * user.attr_name1\0user.attr_name2\0
2177 * <num>attr_name1<num>attr_name2
2179 * Where "<num>" is a single byte number of characters in the attribute name.
2182 * prefix - exattr namespace prefix string
2183 * list, list_len - input list with namespace prefixes
2184 * bsd_list, bsd_list_len - output list compatible with bsd vfs
2187 fuse_xattrlist_convert(char *prefix, const char *list, int list_len,
2188 char *bsd_list, int *bsd_list_len)
2190 int len, pos, dist_to_next, prefix_len;
2194 prefix_len = strlen(prefix);
2196 while (pos < list_len && list[pos] != '\0') {
2197 dist_to_next = strlen(&list[pos]) + 1;
2198 if (bcmp(&list[pos], prefix, prefix_len) == 0 &&
2199 list[pos + prefix_len] == extattr_namespace_separator) {
2200 len = dist_to_next -
2201 (prefix_len + sizeof(extattr_namespace_separator)) - 1;
2202 if (len >= EXTATTR_MAXNAMELEN)
2203 return (ENAMETOOLONG);
2205 bsd_list[*bsd_list_len] = len;
2206 memcpy(&bsd_list[*bsd_list_len + 1],
2207 &list[pos + prefix_len +
2208 sizeof(extattr_namespace_separator)], len);
2210 *bsd_list_len += len + 1;
2213 pos += dist_to_next;
2220 struct vop_listextattr_args {
2221 struct vop_generic_args a_gen;
2223 int a_attrnamespace;
2226 struct ucred *a_cred;
2227 struct thread *a_td;
2231 fuse_vnop_listextattr(struct vop_listextattr_args *ap)
2233 struct vnode *vp = ap->a_vp;
2234 struct uio *uio = ap->a_uio;
2235 struct fuse_dispatcher fdi;
2236 struct fuse_listxattr_in *list_xattr_in;
2237 struct fuse_listxattr_out *list_xattr_out;
2238 struct mount *mp = vnode_mount(vp);
2239 struct thread *td = ap->a_td;
2240 struct ucred *cred = ap->a_cred;
2244 char *bsd_list = NULL;
2250 if (fuse_isdeadfs(vp))
2254 * Add space for a NUL and the period separator if enabled.
2255 * Default to looking for user attributes.
2257 if (ap->a_attrnamespace == EXTATTR_NAMESPACE_SYSTEM)
2258 prefix = EXTATTR_NAMESPACE_SYSTEM_STRING;
2260 prefix = EXTATTR_NAMESPACE_USER_STRING;
2262 len = strlen(prefix) + sizeof(extattr_namespace_separator) + 1;
2264 fdisp_init(&fdi, sizeof(*list_xattr_in) + len);
2265 fdisp_make_vp(&fdi, FUSE_LISTXATTR, vp, td, cred);
2268 * Retrieve Linux / FUSE compatible list size.
2270 list_xattr_in = fdi.indata;
2271 list_xattr_in->size = 0;
2272 attr_str = (char *)fdi.indata + sizeof(*list_xattr_in);
2273 snprintf(attr_str, len, "%s%c", prefix, extattr_namespace_separator);
2275 err = fdisp_wait_answ(&fdi);
2278 fsess_set_notimpl(mp, FUSE_LISTXATTR);
2282 list_xattr_out = fdi.answ;
2283 linux_list_len = list_xattr_out->size;
2284 if (linux_list_len == 0) {
2285 if (ap->a_size != NULL)
2286 *ap->a_size = linux_list_len;
2291 * Retrieve Linux / FUSE compatible list values.
2293 fdisp_make_vp(&fdi, FUSE_LISTXATTR, vp, td, cred);
2294 list_xattr_in = fdi.indata;
2295 list_xattr_in->size = linux_list_len + sizeof(*list_xattr_out);
2296 attr_str = (char *)fdi.indata + sizeof(*list_xattr_in);
2297 snprintf(attr_str, len, "%s%c", prefix, extattr_namespace_separator);
2299 err = fdisp_wait_answ(&fdi);
2303 linux_list = fdi.answ;
2304 linux_list_len = fdi.iosize;
2307 * Retrieve the BSD compatible list values.
2308 * The Linux / FUSE attribute list format isn't the same
2309 * as FreeBSD's format. So we need to transform it into
2310 * FreeBSD's format before giving it to the user.
2312 bsd_list = malloc(linux_list_len, M_TEMP, M_WAITOK);
2313 err = fuse_xattrlist_convert(prefix, linux_list, linux_list_len,
2314 bsd_list, &bsd_list_len);
2318 if (ap->a_size != NULL)
2319 *ap->a_size = bsd_list_len;
2322 err = uiomove(bsd_list, bsd_list_len, uio);
2325 free(bsd_list, M_TEMP);
2326 fdisp_destroy(&fdi);
2331 struct vop_deleteextattr_args {
2332 struct vop_generic_args a_gen;
2334 int a_attrnamespace;
2336 struct ucred *a_cred;
2337 struct thread *a_td;
2341 fuse_vnop_deleteextattr(struct vop_deleteextattr_args *ap)
2343 struct vnode *vp = ap->a_vp;
2344 struct fuse_dispatcher fdi;
2345 struct mount *mp = vnode_mount(vp);
2346 struct thread *td = ap->a_td;
2347 struct ucred *cred = ap->a_cred;
2353 if (fuse_isdeadfs(vp))
2356 /* Default to looking for user attributes. */
2357 if (ap->a_attrnamespace == EXTATTR_NAMESPACE_SYSTEM)
2358 prefix = EXTATTR_NAMESPACE_SYSTEM_STRING;
2360 prefix = EXTATTR_NAMESPACE_USER_STRING;
2362 len = strlen(prefix) + sizeof(extattr_namespace_separator) +
2363 strlen(ap->a_name) + 1;
2365 fdisp_init(&fdi, len);
2366 fdisp_make_vp(&fdi, FUSE_REMOVEXATTR, vp, td, cred);
2368 attr_str = fdi.indata;
2369 snprintf(attr_str, len, "%s%c%s", prefix, extattr_namespace_separator,
2372 err = fdisp_wait_answ(&fdi);
2375 fsess_set_notimpl(mp, FUSE_REMOVEXATTR);
2378 fdisp_destroy(&fdi);
2383 struct vnop_print_args {
2388 fuse_vnop_print(struct vop_print_args *ap)
2390 struct fuse_vnode_data *fvdat = VTOFUD(ap->a_vp);
2392 printf("nodeid: %ju, parent nodeid: %ju, nlookup: %ju, flag: %#x\n",
2393 (uintmax_t)VTOILLU(ap->a_vp), (uintmax_t)fvdat->parent_nid,
2394 (uintmax_t)fvdat->nlookup,