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 * Copyright (c) 2019 The FreeBSD Foundation
38 * Portions of this software were developed by BFF Storage Systems, LLC under
39 * sponsorship from the FreeBSD Foundation.
41 * Redistribution and use in source and binary forms, with or without
42 * modification, are permitted provided that the following conditions
44 * 1. Redistributions of source code must retain the above copyright
45 * notice, this list of conditions and the following disclaimer.
46 * 2. Redistributions in binary form must reproduce the above copyright
47 * notice, this list of conditions and the following disclaimer in the
48 * documentation and/or other materials provided with the distribution.
50 * THIS SOFTWARE IS PROVIDED BY AUTHOR AND CONTRIBUTORS ``AS IS'' AND
51 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
52 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
53 * ARE DISCLAIMED. IN NO EVENT SHALL AUTHOR OR CONTRIBUTORS BE LIABLE
54 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
55 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
56 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
57 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
58 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
59 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
63 #include <sys/cdefs.h>
64 __FBSDID("$FreeBSD$");
66 #include <sys/param.h>
67 #include <sys/module.h>
68 #include <sys/systm.h>
69 #include <sys/errno.h>
70 #include <sys/kernel.h>
72 #include <sys/filio.h>
74 #include <sys/malloc.h>
75 #include <sys/queue.h>
76 #include <sys/limits.h>
78 #include <sys/rwlock.h>
81 #include <sys/mount.h>
82 #include <sys/vnode.h>
83 #include <sys/namei.h>
84 #include <sys/extattr.h>
86 #include <sys/unistd.h>
87 #include <sys/filedesc.h>
89 #include <sys/fcntl.h>
90 #include <sys/dirent.h>
93 #include <sys/sysctl.h>
94 #include <sys/vmmeter.h>
97 #include <vm/vm_extern.h>
99 #include <vm/vm_map.h>
100 #include <vm/vm_page.h>
101 #include <vm/vm_param.h>
102 #include <vm/vm_object.h>
103 #include <vm/vm_pager.h>
104 #include <vm/vnode_pager.h>
105 #include <vm/vm_object.h>
108 #include "fuse_file.h"
109 #include "fuse_internal.h"
110 #include "fuse_ipc.h"
111 #include "fuse_node.h"
114 #include <sys/priv.h>
116 /* Maximum number of hardlinks to a single FUSE file */
117 #define FUSE_LINK_MAX UINT32_MAX
119 SDT_PROVIDER_DECLARE(fusefs);
122 * arg0: verbosity. Higher numbers give more verbose messages
123 * arg1: Textual message
125 SDT_PROBE_DEFINE2(fusefs, , vnops, trace, "int", "char*");
128 static vop_access_t fuse_vnop_access;
129 static vop_advlock_t fuse_vnop_advlock;
130 static vop_allocate_t fuse_vnop_allocate;
131 static vop_bmap_t fuse_vnop_bmap;
132 static vop_close_t fuse_fifo_close;
133 static vop_close_t fuse_vnop_close;
134 static vop_copy_file_range_t fuse_vnop_copy_file_range;
135 static vop_create_t fuse_vnop_create;
136 static vop_deallocate_t fuse_vnop_deallocate;
137 static vop_deleteextattr_t fuse_vnop_deleteextattr;
138 static vop_fdatasync_t fuse_vnop_fdatasync;
139 static vop_fsync_t fuse_vnop_fsync;
140 static vop_getattr_t fuse_vnop_getattr;
141 static vop_getextattr_t fuse_vnop_getextattr;
142 static vop_inactive_t fuse_vnop_inactive;
143 static vop_ioctl_t fuse_vnop_ioctl;
144 static vop_link_t fuse_vnop_link;
145 static vop_listextattr_t fuse_vnop_listextattr;
146 static vop_lookup_t fuse_vnop_lookup;
147 static vop_mkdir_t fuse_vnop_mkdir;
148 static vop_mknod_t fuse_vnop_mknod;
149 static vop_open_t fuse_vnop_open;
150 static vop_pathconf_t fuse_vnop_pathconf;
151 static vop_read_t fuse_vnop_read;
152 static vop_readdir_t fuse_vnop_readdir;
153 static vop_readlink_t fuse_vnop_readlink;
154 static vop_reclaim_t fuse_vnop_reclaim;
155 static vop_remove_t fuse_vnop_remove;
156 static vop_rename_t fuse_vnop_rename;
157 static vop_rmdir_t fuse_vnop_rmdir;
158 static vop_setattr_t fuse_vnop_setattr;
159 static vop_setextattr_t fuse_vnop_setextattr;
160 static vop_strategy_t fuse_vnop_strategy;
161 static vop_symlink_t fuse_vnop_symlink;
162 static vop_write_t fuse_vnop_write;
163 static vop_getpages_t fuse_vnop_getpages;
164 static vop_print_t fuse_vnop_print;
165 static vop_vptofh_t fuse_vnop_vptofh;
167 struct vop_vector fuse_fifoops = {
168 .vop_default = &fifo_specops,
169 .vop_access = fuse_vnop_access,
170 .vop_close = fuse_fifo_close,
171 .vop_fsync = fuse_vnop_fsync,
172 .vop_getattr = fuse_vnop_getattr,
173 .vop_inactive = fuse_vnop_inactive,
174 .vop_pathconf = fuse_vnop_pathconf,
175 .vop_print = fuse_vnop_print,
176 .vop_read = VOP_PANIC,
177 .vop_reclaim = fuse_vnop_reclaim,
178 .vop_setattr = fuse_vnop_setattr,
179 .vop_write = VOP_PANIC,
180 .vop_vptofh = fuse_vnop_vptofh,
182 VFS_VOP_VECTOR_REGISTER(fuse_fifoops);
184 struct vop_vector fuse_vnops = {
185 .vop_allocate = fuse_vnop_allocate,
186 .vop_default = &default_vnodeops,
187 .vop_access = fuse_vnop_access,
188 .vop_advlock = fuse_vnop_advlock,
189 .vop_bmap = fuse_vnop_bmap,
190 .vop_close = fuse_vnop_close,
191 .vop_copy_file_range = fuse_vnop_copy_file_range,
192 .vop_create = fuse_vnop_create,
193 .vop_deallocate = fuse_vnop_deallocate,
194 .vop_deleteextattr = fuse_vnop_deleteextattr,
195 .vop_fsync = fuse_vnop_fsync,
196 .vop_fdatasync = fuse_vnop_fdatasync,
197 .vop_getattr = fuse_vnop_getattr,
198 .vop_getextattr = fuse_vnop_getextattr,
199 .vop_inactive = fuse_vnop_inactive,
200 .vop_ioctl = fuse_vnop_ioctl,
201 .vop_link = fuse_vnop_link,
202 .vop_listextattr = fuse_vnop_listextattr,
203 .vop_lookup = fuse_vnop_lookup,
204 .vop_mkdir = fuse_vnop_mkdir,
205 .vop_mknod = fuse_vnop_mknod,
206 .vop_open = fuse_vnop_open,
207 .vop_pathconf = fuse_vnop_pathconf,
209 * TODO: implement vop_poll after upgrading to protocol 7.21.
210 * FUSE_POLL was added in protocol 7.11, but it's kind of broken until
211 * 7.21, which adds the ability for the client to choose which poll
212 * events it wants, and for a client to deregister a file handle
214 .vop_read = fuse_vnop_read,
215 .vop_readdir = fuse_vnop_readdir,
216 .vop_readlink = fuse_vnop_readlink,
217 .vop_reclaim = fuse_vnop_reclaim,
218 .vop_remove = fuse_vnop_remove,
219 .vop_rename = fuse_vnop_rename,
220 .vop_rmdir = fuse_vnop_rmdir,
221 .vop_setattr = fuse_vnop_setattr,
222 .vop_setextattr = fuse_vnop_setextattr,
223 .vop_strategy = fuse_vnop_strategy,
224 .vop_symlink = fuse_vnop_symlink,
225 .vop_write = fuse_vnop_write,
226 .vop_getpages = fuse_vnop_getpages,
227 .vop_print = fuse_vnop_print,
228 .vop_vptofh = fuse_vnop_vptofh,
230 VFS_VOP_VECTOR_REGISTER(fuse_vnops);
232 uma_zone_t fuse_pbuf_zone;
234 /* Check permission for extattr operations, much like extattr_check_cred */
236 fuse_extattr_check_cred(struct vnode *vp, int ns, struct ucred *cred,
237 struct thread *td, accmode_t accmode)
239 struct mount *mp = vnode_mount(vp);
240 struct fuse_data *data = fuse_get_mpdata(mp);
241 int default_permissions = data->dataflags & FSESS_DEFAULT_PERMISSIONS;
244 * Kernel-invoked always succeeds.
250 * Do not allow privileged processes in jail to directly manipulate
254 case EXTATTR_NAMESPACE_SYSTEM:
255 if (default_permissions) {
256 return (priv_check_cred(cred, PRIV_VFS_EXTATTR_SYSTEM));
259 case EXTATTR_NAMESPACE_USER:
260 if (default_permissions) {
261 return (fuse_internal_access(vp, accmode, td, cred));
269 /* Get a filehandle for a directory */
271 fuse_filehandle_get_dir(struct vnode *vp, struct fuse_filehandle **fufhp,
272 struct ucred *cred, pid_t pid)
274 if (fuse_filehandle_get(vp, FREAD, fufhp, cred, pid) == 0)
276 return fuse_filehandle_get(vp, FEXEC, fufhp, cred, pid);
279 /* Send FUSE_FLUSH for this vnode */
281 fuse_flush(struct vnode *vp, struct ucred *cred, pid_t pid, int fflag)
283 struct fuse_flush_in *ffi;
284 struct fuse_filehandle *fufh;
285 struct fuse_dispatcher fdi;
286 struct thread *td = curthread;
287 struct mount *mp = vnode_mount(vp);
290 if (fsess_not_impl(vnode_mount(vp), FUSE_FLUSH))
293 err = fuse_filehandle_getrw(vp, fflag, &fufh, cred, pid);
297 fdisp_init(&fdi, sizeof(*ffi));
298 fdisp_make_vp(&fdi, FUSE_FLUSH, vp, td, cred);
300 ffi->fh = fufh->fh_id;
302 * If the file has a POSIX lock then we're supposed to set lock_owner.
303 * If not, then lock_owner is undefined. So we may as well always set
306 ffi->lock_owner = td->td_proc->p_pid;
308 err = fdisp_wait_answ(&fdi);
310 fsess_set_notimpl(mp, FUSE_FLUSH);
317 /* Close wrapper for fifos. */
319 fuse_fifo_close(struct vop_close_args *ap)
321 return (fifo_specops.vop_close(ap));
324 /* Invalidate a range of cached data, whether dirty of not */
326 fuse_inval_buf_range(struct vnode *vp, off_t filesize, off_t start, off_t end)
329 daddr_t left_lbn, end_lbn, right_lbn;
331 int iosize, left_on, right_on, right_blksize;
333 iosize = fuse_iosize(vp);
334 left_lbn = start / iosize;
335 end_lbn = howmany(end, iosize);
336 left_on = start & (iosize - 1);
338 bp = getblk(vp, left_lbn, iosize, PCATCH, 0, 0);
339 if ((bp->b_flags & B_CACHE) != 0 && bp->b_dirtyend >= left_on) {
341 * Flush the dirty buffer, because we don't have a
342 * byte-granular way to record which parts of the
347 return (bp->b_error);
352 right_on = end & (iosize - 1);
354 right_lbn = end / iosize;
355 new_filesize = MAX(filesize, end);
356 right_blksize = MIN(iosize, new_filesize - iosize * right_lbn);
357 bp = getblk(vp, right_lbn, right_blksize, PCATCH, 0, 0);
358 if ((bp->b_flags & B_CACHE) != 0 && bp->b_dirtyoff < right_on) {
360 * Flush the dirty buffer, because we don't have a
361 * byte-granular way to record which parts of the
366 return (bp->b_error);
372 v_inval_buf_range(vp, left_lbn, end_lbn, iosize);
377 /* Send FUSE_LSEEK for this node */
379 fuse_vnop_do_lseek(struct vnode *vp, struct thread *td, struct ucred *cred,
380 pid_t pid, off_t *offp, int whence)
382 struct fuse_dispatcher fdi;
383 struct fuse_filehandle *fufh;
384 struct fuse_lseek_in *flsi;
385 struct fuse_lseek_out *flso;
386 struct mount *mp = vnode_mount(vp);
389 ASSERT_VOP_LOCKED(vp, __func__);
391 err = fuse_filehandle_getrw(vp, FREAD, &fufh, cred, pid);
394 fdisp_init(&fdi, sizeof(*flsi));
395 fdisp_make_vp(&fdi, FUSE_LSEEK, vp, td, cred);
397 flsi->fh = fufh->fh_id;
398 flsi->offset = *offp;
399 flsi->whence = whence;
400 err = fdisp_wait_answ(&fdi);
402 fsess_set_notimpl(mp, FUSE_LSEEK);
403 } else if (err == 0) {
404 fsess_set_impl(mp, FUSE_LSEEK);
406 *offp = flso->offset;
414 struct vnop_access_args {
416 #if VOP_ACCESS_TAKES_ACCMODE_T
421 struct ucred *a_cred;
426 fuse_vnop_access(struct vop_access_args *ap)
428 struct vnode *vp = ap->a_vp;
429 int accmode = ap->a_accmode;
430 struct ucred *cred = ap->a_cred;
432 struct fuse_data *data = fuse_get_mpdata(vnode_mount(vp));
436 if (fuse_isdeadfs(vp)) {
437 if (vnode_isvroot(vp)) {
442 if (!(data->dataflags & FSESS_INITED)) {
443 if (vnode_isvroot(vp)) {
444 if (priv_check_cred(cred, PRIV_VFS_ADMIN) ||
445 (fuse_match_cred(data->daemoncred, cred) == 0)) {
451 if (vnode_islnk(vp)) {
455 err = fuse_internal_access(vp, accmode, ap->a_td, ap->a_cred);
460 * struct vop_advlock_args {
461 * struct vop_generic_args a_gen;
462 * struct vnode *a_vp;
465 * struct flock *a_fl;
470 fuse_vnop_advlock(struct vop_advlock_args *ap)
472 struct vnode *vp = ap->a_vp;
473 struct flock *fl = ap->a_fl;
474 struct thread *td = curthread;
475 struct ucred *cred = td->td_ucred;
476 pid_t pid = td->td_proc->p_pid;
477 struct fuse_filehandle *fufh;
478 struct fuse_dispatcher fdi;
479 struct fuse_lk_in *fli;
480 struct fuse_lk_out *flo;
483 int flags = ap->a_flags;
485 dataflags = fuse_get_mpdata(vnode_mount(vp))->dataflags;
487 if (fuse_isdeadfs(vp)) {
508 if (!(dataflags & FSESS_POSIX_LOCKS))
509 return vop_stdadvlock(ap);
510 /* FUSE doesn't properly support flock until protocol 7.17 */
512 return vop_stdadvlock(ap);
514 vn_lock(vp, LK_SHARED | LK_RETRY);
516 err = fuse_filehandle_get_anyflags(vp, &fufh, cred, pid);
520 fdisp_init(&fdi, sizeof(*fli));
522 fdisp_make_vp(&fdi, op, vp, td, cred);
524 fli->fh = fufh->fh_id;
525 fli->owner = td->td_proc->p_pid;
526 fli->lk.start = fl->l_start;
528 fli->lk.end = fl->l_start + fl->l_len - 1;
530 fli->lk.end = INT64_MAX;
531 fli->lk.type = fl->l_type;
532 fli->lk.pid = td->td_proc->p_pid;
534 err = fdisp_wait_answ(&fdi);
537 if (err == 0 && op == FUSE_GETLK) {
539 fl->l_type = flo->lk.type;
540 fl->l_pid = flo->lk.pid;
541 if (flo->lk.type != F_UNLCK) {
542 fl->l_start = flo->lk.start;
543 if (flo->lk.end == INT64_MAX)
546 fl->l_len = flo->lk.end - flo->lk.start + 1;
547 fl->l_start = flo->lk.start;
557 fuse_vnop_allocate(struct vop_allocate_args *ap)
559 struct vnode *vp = ap->a_vp;
560 off_t *len = ap->a_len;
561 off_t *offset = ap->a_offset;
562 struct ucred *cred = ap->a_cred;
563 struct fuse_filehandle *fufh;
564 struct mount *mp = vnode_mount(vp);
565 struct fuse_dispatcher fdi;
566 struct fuse_fallocate_in *ffi;
568 pid_t pid = curthread->td_proc->p_pid;
569 struct fuse_vnode_data *fvdat = VTOFUD(vp);
573 if (fuse_isdeadfs(vp))
576 switch (vp->v_type) {
581 if (vfs_isrdonly(mp))
588 if (vfs_isrdonly(mp))
591 if (fsess_not_impl(mp, FUSE_FALLOCATE))
594 io.uio_offset = *offset;
596 err = vn_rlimit_fsize(vp, &io, curthread);
600 err = fuse_filehandle_getrw(vp, FWRITE, &fufh, cred, pid);
604 fuse_vnode_update(vp, FN_MTIMECHANGE | FN_CTIMECHANGE);
606 err = fuse_vnode_size(vp, &filesize, cred, curthread);
609 fuse_inval_buf_range(vp, filesize, *offset, *offset + *len);
611 fdisp_init(&fdi, sizeof(*ffi));
612 fdisp_make_vp(&fdi, FUSE_FALLOCATE, vp, curthread, cred);
614 ffi->fh = fufh->fh_id;
615 ffi->offset = *offset;
618 err = fdisp_wait_answ(&fdi);
621 fsess_set_notimpl(mp, FUSE_FALLOCATE);
623 } else if (err == EOPNOTSUPP) {
625 * The file system server does not support FUSE_FALLOCATE with
626 * the supplied mode for this particular file.
632 fuse_vnode_undirty_cached_timestamps(vp, false);
633 fuse_internal_clear_suid_on_write(vp, cred, curthread);
634 if (*offset > fvdat->cached_attrs.va_size) {
635 fuse_vnode_setsize(vp, *offset, false);
636 getnanouptime(&fvdat->last_local_modify);
646 struct bufobj **a_bop;
652 fuse_vnop_bmap(struct vop_bmap_args *ap)
654 struct vnode *vp = ap->a_vp;
655 struct bufobj **bo = ap->a_bop;
656 struct thread *td = curthread;
658 struct fuse_dispatcher fdi;
659 struct fuse_bmap_in *fbi;
660 struct fuse_bmap_out *fbo;
661 struct fuse_data *data;
662 struct fuse_vnode_data *fvdat = VTOFUD(vp);
665 daddr_t lbn = ap->a_bn;
666 daddr_t *pbn = ap->a_bnp;
667 int *runp = ap->a_runp;
668 int *runb = ap->a_runb;
672 if (fuse_isdeadfs(vp)) {
676 mp = vnode_mount(vp);
677 data = fuse_get_mpdata(mp);
678 biosize = fuse_iosize(vp);
679 maxrun = MIN(vp->v_mount->mnt_iosize_max / biosize - 1,
680 data->max_readahead_blocks);
686 * The FUSE_BMAP operation does not include the runp and runb
687 * variables, so we must guess. Report nonzero contiguous runs so
688 * cluster_read will combine adjacent reads. It's worthwhile to reduce
689 * upcalls even if we don't know the true physical layout of the file.
691 * FUSE file systems may opt out of read clustering in two ways:
692 * * mounting with -onoclusterr
693 * * Setting max_readahead <= maxbcachebuf during FUSE_INIT
696 *runb = MIN(lbn, maxrun);
697 if (runp != NULL && maxrun == 0)
699 else if (runp != NULL) {
701 * If the file's size is cached, use that value to calculate
702 * runp, even if the cache is expired. runp is only advisory,
703 * and the risk of getting it wrong is not worth the cost of
706 if (fvdat->cached_attrs.va_size != VNOVAL)
707 fsize = fvdat->cached_attrs.va_size;
709 error = fuse_vnode_size(vp, &fsize, td->td_ucred, td);
711 *runp = MIN(MAX(0, fsize / (off_t)biosize - lbn - 1),
717 if (fsess_maybe_impl(mp, FUSE_BMAP)) {
718 fdisp_init(&fdi, sizeof(*fbi));
719 fdisp_make_vp(&fdi, FUSE_BMAP, vp, td, td->td_ucred);
722 fbi->blocksize = biosize;
723 error = fdisp_wait_answ(&fdi);
724 if (error == ENOSYS) {
726 fsess_set_notimpl(mp, FUSE_BMAP);
730 if (error == 0 && pbn != NULL)
737 /* If the daemon doesn't support BMAP, make up a sensible default */
739 *pbn = lbn * btodb(biosize);
744 struct vop_close_args {
747 struct ucred *a_cred;
752 fuse_vnop_close(struct vop_close_args *ap)
754 struct vnode *vp = ap->a_vp;
755 struct ucred *cred = ap->a_cred;
756 int fflag = ap->a_fflag;
757 struct thread *td = ap->a_td;
758 pid_t pid = td->td_proc->p_pid;
759 struct fuse_vnode_data *fvdat = VTOFUD(vp);
762 if (fuse_isdeadfs(vp))
766 if (fflag & IO_NDELAY)
769 err = fuse_flush(vp, cred, pid, fflag);
770 if (err == 0 && (fvdat->flag & FN_ATIMECHANGE)) {
774 vap.va_atime = fvdat->cached_attrs.va_atime;
775 err = fuse_internal_setattr(vp, &vap, td, NULL);
777 /* TODO: close the file handle, if we're sure it's no longer used */
778 if ((fvdat->flag & FN_SIZECHANGE) != 0) {
779 fuse_vnode_savesize(vp, cred, td->td_proc->p_pid);
785 struct vop_copy_file_range_args {
786 struct vop_generic_args a_gen;
787 struct vnode *a_invp;
789 struct vnode *a_outvp;
792 unsigned int a_flags;
793 struct ucred *a_incred;
794 struct ucred *a_outcred;
795 struct thread *a_fsizetd;
799 fuse_vnop_copy_file_range(struct vop_copy_file_range_args *ap)
801 struct vnode *invp = ap->a_invp;
802 struct vnode *outvp = ap->a_outvp;
803 struct mount *mp = vnode_mount(invp);
804 struct fuse_vnode_data *outfvdat = VTOFUD(outvp);
805 struct fuse_dispatcher fdi;
806 struct fuse_filehandle *infufh, *outfufh;
807 struct fuse_copy_file_range_in *fcfri;
808 struct ucred *incred = ap->a_incred;
809 struct ucred *outcred = ap->a_outcred;
810 struct fuse_write_out *fwo;
817 if (mp != vnode_mount(outvp))
820 if (incred->cr_uid != outcred->cr_uid)
823 if (incred->cr_groups[0] != outcred->cr_groups[0])
826 if (fsess_not_impl(mp, FUSE_COPY_FILE_RANGE))
829 if (ap->a_fsizetd == NULL)
833 pid = td->td_proc->p_pid;
835 /* Lock both vnodes, avoiding risk of deadlock. */
837 err = vn_lock(outvp, LK_EXCLUSIVE);
841 err = vn_lock(invp, LK_SHARED | LK_NOWAIT);
845 err = vn_lock(invp, LK_SHARED);
853 err = fuse_filehandle_getrw(invp, FREAD, &infufh, incred, pid);
857 err = fuse_filehandle_getrw(outvp, FWRITE, &outfufh, outcred, pid);
862 io.uio_offset = *ap->a_outoffp;
863 io.uio_resid = *ap->a_lenp;
864 err = vn_rlimit_fsize(outvp, &io, ap->a_fsizetd);
869 err = fuse_vnode_size(outvp, &outfilesize, outcred, curthread);
873 err = fuse_inval_buf_range(outvp, outfilesize, *ap->a_outoffp,
874 *ap->a_outoffp + *ap->a_lenp);
878 fdisp_init(&fdi, sizeof(*fcfri));
879 fdisp_make_vp(&fdi, FUSE_COPY_FILE_RANGE, invp, td, incred);
881 fcfri->fh_in = infufh->fh_id;
882 fcfri->off_in = *ap->a_inoffp;
883 fcfri->nodeid_out = VTOI(outvp);
884 fcfri->fh_out = outfufh->fh_id;
885 fcfri->off_out = *ap->a_outoffp;
886 fcfri->len = *ap->a_lenp;
889 err = fdisp_wait_answ(&fdi);
892 *ap->a_lenp = fwo->size;
893 *ap->a_inoffp += fwo->size;
894 *ap->a_outoffp += fwo->size;
895 fuse_internal_clear_suid_on_write(outvp, outcred, td);
896 if (*ap->a_outoffp > outfvdat->cached_attrs.va_size) {
897 fuse_vnode_setsize(outvp, *ap->a_outoffp, false);
898 getnanouptime(&outfvdat->last_local_modify);
900 fuse_vnode_update(invp, FN_ATIMECHANGE);
901 fuse_vnode_update(outvp, FN_MTIMECHANGE | FN_CTIMECHANGE);
911 fsess_set_notimpl(mp, FUSE_COPY_FILE_RANGE);
913 err = vn_generic_copy_file_range(ap->a_invp, ap->a_inoffp,
914 ap->a_outvp, ap->a_outoffp, ap->a_lenp, ap->a_flags,
915 ap->a_incred, ap->a_outcred, ap->a_fsizetd);
922 fdisp_make_mknod_for_fallback(
923 struct fuse_dispatcher *fdip,
924 struct componentname *cnp,
930 enum fuse_opcode *op)
932 struct fuse_mknod_in *fmni;
934 fdisp_init(fdip, sizeof(*fmni) + cnp->cn_namelen + 1);
936 fdisp_make(fdip, *op, vnode_mount(dvp), parentnid, td, cred);
940 memcpy((char *)fdip->indata + sizeof(*fmni), cnp->cn_nameptr,
942 ((char *)fdip->indata)[sizeof(*fmni) + cnp->cn_namelen] = '\0';
945 struct vnop_create_args {
947 struct vnode **a_vpp;
948 struct componentname *a_cnp;
953 fuse_vnop_create(struct vop_create_args *ap)
955 struct vnode *dvp = ap->a_dvp;
956 struct vnode **vpp = ap->a_vpp;
957 struct componentname *cnp = ap->a_cnp;
958 struct vattr *vap = ap->a_vap;
959 struct thread *td = curthread;
960 struct ucred *cred = cnp->cn_cred;
962 struct fuse_data *data;
963 struct fuse_create_in *fci;
964 struct fuse_entry_out *feo;
965 struct fuse_open_out *foo;
966 struct fuse_dispatcher fdi, fdi2;
967 struct fuse_dispatcher *fdip = &fdi;
968 struct fuse_dispatcher *fdip2 = NULL;
972 struct mount *mp = vnode_mount(dvp);
973 data = fuse_get_mpdata(mp);
974 uint64_t parentnid = VTOFUD(dvp)->nid;
975 mode_t mode = MAKEIMODE(vap->va_type, vap->va_mode);
979 if (fuse_isdeadfs(dvp))
982 /* FUSE expects sockets to be created with FUSE_MKNOD */
983 if (vap->va_type == VSOCK)
984 return fuse_internal_mknod(dvp, vpp, cnp, vap);
987 * VOP_CREATE doesn't tell us the open(2) flags, so we guess. Only a
988 * writable mode makes sense, and we might as well include readability
993 bzero(&fdi, sizeof(fdi));
995 if (vap->va_type != VREG)
998 if (fsess_not_impl(mp, FUSE_CREATE) || vap->va_type == VSOCK) {
999 /* Fallback to FUSE_MKNOD/FUSE_OPEN */
1000 fdisp_make_mknod_for_fallback(fdip, cnp, dvp, parentnid, td,
1003 /* Use FUSE_CREATE */
1007 fdisp_init(fdip, sizeof(*fci) + cnp->cn_namelen + 1);
1008 fdisp_make(fdip, op, vnode_mount(dvp), parentnid, td, cred);
1011 fci->flags = O_CREAT | flags;
1012 if (fuse_libabi_geq(data, 7, 12)) {
1013 insize = sizeof(*fci);
1014 fci->umask = td->td_proc->p_pd->pd_cmask;
1016 insize = sizeof(struct fuse_open_in);
1019 memcpy((char *)fdip->indata + insize, cnp->cn_nameptr,
1021 ((char *)fdip->indata)[insize + cnp->cn_namelen] = '\0';
1024 err = fdisp_wait_answ(fdip);
1027 if (err == ENOSYS && op == FUSE_CREATE) {
1028 fsess_set_notimpl(mp, FUSE_CREATE);
1029 fdisp_destroy(fdip);
1030 fdisp_make_mknod_for_fallback(fdip, cnp, dvp,
1031 parentnid, td, cred, mode, &op);
1032 err = fdisp_wait_answ(fdip);
1040 if ((err = fuse_internal_checkentry(feo, vap->va_type))) {
1044 if (op == FUSE_CREATE) {
1045 if (fuse_libabi_geq(data, 7, 9))
1046 foo = (struct fuse_open_out*)(feo + 1);
1048 foo = (struct fuse_open_out*)((char*)feo +
1049 FUSE_COMPAT_ENTRY_OUT_SIZE);
1051 /* Issue a separate FUSE_OPEN */
1052 struct fuse_open_in *foi;
1055 fdisp_init(fdip2, sizeof(*foi));
1056 fdisp_make(fdip2, FUSE_OPEN, vnode_mount(dvp), feo->nodeid, td,
1058 foi = fdip2->indata;
1060 err = fdisp_wait_answ(fdip2);
1065 err = fuse_vnode_get(mp, feo, feo->nodeid, dvp, vpp, cnp, vap->va_type);
1067 struct fuse_release_in *fri;
1068 uint64_t nodeid = feo->nodeid;
1069 uint64_t fh_id = foo->fh;
1071 fdisp_init(fdip, sizeof(*fri));
1072 fdisp_make(fdip, FUSE_RELEASE, mp, nodeid, td, cred);
1076 fuse_insert_callback(fdip->tick, fuse_internal_forget_callback);
1077 fuse_insert_message(fdip->tick, false);
1080 ASSERT_VOP_ELOCKED(*vpp, "fuse_vnop_create");
1081 fuse_internal_cache_attrs(*vpp, &feo->attr, feo->attr_valid,
1082 feo->attr_valid_nsec, NULL, true);
1084 fuse_filehandle_init(*vpp, FUFH_RDWR, NULL, td, cred, foo);
1085 fuse_vnode_open(*vpp, foo->open_flags, td);
1087 * Purge the parent's attribute cache because the daemon should've
1088 * updated its mtime and ctime
1090 fuse_vnode_clear_attr_cache(dvp);
1091 cache_purge_negative(dvp);
1095 fdisp_destroy(fdip2);
1096 fdisp_destroy(fdip);
1101 struct vnop_fdatasync_args {
1102 struct vop_generic_args a_gen;
1103 struct vnode * a_vp;
1104 struct thread * a_td;
1108 fuse_vnop_fdatasync(struct vop_fdatasync_args *ap)
1110 struct vnode *vp = ap->a_vp;
1111 struct thread *td = ap->a_td;
1112 int waitfor = MNT_WAIT;
1116 if (fuse_isdeadfs(vp)) {
1119 if ((err = vop_stdfdatasync_buf(ap)))
1122 return fuse_internal_fsync(vp, td, waitfor, true);
1126 struct vnop_fsync_args {
1127 struct vop_generic_args a_gen;
1128 struct vnode * a_vp;
1130 struct thread * a_td;
1134 fuse_vnop_fsync(struct vop_fsync_args *ap)
1136 struct vnode *vp = ap->a_vp;
1137 struct thread *td = ap->a_td;
1138 int waitfor = ap->a_waitfor;
1141 if (fuse_isdeadfs(vp)) {
1144 if ((err = vop_stdfsync(ap)))
1147 return fuse_internal_fsync(vp, td, waitfor, false);
1151 struct vnop_getattr_args {
1153 struct vattr *a_vap;
1154 struct ucred *a_cred;
1155 struct thread *a_td;
1159 fuse_vnop_getattr(struct vop_getattr_args *ap)
1161 struct vnode *vp = ap->a_vp;
1162 struct vattr *vap = ap->a_vap;
1163 struct ucred *cred = ap->a_cred;
1164 struct thread *td = curthread;
1169 dataflags = fuse_get_mpdata(vnode_mount(vp))->dataflags;
1171 /* Note that we are not bailing out on a dead file system just yet. */
1173 if (!(dataflags & FSESS_INITED)) {
1174 if (!vnode_isvroot(vp)) {
1175 fdata_set_dead(fuse_get_mpdata(vnode_mount(vp)));
1182 err = fuse_internal_getattr(vp, vap, cred, td);
1183 if (err == ENOTCONN && vnode_isvroot(vp)) {
1184 /* see comment in fuse_vfsop_statfs() */
1191 bzero(vap, sizeof(*vap));
1192 vap->va_type = vnode_vtype(vp);
1198 struct vnop_inactive_args {
1203 fuse_vnop_inactive(struct vop_inactive_args *ap)
1205 struct vnode *vp = ap->a_vp;
1206 struct thread *td = curthread;
1208 struct fuse_vnode_data *fvdat = VTOFUD(vp);
1209 struct fuse_filehandle *fufh, *fufh_tmp;
1213 LIST_FOREACH_SAFE(fufh, &fvdat->handles, next, fufh_tmp) {
1214 if (need_flush && vp->v_type == VREG) {
1215 if ((VTOFUD(vp)->flag & FN_SIZECHANGE) != 0) {
1216 fuse_vnode_savesize(vp, NULL, 0);
1218 if ((fvdat->flag & FN_REVOKED) != 0)
1219 fuse_io_invalbuf(vp, td);
1221 fuse_io_flushbuf(vp, MNT_WAIT, td);
1224 fuse_filehandle_close(vp, fufh, td, NULL);
1227 if ((fvdat->flag & FN_REVOKED) != 0)
1234 struct vnop_ioctl_args {
1239 struct ucred *a_cred;
1240 struct thread *a_td;
1244 fuse_vnop_ioctl(struct vop_ioctl_args *ap)
1246 struct vnode *vp = ap->a_vp;
1247 struct mount *mp = vnode_mount(vp);
1248 struct ucred *cred = ap->a_cred;
1250 pid_t pid = ap->a_td->td_proc->p_pid;
1253 switch (ap->a_command) {
1256 /* Call FUSE_LSEEK, if we can, or fall back to vop_stdioctl */
1257 if (fsess_maybe_impl(mp, FUSE_LSEEK)) {
1261 if (ap->a_command == FIOSEEKDATA)
1266 vn_lock(vp, LK_SHARED | LK_RETRY);
1267 err = fuse_vnop_do_lseek(vp, ap->a_td, cred, pid, offp,
1271 if (fsess_not_impl(mp, FUSE_LSEEK))
1272 err = vop_stdioctl(ap);
1275 /* TODO: implement FUSE_IOCTL */
1284 struct vnop_link_args {
1285 struct vnode *a_tdvp;
1287 struct componentname *a_cnp;
1291 fuse_vnop_link(struct vop_link_args *ap)
1293 struct vnode *vp = ap->a_vp;
1294 struct vnode *tdvp = ap->a_tdvp;
1295 struct componentname *cnp = ap->a_cnp;
1297 struct vattr *vap = VTOVA(vp);
1299 struct fuse_dispatcher fdi;
1300 struct fuse_entry_out *feo;
1301 struct fuse_link_in fli;
1305 if (fuse_isdeadfs(vp)) {
1308 if (vnode_mount(tdvp) != vnode_mount(vp)) {
1313 * This is a seatbelt check to protect naive userspace filesystems from
1314 * themselves and the limitations of the FUSE IPC protocol. If a
1315 * filesystem does not allow attribute caching, assume it is capable of
1316 * validating that nlink does not overflow.
1318 if (vap != NULL && vap->va_nlink >= FUSE_LINK_MAX)
1320 fli.oldnodeid = VTOI(vp);
1322 fdisp_init(&fdi, 0);
1323 fuse_internal_newentry_makerequest(vnode_mount(tdvp), VTOI(tdvp), cnp,
1324 FUSE_LINK, &fli, sizeof(fli), &fdi);
1325 if ((err = fdisp_wait_answ(&fdi))) {
1330 if (fli.oldnodeid != feo->nodeid) {
1331 struct fuse_data *data = fuse_get_mpdata(vnode_mount(vp));
1332 fuse_warn(data, FSESS_WARN_ILLEGAL_INODE,
1333 "Assigned wrong inode for a hard link.");
1334 fuse_vnode_clear_attr_cache(vp);
1335 fuse_vnode_clear_attr_cache(tdvp);
1340 err = fuse_internal_checkentry(feo, vnode_vtype(vp));
1343 * Purge the parent's attribute cache because the daemon
1344 * should've updated its mtime and ctime
1346 fuse_vnode_clear_attr_cache(tdvp);
1347 fuse_internal_cache_attrs(vp, &feo->attr, feo->attr_valid,
1348 feo->attr_valid_nsec, NULL, true);
1351 fdisp_destroy(&fdi);
1355 struct fuse_lookup_alloc_arg {
1356 struct fuse_entry_out *feo;
1357 struct componentname *cnp;
1362 /* Callback for vn_get_ino */
1364 fuse_lookup_alloc(struct mount *mp, void *arg, int lkflags, struct vnode **vpp)
1366 struct fuse_lookup_alloc_arg *flaa = arg;
1368 return fuse_vnode_get(mp, flaa->feo, flaa->nid, NULL, vpp, flaa->cnp,
1372 SDT_PROBE_DEFINE3(fusefs, , vnops, cache_lookup,
1373 "int", "struct timespec*", "struct timespec*");
1375 struct vnop_lookup_args {
1376 struct vnodeop_desc *a_desc;
1377 struct vnode *a_dvp;
1378 struct vnode **a_vpp;
1379 struct componentname *a_cnp;
1383 fuse_vnop_lookup(struct vop_lookup_args *ap)
1385 struct vnode *dvp = ap->a_dvp;
1386 struct vnode **vpp = ap->a_vpp;
1387 struct componentname *cnp = ap->a_cnp;
1388 struct thread *td = curthread;
1389 struct ucred *cred = cnp->cn_cred;
1390 struct timespec now;
1392 int nameiop = cnp->cn_nameiop;
1393 int flags = cnp->cn_flags;
1394 int islastcn = flags & ISLASTCN;
1395 struct mount *mp = vnode_mount(dvp);
1396 struct fuse_data *data = fuse_get_mpdata(mp);
1397 int default_permissions = data->dataflags & FSESS_DEFAULT_PERMISSIONS;
1402 struct vnode *vp = NULL;
1404 struct fuse_dispatcher fdi;
1405 bool did_lookup = false;
1406 struct fuse_entry_out *feo = NULL;
1407 enum vtype vtyp; /* vnode type of target */
1411 if (fuse_isdeadfs(dvp)) {
1415 if (!vnode_isdir(dvp))
1418 if (islastcn && vfs_isrdonly(mp) && (nameiop != LOOKUP))
1421 if ((cnp->cn_flags & NOEXECCHECK) != 0)
1422 cnp->cn_flags &= ~NOEXECCHECK;
1423 else if ((err = fuse_internal_access(dvp, VEXEC, td, cred)))
1426 is_dot = cnp->cn_namelen == 1 && *(cnp->cn_nameptr) == '.';
1427 if ((flags & ISDOTDOT) && !(data->dataflags & FSESS_EXPORT_SUPPORT))
1429 if (!(VTOFUD(dvp)->flag & FN_PARENT_NID)) {
1431 * Since the file system doesn't support ".." lookups,
1432 * we have no way to find this entry.
1436 nid = VTOFUD(dvp)->parent_nid;
1439 /* .. is obviously a directory */
1441 } else if (is_dot) {
1443 /* . is obviously a directory */
1446 struct timespec timeout;
1447 int ncpticks; /* here to accommodate for API contract */
1449 err = cache_lookup(dvp, vpp, cnp, &timeout, &ncpticks);
1450 getnanouptime(&now);
1451 SDT_PROBE3(fusefs, , vnops, cache_lookup, err, &timeout, &now);
1453 case -1: /* positive match */
1454 if (timespeccmp(&timeout, &now, >)) {
1455 counter_u64_add(fuse_lookup_cache_hits, 1);
1458 counter_u64_add(fuse_lookup_cache_misses, 1);
1460 &VTOFUD(*vpp)->entry_cache_timeout);
1471 case 0: /* no match in cache */
1472 counter_u64_add(fuse_lookup_cache_misses, 1);
1475 case ENOENT: /* negative match */
1476 if (timespeccmp(&timeout, &now, <=)) {
1478 cache_purge_negative(dvp);
1486 fdisp_init(&fdi, cnp->cn_namelen + 1);
1487 fdisp_make(&fdi, FUSE_LOOKUP, mp, VTOI(dvp), td, cred);
1489 memcpy(fdi.indata, cnp->cn_nameptr, cnp->cn_namelen);
1490 ((char *)fdi.indata)[cnp->cn_namelen] = '\0';
1491 lookup_err = fdisp_wait_answ(&fdi);
1495 /* lookup call succeeded */
1496 feo = (struct fuse_entry_out *)fdi.answ;
1499 /* zero nodeid means ENOENT and cache it */
1500 struct timespec timeout;
1502 fdi.answ_stat = ENOENT;
1503 lookup_err = ENOENT;
1504 if (cnp->cn_flags & MAKEENTRY) {
1505 fuse_validity_2_timespec(feo, &timeout);
1506 /* Use the same entry_time for .. as for
1507 * the file itself. That doesn't honor
1508 * exactly what the fuse server tells
1509 * us, but to do otherwise would require
1510 * another cache lookup at this point.
1512 struct timespec *dtsp = NULL;
1513 cache_enter_time(dvp, *vpp, cnp,
1517 vtyp = IFTOVT(feo->attr.mode);
1519 if (lookup_err && (!fdi.answ_stat || lookup_err != ENOENT)) {
1520 fdisp_destroy(&fdi);
1524 /* lookup_err, if non-zero, must be ENOENT at this point */
1527 /* Entry not found */
1528 if ((nameiop == CREATE || nameiop == RENAME) && islastcn) {
1529 if (default_permissions)
1530 err = fuse_internal_access(dvp, VWRITE, td,
1541 /* Entry was found */
1542 if (flags & ISDOTDOT) {
1543 struct fuse_lookup_alloc_arg flaa;
1549 err = vn_vget_ino_gen(dvp, fuse_lookup_alloc, &flaa, 0,
1552 } else if (nid == VTOI(dvp)) {
1557 fuse_warn(fuse_get_mpdata(mp),
1558 FSESS_WARN_ILLEGAL_INODE,
1559 "Assigned same inode to both parent and "
1565 struct fuse_vnode_data *fvdat;
1567 err = fuse_vnode_get(vnode_mount(dvp), feo, nid, dvp,
1575 if (timespeccmp(&now, &fvdat->last_local_modify, >)) {
1577 * Attributes from the server are definitely
1578 * newer than the last attributes we sent to
1579 * the server, so cache them.
1581 fuse_internal_cache_attrs(*vpp, &feo->attr,
1582 feo->attr_valid, feo->attr_valid_nsec,
1585 fuse_validity_2_bintime(feo->entry_valid,
1586 feo->entry_valid_nsec,
1587 &fvdat->entry_cache_timeout);
1589 if ((nameiop == DELETE || nameiop == RENAME) &&
1590 islastcn && default_permissions)
1592 struct vattr dvattr;
1594 err = fuse_internal_access(dvp, VWRITE, td,
1599 * if the parent's sticky bit is set, check
1600 * whether we're allowed to remove the file.
1601 * Need to figure out the vnode locking to make
1604 fuse_internal_getattr(dvp, &dvattr, cred, td);
1605 if ((dvattr.va_mode & S_ISTXT) &&
1606 fuse_internal_access(dvp, VADMIN, td,
1608 fuse_internal_access(*vpp, VADMIN, td,
1618 if (vp != NULL && dvp != vp)
1620 else if (vp != NULL)
1625 fdisp_destroy(&fdi);
1631 struct vnop_mkdir_args {
1632 struct vnode *a_dvp;
1633 struct vnode **a_vpp;
1634 struct componentname *a_cnp;
1635 struct vattr *a_vap;
1639 fuse_vnop_mkdir(struct vop_mkdir_args *ap)
1641 struct vnode *dvp = ap->a_dvp;
1642 struct vnode **vpp = ap->a_vpp;
1643 struct componentname *cnp = ap->a_cnp;
1644 struct vattr *vap = ap->a_vap;
1646 struct fuse_mkdir_in fmdi;
1648 if (fuse_isdeadfs(dvp)) {
1651 fmdi.mode = MAKEIMODE(vap->va_type, vap->va_mode);
1652 fmdi.umask = curthread->td_proc->p_pd->pd_cmask;
1654 return (fuse_internal_newentry(dvp, vpp, cnp, FUSE_MKDIR, &fmdi,
1655 sizeof(fmdi), VDIR));
1659 struct vnop_mknod_args {
1660 struct vnode *a_dvp;
1661 struct vnode **a_vpp;
1662 struct componentname *a_cnp;
1663 struct vattr *a_vap;
1667 fuse_vnop_mknod(struct vop_mknod_args *ap)
1670 struct vnode *dvp = ap->a_dvp;
1671 struct vnode **vpp = ap->a_vpp;
1672 struct componentname *cnp = ap->a_cnp;
1673 struct vattr *vap = ap->a_vap;
1675 if (fuse_isdeadfs(dvp))
1678 return fuse_internal_mknod(dvp, vpp, cnp, vap);
1682 struct vop_open_args {
1685 struct ucred *a_cred;
1686 struct thread *a_td;
1687 int a_fdidx; / struct file *a_fp;
1691 fuse_vnop_open(struct vop_open_args *ap)
1693 struct vnode *vp = ap->a_vp;
1694 int a_mode = ap->a_mode;
1695 struct thread *td = ap->a_td;
1696 struct ucred *cred = ap->a_cred;
1697 pid_t pid = td->td_proc->p_pid;
1699 if (fuse_isdeadfs(vp))
1701 if (vp->v_type == VCHR || vp->v_type == VBLK || vp->v_type == VFIFO)
1702 return (EOPNOTSUPP);
1703 if ((a_mode & (FREAD | FWRITE | FEXEC)) == 0)
1706 if (fuse_filehandle_validrw(vp, a_mode, cred, pid)) {
1707 fuse_vnode_open(vp, 0, td);
1711 return fuse_filehandle_open(vp, a_mode, NULL, td, cred);
1715 fuse_vnop_pathconf(struct vop_pathconf_args *ap)
1717 struct vnode *vp = ap->a_vp;
1720 switch (ap->a_name) {
1721 case _PC_FILESIZEBITS:
1725 *ap->a_retval = NAME_MAX;
1728 *ap->a_retval = MIN(LONG_MAX, FUSE_LINK_MAX);
1730 case _PC_SYMLINK_MAX:
1731 *ap->a_retval = MAXPATHLEN;
1736 case _PC_MIN_HOLE_SIZE:
1738 * The FUSE protocol provides no mechanism for a server to
1739 * report _PC_MIN_HOLE_SIZE. It's a protocol bug. Instead,
1740 * return EINVAL if the server does not support FUSE_LSEEK, or
1743 mp = vnode_mount(vp);
1744 if (!fsess_is_impl(mp, FUSE_LSEEK) &&
1745 !fsess_not_impl(mp, FUSE_LSEEK)) {
1748 /* Issue a FUSE_LSEEK to find out if it's implemented */
1749 fuse_vnop_do_lseek(vp, curthread, curthread->td_ucred,
1750 curthread->td_proc->p_pid, &offset, SEEK_DATA);
1753 if (fsess_is_impl(mp, FUSE_LSEEK)) {
1758 * Probably FUSE_LSEEK is not implemented. It might
1759 * be, if the FUSE_LSEEK above returned an error like
1760 * EACCES, but in that case we can't tell, so it's
1761 * safest to report EINVAL anyway.
1766 return (vop_stdpathconf(ap));
1770 SDT_PROBE_DEFINE3(fusefs, , vnops, filehandles_closed, "struct vnode*",
1771 "struct uio*", "struct ucred*");
1773 struct vnop_read_args {
1777 struct ucred *a_cred;
1781 fuse_vnop_read(struct vop_read_args *ap)
1783 struct vnode *vp = ap->a_vp;
1784 struct uio *uio = ap->a_uio;
1785 int ioflag = ap->a_ioflag;
1786 struct ucred *cred = ap->a_cred;
1787 pid_t pid = curthread->td_proc->p_pid;
1788 struct fuse_filehandle *fufh;
1790 bool closefufh = false, directio;
1792 MPASS(vp->v_type == VREG || vp->v_type == VDIR);
1794 if (fuse_isdeadfs(vp)) {
1798 if (VTOFUD(vp)->flag & FN_DIRECTIO) {
1799 ioflag |= IO_DIRECT;
1802 err = fuse_filehandle_getrw(vp, FREAD, &fufh, cred, pid);
1803 if (err == EBADF && vnode_mount(vp)->mnt_flag & MNT_EXPORTED) {
1805 * nfsd will do I/O without first doing VOP_OPEN. We
1806 * must implicitly open the file here
1808 err = fuse_filehandle_open(vp, FREAD, &fufh, curthread, cred);
1812 SDT_PROBE3(fusefs, , vnops, filehandles_closed, vp, uio, cred);
1817 * Ideally, when the daemon asks for direct io at open time, the
1818 * standard file flag should be set according to this, so that would
1819 * just change the default mode, which later on could be changed via
1821 * But this doesn't work, the O_DIRECT flag gets cleared at some point
1822 * (don't know where). So to make any use of the Fuse direct_io option,
1823 * we hardwire it into the file's private data (similarly to Linux,
1826 directio = (ioflag & IO_DIRECT) || !fsess_opt_datacache(vnode_mount(vp));
1828 fuse_vnode_update(vp, FN_ATIMECHANGE);
1830 SDT_PROBE2(fusefs, , vnops, trace, 1, "direct read of vnode");
1831 err = fuse_read_directbackend(vp, uio, cred, fufh);
1833 SDT_PROBE2(fusefs, , vnops, trace, 1, "buffered read of vnode");
1834 err = fuse_read_biobackend(vp, uio, ioflag, cred, fufh, pid);
1838 fuse_filehandle_close(vp, fufh, curthread, cred);
1844 struct vnop_readdir_args {
1847 struct ucred *a_cred;
1850 uint64_t **a_cookies;
1854 fuse_vnop_readdir(struct vop_readdir_args *ap)
1856 struct vnode *vp = ap->a_vp;
1857 struct uio *uio = ap->a_uio;
1858 struct ucred *cred = ap->a_cred;
1859 struct fuse_filehandle *fufh = NULL;
1860 struct mount *mp = vnode_mount(vp);
1861 struct fuse_iov cookediov;
1866 bool closefufh = false;
1867 pid_t pid = curthread->td_proc->p_pid;
1871 if (fuse_isdeadfs(vp)) {
1874 if ( /* XXXIP ((uio_iovcnt(uio) > 1)) || */
1875 (uio_resid(uio) < sizeof(struct dirent))) {
1879 tresid = uio->uio_resid;
1880 err = fuse_filehandle_get_dir(vp, &fufh, cred, pid);
1881 if (err == EBADF && mp->mnt_flag & MNT_EXPORTED) {
1882 KASSERT(fuse_get_mpdata(mp)->dataflags
1883 & FSESS_NO_OPENDIR_SUPPORT,
1884 ("FUSE file systems that don't set "
1885 "FUSE_NO_OPENDIR_SUPPORT should not be exported"));
1887 * nfsd will do VOP_READDIR without first doing VOP_OPEN. We
1888 * must implicitly open the directory here.
1890 err = fuse_filehandle_open(vp, FREAD, &fufh, curthread, cred);
1895 if (ap->a_ncookies != NULL) {
1896 ncookies = uio->uio_resid /
1897 (offsetof(struct dirent, d_name) + 4) + 1;
1898 cookies = malloc(ncookies * sizeof(*cookies), M_TEMP, M_WAITOK);
1899 *ap->a_ncookies = ncookies;
1900 *ap->a_cookies = cookies;
1905 #define DIRCOOKEDSIZE FUSE_DIRENT_ALIGN(FUSE_NAME_OFFSET + MAXNAMLEN + 1)
1906 fiov_init(&cookediov, DIRCOOKEDSIZE);
1908 err = fuse_internal_readdir(vp, uio, fufh, &cookediov,
1909 &ncookies, cookies);
1911 fiov_teardown(&cookediov);
1913 fuse_filehandle_close(vp, fufh, curthread, cred);
1915 if (ap->a_ncookies != NULL) {
1917 *ap->a_ncookies -= ncookies;
1919 free(*ap->a_cookies, M_TEMP);
1920 *ap->a_ncookies = 0;
1921 *ap->a_cookies = NULL;
1924 if (err == 0 && tresid == uio->uio_resid)
1931 struct vnop_readlink_args {
1934 struct ucred *a_cred;
1938 fuse_vnop_readlink(struct vop_readlink_args *ap)
1940 struct vnode *vp = ap->a_vp;
1941 struct uio *uio = ap->a_uio;
1942 struct ucred *cred = ap->a_cred;
1944 struct fuse_dispatcher fdi;
1947 if (fuse_isdeadfs(vp)) {
1950 if (!vnode_islnk(vp)) {
1953 fdisp_init(&fdi, 0);
1954 err = fdisp_simple_putget_vp(&fdi, FUSE_READLINK, vp, curthread, cred);
1958 if (((char *)fdi.answ)[0] == '/' &&
1959 fuse_get_mpdata(vnode_mount(vp))->dataflags & FSESS_PUSH_SYMLINKS_IN) {
1960 char *mpth = vnode_mount(vp)->mnt_stat.f_mntonname;
1962 err = uiomove(mpth, strlen(mpth), uio);
1965 err = uiomove(fdi.answ, fdi.iosize, uio);
1968 fdisp_destroy(&fdi);
1973 struct vnop_reclaim_args {
1978 fuse_vnop_reclaim(struct vop_reclaim_args *ap)
1980 struct vnode *vp = ap->a_vp;
1981 struct thread *td = curthread;
1982 struct fuse_vnode_data *fvdat = VTOFUD(vp);
1983 struct fuse_filehandle *fufh, *fufh_tmp;
1986 panic("FUSE: no vnode data during recycling");
1988 LIST_FOREACH_SAFE(fufh, &fvdat->handles, next, fufh_tmp) {
1989 printf("FUSE: vnode being reclaimed with open fufh "
1990 "(type=%#x)", fufh->fufh_type);
1991 fuse_filehandle_close(vp, fufh, td, NULL);
1994 if (VTOI(vp) == 1) {
1996 * Don't send FUSE_FORGET for the root inode, because
1997 * we never send FUSE_LOOKUP for it (see
1998 * fuse_vfsop_root) and we don't want the server to see
1999 * mismatched lookup counts.
2001 struct fuse_data *data;
2002 struct vnode *vroot;
2004 data = fuse_get_mpdata(vnode_mount(vp));
2006 vroot = data->vroot;
2011 } else if (!fuse_isdeadfs(vp) && fvdat->nlookup > 0) {
2012 fuse_internal_forget_send(vnode_mount(vp), td, NULL, VTOI(vp),
2016 vfs_hash_remove(vp);
2017 fuse_vnode_destroy(vp);
2023 struct vnop_remove_args {
2024 struct vnode *a_dvp;
2026 struct componentname *a_cnp;
2030 fuse_vnop_remove(struct vop_remove_args *ap)
2032 struct vnode *dvp = ap->a_dvp;
2033 struct vnode *vp = ap->a_vp;
2034 struct componentname *cnp = ap->a_cnp;
2038 if (fuse_isdeadfs(vp)) {
2041 if (vnode_isdir(vp)) {
2045 err = fuse_internal_remove(dvp, vp, cnp, FUSE_UNLINK);
2051 struct vnop_rename_args {
2052 struct vnode *a_fdvp;
2053 struct vnode *a_fvp;
2054 struct componentname *a_fcnp;
2055 struct vnode *a_tdvp;
2056 struct vnode *a_tvp;
2057 struct componentname *a_tcnp;
2061 fuse_vnop_rename(struct vop_rename_args *ap)
2063 struct vnode *fdvp = ap->a_fdvp;
2064 struct vnode *fvp = ap->a_fvp;
2065 struct componentname *fcnp = ap->a_fcnp;
2066 struct vnode *tdvp = ap->a_tdvp;
2067 struct vnode *tvp = ap->a_tvp;
2068 struct componentname *tcnp = ap->a_tcnp;
2069 struct fuse_data *data;
2070 bool newparent = fdvp != tdvp;
2071 bool isdir = fvp->v_type == VDIR;
2074 if (fuse_isdeadfs(fdvp)) {
2077 if (fvp->v_mount != tdvp->v_mount ||
2078 (tvp && fvp->v_mount != tvp->v_mount)) {
2079 SDT_PROBE2(fusefs, , vnops, trace, 1, "cross-device rename");
2086 * FUSE library is expected to check if target directory is not
2087 * under the source directory in the file system tree.
2088 * Linux performs this check at VFS level.
2091 * If source is a directory, and it will get a new parent, user must
2092 * have write permission to it, so ".." can be modified.
2094 data = fuse_get_mpdata(vnode_mount(tdvp));
2095 if (data->dataflags & FSESS_DEFAULT_PERMISSIONS && isdir && newparent) {
2096 err = fuse_internal_access(fvp, VWRITE,
2097 curthread, tcnp->cn_cred);
2101 sx_xlock(&data->rename_lock);
2102 err = fuse_internal_rename(fdvp, fcnp, tdvp, tcnp);
2105 fuse_vnode_setparent(fvp, tdvp);
2107 fuse_vnode_setparent(tvp, NULL);
2109 sx_unlock(&data->rename_lock);
2111 if (tvp != NULL && tvp != fvp) {
2114 if (vnode_isdir(fvp)) {
2115 if (((tvp != NULL) && vnode_isdir(tvp)) || vnode_isdir(fvp)) {
2136 struct vnop_rmdir_args {
2137 struct vnode *a_dvp;
2139 struct componentname *a_cnp;
2143 fuse_vnop_rmdir(struct vop_rmdir_args *ap)
2145 struct vnode *dvp = ap->a_dvp;
2146 struct vnode *vp = ap->a_vp;
2150 if (fuse_isdeadfs(vp)) {
2153 if (VTOFUD(vp) == VTOFUD(dvp)) {
2156 err = fuse_internal_remove(dvp, vp, ap->a_cnp, FUSE_RMDIR);
2162 struct vnop_setattr_args {
2164 struct vattr *a_vap;
2165 struct ucred *a_cred;
2166 struct thread *a_td;
2170 fuse_vnop_setattr(struct vop_setattr_args *ap)
2172 struct vnode *vp = ap->a_vp;
2173 struct vattr *vap = ap->a_vap;
2174 struct ucred *cred = ap->a_cred;
2175 struct thread *td = curthread;
2177 struct fuse_data *data;
2178 struct vattr old_va;
2181 accmode_t accmode = 0;
2183 bool drop_suid = false;
2186 mp = vnode_mount(vp);
2187 data = fuse_get_mpdata(mp);
2188 dataflags = data->dataflags;
2189 checkperm = dataflags & FSESS_DEFAULT_PERMISSIONS;
2190 if (cred->cr_ngroups > 0)
2191 cr_gid = cred->cr_groups[0];
2195 if (fuse_isdeadfs(vp)) {
2199 if (vap->va_uid != (uid_t)VNOVAL) {
2201 /* Only root may change a file's owner */
2202 err = priv_check_cred(cred, PRIV_VFS_CHOWN);
2204 /* As a special case, allow the null chown */
2205 err2 = fuse_internal_getattr(vp, &old_va, cred,
2209 if (vap->va_uid != old_va.va_uid)
2219 if (vap->va_gid != (gid_t)VNOVAL) {
2220 if (checkperm && priv_check_cred(cred, PRIV_VFS_CHOWN))
2222 if (checkperm && !groupmember(vap->va_gid, cred))
2225 * Non-root users may only chgrp to one of their own
2228 err = priv_check_cred(cred, PRIV_VFS_CHOWN);
2230 /* As a special case, allow the null chgrp */
2231 err2 = fuse_internal_getattr(vp, &old_va, cred,
2235 if (vap->va_gid != old_va.va_gid)
2243 if (vap->va_size != VNOVAL) {
2244 switch (vp->v_type) {
2249 if (vfs_isrdonly(mp))
2254 * According to POSIX, the result is unspecified
2255 * for file types other than regular files,
2256 * directories and shared memory objects. We
2257 * don't support shared memory objects in the file
2258 * system, and have dubious support for truncating
2259 * symlinks. Just ignore the request in other cases.
2263 /* Don't set accmode. Permission to trunc is checked upstack */
2265 if (vap->va_atime.tv_sec != VNOVAL || vap->va_mtime.tv_sec != VNOVAL) {
2266 if (vap->va_vaflags & VA_UTIMES_NULL)
2272 if (vap->va_mode != (mode_t)VNOVAL)
2273 vap->va_mode &= ~(S_ISUID | S_ISGID);
2275 err = fuse_internal_getattr(vp, &old_va, cred, td);
2278 vap->va_mode = old_va.va_mode & ~(S_ISUID | S_ISGID);
2281 if (vap->va_mode != (mode_t)VNOVAL) {
2282 /* Only root may set the sticky bit on non-directories */
2283 if (checkperm && vp->v_type != VDIR && (vap->va_mode & S_ISTXT)
2284 && priv_check_cred(cred, PRIV_VFS_STICKYFILE))
2286 if (checkperm && (vap->va_mode & S_ISGID)) {
2287 err = fuse_internal_getattr(vp, &old_va, cred, td);
2290 if (!groupmember(old_va.va_gid, cred)) {
2291 err = priv_check_cred(cred, PRIV_VFS_SETGID);
2299 if (vfs_isrdonly(mp))
2303 err = fuse_internal_access(vp, accmode, td, cred);
2310 return fuse_internal_setattr(vp, vap, td, cred);
2314 struct vnop_strategy_args {
2320 fuse_vnop_strategy(struct vop_strategy_args *ap)
2322 struct vnode *vp = ap->a_vp;
2323 struct buf *bp = ap->a_bp;
2325 if (!vp || fuse_isdeadfs(vp)) {
2326 bp->b_ioflags |= BIO_ERROR;
2327 bp->b_error = ENXIO;
2333 * VOP_STRATEGY always returns zero and signals error via bp->b_ioflags.
2334 * fuse_io_strategy sets bp's error fields
2336 (void)fuse_io_strategy(vp, bp);
2342 struct vnop_symlink_args {
2343 struct vnode *a_dvp;
2344 struct vnode **a_vpp;
2345 struct componentname *a_cnp;
2346 struct vattr *a_vap;
2351 fuse_vnop_symlink(struct vop_symlink_args *ap)
2353 struct vnode *dvp = ap->a_dvp;
2354 struct vnode **vpp = ap->a_vpp;
2355 struct componentname *cnp = ap->a_cnp;
2356 const char *target = ap->a_target;
2358 struct fuse_dispatcher fdi;
2363 if (fuse_isdeadfs(dvp)) {
2367 * Unlike the other creator type calls, here we have to create a message
2368 * where the name of the new entry comes first, and the data describing
2369 * the entry comes second.
2370 * Hence we can't rely on our handy fuse_internal_newentry() routine,
2371 * but put together the message manually and just call the core part.
2374 len = strlen(target) + 1;
2375 fdisp_init(&fdi, len + cnp->cn_namelen + 1);
2376 fdisp_make_vp(&fdi, FUSE_SYMLINK, dvp, curthread, NULL);
2378 memcpy(fdi.indata, cnp->cn_nameptr, cnp->cn_namelen);
2379 ((char *)fdi.indata)[cnp->cn_namelen] = '\0';
2380 memcpy((char *)fdi.indata + cnp->cn_namelen + 1, target, len);
2382 err = fuse_internal_newentry_core(dvp, vpp, cnp, VLNK, &fdi);
2383 fdisp_destroy(&fdi);
2388 struct vnop_write_args {
2392 struct ucred *a_cred;
2396 fuse_vnop_write(struct vop_write_args *ap)
2398 struct vnode *vp = ap->a_vp;
2399 struct uio *uio = ap->a_uio;
2400 int ioflag = ap->a_ioflag;
2401 struct ucred *cred = ap->a_cred;
2402 pid_t pid = curthread->td_proc->p_pid;
2403 struct fuse_filehandle *fufh;
2405 bool closefufh = false, directio;
2407 MPASS(vp->v_type == VREG || vp->v_type == VDIR);
2409 if (fuse_isdeadfs(vp)) {
2413 if (VTOFUD(vp)->flag & FN_DIRECTIO) {
2414 ioflag |= IO_DIRECT;
2417 err = fuse_filehandle_getrw(vp, FWRITE, &fufh, cred, pid);
2418 if (err == EBADF && vnode_mount(vp)->mnt_flag & MNT_EXPORTED) {
2420 * nfsd will do I/O without first doing VOP_OPEN. We
2421 * must implicitly open the file here
2423 err = fuse_filehandle_open(vp, FWRITE, &fufh, curthread, cred);
2427 SDT_PROBE3(fusefs, , vnops, filehandles_closed, vp, uio, cred);
2432 * Ideally, when the daemon asks for direct io at open time, the
2433 * standard file flag should be set according to this, so that would
2434 * just change the default mode, which later on could be changed via
2436 * But this doesn't work, the O_DIRECT flag gets cleared at some point
2437 * (don't know where). So to make any use of the Fuse direct_io option,
2438 * we hardwire it into the file's private data (similarly to Linux,
2441 directio = (ioflag & IO_DIRECT) || !fsess_opt_datacache(vnode_mount(vp));
2443 fuse_vnode_update(vp, FN_MTIMECHANGE | FN_CTIMECHANGE);
2445 off_t start, end, filesize;
2446 bool pages = (ioflag & IO_VMIO) != 0;
2448 SDT_PROBE2(fusefs, , vnops, trace, 1, "direct write of vnode");
2450 err = fuse_vnode_size(vp, &filesize, cred, curthread);
2454 start = uio->uio_offset;
2455 end = start + uio->uio_resid;
2457 err = fuse_inval_buf_range(vp, filesize, start,
2462 err = fuse_write_directbackend(vp, uio, cred, fufh,
2463 filesize, ioflag, pages);
2465 SDT_PROBE2(fusefs, , vnops, trace, 1,
2466 "buffered write of vnode");
2467 if (!fsess_opt_writeback(vnode_mount(vp)))
2469 err = fuse_write_biobackend(vp, uio, cred, fufh, ioflag, pid);
2471 fuse_internal_clear_suid_on_write(vp, cred, uio->uio_td);
2475 fuse_filehandle_close(vp, fufh, curthread, cred);
2481 fuse_gbp_getblkno(struct vnode *vp, vm_ooffset_t off)
2483 const int biosize = fuse_iosize(vp);
2485 return (off / biosize);
2489 fuse_gbp_getblksz(struct vnode *vp, daddr_t lbn, long *blksz)
2493 const int biosize = fuse_iosize(vp);
2495 err = fuse_vnode_size(vp, &filesize, NULL, NULL);
2497 /* This will turn into a SIGBUS */
2499 } else if ((off_t)lbn * biosize >= filesize) {
2501 } else if ((off_t)(lbn + 1) * biosize > filesize) {
2502 *blksz = filesize - (off_t)lbn *biosize;
2510 struct vnop_getpages_args {
2518 fuse_vnop_getpages(struct vop_getpages_args *ap)
2520 struct vnode *vp = ap->a_vp;
2522 if (!fsess_opt_mmap(vnode_mount(vp))) {
2523 SDT_PROBE2(fusefs, , vnops, trace, 1,
2524 "called on non-cacheable vnode??\n");
2525 return (VM_PAGER_ERROR);
2528 return (vfs_bio_getpages(vp, ap->a_m, ap->a_count, ap->a_rbehind,
2529 ap->a_rahead, fuse_gbp_getblkno, fuse_gbp_getblksz));
2532 static const char extattr_namespace_separator = '.';
2535 struct vop_getextattr_args {
2536 struct vop_generic_args a_gen;
2538 int a_attrnamespace;
2542 struct ucred *a_cred;
2543 struct thread *a_td;
2547 fuse_vnop_getextattr(struct vop_getextattr_args *ap)
2549 struct vnode *vp = ap->a_vp;
2550 struct uio *uio = ap->a_uio;
2551 struct fuse_dispatcher fdi;
2552 struct fuse_getxattr_in *get_xattr_in;
2553 struct fuse_getxattr_out *get_xattr_out;
2554 struct mount *mp = vnode_mount(vp);
2555 struct thread *td = ap->a_td;
2556 struct ucred *cred = ap->a_cred;
2562 if (fuse_isdeadfs(vp))
2565 if (fsess_not_impl(mp, FUSE_GETXATTR))
2568 err = fuse_extattr_check_cred(vp, ap->a_attrnamespace, cred, td, VREAD);
2572 /* Default to looking for user attributes. */
2573 if (ap->a_attrnamespace == EXTATTR_NAMESPACE_SYSTEM)
2574 prefix = EXTATTR_NAMESPACE_SYSTEM_STRING;
2576 prefix = EXTATTR_NAMESPACE_USER_STRING;
2578 len = strlen(prefix) + sizeof(extattr_namespace_separator) +
2579 strlen(ap->a_name) + 1;
2581 fdisp_init(&fdi, len + sizeof(*get_xattr_in));
2582 fdisp_make_vp(&fdi, FUSE_GETXATTR, vp, td, cred);
2584 get_xattr_in = fdi.indata;
2586 * Check to see whether we're querying the available size or
2587 * issuing the actual request. If we pass in 0, we get back struct
2588 * fuse_getxattr_out. If we pass in a non-zero size, we get back
2589 * that much data, without the struct fuse_getxattr_out header.
2592 get_xattr_in->size = 0;
2594 get_xattr_in->size = uio->uio_resid;
2596 attr_str = (char *)fdi.indata + sizeof(*get_xattr_in);
2597 snprintf(attr_str, len, "%s%c%s", prefix, extattr_namespace_separator,
2600 err = fdisp_wait_answ(&fdi);
2602 if (err == ENOSYS) {
2603 fsess_set_notimpl(mp, FUSE_GETXATTR);
2609 get_xattr_out = fdi.answ;
2611 if (ap->a_size != NULL)
2612 *ap->a_size = get_xattr_out->size;
2615 err = uiomove(fdi.answ, fdi.iosize, uio);
2618 fdisp_destroy(&fdi);
2623 struct vop_setextattr_args {
2624 struct vop_generic_args a_gen;
2626 int a_attrnamespace;
2629 struct ucred *a_cred;
2630 struct thread *a_td;
2634 fuse_vnop_setextattr(struct vop_setextattr_args *ap)
2636 struct vnode *vp = ap->a_vp;
2637 struct uio *uio = ap->a_uio;
2638 struct fuse_dispatcher fdi;
2639 struct fuse_setxattr_in *set_xattr_in;
2640 struct mount *mp = vnode_mount(vp);
2641 struct thread *td = ap->a_td;
2642 struct ucred *cred = ap->a_cred;
2648 if (fuse_isdeadfs(vp))
2651 if (fsess_not_impl(mp, FUSE_SETXATTR))
2654 if (vfs_isrdonly(mp))
2657 /* Deleting xattrs must use VOP_DELETEEXTATTR instead */
2658 if (ap->a_uio == NULL) {
2660 * If we got here as fallback from VOP_DELETEEXTATTR, then
2661 * return EOPNOTSUPP.
2663 if (fsess_not_impl(mp, FUSE_REMOVEXATTR))
2664 return (EOPNOTSUPP);
2669 err = fuse_extattr_check_cred(vp, ap->a_attrnamespace, cred, td,
2674 /* Default to looking for user attributes. */
2675 if (ap->a_attrnamespace == EXTATTR_NAMESPACE_SYSTEM)
2676 prefix = EXTATTR_NAMESPACE_SYSTEM_STRING;
2678 prefix = EXTATTR_NAMESPACE_USER_STRING;
2680 len = strlen(prefix) + sizeof(extattr_namespace_separator) +
2681 strlen(ap->a_name) + 1;
2683 fdisp_init(&fdi, len + sizeof(*set_xattr_in) + uio->uio_resid);
2684 fdisp_make_vp(&fdi, FUSE_SETXATTR, vp, td, cred);
2686 set_xattr_in = fdi.indata;
2687 set_xattr_in->size = uio->uio_resid;
2689 attr_str = (char *)fdi.indata + sizeof(*set_xattr_in);
2690 snprintf(attr_str, len, "%s%c%s", prefix, extattr_namespace_separator,
2693 err = uiomove((char *)fdi.indata + sizeof(*set_xattr_in) + len,
2694 uio->uio_resid, uio);
2699 err = fdisp_wait_answ(&fdi);
2701 if (err == ENOSYS) {
2702 fsess_set_notimpl(mp, FUSE_SETXATTR);
2705 if (err == ERESTART) {
2706 /* Can't restart after calling uiomove */
2711 fdisp_destroy(&fdi);
2716 * The Linux / FUSE extended attribute list is simply a collection of
2717 * NUL-terminated strings. The FreeBSD extended attribute list is a single
2718 * byte length followed by a non-NUL terminated string. So, this allows
2719 * conversion of the Linux / FUSE format to the FreeBSD format in place.
2720 * Linux attribute names are reported with the namespace as a prefix (e.g.
2721 * "user.attribute_name"), but in FreeBSD they are reported without the
2722 * namespace prefix (e.g. "attribute_name"). So, we're going from:
2724 * user.attr_name1\0user.attr_name2\0
2728 * <num>attr_name1<num>attr_name2
2730 * Where "<num>" is a single byte number of characters in the attribute name.
2733 * prefix - exattr namespace prefix string
2734 * list, list_len - input list with namespace prefixes
2735 * bsd_list, bsd_list_len - output list compatible with bsd vfs
2738 fuse_xattrlist_convert(char *prefix, const char *list, int list_len,
2739 char *bsd_list, int *bsd_list_len)
2741 int len, pos, dist_to_next, prefix_len;
2745 prefix_len = strlen(prefix);
2747 while (pos < list_len && list[pos] != '\0') {
2748 dist_to_next = strlen(&list[pos]) + 1;
2749 if (bcmp(&list[pos], prefix, prefix_len) == 0 &&
2750 list[pos + prefix_len] == extattr_namespace_separator) {
2751 len = dist_to_next -
2752 (prefix_len + sizeof(extattr_namespace_separator)) - 1;
2753 if (len >= EXTATTR_MAXNAMELEN)
2754 return (ENAMETOOLONG);
2756 bsd_list[*bsd_list_len] = len;
2757 memcpy(&bsd_list[*bsd_list_len + 1],
2758 &list[pos + prefix_len +
2759 sizeof(extattr_namespace_separator)], len);
2761 *bsd_list_len += len + 1;
2764 pos += dist_to_next;
2771 * List extended attributes
2773 * The FUSE_LISTXATTR operation is based on Linux's listxattr(2) syscall, which
2774 * has a number of differences compared to its FreeBSD equivalent,
2775 * extattr_list_file:
2777 * - FUSE_LISTXATTR returns all extended attributes across all namespaces,
2778 * whereas listxattr(2) only returns attributes for a single namespace
2779 * - FUSE_LISTXATTR prepends each attribute name with "namespace."
2780 * - If the provided buffer is not large enough to hold the result,
2781 * FUSE_LISTXATTR should return ERANGE, whereas listxattr is expected to
2782 * return as many results as will fit.
2785 struct vop_listextattr_args {
2786 struct vop_generic_args a_gen;
2788 int a_attrnamespace;
2791 struct ucred *a_cred;
2792 struct thread *a_td;
2796 fuse_vnop_listextattr(struct vop_listextattr_args *ap)
2798 struct vnode *vp = ap->a_vp;
2799 struct uio *uio = ap->a_uio;
2800 struct fuse_dispatcher fdi;
2801 struct fuse_listxattr_in *list_xattr_in;
2802 struct fuse_listxattr_out *list_xattr_out;
2803 struct mount *mp = vnode_mount(vp);
2804 struct thread *td = ap->a_td;
2805 struct ucred *cred = ap->a_cred;
2807 char *bsd_list = NULL;
2813 if (fuse_isdeadfs(vp))
2816 if (fsess_not_impl(mp, FUSE_LISTXATTR))
2819 err = fuse_extattr_check_cred(vp, ap->a_attrnamespace, cred, td, VREAD);
2824 * Add space for a NUL and the period separator if enabled.
2825 * Default to looking for user attributes.
2827 if (ap->a_attrnamespace == EXTATTR_NAMESPACE_SYSTEM)
2828 prefix = EXTATTR_NAMESPACE_SYSTEM_STRING;
2830 prefix = EXTATTR_NAMESPACE_USER_STRING;
2832 fdisp_init(&fdi, sizeof(*list_xattr_in));
2833 fdisp_make_vp(&fdi, FUSE_LISTXATTR, vp, td, cred);
2836 * Retrieve Linux / FUSE compatible list size.
2838 list_xattr_in = fdi.indata;
2839 list_xattr_in->size = 0;
2841 err = fdisp_wait_answ(&fdi);
2843 if (err == ENOSYS) {
2844 fsess_set_notimpl(mp, FUSE_LISTXATTR);
2850 list_xattr_out = fdi.answ;
2851 linux_list_len = list_xattr_out->size;
2852 if (linux_list_len == 0) {
2853 if (ap->a_size != NULL)
2854 *ap->a_size = linux_list_len;
2859 * Retrieve Linux / FUSE compatible list values.
2861 fdisp_refresh_vp(&fdi, FUSE_LISTXATTR, vp, td, cred);
2862 list_xattr_in = fdi.indata;
2863 list_xattr_in->size = linux_list_len;
2865 err = fdisp_wait_answ(&fdi);
2866 if (err == ERANGE) {
2868 * Race detected. The attribute list must've grown since the
2869 * first FUSE_LISTXATTR call. Start over. Go all the way back
2870 * to userland so we can process signals, if necessary, before
2875 } else if (err != 0)
2878 linux_list = fdi.answ;
2879 /* FUSE doesn't allow the server to return more data than requested */
2880 if (fdi.iosize > linux_list_len) {
2881 struct fuse_data *data = fuse_get_mpdata(mp);
2883 fuse_warn(data, FSESS_WARN_LSEXTATTR_LONG,
2885 "more extended attribute data than requested; "
2886 "should've returned ERANGE instead.");
2888 /* But returning less data is fine */
2889 linux_list_len = fdi.iosize;
2893 * Retrieve the BSD compatible list values.
2894 * The Linux / FUSE attribute list format isn't the same
2895 * as FreeBSD's format. So we need to transform it into
2896 * FreeBSD's format before giving it to the user.
2898 bsd_list = malloc(linux_list_len, M_TEMP, M_WAITOK);
2899 err = fuse_xattrlist_convert(prefix, linux_list, linux_list_len,
2900 bsd_list, &bsd_list_len);
2904 if (ap->a_size != NULL)
2905 *ap->a_size = bsd_list_len;
2908 err = uiomove(bsd_list, bsd_list_len, uio);
2911 free(bsd_list, M_TEMP);
2912 fdisp_destroy(&fdi);
2917 struct vop_deallocate_args {
2918 struct vop_generic_args a_gen;
2924 struct ucred *a_cred;
2928 fuse_vnop_deallocate(struct vop_deallocate_args *ap)
2930 struct vnode *vp = ap->a_vp;
2931 struct mount *mp = vnode_mount(vp);
2932 struct fuse_filehandle *fufh;
2933 struct fuse_dispatcher fdi;
2934 struct fuse_fallocate_in *ffi;
2935 struct ucred *cred = ap->a_cred;
2936 pid_t pid = curthread->td_proc->p_pid;
2937 off_t *len = ap->a_len;
2938 off_t *offset = ap->a_offset;
2939 int ioflag = ap->a_ioflag;
2942 bool closefufh = false;
2944 if (fuse_isdeadfs(vp))
2947 if (vfs_isrdonly(mp))
2950 if (fsess_not_impl(mp, FUSE_FALLOCATE))
2953 err = fuse_filehandle_getrw(vp, FWRITE, &fufh, cred, pid);
2954 if (err == EBADF && vnode_mount(vp)->mnt_flag & MNT_EXPORTED) {
2956 * nfsd will do I/O without first doing VOP_OPEN. We
2957 * must implicitly open the file here
2959 err = fuse_filehandle_open(vp, FWRITE, &fufh, curthread, cred);
2965 fuse_vnode_update(vp, FN_MTIMECHANGE | FN_CTIMECHANGE);
2967 err = fuse_vnode_size(vp, &filesize, cred, curthread);
2970 fuse_inval_buf_range(vp, filesize, *offset, *offset + *len);
2972 fdisp_init(&fdi, sizeof(*ffi));
2973 fdisp_make_vp(&fdi, FUSE_FALLOCATE, vp, curthread, cred);
2975 ffi->fh = fufh->fh_id;
2976 ffi->offset = *offset;
2979 * FreeBSD's fspacectl is equivalent to Linux's fallocate with
2980 * mode == FALLOC_FL_PUNCH_HOLE | FALLOC_FL_KEEP_SIZE
2982 ffi->mode = FUSE_FALLOC_FL_PUNCH_HOLE | FUSE_FALLOC_FL_KEEP_SIZE;
2983 err = fdisp_wait_answ(&fdi);
2985 if (err == ENOSYS) {
2986 fsess_set_notimpl(mp, FUSE_FALLOCATE);
2988 } else if (err == EOPNOTSUPP) {
2990 * The file system server does not support FUSE_FALLOCATE with
2991 * the supplied mode for this particular file.
2996 * Clip the returned offset to EoF. Do it here rather than
2997 * before FUSE_FALLOCATE just in case the kernel's cached file
2998 * size is out of date. Unfortunately, FUSE does not return
2999 * any information about filesize from that operation.
3001 *offset = MIN(*offset + *len, filesize);
3003 fuse_vnode_undirty_cached_timestamps(vp, false);
3004 fuse_internal_clear_suid_on_write(vp, cred, curthread);
3006 if (ioflag & IO_SYNC)
3007 err = fuse_internal_fsync(vp, curthread, MNT_WAIT,
3013 fuse_filehandle_close(vp, fufh, curthread, cred);
3019 fuse_filehandle_close(vp, fufh, curthread, cred);
3021 return (vop_stddeallocate(ap));
3025 struct vop_deleteextattr_args {
3026 struct vop_generic_args a_gen;
3028 int a_attrnamespace;
3030 struct ucred *a_cred;
3031 struct thread *a_td;
3035 fuse_vnop_deleteextattr(struct vop_deleteextattr_args *ap)
3037 struct vnode *vp = ap->a_vp;
3038 struct fuse_dispatcher fdi;
3039 struct mount *mp = vnode_mount(vp);
3040 struct thread *td = ap->a_td;
3041 struct ucred *cred = ap->a_cred;
3047 if (fuse_isdeadfs(vp))
3050 if (fsess_not_impl(mp, FUSE_REMOVEXATTR))
3053 if (vfs_isrdonly(mp))
3056 err = fuse_extattr_check_cred(vp, ap->a_attrnamespace, cred, td,
3061 /* Default to looking for user attributes. */
3062 if (ap->a_attrnamespace == EXTATTR_NAMESPACE_SYSTEM)
3063 prefix = EXTATTR_NAMESPACE_SYSTEM_STRING;
3065 prefix = EXTATTR_NAMESPACE_USER_STRING;
3067 len = strlen(prefix) + sizeof(extattr_namespace_separator) +
3068 strlen(ap->a_name) + 1;
3070 fdisp_init(&fdi, len);
3071 fdisp_make_vp(&fdi, FUSE_REMOVEXATTR, vp, td, cred);
3073 attr_str = fdi.indata;
3074 snprintf(attr_str, len, "%s%c%s", prefix, extattr_namespace_separator,
3077 err = fdisp_wait_answ(&fdi);
3078 if (err == ENOSYS) {
3079 fsess_set_notimpl(mp, FUSE_REMOVEXATTR);
3083 fdisp_destroy(&fdi);
3088 struct vnop_print_args {
3093 fuse_vnop_print(struct vop_print_args *ap)
3095 struct fuse_vnode_data *fvdat = VTOFUD(ap->a_vp);
3097 printf("nodeid: %ju, parent nodeid: %ju, nlookup: %ju, flag: %#x\n",
3098 (uintmax_t)VTOILLU(ap->a_vp), (uintmax_t)fvdat->parent_nid,
3099 (uintmax_t)fvdat->nlookup,
3106 * Get an NFS filehandle for a FUSE file.
3108 * This will only work for FUSE file systems that guarantee the uniqueness of
3109 * nodeid:generation, which most don't.
3113 IN struct vnode *a_vp;
3114 IN struct fid *a_fhp;
3118 fuse_vnop_vptofh(struct vop_vptofh_args *ap)
3120 struct vnode *vp = ap->a_vp;
3121 struct fuse_vnode_data *fvdat = VTOFUD(vp);
3122 struct fuse_fid *fhp = (struct fuse_fid *)(ap->a_fhp);
3123 _Static_assert(sizeof(struct fuse_fid) <= sizeof(struct fid),
3124 "FUSE fid type is too big");
3125 struct mount *mp = vnode_mount(vp);
3126 struct fuse_data *data = fuse_get_mpdata(mp);
3130 if (!(data->dataflags & FSESS_EXPORT_SUPPORT)) {
3131 /* NFS requires lookups for "." and ".." */
3132 SDT_PROBE2(fusefs, , vnops, trace, 1,
3133 "VOP_VPTOFH without FUSE_EXPORT_SUPPORT");
3136 if ((mp->mnt_flag & MNT_EXPORTED) &&
3137 !(data->dataflags & FSESS_NO_OPENDIR_SUPPORT))
3140 * NFS is stateless, so nfsd must reopen a directory on every
3141 * call to VOP_READDIR, passing in the d_off field from the
3142 * final dirent of the previous invocation. But without
3143 * FUSE_NO_OPENDIR_SUPPORT, the FUSE protocol does not
3144 * guarantee that d_off will be valid after a directory is
3145 * closed and reopened. So prohibit exporting FUSE file
3146 * systems that don't set that flag.
3148 * But userspace NFS servers don't have this problem.
3150 SDT_PROBE2(fusefs, , vnops, trace, 1,
3151 "VOP_VPTOFH without FUSE_NO_OPENDIR_SUPPORT");
3155 err = fuse_internal_getattr(vp, &va, curthread->td_ucred, curthread);
3159 /*ip = VTOI(ap->a_vp);*/
3160 /*ufhp = (struct ufid *)ap->a_fhp;*/
3161 fhp->len = sizeof(struct fuse_fid);
3162 fhp->nid = fvdat->nid;
3163 if (fvdat->generation <= UINT32_MAX)
3164 fhp->gen = fvdat->generation;