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)
220 struct vnop_access_args {
222 #if VOP_ACCESS_TAKES_ACCMODE_T
227 struct ucred *a_cred;
232 fuse_vnop_access(struct vop_access_args *ap)
234 struct vnode *vp = ap->a_vp;
235 int accmode = ap->a_accmode;
236 struct ucred *cred = ap->a_cred;
238 struct fuse_access_param facp;
239 struct fuse_data *data = fuse_get_mpdata(vnode_mount(vp));
243 if (fuse_isdeadfs(vp)) {
244 if (vnode_isvroot(vp)) {
249 if (!(data->dataflags & FSESS_INITED)) {
250 if (vnode_isvroot(vp)) {
251 if (priv_check_cred(cred, PRIV_VFS_ADMIN) ||
252 (fuse_match_cred(data->daemoncred, cred) == 0)) {
258 if (vnode_islnk(vp)) {
261 bzero(&facp, sizeof(facp));
263 err = fuse_internal_access(vp, accmode, &facp, ap->a_td, ap->a_cred);
268 struct vnop_close_args {
271 struct ucred *a_cred;
276 fuse_vnop_close(struct vop_close_args *ap)
278 struct vnode *vp = ap->a_vp;
279 struct ucred *cred = ap->a_cred;
280 int fflag = ap->a_fflag;
282 if (fuse_isdeadfs(vp)) {
285 if (vnode_isdir(vp)) {
286 struct fuse_filehandle *fufh;
288 if (fuse_filehandle_get(vp, O_RDONLY, &fufh) == 0)
289 fuse_filehandle_close(vp, fufh, NULL, cred);
292 if (fflag & IO_NDELAY) {
295 /* TODO: close the file handle, if we're sure it's no longer used */
296 if ((VTOFUD(vp)->flag & FN_SIZECHANGE) != 0) {
297 fuse_vnode_savesize(vp, cred);
303 fdisp_make_mknod_for_fallback(
304 struct fuse_dispatcher *fdip,
305 struct componentname *cnp,
311 enum fuse_opcode *op)
313 struct fuse_mknod_in *fmni;
315 fdisp_init(fdip, sizeof(*fmni) + cnp->cn_namelen + 1);
317 fdisp_make(fdip, *op, vnode_mount(dvp), parentnid, td, cred);
321 memcpy((char *)fdip->indata + sizeof(*fmni), cnp->cn_nameptr,
323 ((char *)fdip->indata)[sizeof(*fmni) + cnp->cn_namelen] = '\0';
326 struct vnop_create_args {
328 struct vnode **a_vpp;
329 struct componentname *a_cnp;
334 fuse_vnop_create(struct vop_create_args *ap)
336 struct vnode *dvp = ap->a_dvp;
337 struct vnode **vpp = ap->a_vpp;
338 struct componentname *cnp = ap->a_cnp;
339 struct vattr *vap = ap->a_vap;
340 struct thread *td = cnp->cn_thread;
341 struct ucred *cred = cnp->cn_cred;
343 struct fuse_open_in *foi;
344 struct fuse_entry_out *feo;
345 struct fuse_open_out *foo;
346 struct fuse_dispatcher fdi, fdi2;
347 struct fuse_dispatcher *fdip = &fdi;
348 struct fuse_dispatcher *fdip2 = NULL;
352 struct mount *mp = vnode_mount(dvp);
353 uint64_t parentnid = VTOFUD(dvp)->nid;
354 mode_t mode = MAKEIMODE(vap->va_type, vap->va_mode);
357 if (fuse_isdeadfs(dvp)) {
360 bzero(&fdi, sizeof(fdi));
362 if ((vap->va_type != VREG))
365 if (!fsess_isimpl(mp, FUSE_CREATE)) {
366 /* Fallback to FUSE_MKNOD/FUSE_OPEN */
367 fdisp_make_mknod_for_fallback(fdip, cnp, dvp, parentnid, td,
370 /* Use FUSE_CREATE */
372 fdisp_init(fdip, sizeof(*foi) + cnp->cn_namelen + 1);
373 fdisp_make(fdip, op, vnode_mount(dvp), parentnid, td, cred);
376 foi->flags = O_CREAT | O_RDWR;
377 memcpy((char *)fdip->indata + sizeof(*foi), cnp->cn_nameptr,
379 ((char *)fdip->indata)[sizeof(*foi) + cnp->cn_namelen] = '\0';
382 err = fdisp_wait_answ(fdip);
385 if (err == ENOSYS && op == FUSE_CREATE) {
386 fsess_set_notimpl(mp, FUSE_CREATE);
387 fdisp_make_mknod_for_fallback(fdip, cnp, dvp,
388 parentnid, td, cred, mode, &op);
389 err = fdisp_wait_answ(fdip);
397 if ((err = fuse_internal_checkentry(feo, VREG))) {
401 if (op == FUSE_CREATE) {
402 foo = (struct fuse_open_out*)(feo + 1);
404 /* Issue a separate FUSE_OPEN */
406 fdisp_init(fdip2, sizeof(*foi));
407 fdisp_make(fdip2, FUSE_OPEN, vnode_mount(dvp), feo->nodeid, td,
412 err = fdisp_wait_answ(fdip2);
417 err = fuse_vnode_get(mp, feo, feo->nodeid, dvp, vpp, cnp, VREG);
419 struct fuse_release_in *fri;
420 uint64_t nodeid = feo->nodeid;
421 uint64_t fh_id = foo->fh;
423 fdisp_init(fdip, sizeof(*fri));
424 fdisp_make(fdip, FUSE_RELEASE, mp, nodeid, td, cred);
427 fri->flags = fuse_filehandle_xlate_to_oflags(FUFH_RDWR);
428 fuse_insert_callback(fdip->tick, fuse_internal_forget_callback);
429 fuse_insert_message(fdip->tick);
432 ASSERT_VOP_ELOCKED(*vpp, "fuse_vnop_create");
434 fuse_filehandle_init(*vpp, FUFH_RDWR, NULL, foo->fh);
435 fuse_vnode_open(*vpp, foo->open_flags, td);
436 cache_purge_negative(dvp);
440 fdisp_destroy(fdip2);
446 struct vnop_fdatasync_args {
447 struct vop_generic_args a_gen;
449 struct thread * a_td;
453 fuse_vnop_fdatasync(struct vop_fdatasync_args *ap)
455 struct vnode *vp = ap->a_vp;
456 struct thread *td = ap->a_td;
457 int waitfor = MNT_WAIT;
461 if (fuse_isdeadfs(vp)) {
464 if ((err = vop_stdfdatasync_buf(ap)))
467 return fuse_internal_fsync(vp, td, waitfor, true);
471 struct vnop_fsync_args {
472 struct vop_generic_args a_gen;
475 struct thread * a_td;
479 fuse_vnop_fsync(struct vop_fsync_args *ap)
481 struct vnode *vp = ap->a_vp;
482 struct thread *td = ap->a_td;
483 int waitfor = ap->a_waitfor;
486 if (fuse_isdeadfs(vp)) {
489 if ((err = vop_stdfsync(ap)))
492 return fuse_internal_fsync(vp, td, waitfor, false);
496 struct vnop_getattr_args {
499 struct ucred *a_cred;
504 fuse_vnop_getattr(struct vop_getattr_args *ap)
506 struct vnode *vp = ap->a_vp;
507 struct vattr *vap = ap->a_vap;
508 struct ucred *cred = ap->a_cred;
509 struct thread *td = curthread;
510 struct fuse_vnode_data *fvdat = VTOFUD(vp);
511 struct fuse_attr_out *fao;
515 struct fuse_dispatcher fdi;
517 dataflags = fuse_get_mpdata(vnode_mount(vp))->dataflags;
519 /* Note that we are not bailing out on a dead file system just yet. */
521 if (!(dataflags & FSESS_INITED)) {
522 if (!vnode_isvroot(vp)) {
523 fdata_set_dead(fuse_get_mpdata(vnode_mount(vp)));
531 if ((err = fdisp_simple_putget_vp(&fdi, FUSE_GETATTR, vp, td, cred))) {
532 if ((err == ENOTCONN) && vnode_isvroot(vp)) {
533 /* see comment in fuse_vfsop_statfs() */
538 fuse_internal_vnode_disappear(vp);
543 fao = (struct fuse_attr_out *)fdi.answ;
544 fuse_internal_cache_attrs(vp, &fao->attr, fao->attr_valid,
545 fao->attr_valid_nsec, vap);
546 if (vap->va_type != vnode_vtype(vp)) {
547 fuse_internal_vnode_disappear(vp);
551 if ((fvdat->flag & FN_SIZECHANGE) != 0)
552 vap->va_size = fvdat->filesize;
554 if (vnode_isreg(vp) && (fvdat->flag & FN_SIZECHANGE) == 0) {
556 * This is for those cases when the file size changed without us
557 * knowing, and we want to catch up.
559 off_t new_filesize = ((struct fuse_attr_out *)
560 fdi.answ)->attr.size;
562 if (fvdat->filesize != new_filesize) {
563 fuse_vnode_setsize(vp, cred, new_filesize);
564 fvdat->flag &= ~FN_SIZECHANGE;
573 bzero(vap, sizeof(*vap));
574 vap->va_type = vnode_vtype(vp);
580 struct vnop_inactive_args {
586 fuse_vnop_inactive(struct vop_inactive_args *ap)
588 struct vnode *vp = ap->a_vp;
589 struct thread *td = ap->a_td;
591 struct fuse_vnode_data *fvdat = VTOFUD(vp);
592 struct fuse_filehandle *fufh, *fufh_tmp;
596 LIST_FOREACH_SAFE(fufh, &fvdat->handles, next, fufh_tmp) {
597 if (need_flush && vp->v_type == VREG) {
598 if ((VTOFUD(vp)->flag & FN_SIZECHANGE) != 0) {
599 fuse_vnode_savesize(vp, NULL);
601 if (fuse_data_cache_invalidate ||
602 (fvdat->flag & FN_REVOKED) != 0)
603 fuse_io_invalbuf(vp, td);
605 fuse_io_flushbuf(vp, MNT_WAIT, td);
608 fuse_filehandle_close(vp, fufh, td, NULL);
611 if ((fvdat->flag & FN_REVOKED) != 0 && fuse_reclaim_revoked) {
618 struct vnop_link_args {
619 struct vnode *a_tdvp;
621 struct componentname *a_cnp;
625 fuse_vnop_link(struct vop_link_args *ap)
627 struct vnode *vp = ap->a_vp;
628 struct vnode *tdvp = ap->a_tdvp;
629 struct componentname *cnp = ap->a_cnp;
631 struct vattr *vap = VTOVA(vp);
633 struct fuse_dispatcher fdi;
634 struct fuse_entry_out *feo;
635 struct fuse_link_in fli;
639 if (fuse_isdeadfs(vp)) {
642 if (vnode_mount(tdvp) != vnode_mount(vp)) {
647 * This is a seatbelt check to protect naive userspace filesystems from
648 * themselves and the limitations of the FUSE IPC protocol. If a
649 * filesystem does not allow attribute caching, assume it is capable of
650 * validating that nlink does not overflow.
652 if (vap != NULL && vap->va_nlink >= FUSE_LINK_MAX)
654 fli.oldnodeid = VTOI(vp);
657 fuse_internal_newentry_makerequest(vnode_mount(tdvp), VTOI(tdvp), cnp,
658 FUSE_LINK, &fli, sizeof(fli), &fdi);
659 if ((err = fdisp_wait_answ(&fdi))) {
664 err = fuse_internal_checkentry(feo, vnode_vtype(vp));
671 struct vnop_lookup_args {
672 struct vnodeop_desc *a_desc;
674 struct vnode **a_vpp;
675 struct componentname *a_cnp;
679 fuse_vnop_lookup(struct vop_lookup_args *ap)
681 struct vnode *dvp = ap->a_dvp;
682 struct vnode **vpp = ap->a_vpp;
683 struct componentname *cnp = ap->a_cnp;
684 struct thread *td = cnp->cn_thread;
685 struct ucred *cred = cnp->cn_cred;
687 int nameiop = cnp->cn_nameiop;
688 int flags = cnp->cn_flags;
689 int wantparent = flags & (LOCKPARENT | WANTPARENT);
690 int islastcn = flags & ISLASTCN;
691 struct mount *mp = vnode_mount(dvp);
695 struct vnode *vp = NULL;
697 struct fuse_dispatcher fdi;
701 struct fuse_access_param facp;
703 if (fuse_isdeadfs(dvp)) {
707 if (!vnode_isdir(dvp)) {
710 if (islastcn && vfs_isrdonly(mp) && (nameiop != LOOKUP)) {
714 * We do access check prior to doing anything else only in the case
715 * when we are at fs root (we'd like to say, "we are at the first
716 * component", but that's not exactly the same... nevermind).
717 * See further comments at further access checks.
720 bzero(&facp, sizeof(facp));
721 if (vnode_isvroot(dvp)) { /* early permission check hack */
722 if ((err = fuse_internal_access(dvp, VEXEC, &facp, td, cred))) {
726 if (flags & ISDOTDOT) {
727 nid = VTOFUD(dvp)->parent_nid;
734 } else if (cnp->cn_namelen == 1 && *(cnp->cn_nameptr) == '.') {
739 } else if (fuse_lookup_cache_enable) {
740 err = cache_lookup(dvp, vpp, cnp, NULL, NULL);
743 case -1: /* positive match */
744 atomic_add_acq_long(&fuse_lookup_cache_hits, 1);
747 case 0: /* no match in cache */
748 atomic_add_acq_long(&fuse_lookup_cache_misses, 1);
751 case ENOENT: /* negative match */
758 fdisp_init(&fdi, cnp->cn_namelen + 1);
762 fdisp_make(&fdi, op, mp, nid, td, cred);
764 if (op == FUSE_LOOKUP) {
765 memcpy(fdi.indata, cnp->cn_nameptr, cnp->cn_namelen);
766 ((char *)fdi.indata)[cnp->cn_namelen] = '\0';
768 lookup_err = fdisp_wait_answ(&fdi);
770 if ((op == FUSE_LOOKUP) && !lookup_err) { /* lookup call succeeded */
771 nid = ((struct fuse_entry_out *)fdi.answ)->nodeid;
774 * zero nodeid is the same as "not found",
775 * but it's also cacheable (which we keep
776 * keep on doing not as of writing this)
778 fdi.answ_stat = ENOENT;
780 } else if (nid == FUSE_ROOT_ID) {
785 (!fdi.answ_stat || lookup_err != ENOENT || op != FUSE_LOOKUP)) {
789 /* lookup_err, if non-zero, must be ENOENT at this point */
793 if ((nameiop == CREATE || nameiop == RENAME) && islastcn
794 /* && directory dvp has not been removed */ ) {
796 if (vfs_isrdonly(mp)) {
800 #if 0 /* THINK_ABOUT_THIS */
801 if ((err = fuse_internal_access(dvp, VWRITE, cred, td, &facp))) {
807 * Possibly record the position of a slot in the
808 * directory large enough for the new component name.
809 * This can be recorded in the vnode private data for
810 * dvp. Set the SAVENAME flag to hold onto the
811 * pathname for use later in VOP_CREATE or VOP_RENAME.
813 cnp->cn_flags |= SAVENAME;
818 /* Consider inserting name into cache. */
821 * No we can't use negative caching, as the fs
822 * changes are out of our control.
823 * False positives' falseness turns out just as things
824 * go by, but false negatives' falseness doesn't.
825 * (and aiding the caching mechanism with extra control
826 * mechanisms comes quite close to beating the whole purpose
830 if ((cnp->cn_flags & MAKEENTRY) != 0) {
831 SDT_PROBE2(fuse, , vnops, trace, 1,
832 "inserting NULL into cache");
833 cache_enter(dvp, NULL, cnp);
843 struct fuse_entry_out *feo = NULL;
844 struct fuse_attr *fattr = NULL;
846 if (op == FUSE_GETATTR) {
847 fattr = &((struct fuse_attr_out *)fdi.answ)->attr;
849 feo = (struct fuse_entry_out *)fdi.answ;
850 fattr = &(feo->attr);
854 * If deleting, and at end of pathname, return parameters
855 * which can be used to remove file. If the wantparent flag
856 * isn't set, we return only the directory, otherwise we go on
857 * and lock the inode, being careful with ".".
859 if (nameiop == DELETE && islastcn) {
861 * Check for write access on directory.
863 facp.xuid = fattr->uid;
864 facp.facc_flags |= FACCESS_STICKY;
865 err = fuse_internal_access(dvp, VWRITE, &facp, td, cred);
866 facp.facc_flags &= ~FACCESS_XQUERIES;
871 if (nid == VTOI(dvp)) {
875 err = fuse_vnode_get(dvp->v_mount, feo, nid,
876 dvp, &vp, cnp, IFTOVT(fattr->mode));
883 * Save the name for use in VOP_RMDIR and VOP_REMOVE
886 cnp->cn_flags |= SAVENAME;
891 * If rewriting (RENAME), return the inode and the
892 * information required to rewrite the present directory
893 * Must get inode of directory entry to verify it's a
894 * regular file, or empty directory.
896 if (nameiop == RENAME && wantparent && islastcn) {
898 #if 0 /* THINK_ABOUT_THIS */
899 if ((err = fuse_internal_access(dvp, VWRITE, cred, td, &facp))) {
907 if (nid == VTOI(dvp)) {
911 err = fuse_vnode_get(vnode_mount(dvp), feo, nid, dvp,
912 &vp, cnp, IFTOVT(fattr->mode));
918 * Save the name for use in VOP_RENAME later.
920 cnp->cn_flags |= SAVENAME;
924 if (flags & ISDOTDOT) {
929 * Expanded copy of vn_vget_ino() so that
930 * fuse_vnode_get() can be used.
933 ltype = VOP_ISLOCKED(dvp);
934 err = vfs_busy(mp, MBF_NOWAIT);
938 err = vfs_busy(mp, 0);
939 vn_lock(dvp, ltype | LK_RETRY);
943 if ((dvp->v_iflag & VI_DOOMED) != 0) {
950 err = fuse_vnode_get(vnode_mount(dvp), feo, nid, NULL,
951 &vp, cnp, IFTOVT(fattr->mode));
953 vn_lock(dvp, ltype | LK_RETRY);
954 if ((dvp->v_iflag & VI_DOOMED) != 0) {
962 } else if (nid == VTOI(dvp)) {
966 struct fuse_vnode_data *fvdat;
968 err = fuse_vnode_get(vnode_mount(dvp), feo, nid, dvp,
969 &vp, cnp, IFTOVT(fattr->mode));
973 fuse_vnode_setparent(vp, dvp);
976 * In the case where we are looking up a FUSE node
977 * represented by an existing cached vnode, and the
978 * true size reported by FUSE_LOOKUP doesn't match
979 * the vnode's cached size, fix the vnode cache to
980 * match the real object size.
982 * This can occur via FUSE distributed filesystems,
983 * irregular files, etc.
986 if (vnode_isreg(vp) &&
987 fattr->size != fvdat->filesize) {
989 * The FN_SIZECHANGE flag reflects a dirty
990 * append. If userspace lets us know our cache
991 * is invalid, that write was lost. (Dirty
992 * writes that do not cause append are also
993 * lost, but we don't detect them here.)
995 * XXX: Maybe disable WB caching on this mount.
997 if (fvdat->flag & FN_SIZECHANGE)
998 printf("%s: WB cache incoherent on "
1000 vnode_mount(vp)->mnt_stat.f_mntonname);
1002 (void)fuse_vnode_setsize(vp, cred, fattr->size);
1003 fvdat->flag &= ~FN_SIZECHANGE;
1008 if (op == FUSE_GETATTR) {
1009 struct fuse_attr_out *fao =
1010 (struct fuse_attr_out*)fdi.answ;
1011 fuse_internal_cache_attrs(*vpp,
1012 &fao->attr, fao->attr_valid,
1013 fao->attr_valid_nsec, NULL);
1015 struct fuse_entry_out *feo =
1016 (struct fuse_entry_out*)fdi.answ;
1017 fuse_internal_cache_attrs(*vpp,
1018 &feo->attr, feo->attr_valid,
1019 feo->attr_valid_nsec, NULL);
1022 /* Insert name into cache if appropriate. */
1025 * Nooo, caching is evil. With caching, we can't avoid stale
1026 * information taking over the playground (cached info is not
1027 * just positive/negative, it does have qualitative aspects,
1028 * too). And a (VOP/FUSE)_GETATTR is always thrown anyway, when
1029 * walking down along cached path components, and that's not
1030 * any cheaper than FUSE_LOOKUP. This might change with
1031 * implementing kernel side attr caching, but... In Linux,
1032 * lookup results are not cached, and the daemon is bombarded
1033 * with FUSE_LOOKUPS on and on. This shows that by design, the
1034 * daemon is expected to handle frequent lookup queries
1035 * efficiently, do its caching in userspace, and so on.
1037 * So just leave the name cache alone.
1041 * Well, now I know, Linux caches lookups, but with a
1042 * timeout... So it's the same thing as attribute caching:
1043 * we can deal with it when implement timeouts.
1046 if (cnp->cn_flags & MAKEENTRY) {
1047 cache_enter(dvp, *vpp, cnp);
1054 /* No lookup error; need to clean up. */
1056 if (err) { /* Found inode; exit with no vnode. */
1057 if (op == FUSE_LOOKUP) {
1058 fuse_internal_forget_send(vnode_mount(dvp), td, cred,
1061 fdisp_destroy(&fdi);
1064 #ifndef NO_EARLY_PERM_CHECK_HACK
1067 * We have the attributes of the next item
1068 * *now*, and it's a fact, and we do not
1069 * have to do extra work for it (ie, beg the
1070 * daemon), and it neither depends on such
1071 * accidental things like attr caching. So
1072 * the big idea: check credentials *now*,
1073 * not at the beginning of the next call to
1076 * The first item of the lookup chain (fs root)
1077 * won't be checked then here, of course, as
1078 * its never "the next". But go and see that
1079 * the root is taken care about at the very
1080 * beginning of this function.
1082 * Now, given we want to do the access check
1083 * this way, one might ask: so then why not
1084 * do the access check just after fetching
1085 * the inode and its attributes from the
1086 * daemon? Why bother with producing the
1087 * corresponding vnode at all if something
1088 * is not OK? We know what's the deal as
1089 * soon as we get those attrs... There is
1090 * one bit of info though not given us by
1091 * the daemon: whether his response is
1092 * authoritative or not... His response should
1093 * be ignored if something is mounted over
1094 * the dir in question. But that can be
1095 * known only by having the vnode...
1097 int tmpvtype = vnode_vtype(*vpp);
1099 bzero(&facp, sizeof(facp));
1100 /*the early perm check hack */
1101 facp.facc_flags |= FACCESS_VA_VALID;
1103 if ((tmpvtype != VDIR) && (tmpvtype != VLNK)) {
1106 if (!err && !vnode_mountedhere(*vpp)) {
1107 err = fuse_internal_access(*vpp, VEXEC, &facp, td, cred);
1110 if (tmpvtype == VLNK)
1111 SDT_PROBE2(fuse, , vnops, trace,
1112 1, "weird, permission "
1113 "error with a symlink?");
1121 fdisp_destroy(&fdi);
1127 struct vnop_mkdir_args {
1128 struct vnode *a_dvp;
1129 struct vnode **a_vpp;
1130 struct componentname *a_cnp;
1131 struct vattr *a_vap;
1135 fuse_vnop_mkdir(struct vop_mkdir_args *ap)
1137 struct vnode *dvp = ap->a_dvp;
1138 struct vnode **vpp = ap->a_vpp;
1139 struct componentname *cnp = ap->a_cnp;
1140 struct vattr *vap = ap->a_vap;
1142 struct fuse_mkdir_in fmdi;
1144 if (fuse_isdeadfs(dvp)) {
1147 fmdi.mode = MAKEIMODE(vap->va_type, vap->va_mode);
1149 return (fuse_internal_newentry(dvp, vpp, cnp, FUSE_MKDIR, &fmdi,
1150 sizeof(fmdi), VDIR));
1154 struct vnop_mknod_args {
1155 struct vnode *a_dvp;
1156 struct vnode **a_vpp;
1157 struct componentname *a_cnp;
1158 struct vattr *a_vap;
1162 fuse_vnop_mknod(struct vop_mknod_args *ap)
1165 struct vnode *dvp = ap->a_dvp;
1166 struct vnode **vpp = ap->a_vpp;
1167 struct componentname *cnp = ap->a_cnp;
1168 struct vattr *vap = ap->a_vap;
1169 struct fuse_mknod_in fmni;
1171 if (fuse_isdeadfs(dvp))
1174 fmni.mode = MAKEIMODE(vap->va_type, vap->va_mode);
1175 fmni.rdev = vap->va_rdev;
1176 return (fuse_internal_newentry(dvp, vpp, cnp, FUSE_MKNOD, &fmni,
1177 sizeof(fmni), vap->va_type));
1181 struct vnop_open_args {
1184 struct ucred *a_cred;
1185 struct thread *a_td;
1186 int a_fdidx; / struct file *a_fp;
1190 fuse_vnop_open(struct vop_open_args *ap)
1192 struct vnode *vp = ap->a_vp;
1193 int mode = ap->a_mode;
1194 struct thread *td = ap->a_td;
1195 struct ucred *cred = ap->a_cred;
1197 fufh_type_t fufh_type;
1198 struct fuse_vnode_data *fvdat;
1200 if (fuse_isdeadfs(vp))
1202 if (vp->v_type == VCHR || vp->v_type == VBLK || vp->v_type == VFIFO)
1203 return (EOPNOTSUPP);
1204 if ((mode & (FREAD | FWRITE)) == 0)
1209 if (vnode_isdir(vp)) {
1210 fufh_type = FUFH_RDONLY;
1212 fufh_type = fuse_filehandle_xlate_from_fflags(mode);
1215 if (fuse_filehandle_validrw(vp, fufh_type) != FUFH_INVALID) {
1216 fuse_vnode_open(vp, 0, td);
1220 return fuse_filehandle_open(vp, fufh_type, NULL, td, cred);
1224 fuse_vnop_pathconf(struct vop_pathconf_args *ap)
1227 switch (ap->a_name) {
1228 case _PC_FILESIZEBITS:
1232 *ap->a_retval = NAME_MAX;
1235 *ap->a_retval = MIN(LONG_MAX, FUSE_LINK_MAX);
1237 case _PC_SYMLINK_MAX:
1238 *ap->a_retval = MAXPATHLEN;
1244 return (vop_stdpathconf(ap));
1249 struct vnop_read_args {
1253 struct ucred *a_cred;
1257 fuse_vnop_read(struct vop_read_args *ap)
1259 struct vnode *vp = ap->a_vp;
1260 struct uio *uio = ap->a_uio;
1261 int ioflag = ap->a_ioflag;
1262 struct ucred *cred = ap->a_cred;
1264 if (fuse_isdeadfs(vp)) {
1268 if (VTOFUD(vp)->flag & FN_DIRECTIO) {
1269 ioflag |= IO_DIRECT;
1272 return fuse_io_dispatch(vp, uio, ioflag, cred);
1276 struct vnop_readdir_args {
1279 struct ucred *a_cred;
1286 fuse_vnop_readdir(struct vop_readdir_args *ap)
1288 struct vnode *vp = ap->a_vp;
1289 struct uio *uio = ap->a_uio;
1290 struct ucred *cred = ap->a_cred;
1292 struct fuse_filehandle *fufh = NULL;
1293 struct fuse_iov cookediov;
1298 if (fuse_isdeadfs(vp)) {
1301 if ( /* XXXIP ((uio_iovcnt(uio) > 1)) || */
1302 (uio_resid(uio) < sizeof(struct dirent))) {
1306 if ((err = fuse_filehandle_get(vp, O_RDONLY, &fufh)) != 0) {
1307 SDT_PROBE2(fuse, , vnops, trace, 1,
1308 "calling readdir() before open()");
1309 err = fuse_filehandle_open(vp, O_RDONLY, &fufh, NULL, cred);
1315 #define DIRCOOKEDSIZE FUSE_DIRENT_ALIGN(FUSE_NAME_OFFSET + MAXNAMLEN + 1)
1316 fiov_init(&cookediov, DIRCOOKEDSIZE);
1318 err = fuse_internal_readdir(vp, uio, fufh, &cookediov);
1320 fiov_teardown(&cookediov);
1322 fuse_filehandle_close(vp, fufh, NULL, cred);
1328 struct vnop_readlink_args {
1331 struct ucred *a_cred;
1335 fuse_vnop_readlink(struct vop_readlink_args *ap)
1337 struct vnode *vp = ap->a_vp;
1338 struct uio *uio = ap->a_uio;
1339 struct ucred *cred = ap->a_cred;
1341 struct fuse_dispatcher fdi;
1344 if (fuse_isdeadfs(vp)) {
1347 if (!vnode_islnk(vp)) {
1350 fdisp_init(&fdi, 0);
1351 err = fdisp_simple_putget_vp(&fdi, FUSE_READLINK, vp, curthread, cred);
1355 if (((char *)fdi.answ)[0] == '/' &&
1356 fuse_get_mpdata(vnode_mount(vp))->dataflags & FSESS_PUSH_SYMLINKS_IN) {
1357 char *mpth = vnode_mount(vp)->mnt_stat.f_mntonname;
1359 err = uiomove(mpth, strlen(mpth), uio);
1362 err = uiomove(fdi.answ, fdi.iosize, uio);
1365 fdisp_destroy(&fdi);
1370 struct vnop_reclaim_args {
1372 struct thread *a_td;
1376 fuse_vnop_reclaim(struct vop_reclaim_args *ap)
1378 struct vnode *vp = ap->a_vp;
1379 struct thread *td = ap->a_td;
1380 struct fuse_vnode_data *fvdat = VTOFUD(vp);
1381 struct fuse_filehandle *fufh, *fufh_tmp;
1384 panic("FUSE: no vnode data during recycling");
1386 LIST_FOREACH_SAFE(fufh, &fvdat->handles, next, fufh_tmp) {
1387 printf("FUSE: vnode being reclaimed with open fufh "
1388 "(flags=%#x)", fufh->flags);
1389 fuse_filehandle_close(vp, fufh, td, NULL);
1392 if ((!fuse_isdeadfs(vp)) && (fvdat->nlookup)) {
1393 fuse_internal_forget_send(vnode_mount(vp), td, NULL, VTOI(vp),
1396 fuse_vnode_setparent(vp, NULL);
1398 vfs_hash_remove(vp);
1399 vnode_destroy_vobject(vp);
1400 fuse_vnode_destroy(vp);
1406 struct vnop_remove_args {
1407 struct vnode *a_dvp;
1409 struct componentname *a_cnp;
1413 fuse_vnop_remove(struct vop_remove_args *ap)
1415 struct vnode *dvp = ap->a_dvp;
1416 struct vnode *vp = ap->a_vp;
1417 struct componentname *cnp = ap->a_cnp;
1421 if (fuse_isdeadfs(vp)) {
1424 if (vnode_isdir(vp)) {
1429 err = fuse_internal_remove(dvp, vp, cnp, FUSE_UNLINK);
1432 fuse_internal_vnode_disappear(vp);
1437 struct vnop_rename_args {
1438 struct vnode *a_fdvp;
1439 struct vnode *a_fvp;
1440 struct componentname *a_fcnp;
1441 struct vnode *a_tdvp;
1442 struct vnode *a_tvp;
1443 struct componentname *a_tcnp;
1447 fuse_vnop_rename(struct vop_rename_args *ap)
1449 struct vnode *fdvp = ap->a_fdvp;
1450 struct vnode *fvp = ap->a_fvp;
1451 struct componentname *fcnp = ap->a_fcnp;
1452 struct vnode *tdvp = ap->a_tdvp;
1453 struct vnode *tvp = ap->a_tvp;
1454 struct componentname *tcnp = ap->a_tcnp;
1455 struct fuse_data *data;
1459 if (fuse_isdeadfs(fdvp)) {
1462 if (fvp->v_mount != tdvp->v_mount ||
1463 (tvp && fvp->v_mount != tvp->v_mount)) {
1464 SDT_PROBE2(fuse, , vnops, trace, 1, "cross-device rename");
1471 * FUSE library is expected to check if target directory is not
1472 * under the source directory in the file system tree.
1473 * Linux performs this check at VFS level.
1475 data = fuse_get_mpdata(vnode_mount(tdvp));
1476 sx_xlock(&data->rename_lock);
1477 err = fuse_internal_rename(fdvp, fcnp, tdvp, tcnp);
1480 fuse_vnode_setparent(fvp, tdvp);
1482 fuse_vnode_setparent(tvp, NULL);
1484 sx_unlock(&data->rename_lock);
1486 if (tvp != NULL && tvp != fvp) {
1489 if (vnode_isdir(fvp)) {
1490 if ((tvp != NULL) && vnode_isdir(tvp)) {
1511 struct vnop_rmdir_args {
1512 struct vnode *a_dvp;
1514 struct componentname *a_cnp;
1518 fuse_vnop_rmdir(struct vop_rmdir_args *ap)
1520 struct vnode *dvp = ap->a_dvp;
1521 struct vnode *vp = ap->a_vp;
1525 if (fuse_isdeadfs(vp)) {
1528 if (VTOFUD(vp) == VTOFUD(dvp)) {
1531 err = fuse_internal_remove(dvp, vp, ap->a_cnp, FUSE_RMDIR);
1534 fuse_internal_vnode_disappear(vp);
1539 struct vnop_setattr_args {
1541 struct vattr *a_vap;
1542 struct ucred *a_cred;
1543 struct thread *a_td;
1547 fuse_vnop_setattr(struct vop_setattr_args *ap)
1549 struct vnode *vp = ap->a_vp;
1550 struct vattr *vap = ap->a_vap;
1551 struct ucred *cred = ap->a_cred;
1552 struct thread *td = curthread;
1553 struct fuse_dispatcher fdi;
1554 struct fuse_setattr_in *fsai;
1555 struct fuse_access_param facp;
1559 int sizechanged = 0;
1560 uint64_t newsize = 0;
1562 if (fuse_isdeadfs(vp)) {
1565 fdisp_init(&fdi, sizeof(*fsai));
1566 fdisp_make_vp(&fdi, FUSE_SETATTR, vp, td, cred);
1570 bzero(&facp, sizeof(facp));
1572 facp.xuid = vap->va_uid;
1573 facp.xgid = vap->va_gid;
1575 if (vap->va_uid != (uid_t)VNOVAL) {
1576 facp.facc_flags |= FACCESS_CHOWN;
1577 fsai->uid = vap->va_uid;
1578 fsai->valid |= FATTR_UID;
1580 if (vap->va_gid != (gid_t)VNOVAL) {
1581 facp.facc_flags |= FACCESS_CHOWN;
1582 fsai->gid = vap->va_gid;
1583 fsai->valid |= FATTR_GID;
1585 if (vap->va_size != VNOVAL) {
1587 struct fuse_filehandle *fufh = NULL;
1589 /*Truncate to a new value. */
1590 fsai->size = vap->va_size;
1592 newsize = vap->va_size;
1593 fsai->valid |= FATTR_SIZE;
1595 fuse_filehandle_getrw(vp, FUFH_WRONLY, &fufh);
1597 fsai->fh = fufh->fh_id;
1598 fsai->valid |= FATTR_FH;
1601 if (vap->va_atime.tv_sec != VNOVAL) {
1602 fsai->atime = vap->va_atime.tv_sec;
1603 fsai->atimensec = vap->va_atime.tv_nsec;
1604 fsai->valid |= FATTR_ATIME;
1606 if (vap->va_mtime.tv_sec != VNOVAL) {
1607 fsai->mtime = vap->va_mtime.tv_sec;
1608 fsai->mtimensec = vap->va_mtime.tv_nsec;
1609 fsai->valid |= FATTR_MTIME;
1611 if (vap->va_mode != (mode_t)VNOVAL) {
1612 fsai->mode = vap->va_mode & ALLPERMS;
1613 fsai->valid |= FATTR_MODE;
1618 vtyp = vnode_vtype(vp);
1620 if (fsai->valid & FATTR_SIZE && vtyp == VDIR) {
1624 if (vfs_isrdonly(vnode_mount(vp)) && (fsai->valid & ~FATTR_SIZE || vtyp == VREG)) {
1629 if ((err = fdisp_wait_answ(&fdi)))
1631 vtyp = IFTOVT(((struct fuse_attr_out *)fdi.answ)->attr.mode);
1633 if (vnode_vtype(vp) != vtyp) {
1634 if (vnode_vtype(vp) == VNON && vtyp != VNON) {
1635 SDT_PROBE2(fuse, , vnops, trace, 1, "FUSE: Dang! "
1636 "vnode_vtype is VNON and vtype isn't.");
1639 * STALE vnode, ditch
1641 * The vnode has changed its type "behind our back".
1642 * There's nothing really we can do, so let us just
1643 * force an internal revocation and tell the caller to
1644 * try again, if interested.
1646 fuse_internal_vnode_disappear(vp);
1651 struct fuse_attr_out *fao = (struct fuse_attr_out*)fdi.answ;
1652 fuse_internal_cache_attrs(vp, &fao->attr, fao->attr_valid,
1653 fao->attr_valid_nsec, NULL);
1657 fdisp_destroy(&fdi);
1658 if (!err && sizechanged) {
1659 fuse_vnode_setsize(vp, cred, newsize);
1660 VTOFUD(vp)->flag &= ~FN_SIZECHANGE;
1666 struct vnop_strategy_args {
1672 fuse_vnop_strategy(struct vop_strategy_args *ap)
1674 struct vnode *vp = ap->a_vp;
1675 struct buf *bp = ap->a_bp;
1677 if (!vp || fuse_isdeadfs(vp)) {
1678 bp->b_ioflags |= BIO_ERROR;
1679 bp->b_error = ENXIO;
1683 if (bp->b_iocmd == BIO_WRITE) {
1686 err = fuse_vnode_refreshsize(vp, NOCRED);
1688 bp->b_ioflags |= BIO_ERROR;
1696 * VOP_STRATEGY always returns zero and signals error via bp->b_ioflags.
1697 * fuse_io_strategy sets bp's error fields
1699 (void)fuse_io_strategy(vp, bp);
1706 struct vnop_symlink_args {
1707 struct vnode *a_dvp;
1708 struct vnode **a_vpp;
1709 struct componentname *a_cnp;
1710 struct vattr *a_vap;
1715 fuse_vnop_symlink(struct vop_symlink_args *ap)
1717 struct vnode *dvp = ap->a_dvp;
1718 struct vnode **vpp = ap->a_vpp;
1719 struct componentname *cnp = ap->a_cnp;
1720 const char *target = ap->a_target;
1722 struct fuse_dispatcher fdi;
1727 if (fuse_isdeadfs(dvp)) {
1731 * Unlike the other creator type calls, here we have to create a message
1732 * where the name of the new entry comes first, and the data describing
1733 * the entry comes second.
1734 * Hence we can't rely on our handy fuse_internal_newentry() routine,
1735 * but put together the message manually and just call the core part.
1738 len = strlen(target) + 1;
1739 fdisp_init(&fdi, len + cnp->cn_namelen + 1);
1740 fdisp_make_vp(&fdi, FUSE_SYMLINK, dvp, curthread, NULL);
1742 memcpy(fdi.indata, cnp->cn_nameptr, cnp->cn_namelen);
1743 ((char *)fdi.indata)[cnp->cn_namelen] = '\0';
1744 memcpy((char *)fdi.indata + cnp->cn_namelen + 1, target, len);
1746 err = fuse_internal_newentry_core(dvp, vpp, cnp, VLNK, &fdi);
1747 fdisp_destroy(&fdi);
1752 struct vnop_write_args {
1756 struct ucred *a_cred;
1760 fuse_vnop_write(struct vop_write_args *ap)
1762 struct vnode *vp = ap->a_vp;
1763 struct uio *uio = ap->a_uio;
1764 int ioflag = ap->a_ioflag;
1765 struct ucred *cred = ap->a_cred;
1768 if (fuse_isdeadfs(vp)) {
1771 err = fuse_vnode_refreshsize(vp, cred);
1775 if (VTOFUD(vp)->flag & FN_DIRECTIO) {
1776 ioflag |= IO_DIRECT;
1779 return fuse_io_dispatch(vp, uio, ioflag, cred);
1782 SDT_PROBE_DEFINE1(fuse, , vnops, vnop_getpages_error, "int");
1784 struct vnop_getpages_args {
1792 fuse_vnop_getpages(struct vop_getpages_args *ap)
1794 int i, error, nextoff, size, toff, count, npages;
1805 KASSERT(vp->v_object, ("objectless vp passed to getpages"));
1806 td = curthread; /* XXX */
1807 cred = curthread->td_ucred; /* XXX */
1809 npages = ap->a_count;
1811 if (!fsess_opt_mmap(vnode_mount(vp))) {
1812 SDT_PROBE2(fuse, , vnops, trace, 1,
1813 "called on non-cacheable vnode??\n");
1814 return (VM_PAGER_ERROR);
1818 * If the last page is partially valid, just return it and allow
1819 * the pager to zero-out the blanks. Partially valid pages can
1820 * only occur at the file EOF.
1822 * XXXGL: is that true for FUSE, which is a local filesystem,
1823 * but still somewhat disconnected from the kernel?
1825 VM_OBJECT_WLOCK(vp->v_object);
1826 if (pages[npages - 1]->valid != 0 && --npages == 0)
1828 VM_OBJECT_WUNLOCK(vp->v_object);
1831 * We use only the kva address for the buffer, but this is extremely
1832 * convenient and fast.
1834 bp = uma_zalloc(fuse_pbuf_zone, M_WAITOK);
1836 kva = (vm_offset_t)bp->b_data;
1837 pmap_qenter(kva, pages, npages);
1838 VM_CNT_INC(v_vnodein);
1839 VM_CNT_ADD(v_vnodepgsin, npages);
1841 count = npages << PAGE_SHIFT;
1842 iov.iov_base = (caddr_t)kva;
1843 iov.iov_len = count;
1846 uio.uio_offset = IDX_TO_OFF(pages[0]->pindex);
1847 uio.uio_resid = count;
1848 uio.uio_segflg = UIO_SYSSPACE;
1849 uio.uio_rw = UIO_READ;
1852 error = fuse_io_dispatch(vp, &uio, IO_DIRECT, cred);
1853 pmap_qremove(kva, npages);
1855 uma_zfree(fuse_pbuf_zone, bp);
1857 if (error && (uio.uio_resid == count)) {
1858 SDT_PROBE1(fuse, , vnops, vnop_getpages_error, error);
1859 return VM_PAGER_ERROR;
1862 * Calculate the number of bytes read and validate only that number
1863 * of bytes. Note that due to pending writes, size may be 0. This
1864 * does not mean that the remaining data is invalid!
1867 size = count - uio.uio_resid;
1868 VM_OBJECT_WLOCK(vp->v_object);
1869 fuse_vm_page_lock_queues();
1870 for (i = 0, toff = 0; i < npages; i++, toff = nextoff) {
1873 nextoff = toff + PAGE_SIZE;
1876 if (nextoff <= size) {
1878 * Read operation filled an entire page
1880 m->valid = VM_PAGE_BITS_ALL;
1881 KASSERT(m->dirty == 0,
1882 ("fuse_getpages: page %p is dirty", m));
1883 } else if (size > toff) {
1885 * Read operation filled a partial page.
1888 vm_page_set_valid_range(m, 0, size - toff);
1889 KASSERT(m->dirty == 0,
1890 ("fuse_getpages: page %p is dirty", m));
1893 * Read operation was short. If no error occurred
1894 * we may have hit a zero-fill section. We simply
1895 * leave valid set to 0.
1900 fuse_vm_page_unlock_queues();
1902 VM_OBJECT_WUNLOCK(vp->v_object);
1907 return (VM_PAGER_OK);
1911 struct vnop_putpages_args {
1917 vm_ooffset_t a_offset;
1921 fuse_vnop_putpages(struct vop_putpages_args *ap)
1927 int i, error, npages, count;
1937 KASSERT(vp->v_object, ("objectless vp passed to putpages"));
1938 fsize = vp->v_object->un_pager.vnp.vnp_size;
1939 td = curthread; /* XXX */
1940 cred = curthread->td_ucred; /* XXX */
1942 count = ap->a_count;
1943 rtvals = ap->a_rtvals;
1944 npages = btoc(count);
1945 offset = IDX_TO_OFF(pages[0]->pindex);
1947 if (!fsess_opt_mmap(vnode_mount(vp))) {
1948 SDT_PROBE2(fuse, , vnops, trace, 1,
1949 "called on non-cacheable vnode??\n");
1951 for (i = 0; i < npages; i++)
1952 rtvals[i] = VM_PAGER_AGAIN;
1955 * When putting pages, do not extend file past EOF.
1958 if (offset + count > fsize) {
1959 count = fsize - offset;
1964 * We use only the kva address for the buffer, but this is extremely
1965 * convenient and fast.
1967 bp = uma_zalloc(fuse_pbuf_zone, M_WAITOK);
1969 kva = (vm_offset_t)bp->b_data;
1970 pmap_qenter(kva, pages, npages);
1971 VM_CNT_INC(v_vnodeout);
1972 VM_CNT_ADD(v_vnodepgsout, count);
1974 iov.iov_base = (caddr_t)kva;
1975 iov.iov_len = count;
1978 uio.uio_offset = offset;
1979 uio.uio_resid = count;
1980 uio.uio_segflg = UIO_SYSSPACE;
1981 uio.uio_rw = UIO_WRITE;
1984 error = fuse_io_dispatch(vp, &uio, IO_DIRECT, cred);
1986 pmap_qremove(kva, npages);
1987 uma_zfree(fuse_pbuf_zone, bp);
1990 int nwritten = round_page(count - uio.uio_resid) / PAGE_SIZE;
1992 for (i = 0; i < nwritten; i++) {
1993 rtvals[i] = VM_PAGER_OK;
1994 VM_OBJECT_WLOCK(pages[i]->object);
1995 vm_page_undirty(pages[i]);
1996 VM_OBJECT_WUNLOCK(pages[i]->object);
2002 static const char extattr_namespace_separator = '.';
2005 struct vop_getextattr_args {
2006 struct vop_generic_args a_gen;
2008 int a_attrnamespace;
2012 struct ucred *a_cred;
2013 struct thread *a_td;
2017 fuse_vnop_getextattr(struct vop_getextattr_args *ap)
2019 struct vnode *vp = ap->a_vp;
2020 struct uio *uio = ap->a_uio;
2021 struct fuse_dispatcher fdi;
2022 struct fuse_getxattr_in *get_xattr_in;
2023 struct fuse_getxattr_out *get_xattr_out;
2024 struct mount *mp = vnode_mount(vp);
2025 struct thread *td = ap->a_td;
2026 struct ucred *cred = ap->a_cred;
2032 if (fuse_isdeadfs(vp))
2035 /* Default to looking for user attributes. */
2036 if (ap->a_attrnamespace == EXTATTR_NAMESPACE_SYSTEM)
2037 prefix = EXTATTR_NAMESPACE_SYSTEM_STRING;
2039 prefix = EXTATTR_NAMESPACE_USER_STRING;
2041 len = strlen(prefix) + sizeof(extattr_namespace_separator) +
2042 strlen(ap->a_name) + 1;
2044 fdisp_init(&fdi, len + sizeof(*get_xattr_in));
2045 fdisp_make_vp(&fdi, FUSE_GETXATTR, vp, td, cred);
2047 get_xattr_in = fdi.indata;
2049 * Check to see whether we're querying the available size or
2050 * issuing the actual request. If we pass in 0, we get back struct
2051 * fuse_getxattr_out. If we pass in a non-zero size, we get back
2052 * that much data, without the struct fuse_getxattr_out header.
2055 get_xattr_in->size = 0;
2057 get_xattr_in->size = uio->uio_resid;
2059 attr_str = (char *)fdi.indata + sizeof(*get_xattr_in);
2060 snprintf(attr_str, len, "%s%c%s", prefix, extattr_namespace_separator,
2063 err = fdisp_wait_answ(&fdi);
2066 fsess_set_notimpl(mp, FUSE_GETXATTR);
2070 get_xattr_out = fdi.answ;
2072 if (ap->a_size != NULL)
2073 *ap->a_size = get_xattr_out->size;
2076 err = uiomove(fdi.answ, fdi.iosize, uio);
2079 fdisp_destroy(&fdi);
2084 struct vop_setextattr_args {
2085 struct vop_generic_args a_gen;
2087 int a_attrnamespace;
2090 struct ucred *a_cred;
2091 struct thread *a_td;
2095 fuse_vnop_setextattr(struct vop_setextattr_args *ap)
2097 struct vnode *vp = ap->a_vp;
2098 struct uio *uio = ap->a_uio;
2099 struct fuse_dispatcher fdi;
2100 struct fuse_setxattr_in *set_xattr_in;
2101 struct mount *mp = vnode_mount(vp);
2102 struct thread *td = ap->a_td;
2103 struct ucred *cred = ap->a_cred;
2109 if (fuse_isdeadfs(vp))
2112 /* Default to looking for user attributes. */
2113 if (ap->a_attrnamespace == EXTATTR_NAMESPACE_SYSTEM)
2114 prefix = EXTATTR_NAMESPACE_SYSTEM_STRING;
2116 prefix = EXTATTR_NAMESPACE_USER_STRING;
2118 len = strlen(prefix) + sizeof(extattr_namespace_separator) +
2119 strlen(ap->a_name) + 1;
2121 fdisp_init(&fdi, len + sizeof(*set_xattr_in) + uio->uio_resid);
2122 fdisp_make_vp(&fdi, FUSE_SETXATTR, vp, td, cred);
2124 set_xattr_in = fdi.indata;
2125 set_xattr_in->size = uio->uio_resid;
2127 attr_str = (char *)fdi.indata + sizeof(*set_xattr_in);
2128 snprintf(attr_str, len, "%s%c%s", prefix, extattr_namespace_separator,
2131 err = uiomove((char *)fdi.indata + sizeof(*set_xattr_in) + len,
2132 uio->uio_resid, uio);
2137 err = fdisp_wait_answ(&fdi);
2141 fsess_set_notimpl(mp, FUSE_SETXATTR);
2146 fdisp_destroy(&fdi);
2151 * The Linux / FUSE extended attribute list is simply a collection of
2152 * NUL-terminated strings. The FreeBSD extended attribute list is a single
2153 * byte length followed by a non-NUL terminated string. So, this allows
2154 * conversion of the Linux / FUSE format to the FreeBSD format in place.
2155 * Linux attribute names are reported with the namespace as a prefix (e.g.
2156 * "user.attribute_name"), but in FreeBSD they are reported without the
2157 * namespace prefix (e.g. "attribute_name"). So, we're going from:
2159 * user.attr_name1\0user.attr_name2\0
2163 * <num>attr_name1<num>attr_name2
2165 * Where "<num>" is a single byte number of characters in the attribute name.
2168 * prefix - exattr namespace prefix string
2169 * list, list_len - input list with namespace prefixes
2170 * bsd_list, bsd_list_len - output list compatible with bsd vfs
2173 fuse_xattrlist_convert(char *prefix, const char *list, int list_len,
2174 char *bsd_list, int *bsd_list_len)
2176 int len, pos, dist_to_next, prefix_len;
2180 prefix_len = strlen(prefix);
2182 while (pos < list_len && list[pos] != '\0') {
2183 dist_to_next = strlen(&list[pos]) + 1;
2184 if (bcmp(&list[pos], prefix, prefix_len) == 0 &&
2185 list[pos + prefix_len] == extattr_namespace_separator) {
2186 len = dist_to_next -
2187 (prefix_len + sizeof(extattr_namespace_separator)) - 1;
2188 if (len >= EXTATTR_MAXNAMELEN)
2189 return (ENAMETOOLONG);
2191 bsd_list[*bsd_list_len] = len;
2192 memcpy(&bsd_list[*bsd_list_len + 1],
2193 &list[pos + prefix_len +
2194 sizeof(extattr_namespace_separator)], len);
2196 *bsd_list_len += len + 1;
2199 pos += dist_to_next;
2206 struct vop_listextattr_args {
2207 struct vop_generic_args a_gen;
2209 int a_attrnamespace;
2212 struct ucred *a_cred;
2213 struct thread *a_td;
2217 fuse_vnop_listextattr(struct vop_listextattr_args *ap)
2219 struct vnode *vp = ap->a_vp;
2220 struct uio *uio = ap->a_uio;
2221 struct fuse_dispatcher fdi;
2222 struct fuse_listxattr_in *list_xattr_in;
2223 struct fuse_listxattr_out *list_xattr_out;
2224 struct mount *mp = vnode_mount(vp);
2225 struct thread *td = ap->a_td;
2226 struct ucred *cred = ap->a_cred;
2230 char *bsd_list = NULL;
2236 if (fuse_isdeadfs(vp))
2240 * Add space for a NUL and the period separator if enabled.
2241 * Default to looking for user attributes.
2243 if (ap->a_attrnamespace == EXTATTR_NAMESPACE_SYSTEM)
2244 prefix = EXTATTR_NAMESPACE_SYSTEM_STRING;
2246 prefix = EXTATTR_NAMESPACE_USER_STRING;
2248 len = strlen(prefix) + sizeof(extattr_namespace_separator) + 1;
2250 fdisp_init(&fdi, sizeof(*list_xattr_in) + len);
2251 fdisp_make_vp(&fdi, FUSE_LISTXATTR, vp, td, cred);
2254 * Retrieve Linux / FUSE compatible list size.
2256 list_xattr_in = fdi.indata;
2257 list_xattr_in->size = 0;
2258 attr_str = (char *)fdi.indata + sizeof(*list_xattr_in);
2259 snprintf(attr_str, len, "%s%c", prefix, extattr_namespace_separator);
2261 err = fdisp_wait_answ(&fdi);
2264 fsess_set_notimpl(mp, FUSE_LISTXATTR);
2268 list_xattr_out = fdi.answ;
2269 linux_list_len = list_xattr_out->size;
2270 if (linux_list_len == 0) {
2271 if (ap->a_size != NULL)
2272 *ap->a_size = linux_list_len;
2277 * Retrieve Linux / FUSE compatible list values.
2279 fdisp_make_vp(&fdi, FUSE_LISTXATTR, vp, td, cred);
2280 list_xattr_in = fdi.indata;
2281 list_xattr_in->size = linux_list_len + sizeof(*list_xattr_out);
2282 attr_str = (char *)fdi.indata + sizeof(*list_xattr_in);
2283 snprintf(attr_str, len, "%s%c", prefix, extattr_namespace_separator);
2285 err = fdisp_wait_answ(&fdi);
2289 linux_list = fdi.answ;
2290 linux_list_len = fdi.iosize;
2293 * Retrieve the BSD compatible list values.
2294 * The Linux / FUSE attribute list format isn't the same
2295 * as FreeBSD's format. So we need to transform it into
2296 * FreeBSD's format before giving it to the user.
2298 bsd_list = malloc(linux_list_len, M_TEMP, M_WAITOK);
2299 err = fuse_xattrlist_convert(prefix, linux_list, linux_list_len,
2300 bsd_list, &bsd_list_len);
2304 if (ap->a_size != NULL)
2305 *ap->a_size = bsd_list_len;
2308 err = uiomove(bsd_list, bsd_list_len, uio);
2311 free(bsd_list, M_TEMP);
2312 fdisp_destroy(&fdi);
2317 struct vop_deleteextattr_args {
2318 struct vop_generic_args a_gen;
2320 int a_attrnamespace;
2322 struct ucred *a_cred;
2323 struct thread *a_td;
2327 fuse_vnop_deleteextattr(struct vop_deleteextattr_args *ap)
2329 struct vnode *vp = ap->a_vp;
2330 struct fuse_dispatcher fdi;
2331 struct mount *mp = vnode_mount(vp);
2332 struct thread *td = ap->a_td;
2333 struct ucred *cred = ap->a_cred;
2339 if (fuse_isdeadfs(vp))
2342 /* Default to looking for user attributes. */
2343 if (ap->a_attrnamespace == EXTATTR_NAMESPACE_SYSTEM)
2344 prefix = EXTATTR_NAMESPACE_SYSTEM_STRING;
2346 prefix = EXTATTR_NAMESPACE_USER_STRING;
2348 len = strlen(prefix) + sizeof(extattr_namespace_separator) +
2349 strlen(ap->a_name) + 1;
2351 fdisp_init(&fdi, len);
2352 fdisp_make_vp(&fdi, FUSE_REMOVEXATTR, vp, td, cred);
2354 attr_str = fdi.indata;
2355 snprintf(attr_str, len, "%s%c%s", prefix, extattr_namespace_separator,
2358 err = fdisp_wait_answ(&fdi);
2361 fsess_set_notimpl(mp, FUSE_REMOVEXATTR);
2364 fdisp_destroy(&fdi);
2369 struct vnop_print_args {
2374 fuse_vnop_print(struct vop_print_args *ap)
2376 struct fuse_vnode_data *fvdat = VTOFUD(ap->a_vp);
2378 printf("nodeid: %ju, parent nodeid: %ju, nlookup: %ju, flag: %#x\n",
2379 (uintmax_t)VTOILLU(ap->a_vp), (uintmax_t)fvdat->parent_nid,
2380 (uintmax_t)fvdat->nlookup,