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_bmap_t fuse_vnop_bmap;
124 static vop_close_t fuse_fifo_close;
125 static vop_close_t fuse_vnop_close;
126 static vop_create_t fuse_vnop_create;
127 static vop_deleteextattr_t fuse_vnop_deleteextattr;
128 static vop_fdatasync_t fuse_vnop_fdatasync;
129 static vop_fsync_t fuse_vnop_fsync;
130 static vop_getattr_t fuse_vnop_getattr;
131 static vop_getextattr_t fuse_vnop_getextattr;
132 static vop_inactive_t fuse_vnop_inactive;
133 static vop_link_t fuse_vnop_link;
134 static vop_listextattr_t fuse_vnop_listextattr;
135 static vop_lookup_t fuse_vnop_lookup;
136 static vop_mkdir_t fuse_vnop_mkdir;
137 static vop_mknod_t fuse_vnop_mknod;
138 static vop_open_t fuse_vnop_open;
139 static vop_pathconf_t fuse_vnop_pathconf;
140 static vop_read_t fuse_vnop_read;
141 static vop_readdir_t fuse_vnop_readdir;
142 static vop_readlink_t fuse_vnop_readlink;
143 static vop_reclaim_t fuse_vnop_reclaim;
144 static vop_remove_t fuse_vnop_remove;
145 static vop_rename_t fuse_vnop_rename;
146 static vop_rmdir_t fuse_vnop_rmdir;
147 static vop_setattr_t fuse_vnop_setattr;
148 static vop_setextattr_t fuse_vnop_setextattr;
149 static vop_strategy_t fuse_vnop_strategy;
150 static vop_symlink_t fuse_vnop_symlink;
151 static vop_write_t fuse_vnop_write;
152 static vop_getpages_t fuse_vnop_getpages;
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_bmap = fuse_vnop_bmap,
178 .vop_close = fuse_vnop_close,
179 .vop_create = fuse_vnop_create,
180 .vop_deleteextattr = fuse_vnop_deleteextattr,
181 .vop_fsync = fuse_vnop_fsync,
182 .vop_fdatasync = fuse_vnop_fdatasync,
183 .vop_getattr = fuse_vnop_getattr,
184 .vop_getextattr = fuse_vnop_getextattr,
185 .vop_inactive = fuse_vnop_inactive,
187 * TODO: implement vop_ioctl after upgrading to protocol 7.16.
188 * FUSE_IOCTL was added in 7.11, but 32-bit compat is broken until
191 .vop_link = fuse_vnop_link,
192 .vop_listextattr = fuse_vnop_listextattr,
193 .vop_lookup = fuse_vnop_lookup,
194 .vop_mkdir = fuse_vnop_mkdir,
195 .vop_mknod = fuse_vnop_mknod,
196 .vop_open = fuse_vnop_open,
197 .vop_pathconf = fuse_vnop_pathconf,
199 * TODO: implement vop_poll after upgrading to protocol 7.21.
200 * FUSE_POLL was added in protocol 7.11, but it's kind of broken until
201 * 7.21, which adds the ability for the client to choose which poll
202 * events it wants, and for a client to deregister a file handle
204 .vop_read = fuse_vnop_read,
205 .vop_readdir = fuse_vnop_readdir,
206 .vop_readlink = fuse_vnop_readlink,
207 .vop_reclaim = fuse_vnop_reclaim,
208 .vop_remove = fuse_vnop_remove,
209 .vop_rename = fuse_vnop_rename,
210 .vop_rmdir = fuse_vnop_rmdir,
211 .vop_setattr = fuse_vnop_setattr,
212 .vop_setextattr = fuse_vnop_setextattr,
213 .vop_strategy = fuse_vnop_strategy,
214 .vop_symlink = fuse_vnop_symlink,
215 .vop_write = fuse_vnop_write,
216 .vop_getpages = fuse_vnop_getpages,
217 .vop_print = fuse_vnop_print,
218 .vop_vptofh = fuse_vnop_vptofh,
222 * XXX: This feature is highly experimental and can bring to instabilities,
223 * needs revisiting before to be enabled by default.
225 static int fuse_reclaim_revoked = 0;
227 SYSCTL_INT(_vfs_fusefs, OID_AUTO, reclaim_revoked, CTLFLAG_RW,
228 &fuse_reclaim_revoked, 0, "");
230 uma_zone_t fuse_pbuf_zone;
232 #define fuse_vm_page_lock(m) vm_page_lock((m));
233 #define fuse_vm_page_unlock(m) vm_page_unlock((m));
234 #define fuse_vm_page_lock_queues() ((void)0)
235 #define fuse_vm_page_unlock_queues() ((void)0)
237 /* Check permission for extattr operations, much like extattr_check_cred */
239 fuse_extattr_check_cred(struct vnode *vp, int ns, struct ucred *cred,
240 struct thread *td, accmode_t accmode)
242 struct mount *mp = vnode_mount(vp);
243 struct fuse_data *data = fuse_get_mpdata(mp);
246 * Kernel-invoked always succeeds.
252 * Do not allow privileged processes in jail to directly manipulate
256 case EXTATTR_NAMESPACE_SYSTEM:
257 if (data->dataflags & FSESS_DEFAULT_PERMISSIONS) {
258 return (priv_check_cred(cred, PRIV_VFS_EXTATTR_SYSTEM));
261 case EXTATTR_NAMESPACE_USER:
262 return (fuse_internal_access(vp, accmode, td, cred));
268 /* Get a filehandle for a directory */
270 fuse_filehandle_get_dir(struct vnode *vp, struct fuse_filehandle **fufhp,
271 struct ucred *cred, pid_t pid)
273 if (fuse_filehandle_get(vp, FREAD, fufhp, cred, pid) == 0)
275 return fuse_filehandle_get(vp, FEXEC, fufhp, cred, pid);
278 /* Send FUSE_FLUSH for this vnode */
280 fuse_flush(struct vnode *vp, struct ucred *cred, pid_t pid, int fflag)
282 struct fuse_flush_in *ffi;
283 struct fuse_filehandle *fufh;
284 struct fuse_dispatcher fdi;
285 struct thread *td = curthread;
286 struct mount *mp = vnode_mount(vp);
289 if (!fsess_isimpl(vnode_mount(vp), FUSE_FLUSH))
292 err = fuse_filehandle_getrw(vp, fflag, &fufh, cred, pid);
296 fdisp_init(&fdi, sizeof(*ffi));
297 fdisp_make_vp(&fdi, FUSE_FLUSH, vp, td, cred);
299 ffi->fh = fufh->fh_id;
301 * If the file has a POSIX lock then we're supposed to set lock_owner.
302 * If not, then lock_owner is undefined. So we may as well always set
305 ffi->lock_owner = td->td_proc->p_pid;
307 err = fdisp_wait_answ(&fdi);
309 fsess_set_notimpl(mp, FUSE_FLUSH);
316 /* Close wrapper for fifos. */
318 fuse_fifo_close(struct vop_close_args *ap)
320 return (fifo_specops.vop_close(ap));
324 struct vnop_access_args {
326 #if VOP_ACCESS_TAKES_ACCMODE_T
331 struct ucred *a_cred;
336 fuse_vnop_access(struct vop_access_args *ap)
338 struct vnode *vp = ap->a_vp;
339 int accmode = ap->a_accmode;
340 struct ucred *cred = ap->a_cred;
342 struct fuse_data *data = fuse_get_mpdata(vnode_mount(vp));
346 if (fuse_isdeadfs(vp)) {
347 if (vnode_isvroot(vp)) {
352 if (!(data->dataflags & FSESS_INITED)) {
353 if (vnode_isvroot(vp)) {
354 if (priv_check_cred(cred, PRIV_VFS_ADMIN) ||
355 (fuse_match_cred(data->daemoncred, cred) == 0)) {
361 if (vnode_islnk(vp)) {
365 err = fuse_internal_access(vp, accmode, ap->a_td, ap->a_cred);
370 * struct vop_advlock_args {
371 * struct vop_generic_args a_gen;
372 * struct vnode *a_vp;
375 * struct flock *a_fl;
380 fuse_vnop_advlock(struct vop_advlock_args *ap)
382 struct vnode *vp = ap->a_vp;
383 struct flock *fl = ap->a_fl;
384 struct thread *td = curthread;
385 struct ucred *cred = td->td_ucred;
386 pid_t pid = td->td_proc->p_pid;
387 struct fuse_filehandle *fufh;
388 struct fuse_dispatcher fdi;
389 struct fuse_lk_in *fli;
390 struct fuse_lk_out *flo;
393 int flags = ap->a_flags;
395 dataflags = fuse_get_mpdata(vnode_mount(vp))->dataflags;
397 if (fuse_isdeadfs(vp)) {
401 if (!(dataflags & FSESS_POSIX_LOCKS))
402 return vop_stdadvlock(ap);
403 /* FUSE doesn't properly support flock until protocol 7.17 */
405 return vop_stdadvlock(ap);
407 err = fuse_filehandle_get_anyflags(vp, &fufh, cred, pid);
411 fdisp_init(&fdi, sizeof(*fli));
427 fdisp_make_vp(&fdi, op, vp, td, cred);
429 fli->fh = fufh->fh_id;
430 fli->owner = fl->l_pid;
431 fli->lk.start = fl->l_start;
433 fli->lk.end = fl->l_start + fl->l_len - 1;
435 fli->lk.end = INT64_MAX;
436 fli->lk.type = fl->l_type;
437 fli->lk.pid = fl->l_pid;
439 err = fdisp_wait_answ(&fdi);
442 if (err == 0 && op == FUSE_GETLK) {
444 fl->l_type = flo->lk.type;
445 fl->l_pid = flo->lk.pid;
446 if (flo->lk.type != F_UNLCK) {
447 fl->l_start = flo->lk.start;
448 if (flo->lk.end == INT64_MAX)
451 fl->l_len = flo->lk.end - flo->lk.start + 1;
452 fl->l_start = flo->lk.start;
462 struct bufobj **a_bop;
468 fuse_vnop_bmap(struct vop_bmap_args *ap)
470 struct vnode *vp = ap->a_vp;
471 struct bufobj **bo = ap->a_bop;
472 struct thread *td = curthread;
474 struct fuse_dispatcher fdi;
475 struct fuse_bmap_in *fbi;
476 struct fuse_bmap_out *fbo;
477 struct fuse_data *data;
480 daddr_t lbn = ap->a_bn;
481 daddr_t *pbn = ap->a_bnp;
482 int *runp = ap->a_runp;
483 int *runb = ap->a_runb;
487 if (fuse_isdeadfs(vp)) {
491 mp = vnode_mount(vp);
492 data = fuse_get_mpdata(mp);
493 biosize = fuse_iosize(vp);
494 maxrun = MIN(vp->v_mount->mnt_iosize_max / biosize - 1,
495 data->max_readahead_blocks);
501 * The FUSE_BMAP operation does not include the runp and runb
502 * variables, so we must guess. Report nonzero contiguous runs so
503 * cluster_read will combine adjacent reads. It's worthwhile to reduce
504 * upcalls even if we don't know the true physical layout of the file.
506 * FUSE file systems may opt out of read clustering in two ways:
507 * * mounting with -onoclusterr
508 * * Setting max_readahead <= maxbcachebuf during FUSE_INIT
511 *runb = MIN(lbn, maxrun);
513 error = fuse_vnode_size(vp, &filesize, td->td_ucred, td);
515 *runp = MIN(MAX(0, filesize / biosize - lbn - 1),
521 if (fsess_isimpl(mp, FUSE_BMAP)) {
522 fdisp_init(&fdi, sizeof(*fbi));
523 fdisp_make_vp(&fdi, FUSE_BMAP, vp, td, td->td_ucred);
526 fbi->blocksize = biosize;
527 error = fdisp_wait_answ(&fdi);
528 if (error == ENOSYS) {
530 fsess_set_notimpl(mp, FUSE_BMAP);
534 if (error == 0 && pbn != NULL)
541 /* If the daemon doesn't support BMAP, make up a sensible default */
543 *pbn = lbn * btodb(biosize);
548 struct vop_close_args {
551 struct ucred *a_cred;
556 fuse_vnop_close(struct vop_close_args *ap)
558 struct vnode *vp = ap->a_vp;
559 struct ucred *cred = ap->a_cred;
560 int fflag = ap->a_fflag;
561 struct thread *td = ap->a_td;
562 pid_t pid = td->td_proc->p_pid;
565 if (fuse_isdeadfs(vp))
569 if (fflag & IO_NDELAY)
572 err = fuse_flush(vp, cred, pid, fflag);
573 /* TODO: close the file handle, if we're sure it's no longer used */
574 if ((VTOFUD(vp)->flag & FN_SIZECHANGE) != 0) {
575 fuse_vnode_savesize(vp, cred, td->td_proc->p_pid);
581 fdisp_make_mknod_for_fallback(
582 struct fuse_dispatcher *fdip,
583 struct componentname *cnp,
589 enum fuse_opcode *op)
591 struct fuse_mknod_in *fmni;
593 fdisp_init(fdip, sizeof(*fmni) + cnp->cn_namelen + 1);
595 fdisp_make(fdip, *op, vnode_mount(dvp), parentnid, td, cred);
599 memcpy((char *)fdip->indata + sizeof(*fmni), cnp->cn_nameptr,
601 ((char *)fdip->indata)[sizeof(*fmni) + cnp->cn_namelen] = '\0';
604 struct vnop_create_args {
606 struct vnode **a_vpp;
607 struct componentname *a_cnp;
612 fuse_vnop_create(struct vop_create_args *ap)
614 struct vnode *dvp = ap->a_dvp;
615 struct vnode **vpp = ap->a_vpp;
616 struct componentname *cnp = ap->a_cnp;
617 struct vattr *vap = ap->a_vap;
618 struct thread *td = cnp->cn_thread;
619 struct ucred *cred = cnp->cn_cred;
621 struct fuse_data *data;
622 struct fuse_create_in *fci;
623 struct fuse_entry_out *feo;
624 struct fuse_open_out *foo;
625 struct fuse_dispatcher fdi, fdi2;
626 struct fuse_dispatcher *fdip = &fdi;
627 struct fuse_dispatcher *fdip2 = NULL;
631 struct mount *mp = vnode_mount(dvp);
632 data = fuse_get_mpdata(mp);
633 uint64_t parentnid = VTOFUD(dvp)->nid;
634 mode_t mode = MAKEIMODE(vap->va_type, vap->va_mode);
638 if (fuse_isdeadfs(dvp))
641 /* FUSE expects sockets to be created with FUSE_MKNOD */
642 if (vap->va_type == VSOCK)
643 return fuse_internal_mknod(dvp, vpp, cnp, vap);
646 * VOP_CREATE doesn't tell us the open(2) flags, so we guess. Only a
647 * writable mode makes sense, and we might as well include readability
652 bzero(&fdi, sizeof(fdi));
654 if (vap->va_type != VREG)
657 if (!fsess_isimpl(mp, FUSE_CREATE) || vap->va_type == VSOCK) {
658 /* Fallback to FUSE_MKNOD/FUSE_OPEN */
659 fdisp_make_mknod_for_fallback(fdip, cnp, dvp, parentnid, td,
662 /* Use FUSE_CREATE */
666 fdisp_init(fdip, sizeof(*fci) + cnp->cn_namelen + 1);
667 fdisp_make(fdip, op, vnode_mount(dvp), parentnid, td, cred);
670 fci->flags = O_CREAT | flags;
671 if (fuse_libabi_geq(data, 7, 12)) {
672 insize = sizeof(*fci);
673 fci->umask = td->td_proc->p_fd->fd_cmask;
675 insize = sizeof(struct fuse_open_in);
678 memcpy((char *)fdip->indata + insize, cnp->cn_nameptr,
680 ((char *)fdip->indata)[insize + cnp->cn_namelen] = '\0';
683 err = fdisp_wait_answ(fdip);
686 if (err == ENOSYS && op == FUSE_CREATE) {
687 fsess_set_notimpl(mp, FUSE_CREATE);
689 fdisp_make_mknod_for_fallback(fdip, cnp, dvp,
690 parentnid, td, cred, mode, &op);
691 err = fdisp_wait_answ(fdip);
699 if ((err = fuse_internal_checkentry(feo, vap->va_type))) {
703 if (op == FUSE_CREATE) {
704 foo = (struct fuse_open_out*)(feo + 1);
706 /* Issue a separate FUSE_OPEN */
707 struct fuse_open_in *foi;
710 fdisp_init(fdip2, sizeof(*foi));
711 fdisp_make(fdip2, FUSE_OPEN, vnode_mount(dvp), feo->nodeid, td,
715 err = fdisp_wait_answ(fdip2);
720 err = fuse_vnode_get(mp, feo, feo->nodeid, dvp, vpp, cnp, vap->va_type);
722 struct fuse_release_in *fri;
723 uint64_t nodeid = feo->nodeid;
724 uint64_t fh_id = foo->fh;
726 fdisp_init(fdip, sizeof(*fri));
727 fdisp_make(fdip, FUSE_RELEASE, mp, nodeid, td, cred);
731 fuse_insert_callback(fdip->tick, fuse_internal_forget_callback);
732 fuse_insert_message(fdip->tick, false);
735 ASSERT_VOP_ELOCKED(*vpp, "fuse_vnop_create");
736 fuse_internal_cache_attrs(*vpp, &feo->attr, feo->attr_valid,
737 feo->attr_valid_nsec, NULL);
739 fuse_filehandle_init(*vpp, FUFH_RDWR, NULL, td, cred, foo);
740 fuse_vnode_open(*vpp, foo->open_flags, td);
742 * Purge the parent's attribute cache because the daemon should've
743 * updated its mtime and ctime
745 fuse_vnode_clear_attr_cache(dvp);
746 cache_purge_negative(dvp);
750 fdisp_destroy(fdip2);
756 struct vnop_fdatasync_args {
757 struct vop_generic_args a_gen;
759 struct thread * a_td;
763 fuse_vnop_fdatasync(struct vop_fdatasync_args *ap)
765 struct vnode *vp = ap->a_vp;
766 struct thread *td = ap->a_td;
767 int waitfor = MNT_WAIT;
771 if (fuse_isdeadfs(vp)) {
774 if ((err = vop_stdfdatasync_buf(ap)))
777 return fuse_internal_fsync(vp, td, waitfor, true);
781 struct vnop_fsync_args {
782 struct vop_generic_args a_gen;
785 struct thread * a_td;
789 fuse_vnop_fsync(struct vop_fsync_args *ap)
791 struct vnode *vp = ap->a_vp;
792 struct thread *td = ap->a_td;
793 int waitfor = ap->a_waitfor;
796 if (fuse_isdeadfs(vp)) {
799 if ((err = vop_stdfsync(ap)))
802 return fuse_internal_fsync(vp, td, waitfor, false);
806 struct vnop_getattr_args {
809 struct ucred *a_cred;
814 fuse_vnop_getattr(struct vop_getattr_args *ap)
816 struct vnode *vp = ap->a_vp;
817 struct vattr *vap = ap->a_vap;
818 struct ucred *cred = ap->a_cred;
819 struct thread *td = curthread;
824 dataflags = fuse_get_mpdata(vnode_mount(vp))->dataflags;
826 /* Note that we are not bailing out on a dead file system just yet. */
828 if (!(dataflags & FSESS_INITED)) {
829 if (!vnode_isvroot(vp)) {
830 fdata_set_dead(fuse_get_mpdata(vnode_mount(vp)));
837 err = fuse_internal_getattr(vp, vap, cred, td);
838 if (err == ENOTCONN && vnode_isvroot(vp)) {
839 /* see comment in fuse_vfsop_statfs() */
846 bzero(vap, sizeof(*vap));
847 vap->va_type = vnode_vtype(vp);
853 struct vnop_inactive_args {
859 fuse_vnop_inactive(struct vop_inactive_args *ap)
861 struct vnode *vp = ap->a_vp;
862 struct thread *td = ap->a_td;
864 struct fuse_vnode_data *fvdat = VTOFUD(vp);
865 struct fuse_filehandle *fufh, *fufh_tmp;
869 LIST_FOREACH_SAFE(fufh, &fvdat->handles, next, fufh_tmp) {
870 if (need_flush && vp->v_type == VREG) {
871 if ((VTOFUD(vp)->flag & FN_SIZECHANGE) != 0) {
872 fuse_vnode_savesize(vp, NULL, 0);
874 if ((fvdat->flag & FN_REVOKED) != 0)
875 fuse_io_invalbuf(vp, td);
877 fuse_io_flushbuf(vp, MNT_WAIT, td);
880 fuse_filehandle_close(vp, fufh, td, NULL);
883 if ((fvdat->flag & FN_REVOKED) != 0 && fuse_reclaim_revoked) {
890 struct vnop_link_args {
891 struct vnode *a_tdvp;
893 struct componentname *a_cnp;
897 fuse_vnop_link(struct vop_link_args *ap)
899 struct vnode *vp = ap->a_vp;
900 struct vnode *tdvp = ap->a_tdvp;
901 struct componentname *cnp = ap->a_cnp;
903 struct vattr *vap = VTOVA(vp);
905 struct fuse_dispatcher fdi;
906 struct fuse_entry_out *feo;
907 struct fuse_link_in fli;
911 if (fuse_isdeadfs(vp)) {
914 if (vnode_mount(tdvp) != vnode_mount(vp)) {
919 * This is a seatbelt check to protect naive userspace filesystems from
920 * themselves and the limitations of the FUSE IPC protocol. If a
921 * filesystem does not allow attribute caching, assume it is capable of
922 * validating that nlink does not overflow.
924 if (vap != NULL && vap->va_nlink >= FUSE_LINK_MAX)
926 fli.oldnodeid = VTOI(vp);
929 fuse_internal_newentry_makerequest(vnode_mount(tdvp), VTOI(tdvp), cnp,
930 FUSE_LINK, &fli, sizeof(fli), &fdi);
931 if ((err = fdisp_wait_answ(&fdi))) {
936 err = fuse_internal_checkentry(feo, vnode_vtype(vp));
939 * Purge the parent's attribute cache because the daemon
940 * should've updated its mtime and ctime
942 fuse_vnode_clear_attr_cache(tdvp);
943 fuse_internal_cache_attrs(vp, &feo->attr, feo->attr_valid,
944 feo->attr_valid_nsec, NULL);
951 struct fuse_lookup_alloc_arg {
952 struct fuse_entry_out *feo;
953 struct componentname *cnp;
958 /* Callback for vn_get_ino */
960 fuse_lookup_alloc(struct mount *mp, void *arg, int lkflags, struct vnode **vpp)
962 struct fuse_lookup_alloc_arg *flaa = arg;
964 return fuse_vnode_get(mp, flaa->feo, flaa->nid, NULL, vpp, flaa->cnp,
968 SDT_PROBE_DEFINE3(fusefs, , vnops, cache_lookup,
969 "int", "struct timespec*", "struct timespec*");
971 struct vnop_lookup_args {
972 struct vnodeop_desc *a_desc;
974 struct vnode **a_vpp;
975 struct componentname *a_cnp;
979 fuse_vnop_lookup(struct vop_lookup_args *ap)
981 struct vnode *dvp = ap->a_dvp;
982 struct vnode **vpp = ap->a_vpp;
983 struct componentname *cnp = ap->a_cnp;
984 struct thread *td = cnp->cn_thread;
985 struct ucred *cred = cnp->cn_cred;
987 int nameiop = cnp->cn_nameiop;
988 int flags = cnp->cn_flags;
989 int wantparent = flags & (LOCKPARENT | WANTPARENT);
990 int islastcn = flags & ISLASTCN;
991 struct mount *mp = vnode_mount(dvp);
995 struct vnode *vp = NULL;
997 struct fuse_dispatcher fdi;
998 bool did_lookup = false;
999 struct fuse_entry_out *feo = NULL;
1000 enum vtype vtyp; /* vnode type of target */
1001 off_t filesize; /* filesize of target */
1005 if (fuse_isdeadfs(dvp)) {
1009 if (!vnode_isdir(dvp))
1012 if (islastcn && vfs_isrdonly(mp) && (nameiop != LOOKUP))
1015 if ((err = fuse_internal_access(dvp, VEXEC, td, cred)))
1018 if (flags & ISDOTDOT) {
1019 KASSERT(VTOFUD(dvp)->flag & FN_PARENT_NID,
1020 ("Looking up .. is TODO"));
1021 nid = VTOFUD(dvp)->parent_nid;
1024 /* .. is obviously a directory */
1027 } else if (cnp->cn_namelen == 1 && *(cnp->cn_nameptr) == '.') {
1029 /* . is obviously a directory */
1033 struct timespec now, timeout;
1035 err = cache_lookup(dvp, vpp, cnp, &timeout, NULL);
1036 getnanouptime(&now);
1037 SDT_PROBE3(fusefs, , vnops, cache_lookup, err, &timeout, &now);
1039 case -1: /* positive match */
1040 if (timespeccmp(&timeout, &now, >)) {
1041 counter_u64_add(fuse_lookup_cache_hits, 1);
1044 counter_u64_add(fuse_lookup_cache_misses, 1);
1046 &VTOFUD(*vpp)->entry_cache_timeout);
1057 case 0: /* no match in cache */
1058 counter_u64_add(fuse_lookup_cache_misses, 1);
1061 case ENOENT: /* negative match */
1062 getnanouptime(&now);
1063 if (timespeccmp(&timeout, &now, <=)) {
1065 cache_purge_negative(dvp);
1074 fdisp_init(&fdi, cnp->cn_namelen + 1);
1075 fdisp_make(&fdi, FUSE_LOOKUP, mp, nid, td, cred);
1077 memcpy(fdi.indata, cnp->cn_nameptr, cnp->cn_namelen);
1078 ((char *)fdi.indata)[cnp->cn_namelen] = '\0';
1079 lookup_err = fdisp_wait_answ(&fdi);
1083 /* lookup call succeeded */
1084 feo = (struct fuse_entry_out *)fdi.answ;
1087 /* zero nodeid means ENOENT and cache it */
1088 struct timespec timeout;
1090 fdi.answ_stat = ENOENT;
1091 lookup_err = ENOENT;
1092 if (cnp->cn_flags & MAKEENTRY) {
1093 fuse_validity_2_timespec(feo, &timeout);
1094 cache_enter_time(dvp, *vpp, cnp,
1097 } else if (nid == FUSE_ROOT_ID) {
1098 lookup_err = EINVAL;
1100 vtyp = IFTOVT(feo->attr.mode);
1101 filesize = feo->attr.size;
1103 if (lookup_err && (!fdi.answ_stat || lookup_err != ENOENT)) {
1104 fdisp_destroy(&fdi);
1108 /* lookup_err, if non-zero, must be ENOENT at this point */
1111 /* Entry not found */
1112 if ((nameiop == CREATE || nameiop == RENAME) && islastcn) {
1113 err = fuse_internal_access(dvp, VWRITE, td, cred);
1116 * Set the SAVENAME flag to hold onto the
1117 * pathname for use later in VOP_CREATE or
1120 cnp->cn_flags |= SAVENAME;
1128 /* Entry was found */
1129 if (flags & ISDOTDOT) {
1130 struct fuse_lookup_alloc_arg flaa;
1136 err = vn_vget_ino_gen(dvp, fuse_lookup_alloc, &flaa, 0,
1139 } else if (nid == VTOI(dvp)) {
1143 struct fuse_vnode_data *fvdat;
1145 err = fuse_vnode_get(vnode_mount(dvp), feo, nid, dvp,
1152 * In the case where we are looking up a FUSE node
1153 * represented by an existing cached vnode, and the
1154 * true size reported by FUSE_LOOKUP doesn't match
1155 * the vnode's cached size, then any cached writes
1156 * beyond the file's current size are lost.
1159 * * following attribute cache expiration, or
1160 * * due a bug in the daemon, or
1163 if (vnode_isreg(vp) &&
1164 filesize != fvdat->cached_attrs.va_size &&
1165 fvdat->flag & FN_SIZECHANGE) {
1167 * The FN_SIZECHANGE flag reflects a dirty
1168 * append. If userspace lets us know our cache
1169 * is invalid, that write was lost. (Dirty
1170 * writes that do not cause append are also
1171 * lost, but we don't detect them here.)
1173 * XXX: Maybe disable WB caching on this mount.
1175 printf("%s: WB cache incoherent on %s!\n",
1177 vnode_mount(vp)->mnt_stat.f_mntonname);
1179 fvdat->flag &= ~FN_SIZECHANGE;
1183 fuse_internal_cache_attrs(*vpp, &feo->attr,
1184 feo->attr_valid, feo->attr_valid_nsec, NULL);
1185 fuse_validity_2_bintime(feo->entry_valid,
1186 feo->entry_valid_nsec,
1187 &fvdat->entry_cache_timeout);
1189 if ((nameiop == DELETE || nameiop == RENAME) &&
1192 struct vattr dvattr;
1194 err = fuse_internal_access(dvp, VWRITE, td,
1199 * if the parent's sticky bit is set, check
1200 * whether we're allowed to remove the file.
1201 * Need to figure out the vnode locking to make
1204 fuse_internal_getattr(dvp, &dvattr, cred, td);
1205 if ((dvattr.va_mode & S_ISTXT) &&
1206 fuse_internal_access(dvp, VADMIN, td,
1208 fuse_internal_access(*vpp, VADMIN, td,
1216 (nameiop == DELETE) ||
1217 (nameiop == RENAME && wantparent))) {
1218 cnp->cn_flags |= SAVENAME;
1225 if (vp != NULL && dvp != vp)
1227 else if (vp != NULL)
1232 fdisp_destroy(&fdi);
1238 struct vnop_mkdir_args {
1239 struct vnode *a_dvp;
1240 struct vnode **a_vpp;
1241 struct componentname *a_cnp;
1242 struct vattr *a_vap;
1246 fuse_vnop_mkdir(struct vop_mkdir_args *ap)
1248 struct vnode *dvp = ap->a_dvp;
1249 struct vnode **vpp = ap->a_vpp;
1250 struct componentname *cnp = ap->a_cnp;
1251 struct vattr *vap = ap->a_vap;
1253 struct fuse_mkdir_in fmdi;
1255 if (fuse_isdeadfs(dvp)) {
1258 fmdi.mode = MAKEIMODE(vap->va_type, vap->va_mode);
1259 fmdi.umask = curthread->td_proc->p_fd->fd_cmask;
1261 return (fuse_internal_newentry(dvp, vpp, cnp, FUSE_MKDIR, &fmdi,
1262 sizeof(fmdi), VDIR));
1266 struct vnop_mknod_args {
1267 struct vnode *a_dvp;
1268 struct vnode **a_vpp;
1269 struct componentname *a_cnp;
1270 struct vattr *a_vap;
1274 fuse_vnop_mknod(struct vop_mknod_args *ap)
1277 struct vnode *dvp = ap->a_dvp;
1278 struct vnode **vpp = ap->a_vpp;
1279 struct componentname *cnp = ap->a_cnp;
1280 struct vattr *vap = ap->a_vap;
1282 if (fuse_isdeadfs(dvp))
1285 return fuse_internal_mknod(dvp, vpp, cnp, vap);
1289 struct vop_open_args {
1292 struct ucred *a_cred;
1293 struct thread *a_td;
1294 int a_fdidx; / struct file *a_fp;
1298 fuse_vnop_open(struct vop_open_args *ap)
1300 struct vnode *vp = ap->a_vp;
1301 int a_mode = ap->a_mode;
1302 struct thread *td = ap->a_td;
1303 struct ucred *cred = ap->a_cred;
1304 pid_t pid = td->td_proc->p_pid;
1305 struct fuse_vnode_data *fvdat;
1307 if (fuse_isdeadfs(vp))
1309 if (vp->v_type == VCHR || vp->v_type == VBLK || vp->v_type == VFIFO)
1310 return (EOPNOTSUPP);
1311 if ((a_mode & (FREAD | FWRITE | FEXEC)) == 0)
1316 if (fuse_filehandle_validrw(vp, a_mode, cred, pid)) {
1317 fuse_vnode_open(vp, 0, td);
1321 return fuse_filehandle_open(vp, a_mode, NULL, td, cred);
1325 fuse_vnop_pathconf(struct vop_pathconf_args *ap)
1328 switch (ap->a_name) {
1329 case _PC_FILESIZEBITS:
1333 *ap->a_retval = NAME_MAX;
1336 *ap->a_retval = MIN(LONG_MAX, FUSE_LINK_MAX);
1338 case _PC_SYMLINK_MAX:
1339 *ap->a_retval = MAXPATHLEN;
1345 return (vop_stdpathconf(ap));
1350 struct vnop_read_args {
1354 struct ucred *a_cred;
1358 fuse_vnop_read(struct vop_read_args *ap)
1360 struct vnode *vp = ap->a_vp;
1361 struct uio *uio = ap->a_uio;
1362 int ioflag = ap->a_ioflag;
1363 struct ucred *cred = ap->a_cred;
1364 pid_t pid = curthread->td_proc->p_pid;
1366 if (fuse_isdeadfs(vp)) {
1370 if (VTOFUD(vp)->flag & FN_DIRECTIO) {
1371 ioflag |= IO_DIRECT;
1374 return fuse_io_dispatch(vp, uio, ioflag, cred, pid);
1378 struct vnop_readdir_args {
1381 struct ucred *a_cred;
1388 fuse_vnop_readdir(struct vop_readdir_args *ap)
1390 struct vnode *vp = ap->a_vp;
1391 struct uio *uio = ap->a_uio;
1392 struct ucred *cred = ap->a_cred;
1393 struct fuse_filehandle *fufh = NULL;
1394 struct fuse_iov cookediov;
1400 bool closefufh = false;
1401 pid_t pid = curthread->td_proc->p_pid;
1405 if (fuse_isdeadfs(vp)) {
1408 if ( /* XXXIP ((uio_iovcnt(uio) > 1)) || */
1409 (uio_resid(uio) < sizeof(struct dirent))) {
1413 tresid = uio->uio_resid;
1414 startoff = uio->uio_offset;
1415 err = fuse_filehandle_get_dir(vp, &fufh, cred, pid);
1416 if (err == EBADF && vnode_mount(vp)->mnt_flag & MNT_EXPORTED) {
1418 * nfsd will do VOP_READDIR without first doing VOP_OPEN. We
1419 * must implicitly open the directory here
1421 err = fuse_filehandle_open(vp, FREAD, &fufh, curthread, cred);
1424 * When a directory is opened, it must be read from
1425 * the beginning. Hopefully, the "startoff" still
1426 * exists as an offset cookie for the directory.
1427 * If not, it will read the entire directory without
1428 * returning any entries and just return eof.
1430 uio->uio_offset = 0;
1436 if (ap->a_ncookies != NULL) {
1437 ncookies = uio->uio_resid /
1438 (offsetof(struct dirent, d_name) + 4) + 1;
1439 cookies = malloc(ncookies * sizeof(*cookies), M_TEMP, M_WAITOK);
1440 *ap->a_ncookies = ncookies;
1441 *ap->a_cookies = cookies;
1446 #define DIRCOOKEDSIZE FUSE_DIRENT_ALIGN(FUSE_NAME_OFFSET + MAXNAMLEN + 1)
1447 fiov_init(&cookediov, DIRCOOKEDSIZE);
1449 err = fuse_internal_readdir(vp, uio, startoff, fufh, &cookediov,
1450 &ncookies, cookies);
1452 fiov_teardown(&cookediov);
1454 fuse_filehandle_close(vp, fufh, curthread, cred);
1456 if (ap->a_ncookies != NULL) {
1458 *ap->a_ncookies -= ncookies;
1460 free(*ap->a_cookies, M_TEMP);
1461 *ap->a_ncookies = 0;
1462 *ap->a_cookies = NULL;
1465 if (err == 0 && tresid == uio->uio_resid)
1472 struct vnop_readlink_args {
1475 struct ucred *a_cred;
1479 fuse_vnop_readlink(struct vop_readlink_args *ap)
1481 struct vnode *vp = ap->a_vp;
1482 struct uio *uio = ap->a_uio;
1483 struct ucred *cred = ap->a_cred;
1485 struct fuse_dispatcher fdi;
1488 if (fuse_isdeadfs(vp)) {
1491 if (!vnode_islnk(vp)) {
1494 fdisp_init(&fdi, 0);
1495 err = fdisp_simple_putget_vp(&fdi, FUSE_READLINK, vp, curthread, cred);
1499 if (((char *)fdi.answ)[0] == '/' &&
1500 fuse_get_mpdata(vnode_mount(vp))->dataflags & FSESS_PUSH_SYMLINKS_IN) {
1501 char *mpth = vnode_mount(vp)->mnt_stat.f_mntonname;
1503 err = uiomove(mpth, strlen(mpth), uio);
1506 err = uiomove(fdi.answ, fdi.iosize, uio);
1509 fdisp_destroy(&fdi);
1514 struct vnop_reclaim_args {
1516 struct thread *a_td;
1520 fuse_vnop_reclaim(struct vop_reclaim_args *ap)
1522 struct vnode *vp = ap->a_vp;
1523 struct thread *td = ap->a_td;
1524 struct fuse_vnode_data *fvdat = VTOFUD(vp);
1525 struct fuse_filehandle *fufh, *fufh_tmp;
1528 panic("FUSE: no vnode data during recycling");
1530 LIST_FOREACH_SAFE(fufh, &fvdat->handles, next, fufh_tmp) {
1531 printf("FUSE: vnode being reclaimed with open fufh "
1532 "(type=%#x)", fufh->fufh_type);
1533 fuse_filehandle_close(vp, fufh, td, NULL);
1536 if ((!fuse_isdeadfs(vp)) && (fvdat->nlookup)) {
1537 fuse_internal_forget_send(vnode_mount(vp), td, NULL, VTOI(vp),
1540 fuse_vnode_setparent(vp, NULL);
1542 vfs_hash_remove(vp);
1543 vnode_destroy_vobject(vp);
1544 fuse_vnode_destroy(vp);
1550 struct vnop_remove_args {
1551 struct vnode *a_dvp;
1553 struct componentname *a_cnp;
1557 fuse_vnop_remove(struct vop_remove_args *ap)
1559 struct vnode *dvp = ap->a_dvp;
1560 struct vnode *vp = ap->a_vp;
1561 struct componentname *cnp = ap->a_cnp;
1565 if (fuse_isdeadfs(vp)) {
1568 if (vnode_isdir(vp)) {
1573 err = fuse_internal_remove(dvp, vp, cnp, FUSE_UNLINK);
1576 fuse_internal_vnode_disappear(vp);
1578 * Purge the parent's attribute cache because the daemon
1579 * should've updated its mtime and ctime
1581 fuse_vnode_clear_attr_cache(dvp);
1587 struct vnop_rename_args {
1588 struct vnode *a_fdvp;
1589 struct vnode *a_fvp;
1590 struct componentname *a_fcnp;
1591 struct vnode *a_tdvp;
1592 struct vnode *a_tvp;
1593 struct componentname *a_tcnp;
1597 fuse_vnop_rename(struct vop_rename_args *ap)
1599 struct vnode *fdvp = ap->a_fdvp;
1600 struct vnode *fvp = ap->a_fvp;
1601 struct componentname *fcnp = ap->a_fcnp;
1602 struct vnode *tdvp = ap->a_tdvp;
1603 struct vnode *tvp = ap->a_tvp;
1604 struct componentname *tcnp = ap->a_tcnp;
1605 struct fuse_data *data;
1606 bool newparent = fdvp != tdvp;
1607 bool isdir = fvp->v_type == VDIR;
1610 if (fuse_isdeadfs(fdvp)) {
1613 if (fvp->v_mount != tdvp->v_mount ||
1614 (tvp && fvp->v_mount != tvp->v_mount)) {
1615 SDT_PROBE2(fusefs, , vnops, trace, 1, "cross-device rename");
1622 * FUSE library is expected to check if target directory is not
1623 * under the source directory in the file system tree.
1624 * Linux performs this check at VFS level.
1627 * If source is a directory, and it will get a new parent, user must
1628 * have write permission to it, so ".." can be modified.
1630 data = fuse_get_mpdata(vnode_mount(tdvp));
1631 if (data->dataflags & FSESS_DEFAULT_PERMISSIONS && isdir && newparent) {
1632 err = fuse_internal_access(fvp, VWRITE,
1633 tcnp->cn_thread, tcnp->cn_cred);
1637 sx_xlock(&data->rename_lock);
1638 err = fuse_internal_rename(fdvp, fcnp, tdvp, tcnp);
1641 fuse_vnode_setparent(fvp, tdvp);
1643 fuse_vnode_setparent(tvp, NULL);
1645 sx_unlock(&data->rename_lock);
1647 if (tvp != NULL && tvp != fvp) {
1650 if (vnode_isdir(fvp)) {
1651 if ((tvp != NULL) && vnode_isdir(tvp)) {
1672 struct vnop_rmdir_args {
1673 struct vnode *a_dvp;
1675 struct componentname *a_cnp;
1679 fuse_vnop_rmdir(struct vop_rmdir_args *ap)
1681 struct vnode *dvp = ap->a_dvp;
1682 struct vnode *vp = ap->a_vp;
1686 if (fuse_isdeadfs(vp)) {
1689 if (VTOFUD(vp) == VTOFUD(dvp)) {
1692 err = fuse_internal_remove(dvp, vp, ap->a_cnp, FUSE_RMDIR);
1695 fuse_internal_vnode_disappear(vp);
1697 * Purge the parent's attribute cache because the daemon
1698 * should've updated its mtime and ctime
1700 fuse_vnode_clear_attr_cache(dvp);
1706 struct vnop_setattr_args {
1708 struct vattr *a_vap;
1709 struct ucred *a_cred;
1710 struct thread *a_td;
1714 fuse_vnop_setattr(struct vop_setattr_args *ap)
1716 struct vnode *vp = ap->a_vp;
1717 struct vattr *vap = ap->a_vap;
1718 struct ucred *cred = ap->a_cred;
1719 struct thread *td = curthread;
1721 struct fuse_data *data;
1722 struct vattr old_va;
1725 accmode_t accmode = 0;
1727 bool drop_suid = false;
1730 mp = vnode_mount(vp);
1731 data = fuse_get_mpdata(mp);
1732 dataflags = data->dataflags;
1733 checkperm = dataflags & FSESS_DEFAULT_PERMISSIONS;
1734 if (cred->cr_ngroups > 0)
1735 cr_gid = cred->cr_groups[0];
1739 if (fuse_isdeadfs(vp)) {
1743 if (vap->va_uid != (uid_t)VNOVAL) {
1745 /* Only root may change a file's owner */
1746 err = priv_check_cred(cred, PRIV_VFS_CHOWN);
1748 /* As a special case, allow the null chown */
1749 err2 = fuse_internal_getattr(vp, &old_va, cred,
1753 if (vap->va_uid != old_va.va_uid)
1763 if (vap->va_gid != (gid_t)VNOVAL) {
1764 if (checkperm && priv_check_cred(cred, PRIV_VFS_CHOWN))
1766 if (checkperm && !groupmember(vap->va_gid, cred))
1769 * Non-root users may only chgrp to one of their own
1772 err = priv_check_cred(cred, PRIV_VFS_CHOWN);
1774 /* As a special case, allow the null chgrp */
1775 err2 = fuse_internal_getattr(vp, &old_va, cred,
1779 if (vap->va_gid != old_va.va_gid)
1787 if (vap->va_size != VNOVAL) {
1788 switch (vp->v_type) {
1793 if (vfs_isrdonly(mp))
1798 * According to POSIX, the result is unspecified
1799 * for file types other than regular files,
1800 * directories and shared memory objects. We
1801 * don't support shared memory objects in the file
1802 * system, and have dubious support for truncating
1803 * symlinks. Just ignore the request in other cases.
1807 /* Don't set accmode. Permission to trunc is checked upstack */
1809 if (vap->va_atime.tv_sec != VNOVAL || vap->va_mtime.tv_sec != VNOVAL) {
1810 if (vap->va_vaflags & VA_UTIMES_NULL)
1816 if (vap->va_mode != (mode_t)VNOVAL)
1817 vap->va_mode &= ~(S_ISUID | S_ISGID);
1819 err = fuse_internal_getattr(vp, &old_va, cred, td);
1822 vap->va_mode = old_va.va_mode & ~(S_ISUID | S_ISGID);
1825 if (vap->va_mode != (mode_t)VNOVAL) {
1826 /* Only root may set the sticky bit on non-directories */
1827 if (checkperm && vp->v_type != VDIR && (vap->va_mode & S_ISTXT)
1828 && priv_check_cred(cred, PRIV_VFS_STICKYFILE))
1830 if (checkperm && (vap->va_mode & S_ISGID)) {
1831 err = fuse_internal_getattr(vp, &old_va, cred, td);
1834 if (!groupmember(old_va.va_gid, cred)) {
1835 err = priv_check_cred(cred, PRIV_VFS_SETGID);
1843 if (vfs_isrdonly(mp))
1846 err = fuse_internal_access(vp, accmode, td, cred);
1850 return fuse_internal_setattr(vp, vap, td, cred);
1854 struct vnop_strategy_args {
1860 fuse_vnop_strategy(struct vop_strategy_args *ap)
1862 struct vnode *vp = ap->a_vp;
1863 struct buf *bp = ap->a_bp;
1865 if (!vp || fuse_isdeadfs(vp)) {
1866 bp->b_ioflags |= BIO_ERROR;
1867 bp->b_error = ENXIO;
1873 * VOP_STRATEGY always returns zero and signals error via bp->b_ioflags.
1874 * fuse_io_strategy sets bp's error fields
1876 (void)fuse_io_strategy(vp, bp);
1883 struct vnop_symlink_args {
1884 struct vnode *a_dvp;
1885 struct vnode **a_vpp;
1886 struct componentname *a_cnp;
1887 struct vattr *a_vap;
1892 fuse_vnop_symlink(struct vop_symlink_args *ap)
1894 struct vnode *dvp = ap->a_dvp;
1895 struct vnode **vpp = ap->a_vpp;
1896 struct componentname *cnp = ap->a_cnp;
1897 const char *target = ap->a_target;
1899 struct fuse_dispatcher fdi;
1904 if (fuse_isdeadfs(dvp)) {
1908 * Unlike the other creator type calls, here we have to create a message
1909 * where the name of the new entry comes first, and the data describing
1910 * the entry comes second.
1911 * Hence we can't rely on our handy fuse_internal_newentry() routine,
1912 * but put together the message manually and just call the core part.
1915 len = strlen(target) + 1;
1916 fdisp_init(&fdi, len + cnp->cn_namelen + 1);
1917 fdisp_make_vp(&fdi, FUSE_SYMLINK, dvp, curthread, NULL);
1919 memcpy(fdi.indata, cnp->cn_nameptr, cnp->cn_namelen);
1920 ((char *)fdi.indata)[cnp->cn_namelen] = '\0';
1921 memcpy((char *)fdi.indata + cnp->cn_namelen + 1, target, len);
1923 err = fuse_internal_newentry_core(dvp, vpp, cnp, VLNK, &fdi);
1924 fdisp_destroy(&fdi);
1929 struct vnop_write_args {
1933 struct ucred *a_cred;
1937 fuse_vnop_write(struct vop_write_args *ap)
1939 struct vnode *vp = ap->a_vp;
1940 struct uio *uio = ap->a_uio;
1941 int ioflag = ap->a_ioflag;
1942 struct ucred *cred = ap->a_cred;
1943 pid_t pid = curthread->td_proc->p_pid;
1945 if (fuse_isdeadfs(vp)) {
1949 if (VTOFUD(vp)->flag & FN_DIRECTIO) {
1950 ioflag |= IO_DIRECT;
1953 return fuse_io_dispatch(vp, uio, ioflag, cred, pid);
1957 fuse_gbp_getblkno(struct vnode *vp, vm_ooffset_t off)
1959 const int biosize = fuse_iosize(vp);
1961 return (off / biosize);
1965 fuse_gbp_getblksz(struct vnode *vp, daddr_t lbn)
1969 const int biosize = fuse_iosize(vp);
1971 err = fuse_vnode_size(vp, &filesize, NULL, NULL);
1972 KASSERT(err == 0, ("vfs_bio_getpages can't handle errors here"));
1976 if ((off_t)lbn * biosize >= filesize) {
1978 } else if ((off_t)(lbn + 1) * biosize > filesize) {
1979 blksz = filesize - (off_t)lbn *biosize;
1987 struct vnop_getpages_args {
1995 fuse_vnop_getpages(struct vop_getpages_args *ap)
1997 struct vnode *vp = ap->a_vp;
1999 if (!fsess_opt_mmap(vnode_mount(vp))) {
2000 SDT_PROBE2(fusefs, , vnops, trace, 1,
2001 "called on non-cacheable vnode??\n");
2002 return (VM_PAGER_ERROR);
2005 return (vfs_bio_getpages(vp, ap->a_m, ap->a_count, ap->a_rbehind,
2006 ap->a_rahead, fuse_gbp_getblkno, fuse_gbp_getblksz));
2009 static const char extattr_namespace_separator = '.';
2012 struct vop_getextattr_args {
2013 struct vop_generic_args a_gen;
2015 int a_attrnamespace;
2019 struct ucred *a_cred;
2020 struct thread *a_td;
2024 fuse_vnop_getextattr(struct vop_getextattr_args *ap)
2026 struct vnode *vp = ap->a_vp;
2027 struct uio *uio = ap->a_uio;
2028 struct fuse_dispatcher fdi;
2029 struct fuse_getxattr_in *get_xattr_in;
2030 struct fuse_getxattr_out *get_xattr_out;
2031 struct mount *mp = vnode_mount(vp);
2032 struct thread *td = ap->a_td;
2033 struct ucred *cred = ap->a_cred;
2039 if (fuse_isdeadfs(vp))
2042 if (!fsess_isimpl(mp, FUSE_GETXATTR))
2045 err = fuse_extattr_check_cred(vp, ap->a_attrnamespace, cred, td, VREAD);
2049 /* Default to looking for user attributes. */
2050 if (ap->a_attrnamespace == EXTATTR_NAMESPACE_SYSTEM)
2051 prefix = EXTATTR_NAMESPACE_SYSTEM_STRING;
2053 prefix = EXTATTR_NAMESPACE_USER_STRING;
2055 len = strlen(prefix) + sizeof(extattr_namespace_separator) +
2056 strlen(ap->a_name) + 1;
2058 fdisp_init(&fdi, len + sizeof(*get_xattr_in));
2059 fdisp_make_vp(&fdi, FUSE_GETXATTR, vp, td, cred);
2061 get_xattr_in = fdi.indata;
2063 * Check to see whether we're querying the available size or
2064 * issuing the actual request. If we pass in 0, we get back struct
2065 * fuse_getxattr_out. If we pass in a non-zero size, we get back
2066 * that much data, without the struct fuse_getxattr_out header.
2069 get_xattr_in->size = 0;
2071 get_xattr_in->size = uio->uio_resid;
2073 attr_str = (char *)fdi.indata + sizeof(*get_xattr_in);
2074 snprintf(attr_str, len, "%s%c%s", prefix, extattr_namespace_separator,
2077 err = fdisp_wait_answ(&fdi);
2079 if (err == ENOSYS) {
2080 fsess_set_notimpl(mp, FUSE_GETXATTR);
2086 get_xattr_out = fdi.answ;
2088 if (ap->a_size != NULL)
2089 *ap->a_size = get_xattr_out->size;
2092 err = uiomove(fdi.answ, fdi.iosize, uio);
2095 fdisp_destroy(&fdi);
2100 struct vop_setextattr_args {
2101 struct vop_generic_args a_gen;
2103 int a_attrnamespace;
2106 struct ucred *a_cred;
2107 struct thread *a_td;
2111 fuse_vnop_setextattr(struct vop_setextattr_args *ap)
2113 struct vnode *vp = ap->a_vp;
2114 struct uio *uio = ap->a_uio;
2115 struct fuse_dispatcher fdi;
2116 struct fuse_setxattr_in *set_xattr_in;
2117 struct mount *mp = vnode_mount(vp);
2118 struct thread *td = ap->a_td;
2119 struct ucred *cred = ap->a_cred;
2125 if (fuse_isdeadfs(vp))
2128 if (!fsess_isimpl(mp, FUSE_SETXATTR))
2131 if (vfs_isrdonly(mp))
2134 /* Deleting xattrs must use VOP_DELETEEXTATTR instead */
2135 if (ap->a_uio == NULL) {
2137 * If we got here as fallback from VOP_DELETEEXTATTR, then
2138 * return EOPNOTSUPP.
2140 if (!fsess_isimpl(mp, FUSE_REMOVEXATTR))
2141 return (EOPNOTSUPP);
2146 err = fuse_extattr_check_cred(vp, ap->a_attrnamespace, cred, td,
2151 /* Default to looking for user attributes. */
2152 if (ap->a_attrnamespace == EXTATTR_NAMESPACE_SYSTEM)
2153 prefix = EXTATTR_NAMESPACE_SYSTEM_STRING;
2155 prefix = EXTATTR_NAMESPACE_USER_STRING;
2157 len = strlen(prefix) + sizeof(extattr_namespace_separator) +
2158 strlen(ap->a_name) + 1;
2160 fdisp_init(&fdi, len + sizeof(*set_xattr_in) + uio->uio_resid);
2161 fdisp_make_vp(&fdi, FUSE_SETXATTR, vp, td, cred);
2163 set_xattr_in = fdi.indata;
2164 set_xattr_in->size = uio->uio_resid;
2166 attr_str = (char *)fdi.indata + sizeof(*set_xattr_in);
2167 snprintf(attr_str, len, "%s%c%s", prefix, extattr_namespace_separator,
2170 err = uiomove((char *)fdi.indata + sizeof(*set_xattr_in) + len,
2171 uio->uio_resid, uio);
2176 err = fdisp_wait_answ(&fdi);
2178 if (err == ENOSYS) {
2179 fsess_set_notimpl(mp, FUSE_SETXATTR);
2182 if (err == ERESTART) {
2183 /* Can't restart after calling uiomove */
2188 fdisp_destroy(&fdi);
2193 * The Linux / FUSE extended attribute list is simply a collection of
2194 * NUL-terminated strings. The FreeBSD extended attribute list is a single
2195 * byte length followed by a non-NUL terminated string. So, this allows
2196 * conversion of the Linux / FUSE format to the FreeBSD format in place.
2197 * Linux attribute names are reported with the namespace as a prefix (e.g.
2198 * "user.attribute_name"), but in FreeBSD they are reported without the
2199 * namespace prefix (e.g. "attribute_name"). So, we're going from:
2201 * user.attr_name1\0user.attr_name2\0
2205 * <num>attr_name1<num>attr_name2
2207 * Where "<num>" is a single byte number of characters in the attribute name.
2210 * prefix - exattr namespace prefix string
2211 * list, list_len - input list with namespace prefixes
2212 * bsd_list, bsd_list_len - output list compatible with bsd vfs
2215 fuse_xattrlist_convert(char *prefix, const char *list, int list_len,
2216 char *bsd_list, int *bsd_list_len)
2218 int len, pos, dist_to_next, prefix_len;
2222 prefix_len = strlen(prefix);
2224 while (pos < list_len && list[pos] != '\0') {
2225 dist_to_next = strlen(&list[pos]) + 1;
2226 if (bcmp(&list[pos], prefix, prefix_len) == 0 &&
2227 list[pos + prefix_len] == extattr_namespace_separator) {
2228 len = dist_to_next -
2229 (prefix_len + sizeof(extattr_namespace_separator)) - 1;
2230 if (len >= EXTATTR_MAXNAMELEN)
2231 return (ENAMETOOLONG);
2233 bsd_list[*bsd_list_len] = len;
2234 memcpy(&bsd_list[*bsd_list_len + 1],
2235 &list[pos + prefix_len +
2236 sizeof(extattr_namespace_separator)], len);
2238 *bsd_list_len += len + 1;
2241 pos += dist_to_next;
2248 struct vop_listextattr_args {
2249 struct vop_generic_args a_gen;
2251 int a_attrnamespace;
2254 struct ucred *a_cred;
2255 struct thread *a_td;
2259 fuse_vnop_listextattr(struct vop_listextattr_args *ap)
2261 struct vnode *vp = ap->a_vp;
2262 struct uio *uio = ap->a_uio;
2263 struct fuse_dispatcher fdi;
2264 struct fuse_listxattr_in *list_xattr_in;
2265 struct fuse_listxattr_out *list_xattr_out;
2266 struct mount *mp = vnode_mount(vp);
2267 struct thread *td = ap->a_td;
2268 struct ucred *cred = ap->a_cred;
2272 char *bsd_list = NULL;
2278 if (fuse_isdeadfs(vp))
2281 if (!fsess_isimpl(mp, FUSE_LISTXATTR))
2284 err = fuse_extattr_check_cred(vp, ap->a_attrnamespace, cred, td, VREAD);
2289 * Add space for a NUL and the period separator if enabled.
2290 * Default to looking for user attributes.
2292 if (ap->a_attrnamespace == EXTATTR_NAMESPACE_SYSTEM)
2293 prefix = EXTATTR_NAMESPACE_SYSTEM_STRING;
2295 prefix = EXTATTR_NAMESPACE_USER_STRING;
2297 len = strlen(prefix) + sizeof(extattr_namespace_separator) + 1;
2299 fdisp_init(&fdi, sizeof(*list_xattr_in) + len);
2300 fdisp_make_vp(&fdi, FUSE_LISTXATTR, vp, td, cred);
2303 * Retrieve Linux / FUSE compatible list size.
2305 list_xattr_in = fdi.indata;
2306 list_xattr_in->size = 0;
2307 attr_str = (char *)fdi.indata + sizeof(*list_xattr_in);
2308 snprintf(attr_str, len, "%s%c", prefix, extattr_namespace_separator);
2310 err = fdisp_wait_answ(&fdi);
2312 if (err == ENOSYS) {
2313 fsess_set_notimpl(mp, FUSE_LISTXATTR);
2319 list_xattr_out = fdi.answ;
2320 linux_list_len = list_xattr_out->size;
2321 if (linux_list_len == 0) {
2322 if (ap->a_size != NULL)
2323 *ap->a_size = linux_list_len;
2328 * Retrieve Linux / FUSE compatible list values.
2330 fdisp_refresh_vp(&fdi, FUSE_LISTXATTR, vp, td, cred);
2331 list_xattr_in = fdi.indata;
2332 list_xattr_in->size = linux_list_len + sizeof(*list_xattr_out);
2333 attr_str = (char *)fdi.indata + sizeof(*list_xattr_in);
2334 snprintf(attr_str, len, "%s%c", prefix, extattr_namespace_separator);
2336 err = fdisp_wait_answ(&fdi);
2340 linux_list = fdi.answ;
2341 linux_list_len = fdi.iosize;
2344 * Retrieve the BSD compatible list values.
2345 * The Linux / FUSE attribute list format isn't the same
2346 * as FreeBSD's format. So we need to transform it into
2347 * FreeBSD's format before giving it to the user.
2349 bsd_list = malloc(linux_list_len, M_TEMP, M_WAITOK);
2350 err = fuse_xattrlist_convert(prefix, linux_list, linux_list_len,
2351 bsd_list, &bsd_list_len);
2355 if (ap->a_size != NULL)
2356 *ap->a_size = bsd_list_len;
2359 err = uiomove(bsd_list, bsd_list_len, uio);
2362 free(bsd_list, M_TEMP);
2363 fdisp_destroy(&fdi);
2368 struct vop_deleteextattr_args {
2369 struct vop_generic_args a_gen;
2371 int a_attrnamespace;
2373 struct ucred *a_cred;
2374 struct thread *a_td;
2378 fuse_vnop_deleteextattr(struct vop_deleteextattr_args *ap)
2380 struct vnode *vp = ap->a_vp;
2381 struct fuse_dispatcher fdi;
2382 struct mount *mp = vnode_mount(vp);
2383 struct thread *td = ap->a_td;
2384 struct ucred *cred = ap->a_cred;
2390 if (fuse_isdeadfs(vp))
2393 if (!fsess_isimpl(mp, FUSE_REMOVEXATTR))
2396 if (vfs_isrdonly(mp))
2399 err = fuse_extattr_check_cred(vp, ap->a_attrnamespace, cred, td,
2404 /* Default to looking for user attributes. */
2405 if (ap->a_attrnamespace == EXTATTR_NAMESPACE_SYSTEM)
2406 prefix = EXTATTR_NAMESPACE_SYSTEM_STRING;
2408 prefix = EXTATTR_NAMESPACE_USER_STRING;
2410 len = strlen(prefix) + sizeof(extattr_namespace_separator) +
2411 strlen(ap->a_name) + 1;
2413 fdisp_init(&fdi, len);
2414 fdisp_make_vp(&fdi, FUSE_REMOVEXATTR, vp, td, cred);
2416 attr_str = fdi.indata;
2417 snprintf(attr_str, len, "%s%c%s", prefix, extattr_namespace_separator,
2420 err = fdisp_wait_answ(&fdi);
2421 if (err == ENOSYS) {
2422 fsess_set_notimpl(mp, FUSE_REMOVEXATTR);
2426 fdisp_destroy(&fdi);
2431 struct vnop_print_args {
2436 fuse_vnop_print(struct vop_print_args *ap)
2438 struct fuse_vnode_data *fvdat = VTOFUD(ap->a_vp);
2440 printf("nodeid: %ju, parent nodeid: %ju, nlookup: %ju, flag: %#x\n",
2441 (uintmax_t)VTOILLU(ap->a_vp), (uintmax_t)fvdat->parent_nid,
2442 (uintmax_t)fvdat->nlookup,
2449 * Get an NFS filehandle for a FUSE file.
2451 * This will only work for FUSE file systems that guarantee the uniqueness of
2452 * nodeid:generation, which most don't.
2456 IN struct vnode *a_vp;
2457 IN struct fid *a_fhp;
2461 fuse_vnop_vptofh(struct vop_vptofh_args *ap)
2463 struct vnode *vp = ap->a_vp;
2464 struct fuse_vnode_data *fvdat = VTOFUD(vp);
2465 struct fuse_fid *fhp = (struct fuse_fid *)(ap->a_fhp);
2466 _Static_assert(sizeof(struct fuse_fid) <= sizeof(struct fid),
2467 "FUSE fid type is too big");
2468 struct mount *mp = vnode_mount(vp);
2469 struct fuse_data *data = fuse_get_mpdata(mp);
2473 if (!(data->dataflags & FSESS_EXPORT_SUPPORT))
2476 err = fuse_internal_getattr(vp, &va, curthread->td_ucred, curthread);
2480 /*ip = VTOI(ap->a_vp);*/
2481 /*ufhp = (struct ufid *)ap->a_fhp;*/
2482 fhp->len = sizeof(struct fuse_fid);
2483 fhp->nid = fvdat->nid;
2484 if (fvdat->generation <= UINT32_MAX)
2485 fhp->gen = fvdat->generation;