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(fusefs);
115 * arg0: verbosity. Higher numbers give more verbose messages
116 * arg1: Textual message
118 SDT_PROBE_DEFINE2(fusefs, , vnops, trace, "int", "char*");
121 static vop_access_t fuse_vnop_access;
122 static vop_advlock_t fuse_vnop_advlock;
123 static vop_close_t fuse_fifo_close;
124 static vop_close_t fuse_vnop_close;
125 static vop_create_t fuse_vnop_create;
126 static vop_deleteextattr_t fuse_vnop_deleteextattr;
127 static vop_fdatasync_t fuse_vnop_fdatasync;
128 static vop_fsync_t fuse_vnop_fsync;
129 static vop_getattr_t fuse_vnop_getattr;
130 static vop_getextattr_t fuse_vnop_getextattr;
131 static vop_inactive_t fuse_vnop_inactive;
132 static vop_link_t fuse_vnop_link;
133 static vop_listextattr_t fuse_vnop_listextattr;
134 static vop_lookup_t fuse_vnop_lookup;
135 static vop_mkdir_t fuse_vnop_mkdir;
136 static vop_mknod_t fuse_vnop_mknod;
137 static vop_open_t fuse_vnop_open;
138 static vop_pathconf_t fuse_vnop_pathconf;
139 static vop_read_t fuse_vnop_read;
140 static vop_readdir_t fuse_vnop_readdir;
141 static vop_readlink_t fuse_vnop_readlink;
142 static vop_reclaim_t fuse_vnop_reclaim;
143 static vop_remove_t fuse_vnop_remove;
144 static vop_rename_t fuse_vnop_rename;
145 static vop_rmdir_t fuse_vnop_rmdir;
146 static vop_setattr_t fuse_vnop_setattr;
147 static vop_setextattr_t fuse_vnop_setextattr;
148 static vop_strategy_t fuse_vnop_strategy;
149 static vop_symlink_t fuse_vnop_symlink;
150 static vop_write_t fuse_vnop_write;
151 static vop_getpages_t fuse_vnop_getpages;
152 static vop_putpages_t fuse_vnop_putpages;
153 static vop_print_t fuse_vnop_print;
154 static vop_vptofh_t fuse_vnop_vptofh;
156 struct vop_vector fuse_fifoops = {
157 .vop_default = &fifo_specops,
158 .vop_access = fuse_vnop_access,
159 .vop_close = fuse_fifo_close,
160 .vop_fsync = fuse_vnop_fsync,
161 .vop_getattr = fuse_vnop_getattr,
162 .vop_inactive = fuse_vnop_inactive,
163 .vop_pathconf = fuse_vnop_pathconf,
164 .vop_print = fuse_vnop_print,
165 .vop_read = VOP_PANIC,
166 .vop_reclaim = fuse_vnop_reclaim,
167 .vop_setattr = fuse_vnop_setattr,
168 .vop_write = VOP_PANIC,
169 .vop_vptofh = fuse_vnop_vptofh,
172 struct vop_vector fuse_vnops = {
173 .vop_allocate = VOP_EINVAL,
174 .vop_default = &default_vnodeops,
175 .vop_access = fuse_vnop_access,
176 .vop_advlock = fuse_vnop_advlock,
177 .vop_close = fuse_vnop_close,
178 .vop_create = fuse_vnop_create,
179 .vop_deleteextattr = fuse_vnop_deleteextattr,
180 .vop_fsync = fuse_vnop_fsync,
181 .vop_fdatasync = fuse_vnop_fdatasync,
182 .vop_getattr = fuse_vnop_getattr,
183 .vop_getextattr = fuse_vnop_getextattr,
184 .vop_inactive = fuse_vnop_inactive,
186 * TODO: implement vop_ioctl after upgrading to protocol 7.16.
187 * FUSE_IOCTL was added in 7.11, but 32-bit compat is broken until
190 .vop_link = fuse_vnop_link,
191 .vop_listextattr = fuse_vnop_listextattr,
192 .vop_lookup = fuse_vnop_lookup,
193 .vop_mkdir = fuse_vnop_mkdir,
194 .vop_mknod = fuse_vnop_mknod,
195 .vop_open = fuse_vnop_open,
196 .vop_pathconf = fuse_vnop_pathconf,
198 * TODO: implement vop_poll after upgrading to protocol 7.21.
199 * FUSE_POLL was added in protocol 7.11, but it's kind of broken until
200 * 7.21, which adds the ability for the client to choose which poll
201 * events it wants, and for a client to deregister a file handle
203 .vop_read = fuse_vnop_read,
204 .vop_readdir = fuse_vnop_readdir,
205 .vop_readlink = fuse_vnop_readlink,
206 .vop_reclaim = fuse_vnop_reclaim,
207 .vop_remove = fuse_vnop_remove,
208 .vop_rename = fuse_vnop_rename,
209 .vop_rmdir = fuse_vnop_rmdir,
210 .vop_setattr = fuse_vnop_setattr,
211 .vop_setextattr = fuse_vnop_setextattr,
212 .vop_strategy = fuse_vnop_strategy,
213 .vop_symlink = fuse_vnop_symlink,
214 .vop_write = fuse_vnop_write,
215 .vop_getpages = fuse_vnop_getpages,
216 .vop_putpages = fuse_vnop_putpages,
217 .vop_print = fuse_vnop_print,
218 .vop_vptofh = fuse_vnop_vptofh,
221 static u_long fuse_lookup_cache_hits = 0;
223 SYSCTL_ULONG(_vfs_fusefs, OID_AUTO, lookup_cache_hits, CTLFLAG_RD,
224 &fuse_lookup_cache_hits, 0, "number of positive cache hits in lookup");
226 static u_long fuse_lookup_cache_misses = 0;
228 SYSCTL_ULONG(_vfs_fusefs, OID_AUTO, lookup_cache_misses, CTLFLAG_RD,
229 &fuse_lookup_cache_misses, 0, "number of cache misses in lookup");
232 * XXX: This feature is highly experimental and can bring to instabilities,
233 * needs revisiting before to be enabled by default.
235 static int fuse_reclaim_revoked = 0;
237 SYSCTL_INT(_vfs_fusefs, OID_AUTO, reclaim_revoked, CTLFLAG_RW,
238 &fuse_reclaim_revoked, 0, "");
240 uma_zone_t fuse_pbuf_zone;
242 #define fuse_vm_page_lock(m) vm_page_lock((m));
243 #define fuse_vm_page_unlock(m) vm_page_unlock((m));
244 #define fuse_vm_page_lock_queues() ((void)0)
245 #define fuse_vm_page_unlock_queues() ((void)0)
247 /* Check permission for extattr operations, much like extattr_check_cred */
249 fuse_extattr_check_cred(struct vnode *vp, int ns, struct ucred *cred,
250 struct thread *td, accmode_t accmode)
252 struct mount *mp = vnode_mount(vp);
253 struct fuse_data *data = fuse_get_mpdata(mp);
256 * Kernel-invoked always succeeds.
262 * Do not allow privileged processes in jail to directly manipulate
266 case EXTATTR_NAMESPACE_SYSTEM:
267 if (data->dataflags & FSESS_DEFAULT_PERMISSIONS) {
268 return (priv_check_cred(cred, PRIV_VFS_EXTATTR_SYSTEM));
271 case EXTATTR_NAMESPACE_USER:
272 return (fuse_internal_access(vp, accmode, td, cred));
278 /* Get a filehandle for a directory */
280 fuse_filehandle_get_dir(struct vnode *vp, struct fuse_filehandle **fufhp,
281 struct ucred *cred, pid_t pid)
283 if (fuse_filehandle_get(vp, FREAD, fufhp, cred, pid) == 0)
285 return fuse_filehandle_get(vp, FEXEC, fufhp, cred, pid);
288 /* Send FUSE_FLUSH for this vnode */
290 fuse_flush(struct vnode *vp, struct ucred *cred, pid_t pid, int fflag)
292 struct fuse_flush_in *ffi;
293 struct fuse_filehandle *fufh;
294 struct fuse_dispatcher fdi;
295 struct thread *td = curthread;
296 struct mount *mp = vnode_mount(vp);
299 if (!fsess_isimpl(vnode_mount(vp), FUSE_FLUSH))
302 err = fuse_filehandle_getrw(vp, fflag, &fufh, cred, pid);
306 fdisp_init(&fdi, sizeof(*ffi));
307 fdisp_make_vp(&fdi, FUSE_FLUSH, vp, td, cred);
309 ffi->fh = fufh->fh_id;
311 * If the file has a POSIX lock then we're supposed to set lock_owner.
312 * If not, then lock_owner is undefined. So we may as well always set
315 ffi->lock_owner = td->td_proc->p_pid;
317 err = fdisp_wait_answ(&fdi);
319 fsess_set_notimpl(mp, FUSE_FLUSH);
326 /* Close wrapper for fifos. */
328 fuse_fifo_close(struct vop_close_args *ap)
330 return (fifo_specops.vop_close(ap));
334 struct vnop_access_args {
336 #if VOP_ACCESS_TAKES_ACCMODE_T
341 struct ucred *a_cred;
346 fuse_vnop_access(struct vop_access_args *ap)
348 struct vnode *vp = ap->a_vp;
349 int accmode = ap->a_accmode;
350 struct ucred *cred = ap->a_cred;
352 struct fuse_data *data = fuse_get_mpdata(vnode_mount(vp));
356 if (fuse_isdeadfs(vp)) {
357 if (vnode_isvroot(vp)) {
362 if (!(data->dataflags & FSESS_INITED)) {
363 if (vnode_isvroot(vp)) {
364 if (priv_check_cred(cred, PRIV_VFS_ADMIN) ||
365 (fuse_match_cred(data->daemoncred, cred) == 0)) {
371 if (vnode_islnk(vp)) {
375 err = fuse_internal_access(vp, accmode, ap->a_td, ap->a_cred);
380 * struct vop_advlock_args {
381 * struct vop_generic_args a_gen;
382 * struct vnode *a_vp;
385 * struct flock *a_fl;
390 fuse_vnop_advlock(struct vop_advlock_args *ap)
392 struct vnode *vp = ap->a_vp;
393 struct flock *fl = ap->a_fl;
394 struct thread *td = curthread;
395 struct ucred *cred = td->td_ucred;
396 pid_t pid = td->td_proc->p_pid;
397 struct fuse_filehandle *fufh;
398 struct fuse_dispatcher fdi;
399 struct fuse_lk_in *fli;
400 struct fuse_lk_out *flo;
403 int flags = ap->a_flags;
405 dataflags = fuse_get_mpdata(vnode_mount(vp))->dataflags;
407 if (fuse_isdeadfs(vp)) {
411 if (!(dataflags & FSESS_POSIX_LOCKS))
412 return vop_stdadvlock(ap);
413 /* FUSE doesn't properly support flock until protocol 7.17 */
415 return vop_stdadvlock(ap);
417 err = fuse_filehandle_get_anyflags(vp, &fufh, cred, pid);
421 fdisp_init(&fdi, sizeof(*fli));
437 fdisp_make_vp(&fdi, op, vp, td, cred);
439 fli->fh = fufh->fh_id;
440 fli->owner = fl->l_pid;
441 fli->lk.start = fl->l_start;
443 fli->lk.end = fl->l_start + fl->l_len - 1;
445 fli->lk.end = INT64_MAX;
446 fli->lk.type = fl->l_type;
447 fli->lk.pid = fl->l_pid;
449 err = fdisp_wait_answ(&fdi);
452 if (err == 0 && op == FUSE_GETLK) {
454 fl->l_type = flo->lk.type;
455 fl->l_pid = flo->lk.pid;
456 if (flo->lk.type != F_UNLCK) {
457 fl->l_start = flo->lk.start;
458 if (flo->lk.end == INT64_MAX)
461 fl->l_len = flo->lk.end - flo->lk.start + 1;
462 fl->l_start = flo->lk.start;
470 struct vop_close_args {
473 struct ucred *a_cred;
478 fuse_vnop_close(struct vop_close_args *ap)
480 struct vnode *vp = ap->a_vp;
481 struct ucred *cred = ap->a_cred;
482 int fflag = ap->a_fflag;
483 struct thread *td = ap->a_td;
484 pid_t pid = td->td_proc->p_pid;
487 if (fuse_isdeadfs(vp))
491 if (fflag & IO_NDELAY)
494 err = fuse_flush(vp, cred, pid, fflag);
495 /* TODO: close the file handle, if we're sure it's no longer used */
496 if ((VTOFUD(vp)->flag & FN_SIZECHANGE) != 0) {
497 fuse_vnode_savesize(vp, cred, td->td_proc->p_pid);
503 fdisp_make_mknod_for_fallback(
504 struct fuse_dispatcher *fdip,
505 struct componentname *cnp,
511 enum fuse_opcode *op)
513 struct fuse_mknod_in *fmni;
515 fdisp_init(fdip, sizeof(*fmni) + cnp->cn_namelen + 1);
517 fdisp_make(fdip, *op, vnode_mount(dvp), parentnid, td, cred);
521 memcpy((char *)fdip->indata + sizeof(*fmni), cnp->cn_nameptr,
523 ((char *)fdip->indata)[sizeof(*fmni) + cnp->cn_namelen] = '\0';
526 struct vnop_create_args {
528 struct vnode **a_vpp;
529 struct componentname *a_cnp;
534 fuse_vnop_create(struct vop_create_args *ap)
536 struct vnode *dvp = ap->a_dvp;
537 struct vnode **vpp = ap->a_vpp;
538 struct componentname *cnp = ap->a_cnp;
539 struct vattr *vap = ap->a_vap;
540 struct thread *td = cnp->cn_thread;
541 struct ucred *cred = cnp->cn_cred;
543 struct fuse_data *data;
544 struct fuse_create_in *fci;
545 struct fuse_entry_out *feo;
546 struct fuse_open_out *foo;
547 struct fuse_dispatcher fdi, fdi2;
548 struct fuse_dispatcher *fdip = &fdi;
549 struct fuse_dispatcher *fdip2 = NULL;
553 struct mount *mp = vnode_mount(dvp);
554 data = fuse_get_mpdata(mp);
555 uint64_t parentnid = VTOFUD(dvp)->nid;
556 mode_t mode = MAKEIMODE(vap->va_type, vap->va_mode);
560 if (fuse_isdeadfs(dvp))
563 /* FUSE expects sockets to be created with FUSE_MKNOD */
564 if (vap->va_type == VSOCK)
565 return fuse_internal_mknod(dvp, vpp, cnp, vap);
568 * VOP_CREATE doesn't tell us the open(2) flags, so we guess. Only a
569 * writable mode makes sense, and we might as well include readability
574 bzero(&fdi, sizeof(fdi));
576 if (vap->va_type != VREG)
579 if (!fsess_isimpl(mp, FUSE_CREATE) || vap->va_type == VSOCK) {
580 /* Fallback to FUSE_MKNOD/FUSE_OPEN */
581 fdisp_make_mknod_for_fallback(fdip, cnp, dvp, parentnid, td,
584 /* Use FUSE_CREATE */
588 fdisp_init(fdip, sizeof(*fci) + cnp->cn_namelen + 1);
589 fdisp_make(fdip, op, vnode_mount(dvp), parentnid, td, cred);
592 fci->flags = O_CREAT | flags;
593 if (fuse_libabi_geq(data, 7, 12)) {
594 insize = sizeof(*fci);
595 fci->umask = td->td_proc->p_fd->fd_cmask;
597 insize = sizeof(struct fuse_open_in);
600 memcpy((char *)fdip->indata + insize, cnp->cn_nameptr,
602 ((char *)fdip->indata)[insize + cnp->cn_namelen] = '\0';
605 err = fdisp_wait_answ(fdip);
608 if (err == ENOSYS && op == FUSE_CREATE) {
609 fsess_set_notimpl(mp, FUSE_CREATE);
610 fdisp_make_mknod_for_fallback(fdip, cnp, dvp,
611 parentnid, td, cred, mode, &op);
612 err = fdisp_wait_answ(fdip);
620 if ((err = fuse_internal_checkentry(feo, vap->va_type))) {
624 if (op == FUSE_CREATE) {
625 foo = (struct fuse_open_out*)(feo + 1);
627 /* Issue a separate FUSE_OPEN */
628 struct fuse_open_in *foi;
631 fdisp_init(fdip2, sizeof(*foi));
632 fdisp_make(fdip2, FUSE_OPEN, vnode_mount(dvp), feo->nodeid, td,
636 err = fdisp_wait_answ(fdip2);
641 err = fuse_vnode_get(mp, feo, feo->nodeid, dvp, vpp, cnp, vap->va_type);
643 struct fuse_release_in *fri;
644 uint64_t nodeid = feo->nodeid;
645 uint64_t fh_id = foo->fh;
647 fdisp_init(fdip, sizeof(*fri));
648 fdisp_make(fdip, FUSE_RELEASE, mp, nodeid, td, cred);
652 fuse_insert_callback(fdip->tick, fuse_internal_forget_callback);
653 fuse_insert_message(fdip->tick, false);
656 ASSERT_VOP_ELOCKED(*vpp, "fuse_vnop_create");
657 fuse_internal_cache_attrs(*vpp, cred, &feo->attr, feo->attr_valid,
658 feo->attr_valid_nsec, NULL);
660 fuse_filehandle_init(*vpp, FUFH_RDWR, NULL, td, cred, foo);
661 fuse_vnode_open(*vpp, foo->open_flags, td);
663 * Purge the parent's attribute cache because the daemon should've
664 * updated its mtime and ctime
666 fuse_vnode_clear_attr_cache(dvp);
667 cache_purge_negative(dvp);
671 fdisp_destroy(fdip2);
677 struct vnop_fdatasync_args {
678 struct vop_generic_args a_gen;
680 struct thread * a_td;
684 fuse_vnop_fdatasync(struct vop_fdatasync_args *ap)
686 struct vnode *vp = ap->a_vp;
687 struct thread *td = ap->a_td;
688 int waitfor = MNT_WAIT;
692 if (fuse_isdeadfs(vp)) {
695 if ((err = vop_stdfdatasync_buf(ap)))
698 return fuse_internal_fsync(vp, td, waitfor, true);
702 struct vnop_fsync_args {
703 struct vop_generic_args a_gen;
706 struct thread * a_td;
710 fuse_vnop_fsync(struct vop_fsync_args *ap)
712 struct vnode *vp = ap->a_vp;
713 struct thread *td = ap->a_td;
714 int waitfor = ap->a_waitfor;
717 if (fuse_isdeadfs(vp)) {
720 if ((err = vop_stdfsync(ap)))
723 return fuse_internal_fsync(vp, td, waitfor, false);
727 struct vnop_getattr_args {
730 struct ucred *a_cred;
735 fuse_vnop_getattr(struct vop_getattr_args *ap)
737 struct vnode *vp = ap->a_vp;
738 struct vattr *vap = ap->a_vap;
739 struct ucred *cred = ap->a_cred;
740 struct thread *td = curthread;
745 dataflags = fuse_get_mpdata(vnode_mount(vp))->dataflags;
747 /* Note that we are not bailing out on a dead file system just yet. */
749 if (!(dataflags & FSESS_INITED)) {
750 if (!vnode_isvroot(vp)) {
751 fdata_set_dead(fuse_get_mpdata(vnode_mount(vp)));
758 err = fuse_internal_getattr(vp, vap, cred, td);
759 if (err == ENOTCONN && vnode_isvroot(vp)) {
760 /* see comment in fuse_vfsop_statfs() */
767 bzero(vap, sizeof(*vap));
768 vap->va_type = vnode_vtype(vp);
774 struct vnop_inactive_args {
780 fuse_vnop_inactive(struct vop_inactive_args *ap)
782 struct vnode *vp = ap->a_vp;
783 struct thread *td = ap->a_td;
785 struct fuse_vnode_data *fvdat = VTOFUD(vp);
786 struct fuse_filehandle *fufh, *fufh_tmp;
790 LIST_FOREACH_SAFE(fufh, &fvdat->handles, next, fufh_tmp) {
791 if (need_flush && vp->v_type == VREG) {
792 if ((VTOFUD(vp)->flag & FN_SIZECHANGE) != 0) {
793 fuse_vnode_savesize(vp, NULL, 0);
795 if ((fvdat->flag & FN_REVOKED) != 0)
796 fuse_io_invalbuf(vp, td);
798 fuse_io_flushbuf(vp, MNT_WAIT, td);
801 fuse_filehandle_close(vp, fufh, td, NULL);
804 if ((fvdat->flag & FN_REVOKED) != 0 && fuse_reclaim_revoked) {
811 struct vnop_link_args {
812 struct vnode *a_tdvp;
814 struct componentname *a_cnp;
818 fuse_vnop_link(struct vop_link_args *ap)
820 struct vnode *vp = ap->a_vp;
821 struct vnode *tdvp = ap->a_tdvp;
822 struct componentname *cnp = ap->a_cnp;
823 struct ucred *cred = cnp->cn_cred;
825 struct vattr *vap = VTOVA(vp);
827 struct fuse_dispatcher fdi;
828 struct fuse_entry_out *feo;
829 struct fuse_link_in fli;
833 if (fuse_isdeadfs(vp)) {
836 if (vnode_mount(tdvp) != vnode_mount(vp)) {
841 * This is a seatbelt check to protect naive userspace filesystems from
842 * themselves and the limitations of the FUSE IPC protocol. If a
843 * filesystem does not allow attribute caching, assume it is capable of
844 * validating that nlink does not overflow.
846 if (vap != NULL && vap->va_nlink >= FUSE_LINK_MAX)
848 fli.oldnodeid = VTOI(vp);
851 fuse_internal_newentry_makerequest(vnode_mount(tdvp), VTOI(tdvp), cnp,
852 FUSE_LINK, &fli, sizeof(fli), &fdi);
853 if ((err = fdisp_wait_answ(&fdi))) {
858 err = fuse_internal_checkentry(feo, vnode_vtype(vp));
861 * Purge the parent's attribute cache because the daemon
862 * should've updated its mtime and ctime
864 fuse_vnode_clear_attr_cache(tdvp);
865 fuse_internal_cache_attrs(vp, cred, &feo->attr, feo->attr_valid,
866 feo->attr_valid_nsec, NULL);
873 struct fuse_lookup_alloc_arg {
874 struct fuse_entry_out *feo;
875 struct componentname *cnp;
880 /* Callback for vn_get_ino */
882 fuse_lookup_alloc(struct mount *mp, void *arg, int lkflags, struct vnode **vpp)
884 struct fuse_lookup_alloc_arg *flaa = arg;
886 return fuse_vnode_get(mp, flaa->feo, flaa->nid, NULL, vpp, flaa->cnp,
890 SDT_PROBE_DEFINE3(fusefs, , vnops, cache_lookup,
891 "int", "struct timespec*", "struct timespec*");
893 struct vnop_lookup_args {
894 struct vnodeop_desc *a_desc;
896 struct vnode **a_vpp;
897 struct componentname *a_cnp;
901 fuse_vnop_lookup(struct vop_lookup_args *ap)
903 struct vnode *dvp = ap->a_dvp;
904 struct vnode **vpp = ap->a_vpp;
905 struct componentname *cnp = ap->a_cnp;
906 struct thread *td = cnp->cn_thread;
907 struct ucred *cred = cnp->cn_cred;
909 int nameiop = cnp->cn_nameiop;
910 int flags = cnp->cn_flags;
911 int wantparent = flags & (LOCKPARENT | WANTPARENT);
912 int islastcn = flags & ISLASTCN;
913 struct mount *mp = vnode_mount(dvp);
917 struct vnode *vp = NULL;
919 struct fuse_dispatcher fdi;
920 bool did_lookup = false;
921 struct fuse_entry_out *feo = NULL;
922 enum vtype vtyp; /* vnode type of target */
923 off_t filesize; /* filesize of target */
927 if (fuse_isdeadfs(dvp)) {
931 if (!vnode_isdir(dvp))
934 if (islastcn && vfs_isrdonly(mp) && (nameiop != LOOKUP))
937 if ((err = fuse_internal_access(dvp, VEXEC, td, cred)))
940 if (flags & ISDOTDOT) {
941 KASSERT(VTOFUD(dvp)->flag & FN_PARENT_NID,
942 ("Looking up .. is TODO"));
943 nid = VTOFUD(dvp)->parent_nid;
946 /* .. is obviously a directory */
949 } else if (cnp->cn_namelen == 1 && *(cnp->cn_nameptr) == '.') {
951 /* . is obviously a directory */
955 struct timespec now, timeout;
957 err = cache_lookup(dvp, vpp, cnp, &timeout, NULL);
959 SDT_PROBE3(fusefs, , vnops, cache_lookup, err, &timeout, &now);
961 case -1: /* positive match */
962 if (timespeccmp(&timeout, &now, >)) {
963 atomic_add_acq_long(&fuse_lookup_cache_hits, 1);
966 atomic_add_acq_long(&fuse_lookup_cache_misses,
978 case 0: /* no match in cache */
979 atomic_add_acq_long(&fuse_lookup_cache_misses, 1);
982 case ENOENT: /* negative match */
984 if (timespeccmp(&timeout, &now, <=)) {
986 cache_purge_negative(dvp);
995 fdisp_init(&fdi, cnp->cn_namelen + 1);
996 fdisp_make(&fdi, FUSE_LOOKUP, mp, nid, td, cred);
998 memcpy(fdi.indata, cnp->cn_nameptr, cnp->cn_namelen);
999 ((char *)fdi.indata)[cnp->cn_namelen] = '\0';
1000 lookup_err = fdisp_wait_answ(&fdi);
1004 /* lookup call succeeded */
1005 feo = (struct fuse_entry_out *)fdi.answ;
1008 /* zero nodeid means ENOENT and cache it */
1009 struct timespec timeout;
1011 fdi.answ_stat = ENOENT;
1012 lookup_err = ENOENT;
1013 if (cnp->cn_flags & MAKEENTRY) {
1014 fuse_validity_2_timespec(feo, &timeout);
1015 cache_enter_time(dvp, *vpp, cnp,
1018 } else if (nid == FUSE_ROOT_ID) {
1019 lookup_err = EINVAL;
1021 vtyp = IFTOVT(feo->attr.mode);
1022 filesize = feo->attr.size;
1024 if (lookup_err && (!fdi.answ_stat || lookup_err != ENOENT)) {
1025 fdisp_destroy(&fdi);
1029 /* lookup_err, if non-zero, must be ENOENT at this point */
1032 /* Entry not found */
1033 if ((nameiop == CREATE || nameiop == RENAME) && islastcn) {
1034 err = fuse_internal_access(dvp, VWRITE, td, cred);
1037 * Set the SAVENAME flag to hold onto the
1038 * pathname for use later in VOP_CREATE or
1041 cnp->cn_flags |= SAVENAME;
1049 /* Entry was found */
1050 if (flags & ISDOTDOT) {
1051 struct fuse_lookup_alloc_arg flaa;
1057 err = vn_vget_ino_gen(dvp, fuse_lookup_alloc, &flaa, 0,
1060 } else if (nid == VTOI(dvp)) {
1064 struct fuse_vnode_data *fvdat;
1066 err = fuse_vnode_get(vnode_mount(dvp), feo, nid, dvp,
1073 * In the case where we are looking up a FUSE node
1074 * represented by an existing cached vnode, and the
1075 * true size reported by FUSE_LOOKUP doesn't match
1076 * the vnode's cached size, then any cached writes
1077 * beyond the file's current size are lost.
1080 * * following attribute cache expiration, or
1081 * * due a bug in the daemon, or
1084 if (vnode_isreg(vp) &&
1085 filesize != fvdat->cached_attrs.va_size &&
1086 fvdat->flag & FN_SIZECHANGE) {
1088 * The FN_SIZECHANGE flag reflects a dirty
1089 * append. If userspace lets us know our cache
1090 * is invalid, that write was lost. (Dirty
1091 * writes that do not cause append are also
1092 * lost, but we don't detect them here.)
1094 * XXX: Maybe disable WB caching on this mount.
1096 printf("%s: WB cache incoherent on %s!\n",
1098 vnode_mount(vp)->mnt_stat.f_mntonname);
1100 fvdat->flag &= ~FN_SIZECHANGE;
1104 fuse_internal_cache_attrs(*vpp, cred, &feo->attr,
1105 feo->attr_valid, feo->attr_valid_nsec, NULL);
1107 if ((nameiop == DELETE || nameiop == RENAME) &&
1110 struct vattr dvattr;
1112 err = fuse_internal_access(dvp, VWRITE, td,
1117 * if the parent's sticky bit is set, check
1118 * whether we're allowed to remove the file.
1119 * Need to figure out the vnode locking to make
1122 fuse_internal_getattr(dvp, &dvattr, cred, td);
1123 if ((dvattr.va_mode & S_ISTXT) &&
1124 fuse_internal_access(dvp, VADMIN, td,
1126 fuse_internal_access(*vpp, VADMIN, td,
1134 (nameiop == DELETE) ||
1135 (nameiop == RENAME && wantparent))) {
1136 cnp->cn_flags |= SAVENAME;
1143 if (vp != NULL && dvp != vp)
1145 else if (vp != NULL)
1150 fdisp_destroy(&fdi);
1156 struct vnop_mkdir_args {
1157 struct vnode *a_dvp;
1158 struct vnode **a_vpp;
1159 struct componentname *a_cnp;
1160 struct vattr *a_vap;
1164 fuse_vnop_mkdir(struct vop_mkdir_args *ap)
1166 struct vnode *dvp = ap->a_dvp;
1167 struct vnode **vpp = ap->a_vpp;
1168 struct componentname *cnp = ap->a_cnp;
1169 struct vattr *vap = ap->a_vap;
1171 struct fuse_mkdir_in fmdi;
1173 if (fuse_isdeadfs(dvp)) {
1176 fmdi.mode = MAKEIMODE(vap->va_type, vap->va_mode);
1177 fmdi.umask = curthread->td_proc->p_fd->fd_cmask;
1179 return (fuse_internal_newentry(dvp, vpp, cnp, FUSE_MKDIR, &fmdi,
1180 sizeof(fmdi), VDIR));
1184 struct vnop_mknod_args {
1185 struct vnode *a_dvp;
1186 struct vnode **a_vpp;
1187 struct componentname *a_cnp;
1188 struct vattr *a_vap;
1192 fuse_vnop_mknod(struct vop_mknod_args *ap)
1195 struct vnode *dvp = ap->a_dvp;
1196 struct vnode **vpp = ap->a_vpp;
1197 struct componentname *cnp = ap->a_cnp;
1198 struct vattr *vap = ap->a_vap;
1200 if (fuse_isdeadfs(dvp))
1203 return fuse_internal_mknod(dvp, vpp, cnp, vap);
1207 struct vop_open_args {
1210 struct ucred *a_cred;
1211 struct thread *a_td;
1212 int a_fdidx; / struct file *a_fp;
1216 fuse_vnop_open(struct vop_open_args *ap)
1218 struct vnode *vp = ap->a_vp;
1219 int a_mode = ap->a_mode;
1220 struct thread *td = ap->a_td;
1221 struct ucred *cred = ap->a_cred;
1222 pid_t pid = td->td_proc->p_pid;
1223 struct fuse_vnode_data *fvdat;
1225 if (fuse_isdeadfs(vp))
1227 if (vp->v_type == VCHR || vp->v_type == VBLK || vp->v_type == VFIFO)
1228 return (EOPNOTSUPP);
1229 if ((a_mode & (FREAD | FWRITE | FEXEC)) == 0)
1234 if (fuse_filehandle_validrw(vp, a_mode, cred, pid)) {
1235 fuse_vnode_open(vp, 0, td);
1239 return fuse_filehandle_open(vp, a_mode, NULL, td, cred);
1243 fuse_vnop_pathconf(struct vop_pathconf_args *ap)
1246 switch (ap->a_name) {
1247 case _PC_FILESIZEBITS:
1251 *ap->a_retval = NAME_MAX;
1254 *ap->a_retval = MIN(LONG_MAX, FUSE_LINK_MAX);
1256 case _PC_SYMLINK_MAX:
1257 *ap->a_retval = MAXPATHLEN;
1263 return (vop_stdpathconf(ap));
1268 struct vnop_read_args {
1272 struct ucred *a_cred;
1276 fuse_vnop_read(struct vop_read_args *ap)
1278 struct vnode *vp = ap->a_vp;
1279 struct uio *uio = ap->a_uio;
1280 int ioflag = ap->a_ioflag;
1281 struct ucred *cred = ap->a_cred;
1282 pid_t pid = curthread->td_proc->p_pid;
1284 if (fuse_isdeadfs(vp)) {
1288 if (VTOFUD(vp)->flag & FN_DIRECTIO) {
1289 ioflag |= IO_DIRECT;
1292 return fuse_io_dispatch(vp, uio, ioflag, false, cred, pid);
1296 struct vnop_readdir_args {
1299 struct ucred *a_cred;
1306 fuse_vnop_readdir(struct vop_readdir_args *ap)
1308 struct vnode *vp = ap->a_vp;
1309 struct uio *uio = ap->a_uio;
1310 struct ucred *cred = ap->a_cred;
1311 struct fuse_filehandle *fufh = NULL;
1312 struct fuse_iov cookediov;
1318 bool closefufh = false;
1319 pid_t pid = curthread->td_proc->p_pid;
1323 if (fuse_isdeadfs(vp)) {
1326 if ( /* XXXIP ((uio_iovcnt(uio) > 1)) || */
1327 (uio_resid(uio) < sizeof(struct dirent))) {
1331 tresid = uio->uio_resid;
1332 startoff = uio->uio_offset;
1333 err = fuse_filehandle_get_dir(vp, &fufh, cred, pid);
1334 if (err == EBADF && vnode_mount(vp)->mnt_flag & MNT_EXPORTED) {
1336 * nfsd will do VOP_READDIR without first doing VOP_OPEN. We
1337 * must implicitly open the directory here
1339 err = fuse_filehandle_open(vp, FREAD, &fufh, curthread, cred);
1342 * When a directory is opened, it must be read from
1343 * the beginning. Hopefully, the "startoff" still
1344 * exists as an offset cookie for the directory.
1345 * If not, it will read the entire directory without
1346 * returning any entries and just return eof.
1348 uio->uio_offset = 0;
1354 if (ap->a_ncookies != NULL) {
1355 ncookies = uio->uio_resid /
1356 (offsetof(struct dirent, d_name) + 4) + 1;
1357 cookies = malloc(ncookies * sizeof(*cookies), M_TEMP, M_WAITOK);
1358 *ap->a_ncookies = ncookies;
1359 *ap->a_cookies = cookies;
1364 #define DIRCOOKEDSIZE FUSE_DIRENT_ALIGN(FUSE_NAME_OFFSET + MAXNAMLEN + 1)
1365 fiov_init(&cookediov, DIRCOOKEDSIZE);
1367 err = fuse_internal_readdir(vp, uio, startoff, fufh, &cookediov,
1368 &ncookies, cookies);
1370 fiov_teardown(&cookediov);
1372 fuse_filehandle_close(vp, fufh, curthread, cred);
1374 if (ap->a_ncookies != NULL) {
1376 *ap->a_ncookies -= ncookies;
1378 free(*ap->a_cookies, M_TEMP);
1379 *ap->a_ncookies = 0;
1380 *ap->a_cookies = NULL;
1383 if (err == 0 && tresid == uio->uio_resid)
1390 struct vnop_readlink_args {
1393 struct ucred *a_cred;
1397 fuse_vnop_readlink(struct vop_readlink_args *ap)
1399 struct vnode *vp = ap->a_vp;
1400 struct uio *uio = ap->a_uio;
1401 struct ucred *cred = ap->a_cred;
1403 struct fuse_dispatcher fdi;
1406 if (fuse_isdeadfs(vp)) {
1409 if (!vnode_islnk(vp)) {
1412 fdisp_init(&fdi, 0);
1413 err = fdisp_simple_putget_vp(&fdi, FUSE_READLINK, vp, curthread, cred);
1417 if (((char *)fdi.answ)[0] == '/' &&
1418 fuse_get_mpdata(vnode_mount(vp))->dataflags & FSESS_PUSH_SYMLINKS_IN) {
1419 char *mpth = vnode_mount(vp)->mnt_stat.f_mntonname;
1421 err = uiomove(mpth, strlen(mpth), uio);
1424 err = uiomove(fdi.answ, fdi.iosize, uio);
1427 fdisp_destroy(&fdi);
1432 struct vnop_reclaim_args {
1434 struct thread *a_td;
1438 fuse_vnop_reclaim(struct vop_reclaim_args *ap)
1440 struct vnode *vp = ap->a_vp;
1441 struct thread *td = ap->a_td;
1442 struct fuse_vnode_data *fvdat = VTOFUD(vp);
1443 struct fuse_filehandle *fufh, *fufh_tmp;
1446 panic("FUSE: no vnode data during recycling");
1448 LIST_FOREACH_SAFE(fufh, &fvdat->handles, next, fufh_tmp) {
1449 printf("FUSE: vnode being reclaimed with open fufh "
1450 "(type=%#x)", fufh->fufh_type);
1451 fuse_filehandle_close(vp, fufh, td, NULL);
1454 if ((!fuse_isdeadfs(vp)) && (fvdat->nlookup)) {
1455 fuse_internal_forget_send(vnode_mount(vp), td, NULL, VTOI(vp),
1458 fuse_vnode_setparent(vp, NULL);
1460 vfs_hash_remove(vp);
1461 vnode_destroy_vobject(vp);
1462 fuse_vnode_destroy(vp);
1468 struct vnop_remove_args {
1469 struct vnode *a_dvp;
1471 struct componentname *a_cnp;
1475 fuse_vnop_remove(struct vop_remove_args *ap)
1477 struct vnode *dvp = ap->a_dvp;
1478 struct vnode *vp = ap->a_vp;
1479 struct componentname *cnp = ap->a_cnp;
1483 if (fuse_isdeadfs(vp)) {
1486 if (vnode_isdir(vp)) {
1491 err = fuse_internal_remove(dvp, vp, cnp, FUSE_UNLINK);
1494 fuse_internal_vnode_disappear(vp);
1496 * Purge the parent's attribute cache because the daemon
1497 * should've updated its mtime and ctime
1499 fuse_vnode_clear_attr_cache(dvp);
1505 struct vnop_rename_args {
1506 struct vnode *a_fdvp;
1507 struct vnode *a_fvp;
1508 struct componentname *a_fcnp;
1509 struct vnode *a_tdvp;
1510 struct vnode *a_tvp;
1511 struct componentname *a_tcnp;
1515 fuse_vnop_rename(struct vop_rename_args *ap)
1517 struct vnode *fdvp = ap->a_fdvp;
1518 struct vnode *fvp = ap->a_fvp;
1519 struct componentname *fcnp = ap->a_fcnp;
1520 struct vnode *tdvp = ap->a_tdvp;
1521 struct vnode *tvp = ap->a_tvp;
1522 struct componentname *tcnp = ap->a_tcnp;
1523 struct fuse_data *data;
1524 bool newparent = fdvp != tdvp;
1525 bool isdir = fvp->v_type == VDIR;
1528 if (fuse_isdeadfs(fdvp)) {
1531 if (fvp->v_mount != tdvp->v_mount ||
1532 (tvp && fvp->v_mount != tvp->v_mount)) {
1533 SDT_PROBE2(fusefs, , vnops, trace, 1, "cross-device rename");
1540 * FUSE library is expected to check if target directory is not
1541 * under the source directory in the file system tree.
1542 * Linux performs this check at VFS level.
1545 * If source is a directory, and it will get a new parent, user must
1546 * have write permission to it, so ".." can be modified.
1548 data = fuse_get_mpdata(vnode_mount(tdvp));
1549 if (data->dataflags & FSESS_DEFAULT_PERMISSIONS && isdir && newparent) {
1550 err = fuse_internal_access(fvp, VWRITE,
1551 tcnp->cn_thread, tcnp->cn_cred);
1555 sx_xlock(&data->rename_lock);
1556 err = fuse_internal_rename(fdvp, fcnp, tdvp, tcnp);
1559 fuse_vnode_setparent(fvp, tdvp);
1561 fuse_vnode_setparent(tvp, NULL);
1563 sx_unlock(&data->rename_lock);
1565 if (tvp != NULL && tvp != fvp) {
1568 if (vnode_isdir(fvp)) {
1569 if ((tvp != NULL) && vnode_isdir(tvp)) {
1590 struct vnop_rmdir_args {
1591 struct vnode *a_dvp;
1593 struct componentname *a_cnp;
1597 fuse_vnop_rmdir(struct vop_rmdir_args *ap)
1599 struct vnode *dvp = ap->a_dvp;
1600 struct vnode *vp = ap->a_vp;
1604 if (fuse_isdeadfs(vp)) {
1607 if (VTOFUD(vp) == VTOFUD(dvp)) {
1610 err = fuse_internal_remove(dvp, vp, ap->a_cnp, FUSE_RMDIR);
1613 fuse_internal_vnode_disappear(vp);
1615 * Purge the parent's attribute cache because the daemon
1616 * should've updated its mtime and ctime
1618 fuse_vnode_clear_attr_cache(dvp);
1624 struct vnop_setattr_args {
1626 struct vattr *a_vap;
1627 struct ucred *a_cred;
1628 struct thread *a_td;
1632 fuse_vnop_setattr(struct vop_setattr_args *ap)
1634 struct vnode *vp = ap->a_vp;
1635 struct vattr *vap = ap->a_vap;
1636 struct ucred *cred = ap->a_cred;
1637 struct thread *td = curthread;
1639 struct fuse_data *data;
1640 struct vattr old_va;
1643 accmode_t accmode = 0;
1645 bool drop_suid = false;
1648 mp = vnode_mount(vp);
1649 data = fuse_get_mpdata(mp);
1650 dataflags = data->dataflags;
1651 checkperm = dataflags & FSESS_DEFAULT_PERMISSIONS;
1652 if (cred->cr_ngroups > 0)
1653 cr_gid = cred->cr_groups[0];
1657 if (fuse_isdeadfs(vp)) {
1661 if (vap->va_uid != (uid_t)VNOVAL) {
1663 /* Only root may change a file's owner */
1664 err = priv_check_cred(cred, PRIV_VFS_CHOWN);
1666 /* As a special case, allow the null chown */
1667 err2 = fuse_internal_getattr(vp, &old_va, cred,
1671 if (vap->va_uid != old_va.va_uid)
1681 if (vap->va_gid != (gid_t)VNOVAL) {
1682 if (checkperm && priv_check_cred(cred, PRIV_VFS_CHOWN))
1684 if (checkperm && !groupmember(vap->va_gid, cred))
1687 * Non-root users may only chgrp to one of their own
1690 err = priv_check_cred(cred, PRIV_VFS_CHOWN);
1692 /* As a special case, allow the null chgrp */
1693 err2 = fuse_internal_getattr(vp, &old_va, cred,
1697 if (vap->va_gid != old_va.va_gid)
1705 if (vap->va_size != VNOVAL) {
1706 switch (vp->v_type) {
1711 if (vfs_isrdonly(mp))
1716 * According to POSIX, the result is unspecified
1717 * for file types other than regular files,
1718 * directories and shared memory objects. We
1719 * don't support shared memory objects in the file
1720 * system, and have dubious support for truncating
1721 * symlinks. Just ignore the request in other cases.
1725 /* Don't set accmode. Permission to trunc is checked upstack */
1727 if (vap->va_atime.tv_sec != VNOVAL || vap->va_mtime.tv_sec != VNOVAL) {
1728 if (vap->va_vaflags & VA_UTIMES_NULL)
1734 if (vap->va_mode != (mode_t)VNOVAL)
1735 vap->va_mode &= ~(S_ISUID | S_ISGID);
1737 err = fuse_internal_getattr(vp, &old_va, cred, td);
1740 vap->va_mode = old_va.va_mode & ~(S_ISUID | S_ISGID);
1743 if (vap->va_mode != (mode_t)VNOVAL) {
1744 /* Only root may set the sticky bit on non-directories */
1745 if (checkperm && vp->v_type != VDIR && (vap->va_mode & S_ISTXT)
1746 && priv_check_cred(cred, PRIV_VFS_STICKYFILE))
1748 if (checkperm && (vap->va_mode & S_ISGID)) {
1749 err = fuse_internal_getattr(vp, &old_va, cred, td);
1752 if (!groupmember(old_va.va_gid, cred)) {
1753 err = priv_check_cred(cred, PRIV_VFS_SETGID);
1761 if (vfs_isrdonly(mp))
1764 err = fuse_internal_access(vp, accmode, td, cred);
1768 return fuse_internal_setattr(vp, vap, td, cred);
1772 struct vnop_strategy_args {
1778 fuse_vnop_strategy(struct vop_strategy_args *ap)
1780 struct vnode *vp = ap->a_vp;
1781 struct buf *bp = ap->a_bp;
1783 if (!vp || fuse_isdeadfs(vp)) {
1784 bp->b_ioflags |= BIO_ERROR;
1785 bp->b_error = ENXIO;
1791 * VOP_STRATEGY always returns zero and signals error via bp->b_ioflags.
1792 * fuse_io_strategy sets bp's error fields
1794 (void)fuse_io_strategy(vp, bp);
1801 struct vnop_symlink_args {
1802 struct vnode *a_dvp;
1803 struct vnode **a_vpp;
1804 struct componentname *a_cnp;
1805 struct vattr *a_vap;
1810 fuse_vnop_symlink(struct vop_symlink_args *ap)
1812 struct vnode *dvp = ap->a_dvp;
1813 struct vnode **vpp = ap->a_vpp;
1814 struct componentname *cnp = ap->a_cnp;
1815 const char *target = ap->a_target;
1817 struct fuse_dispatcher fdi;
1822 if (fuse_isdeadfs(dvp)) {
1826 * Unlike the other creator type calls, here we have to create a message
1827 * where the name of the new entry comes first, and the data describing
1828 * the entry comes second.
1829 * Hence we can't rely on our handy fuse_internal_newentry() routine,
1830 * but put together the message manually and just call the core part.
1833 len = strlen(target) + 1;
1834 fdisp_init(&fdi, len + cnp->cn_namelen + 1);
1835 fdisp_make_vp(&fdi, FUSE_SYMLINK, dvp, curthread, NULL);
1837 memcpy(fdi.indata, cnp->cn_nameptr, cnp->cn_namelen);
1838 ((char *)fdi.indata)[cnp->cn_namelen] = '\0';
1839 memcpy((char *)fdi.indata + cnp->cn_namelen + 1, target, len);
1841 err = fuse_internal_newentry_core(dvp, vpp, cnp, VLNK, &fdi);
1842 fdisp_destroy(&fdi);
1847 struct vnop_write_args {
1851 struct ucred *a_cred;
1855 fuse_vnop_write(struct vop_write_args *ap)
1857 struct vnode *vp = ap->a_vp;
1858 struct uio *uio = ap->a_uio;
1859 int ioflag = ap->a_ioflag;
1860 struct ucred *cred = ap->a_cred;
1861 pid_t pid = curthread->td_proc->p_pid;
1863 if (fuse_isdeadfs(vp)) {
1867 if (VTOFUD(vp)->flag & FN_DIRECTIO) {
1868 ioflag |= IO_DIRECT;
1871 return fuse_io_dispatch(vp, uio, ioflag, false, cred, pid);
1874 SDT_PROBE_DEFINE1(fusefs, , vnops, vnop_getpages_error, "int");
1876 struct vnop_getpages_args {
1884 fuse_vnop_getpages(struct vop_getpages_args *ap)
1886 int i, error, nextoff, size, toff, count, npages;
1895 pid_t pid = curthread->td_proc->p_pid;
1898 KASSERT(vp->v_object, ("objectless vp passed to getpages"));
1899 td = curthread; /* XXX */
1900 cred = curthread->td_ucred; /* XXX */
1902 npages = ap->a_count;
1904 if (!fsess_opt_mmap(vnode_mount(vp))) {
1905 SDT_PROBE2(fusefs, , vnops, trace, 1,
1906 "called on non-cacheable vnode??\n");
1907 return (VM_PAGER_ERROR);
1911 * If the last page is partially valid, just return it and allow
1912 * the pager to zero-out the blanks. Partially valid pages can
1913 * only occur at the file EOF.
1915 * XXXGL: is that true for FUSE, which is a local filesystem,
1916 * but still somewhat disconnected from the kernel?
1918 VM_OBJECT_WLOCK(vp->v_object);
1919 if (pages[npages - 1]->valid != 0 && --npages == 0)
1921 VM_OBJECT_WUNLOCK(vp->v_object);
1924 * We use only the kva address for the buffer, but this is extremely
1925 * convenient and fast.
1927 bp = uma_zalloc(fuse_pbuf_zone, M_WAITOK);
1929 kva = (vm_offset_t)bp->b_data;
1930 pmap_qenter(kva, pages, npages);
1931 VM_CNT_INC(v_vnodein);
1932 VM_CNT_ADD(v_vnodepgsin, npages);
1934 count = npages << PAGE_SHIFT;
1935 iov.iov_base = (caddr_t)kva;
1936 iov.iov_len = count;
1939 uio.uio_offset = IDX_TO_OFF(pages[0]->pindex);
1940 uio.uio_resid = count;
1941 uio.uio_segflg = UIO_SYSSPACE;
1942 uio.uio_rw = UIO_READ;
1945 error = fuse_io_dispatch(vp, &uio, IO_DIRECT, true, cred, pid);
1946 pmap_qremove(kva, npages);
1948 uma_zfree(fuse_pbuf_zone, bp);
1950 if (error && (uio.uio_resid == count)) {
1951 SDT_PROBE1(fusefs, , vnops, vnop_getpages_error, error);
1952 return VM_PAGER_ERROR;
1955 * Calculate the number of bytes read and validate only that number
1956 * of bytes. Note that due to pending writes, size may be 0. This
1957 * does not mean that the remaining data is invalid!
1960 size = count - uio.uio_resid;
1961 VM_OBJECT_WLOCK(vp->v_object);
1962 fuse_vm_page_lock_queues();
1963 for (i = 0, toff = 0; i < npages; i++, toff = nextoff) {
1966 nextoff = toff + PAGE_SIZE;
1969 if (nextoff <= size) {
1971 * Read operation filled an entire page
1973 m->valid = VM_PAGE_BITS_ALL;
1974 KASSERT(m->dirty == 0,
1975 ("fuse_getpages: page %p is dirty", m));
1976 } else if (size > toff) {
1978 * Read operation filled a partial page.
1981 vm_page_set_valid_range(m, 0, size - toff);
1982 KASSERT(m->dirty == 0,
1983 ("fuse_getpages: page %p is dirty", m));
1986 * Read operation was short. If no error occurred
1987 * we may have hit a zero-fill section. We simply
1988 * leave valid set to 0.
1993 fuse_vm_page_unlock_queues();
1995 VM_OBJECT_WUNLOCK(vp->v_object);
2000 return (VM_PAGER_OK);
2004 struct vnop_putpages_args {
2010 vm_ooffset_t a_offset;
2014 fuse_vnop_putpages(struct vop_putpages_args *ap)
2020 int i, error, npages, count;
2028 pid_t pid = curthread->td_proc->p_pid;
2031 KASSERT(vp->v_object, ("objectless vp passed to putpages"));
2032 fsize = vp->v_object->un_pager.vnp.vnp_size;
2033 td = curthread; /* XXX */
2034 cred = curthread->td_ucred; /* XXX */
2036 count = ap->a_count;
2037 rtvals = ap->a_rtvals;
2038 npages = btoc(count);
2039 offset = IDX_TO_OFF(pages[0]->pindex);
2041 if (!fsess_opt_mmap(vnode_mount(vp))) {
2042 SDT_PROBE2(fusefs, , vnops, trace, 1,
2043 "called on non-cacheable vnode??\n");
2045 for (i = 0; i < npages; i++)
2046 rtvals[i] = VM_PAGER_AGAIN;
2049 * When putting pages, do not extend file past EOF.
2052 if (offset + count > fsize) {
2053 count = fsize - offset;
2058 * We use only the kva address for the buffer, but this is extremely
2059 * convenient and fast.
2061 bp = uma_zalloc(fuse_pbuf_zone, M_WAITOK);
2063 kva = (vm_offset_t)bp->b_data;
2064 pmap_qenter(kva, pages, npages);
2065 VM_CNT_INC(v_vnodeout);
2066 VM_CNT_ADD(v_vnodepgsout, count);
2068 iov.iov_base = (caddr_t)kva;
2069 iov.iov_len = count;
2072 uio.uio_offset = offset;
2073 uio.uio_resid = count;
2074 uio.uio_segflg = UIO_SYSSPACE;
2075 uio.uio_rw = UIO_WRITE;
2078 error = fuse_io_dispatch(vp, &uio, IO_DIRECT, true, cred, pid);
2080 pmap_qremove(kva, npages);
2081 uma_zfree(fuse_pbuf_zone, bp);
2084 int nwritten = round_page(count - uio.uio_resid) / PAGE_SIZE;
2086 for (i = 0; i < nwritten; i++) {
2087 rtvals[i] = VM_PAGER_OK;
2088 VM_OBJECT_WLOCK(pages[i]->object);
2089 vm_page_undirty(pages[i]);
2090 VM_OBJECT_WUNLOCK(pages[i]->object);
2096 static const char extattr_namespace_separator = '.';
2099 struct vop_getextattr_args {
2100 struct vop_generic_args a_gen;
2102 int a_attrnamespace;
2106 struct ucred *a_cred;
2107 struct thread *a_td;
2111 fuse_vnop_getextattr(struct vop_getextattr_args *ap)
2113 struct vnode *vp = ap->a_vp;
2114 struct uio *uio = ap->a_uio;
2115 struct fuse_dispatcher fdi;
2116 struct fuse_getxattr_in *get_xattr_in;
2117 struct fuse_getxattr_out *get_xattr_out;
2118 struct mount *mp = vnode_mount(vp);
2119 struct thread *td = ap->a_td;
2120 struct ucred *cred = ap->a_cred;
2126 if (fuse_isdeadfs(vp))
2129 if (!fsess_isimpl(mp, FUSE_GETXATTR))
2132 err = fuse_extattr_check_cred(vp, ap->a_attrnamespace, cred, td, VREAD);
2136 /* Default to looking for user attributes. */
2137 if (ap->a_attrnamespace == EXTATTR_NAMESPACE_SYSTEM)
2138 prefix = EXTATTR_NAMESPACE_SYSTEM_STRING;
2140 prefix = EXTATTR_NAMESPACE_USER_STRING;
2142 len = strlen(prefix) + sizeof(extattr_namespace_separator) +
2143 strlen(ap->a_name) + 1;
2145 fdisp_init(&fdi, len + sizeof(*get_xattr_in));
2146 fdisp_make_vp(&fdi, FUSE_GETXATTR, vp, td, cred);
2148 get_xattr_in = fdi.indata;
2150 * Check to see whether we're querying the available size or
2151 * issuing the actual request. If we pass in 0, we get back struct
2152 * fuse_getxattr_out. If we pass in a non-zero size, we get back
2153 * that much data, without the struct fuse_getxattr_out header.
2156 get_xattr_in->size = 0;
2158 get_xattr_in->size = uio->uio_resid;
2160 attr_str = (char *)fdi.indata + sizeof(*get_xattr_in);
2161 snprintf(attr_str, len, "%s%c%s", prefix, extattr_namespace_separator,
2164 err = fdisp_wait_answ(&fdi);
2166 if (err == ENOSYS) {
2167 fsess_set_notimpl(mp, FUSE_GETXATTR);
2173 get_xattr_out = fdi.answ;
2175 if (ap->a_size != NULL)
2176 *ap->a_size = get_xattr_out->size;
2179 err = uiomove(fdi.answ, fdi.iosize, uio);
2182 fdisp_destroy(&fdi);
2187 struct vop_setextattr_args {
2188 struct vop_generic_args a_gen;
2190 int a_attrnamespace;
2193 struct ucred *a_cred;
2194 struct thread *a_td;
2198 fuse_vnop_setextattr(struct vop_setextattr_args *ap)
2200 struct vnode *vp = ap->a_vp;
2201 struct uio *uio = ap->a_uio;
2202 struct fuse_dispatcher fdi;
2203 struct fuse_setxattr_in *set_xattr_in;
2204 struct mount *mp = vnode_mount(vp);
2205 struct thread *td = ap->a_td;
2206 struct ucred *cred = ap->a_cred;
2212 if (fuse_isdeadfs(vp))
2215 if (!fsess_isimpl(mp, FUSE_SETXATTR))
2218 if (vfs_isrdonly(mp))
2221 /* Deleting xattrs must use VOP_DELETEEXTATTR instead */
2222 if (ap->a_uio == NULL) {
2224 * If we got here as fallback from VOP_DELETEEXTATTR, then
2225 * return EOPNOTSUPP.
2227 if (!fsess_isimpl(mp, FUSE_REMOVEXATTR))
2228 return (EOPNOTSUPP);
2233 err = fuse_extattr_check_cred(vp, ap->a_attrnamespace, cred, td,
2238 /* Default to looking for user attributes. */
2239 if (ap->a_attrnamespace == EXTATTR_NAMESPACE_SYSTEM)
2240 prefix = EXTATTR_NAMESPACE_SYSTEM_STRING;
2242 prefix = EXTATTR_NAMESPACE_USER_STRING;
2244 len = strlen(prefix) + sizeof(extattr_namespace_separator) +
2245 strlen(ap->a_name) + 1;
2247 fdisp_init(&fdi, len + sizeof(*set_xattr_in) + uio->uio_resid);
2248 fdisp_make_vp(&fdi, FUSE_SETXATTR, vp, td, cred);
2250 set_xattr_in = fdi.indata;
2251 set_xattr_in->size = uio->uio_resid;
2253 attr_str = (char *)fdi.indata + sizeof(*set_xattr_in);
2254 snprintf(attr_str, len, "%s%c%s", prefix, extattr_namespace_separator,
2257 err = uiomove((char *)fdi.indata + sizeof(*set_xattr_in) + len,
2258 uio->uio_resid, uio);
2263 err = fdisp_wait_answ(&fdi);
2265 if (err == ENOSYS) {
2266 fsess_set_notimpl(mp, FUSE_SETXATTR);
2269 if (err == ERESTART) {
2270 /* Can't restart after calling uiomove */
2275 fdisp_destroy(&fdi);
2280 * The Linux / FUSE extended attribute list is simply a collection of
2281 * NUL-terminated strings. The FreeBSD extended attribute list is a single
2282 * byte length followed by a non-NUL terminated string. So, this allows
2283 * conversion of the Linux / FUSE format to the FreeBSD format in place.
2284 * Linux attribute names are reported with the namespace as a prefix (e.g.
2285 * "user.attribute_name"), but in FreeBSD they are reported without the
2286 * namespace prefix (e.g. "attribute_name"). So, we're going from:
2288 * user.attr_name1\0user.attr_name2\0
2292 * <num>attr_name1<num>attr_name2
2294 * Where "<num>" is a single byte number of characters in the attribute name.
2297 * prefix - exattr namespace prefix string
2298 * list, list_len - input list with namespace prefixes
2299 * bsd_list, bsd_list_len - output list compatible with bsd vfs
2302 fuse_xattrlist_convert(char *prefix, const char *list, int list_len,
2303 char *bsd_list, int *bsd_list_len)
2305 int len, pos, dist_to_next, prefix_len;
2309 prefix_len = strlen(prefix);
2311 while (pos < list_len && list[pos] != '\0') {
2312 dist_to_next = strlen(&list[pos]) + 1;
2313 if (bcmp(&list[pos], prefix, prefix_len) == 0 &&
2314 list[pos + prefix_len] == extattr_namespace_separator) {
2315 len = dist_to_next -
2316 (prefix_len + sizeof(extattr_namespace_separator)) - 1;
2317 if (len >= EXTATTR_MAXNAMELEN)
2318 return (ENAMETOOLONG);
2320 bsd_list[*bsd_list_len] = len;
2321 memcpy(&bsd_list[*bsd_list_len + 1],
2322 &list[pos + prefix_len +
2323 sizeof(extattr_namespace_separator)], len);
2325 *bsd_list_len += len + 1;
2328 pos += dist_to_next;
2335 struct vop_listextattr_args {
2336 struct vop_generic_args a_gen;
2338 int a_attrnamespace;
2341 struct ucred *a_cred;
2342 struct thread *a_td;
2346 fuse_vnop_listextattr(struct vop_listextattr_args *ap)
2348 struct vnode *vp = ap->a_vp;
2349 struct uio *uio = ap->a_uio;
2350 struct fuse_dispatcher fdi;
2351 struct fuse_listxattr_in *list_xattr_in;
2352 struct fuse_listxattr_out *list_xattr_out;
2353 struct mount *mp = vnode_mount(vp);
2354 struct thread *td = ap->a_td;
2355 struct ucred *cred = ap->a_cred;
2359 char *bsd_list = NULL;
2365 if (fuse_isdeadfs(vp))
2368 if (!fsess_isimpl(mp, FUSE_LISTXATTR))
2371 err = fuse_extattr_check_cred(vp, ap->a_attrnamespace, cred, td, VREAD);
2376 * Add space for a NUL and the period separator if enabled.
2377 * Default to looking for user attributes.
2379 if (ap->a_attrnamespace == EXTATTR_NAMESPACE_SYSTEM)
2380 prefix = EXTATTR_NAMESPACE_SYSTEM_STRING;
2382 prefix = EXTATTR_NAMESPACE_USER_STRING;
2384 len = strlen(prefix) + sizeof(extattr_namespace_separator) + 1;
2386 fdisp_init(&fdi, sizeof(*list_xattr_in) + len);
2387 fdisp_make_vp(&fdi, FUSE_LISTXATTR, vp, td, cred);
2390 * Retrieve Linux / FUSE compatible list size.
2392 list_xattr_in = fdi.indata;
2393 list_xattr_in->size = 0;
2394 attr_str = (char *)fdi.indata + sizeof(*list_xattr_in);
2395 snprintf(attr_str, len, "%s%c", prefix, extattr_namespace_separator);
2397 err = fdisp_wait_answ(&fdi);
2399 if (err == ENOSYS) {
2400 fsess_set_notimpl(mp, FUSE_LISTXATTR);
2406 list_xattr_out = fdi.answ;
2407 linux_list_len = list_xattr_out->size;
2408 if (linux_list_len == 0) {
2409 if (ap->a_size != NULL)
2410 *ap->a_size = linux_list_len;
2415 * Retrieve Linux / FUSE compatible list values.
2417 fdisp_refresh_vp(&fdi, FUSE_LISTXATTR, vp, td, cred);
2418 list_xattr_in = fdi.indata;
2419 list_xattr_in->size = linux_list_len + sizeof(*list_xattr_out);
2420 attr_str = (char *)fdi.indata + sizeof(*list_xattr_in);
2421 snprintf(attr_str, len, "%s%c", prefix, extattr_namespace_separator);
2423 err = fdisp_wait_answ(&fdi);
2427 linux_list = fdi.answ;
2428 linux_list_len = fdi.iosize;
2431 * Retrieve the BSD compatible list values.
2432 * The Linux / FUSE attribute list format isn't the same
2433 * as FreeBSD's format. So we need to transform it into
2434 * FreeBSD's format before giving it to the user.
2436 bsd_list = malloc(linux_list_len, M_TEMP, M_WAITOK);
2437 err = fuse_xattrlist_convert(prefix, linux_list, linux_list_len,
2438 bsd_list, &bsd_list_len);
2442 if (ap->a_size != NULL)
2443 *ap->a_size = bsd_list_len;
2446 err = uiomove(bsd_list, bsd_list_len, uio);
2449 free(bsd_list, M_TEMP);
2450 fdisp_destroy(&fdi);
2455 struct vop_deleteextattr_args {
2456 struct vop_generic_args a_gen;
2458 int a_attrnamespace;
2460 struct ucred *a_cred;
2461 struct thread *a_td;
2465 fuse_vnop_deleteextattr(struct vop_deleteextattr_args *ap)
2467 struct vnode *vp = ap->a_vp;
2468 struct fuse_dispatcher fdi;
2469 struct mount *mp = vnode_mount(vp);
2470 struct thread *td = ap->a_td;
2471 struct ucred *cred = ap->a_cred;
2477 if (fuse_isdeadfs(vp))
2480 if (!fsess_isimpl(mp, FUSE_REMOVEXATTR))
2483 if (vfs_isrdonly(mp))
2486 err = fuse_extattr_check_cred(vp, ap->a_attrnamespace, cred, td,
2491 /* Default to looking for user attributes. */
2492 if (ap->a_attrnamespace == EXTATTR_NAMESPACE_SYSTEM)
2493 prefix = EXTATTR_NAMESPACE_SYSTEM_STRING;
2495 prefix = EXTATTR_NAMESPACE_USER_STRING;
2497 len = strlen(prefix) + sizeof(extattr_namespace_separator) +
2498 strlen(ap->a_name) + 1;
2500 fdisp_init(&fdi, len);
2501 fdisp_make_vp(&fdi, FUSE_REMOVEXATTR, vp, td, cred);
2503 attr_str = fdi.indata;
2504 snprintf(attr_str, len, "%s%c%s", prefix, extattr_namespace_separator,
2507 err = fdisp_wait_answ(&fdi);
2508 if (err == ENOSYS) {
2509 fsess_set_notimpl(mp, FUSE_REMOVEXATTR);
2513 fdisp_destroy(&fdi);
2518 struct vnop_print_args {
2523 fuse_vnop_print(struct vop_print_args *ap)
2525 struct fuse_vnode_data *fvdat = VTOFUD(ap->a_vp);
2527 printf("nodeid: %ju, parent nodeid: %ju, nlookup: %ju, flag: %#x\n",
2528 (uintmax_t)VTOILLU(ap->a_vp), (uintmax_t)fvdat->parent_nid,
2529 (uintmax_t)fvdat->nlookup,
2536 * Get an NFS filehandle for a FUSE file.
2538 * This will only work for FUSE file systems that guarantee the uniqueness of
2539 * nodeid:generation, which most don't
2543 IN struct vnode *a_vp;
2544 IN struct fid *a_fhp;
2548 fuse_vnop_vptofh(struct vop_vptofh_args *ap)
2550 struct vnode *vp = ap->a_vp;
2551 struct fuse_vnode_data *fvdat = VTOFUD(vp);
2552 struct fuse_fid *fhp = (struct fuse_fid *)(ap->a_fhp);
2553 _Static_assert(sizeof(struct fuse_fid) <= sizeof(struct fid),
2554 "FUSE fid type is too big");
2555 struct mount *mp = vnode_mount(vp);
2556 struct fuse_data *data = fuse_get_mpdata(mp);
2560 if (!(data->dataflags & FSESS_EXPORT_SUPPORT))
2563 err = fuse_internal_getattr(vp, &va, curthread->td_ucred, curthread);
2567 /*ip = VTOI(ap->a_vp);*/
2568 /*ufhp = (struct ufid *)ap->a_fhp;*/
2569 fhp->len = sizeof(struct fuse_fid);
2570 fhp->nid = fvdat->nid;
2571 if (fvdat->generation <= UINT32_MAX)
2572 fhp->gen = fvdat->generation;