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,
221 u_long fuse_lookup_cache_hits = 0;
223 SYSCTL_ULONG(_vfs_fusefs, OID_AUTO, lookup_cache_hits, CTLFLAG_RD,
224 &fuse_lookup_cache_hits, 0, "number of positive cache hits in lookup");
226 u_long fuse_lookup_cache_misses = 0;
228 SYSCTL_ULONG(_vfs_fusefs, OID_AUTO, lookup_cache_misses, CTLFLAG_RD,
229 &fuse_lookup_cache_misses, 0, "number of cache misses in lookup");
232 * XXX: This feature is highly experimental and can bring to instabilities,
233 * needs revisiting before to be enabled by default.
235 static int fuse_reclaim_revoked = 0;
237 SYSCTL_INT(_vfs_fusefs, OID_AUTO, reclaim_revoked, CTLFLAG_RW,
238 &fuse_reclaim_revoked, 0, "");
240 uma_zone_t fuse_pbuf_zone;
242 #define fuse_vm_page_lock(m) vm_page_lock((m));
243 #define fuse_vm_page_unlock(m) vm_page_unlock((m));
244 #define fuse_vm_page_lock_queues() ((void)0)
245 #define fuse_vm_page_unlock_queues() ((void)0)
247 /* Check permission for extattr operations, much like extattr_check_cred */
249 fuse_extattr_check_cred(struct vnode *vp, int ns, struct ucred *cred,
250 struct thread *td, accmode_t accmode)
252 struct mount *mp = vnode_mount(vp);
253 struct fuse_data *data = fuse_get_mpdata(mp);
256 * Kernel-invoked always succeeds.
262 * Do not allow privileged processes in jail to directly manipulate
266 case EXTATTR_NAMESPACE_SYSTEM:
267 if (data->dataflags & FSESS_DEFAULT_PERMISSIONS) {
268 return (priv_check_cred(cred, PRIV_VFS_EXTATTR_SYSTEM));
271 case EXTATTR_NAMESPACE_USER:
272 return (fuse_internal_access(vp, accmode, td, cred));
278 /* Get a filehandle for a directory */
280 fuse_filehandle_get_dir(struct vnode *vp, struct fuse_filehandle **fufhp,
281 struct ucred *cred, pid_t pid)
283 if (fuse_filehandle_get(vp, FREAD, fufhp, cred, pid) == 0)
285 return fuse_filehandle_get(vp, FEXEC, fufhp, cred, pid);
288 /* Send FUSE_FLUSH for this vnode */
290 fuse_flush(struct vnode *vp, struct ucred *cred, pid_t pid, int fflag)
292 struct fuse_flush_in *ffi;
293 struct fuse_filehandle *fufh;
294 struct fuse_dispatcher fdi;
295 struct thread *td = curthread;
296 struct mount *mp = vnode_mount(vp);
299 if (!fsess_isimpl(vnode_mount(vp), FUSE_FLUSH))
302 err = fuse_filehandle_getrw(vp, fflag, &fufh, cred, pid);
306 fdisp_init(&fdi, sizeof(*ffi));
307 fdisp_make_vp(&fdi, FUSE_FLUSH, vp, td, cred);
309 ffi->fh = fufh->fh_id;
311 * If the file has a POSIX lock then we're supposed to set lock_owner.
312 * If not, then lock_owner is undefined. So we may as well always set
315 ffi->lock_owner = td->td_proc->p_pid;
317 err = fdisp_wait_answ(&fdi);
319 fsess_set_notimpl(mp, FUSE_FLUSH);
326 /* Close wrapper for fifos. */
328 fuse_fifo_close(struct vop_close_args *ap)
330 return (fifo_specops.vop_close(ap));
334 struct vnop_access_args {
336 #if VOP_ACCESS_TAKES_ACCMODE_T
341 struct ucred *a_cred;
346 fuse_vnop_access(struct vop_access_args *ap)
348 struct vnode *vp = ap->a_vp;
349 int accmode = ap->a_accmode;
350 struct ucred *cred = ap->a_cred;
352 struct fuse_data *data = fuse_get_mpdata(vnode_mount(vp));
356 if (fuse_isdeadfs(vp)) {
357 if (vnode_isvroot(vp)) {
362 if (!(data->dataflags & FSESS_INITED)) {
363 if (vnode_isvroot(vp)) {
364 if (priv_check_cred(cred, PRIV_VFS_ADMIN) ||
365 (fuse_match_cred(data->daemoncred, cred) == 0)) {
371 if (vnode_islnk(vp)) {
375 err = fuse_internal_access(vp, accmode, ap->a_td, ap->a_cred);
380 * struct vop_advlock_args {
381 * struct vop_generic_args a_gen;
382 * struct vnode *a_vp;
385 * struct flock *a_fl;
390 fuse_vnop_advlock(struct vop_advlock_args *ap)
392 struct vnode *vp = ap->a_vp;
393 struct flock *fl = ap->a_fl;
394 struct thread *td = curthread;
395 struct ucred *cred = td->td_ucred;
396 pid_t pid = td->td_proc->p_pid;
397 struct fuse_filehandle *fufh;
398 struct fuse_dispatcher fdi;
399 struct fuse_lk_in *fli;
400 struct fuse_lk_out *flo;
403 int flags = ap->a_flags;
405 dataflags = fuse_get_mpdata(vnode_mount(vp))->dataflags;
407 if (fuse_isdeadfs(vp)) {
411 if (!(dataflags & FSESS_POSIX_LOCKS))
412 return vop_stdadvlock(ap);
413 /* FUSE doesn't properly support flock until protocol 7.17 */
415 return vop_stdadvlock(ap);
417 err = fuse_filehandle_get_anyflags(vp, &fufh, cred, pid);
421 fdisp_init(&fdi, sizeof(*fli));
437 fdisp_make_vp(&fdi, op, vp, td, cred);
439 fli->fh = fufh->fh_id;
440 fli->owner = fl->l_pid;
441 fli->lk.start = fl->l_start;
443 fli->lk.end = fl->l_start + fl->l_len - 1;
445 fli->lk.end = INT64_MAX;
446 fli->lk.type = fl->l_type;
447 fli->lk.pid = fl->l_pid;
449 err = fdisp_wait_answ(&fdi);
452 if (err == 0 && op == FUSE_GETLK) {
454 fl->l_type = flo->lk.type;
455 fl->l_pid = flo->lk.pid;
456 if (flo->lk.type != F_UNLCK) {
457 fl->l_start = flo->lk.start;
458 if (flo->lk.end == INT64_MAX)
461 fl->l_len = flo->lk.end - flo->lk.start + 1;
462 fl->l_start = flo->lk.start;
472 struct bufobj **a_bop;
478 fuse_vnop_bmap(struct vop_bmap_args *ap)
480 struct vnode *vp = ap->a_vp;
481 struct bufobj **bo = ap->a_bop;
482 struct thread *td = curthread;
484 struct fuse_dispatcher fdi;
485 struct fuse_bmap_in *fbi;
486 struct fuse_bmap_out *fbo;
487 struct fuse_data *data;
490 daddr_t lbn = ap->a_bn;
491 daddr_t *pbn = ap->a_bnp;
492 int *runp = ap->a_runp;
493 int *runb = ap->a_runb;
497 if (fuse_isdeadfs(vp)) {
501 mp = vnode_mount(vp);
502 data = fuse_get_mpdata(mp);
503 biosize = fuse_iosize(vp);
504 maxrun = MIN(vp->v_mount->mnt_iosize_max / biosize - 1,
505 data->max_readahead_blocks);
511 * The FUSE_BMAP operation does not include the runp and runb
512 * variables, so we must guess. Report nonzero contiguous runs so
513 * cluster_read will combine adjacent reads. It's worthwhile to reduce
514 * upcalls even if we don't know the true physical layout of the file.
516 * FUSE file systems may opt out of read clustering in two ways:
517 * * mounting with -onoclusterr
518 * * Setting max_readahead <= maxbcachebuf during FUSE_INIT
521 *runb = MIN(lbn, maxrun);
523 error = fuse_vnode_size(vp, &filesize, td->td_ucred, td);
525 *runp = MIN(MAX(0, filesize / biosize - lbn - 1),
531 if (fsess_isimpl(mp, FUSE_BMAP)) {
532 fdisp_init(&fdi, sizeof(*fbi));
533 fdisp_make_vp(&fdi, FUSE_BMAP, vp, td, td->td_ucred);
536 fbi->blocksize = biosize;
537 error = fdisp_wait_answ(&fdi);
538 if (error == ENOSYS) {
540 fsess_set_notimpl(mp, FUSE_BMAP);
544 if (error == 0 && pbn != NULL)
551 /* If the daemon doesn't support BMAP, make up a sensible default */
553 *pbn = lbn * btodb(biosize);
558 struct vop_close_args {
561 struct ucred *a_cred;
566 fuse_vnop_close(struct vop_close_args *ap)
568 struct vnode *vp = ap->a_vp;
569 struct ucred *cred = ap->a_cred;
570 int fflag = ap->a_fflag;
571 struct thread *td = ap->a_td;
572 pid_t pid = td->td_proc->p_pid;
575 if (fuse_isdeadfs(vp))
579 if (fflag & IO_NDELAY)
582 err = fuse_flush(vp, cred, pid, fflag);
583 /* TODO: close the file handle, if we're sure it's no longer used */
584 if ((VTOFUD(vp)->flag & FN_SIZECHANGE) != 0) {
585 fuse_vnode_savesize(vp, cred, td->td_proc->p_pid);
591 fdisp_make_mknod_for_fallback(
592 struct fuse_dispatcher *fdip,
593 struct componentname *cnp,
599 enum fuse_opcode *op)
601 struct fuse_mknod_in *fmni;
603 fdisp_init(fdip, sizeof(*fmni) + cnp->cn_namelen + 1);
605 fdisp_make(fdip, *op, vnode_mount(dvp), parentnid, td, cred);
609 memcpy((char *)fdip->indata + sizeof(*fmni), cnp->cn_nameptr,
611 ((char *)fdip->indata)[sizeof(*fmni) + cnp->cn_namelen] = '\0';
614 struct vnop_create_args {
616 struct vnode **a_vpp;
617 struct componentname *a_cnp;
622 fuse_vnop_create(struct vop_create_args *ap)
624 struct vnode *dvp = ap->a_dvp;
625 struct vnode **vpp = ap->a_vpp;
626 struct componentname *cnp = ap->a_cnp;
627 struct vattr *vap = ap->a_vap;
628 struct thread *td = cnp->cn_thread;
629 struct ucred *cred = cnp->cn_cred;
631 struct fuse_data *data;
632 struct fuse_create_in *fci;
633 struct fuse_entry_out *feo;
634 struct fuse_open_out *foo;
635 struct fuse_dispatcher fdi, fdi2;
636 struct fuse_dispatcher *fdip = &fdi;
637 struct fuse_dispatcher *fdip2 = NULL;
641 struct mount *mp = vnode_mount(dvp);
642 data = fuse_get_mpdata(mp);
643 uint64_t parentnid = VTOFUD(dvp)->nid;
644 mode_t mode = MAKEIMODE(vap->va_type, vap->va_mode);
648 if (fuse_isdeadfs(dvp))
651 /* FUSE expects sockets to be created with FUSE_MKNOD */
652 if (vap->va_type == VSOCK)
653 return fuse_internal_mknod(dvp, vpp, cnp, vap);
656 * VOP_CREATE doesn't tell us the open(2) flags, so we guess. Only a
657 * writable mode makes sense, and we might as well include readability
662 bzero(&fdi, sizeof(fdi));
664 if (vap->va_type != VREG)
667 if (!fsess_isimpl(mp, FUSE_CREATE) || vap->va_type == VSOCK) {
668 /* Fallback to FUSE_MKNOD/FUSE_OPEN */
669 fdisp_make_mknod_for_fallback(fdip, cnp, dvp, parentnid, td,
672 /* Use FUSE_CREATE */
676 fdisp_init(fdip, sizeof(*fci) + cnp->cn_namelen + 1);
677 fdisp_make(fdip, op, vnode_mount(dvp), parentnid, td, cred);
680 fci->flags = O_CREAT | flags;
681 if (fuse_libabi_geq(data, 7, 12)) {
682 insize = sizeof(*fci);
683 fci->umask = td->td_proc->p_fd->fd_cmask;
685 insize = sizeof(struct fuse_open_in);
688 memcpy((char *)fdip->indata + insize, cnp->cn_nameptr,
690 ((char *)fdip->indata)[insize + cnp->cn_namelen] = '\0';
693 err = fdisp_wait_answ(fdip);
696 if (err == ENOSYS && op == FUSE_CREATE) {
697 fsess_set_notimpl(mp, FUSE_CREATE);
699 fdisp_make_mknod_for_fallback(fdip, cnp, dvp,
700 parentnid, td, cred, mode, &op);
701 err = fdisp_wait_answ(fdip);
709 if ((err = fuse_internal_checkentry(feo, vap->va_type))) {
713 if (op == FUSE_CREATE) {
714 foo = (struct fuse_open_out*)(feo + 1);
716 /* Issue a separate FUSE_OPEN */
717 struct fuse_open_in *foi;
720 fdisp_init(fdip2, sizeof(*foi));
721 fdisp_make(fdip2, FUSE_OPEN, vnode_mount(dvp), feo->nodeid, td,
725 err = fdisp_wait_answ(fdip2);
730 err = fuse_vnode_get(mp, feo, feo->nodeid, dvp, vpp, cnp, vap->va_type);
732 struct fuse_release_in *fri;
733 uint64_t nodeid = feo->nodeid;
734 uint64_t fh_id = foo->fh;
736 fdisp_init(fdip, sizeof(*fri));
737 fdisp_make(fdip, FUSE_RELEASE, mp, nodeid, td, cred);
741 fuse_insert_callback(fdip->tick, fuse_internal_forget_callback);
742 fuse_insert_message(fdip->tick, false);
745 ASSERT_VOP_ELOCKED(*vpp, "fuse_vnop_create");
746 fuse_internal_cache_attrs(*vpp, &feo->attr, feo->attr_valid,
747 feo->attr_valid_nsec, NULL);
749 fuse_filehandle_init(*vpp, FUFH_RDWR, NULL, td, cred, foo);
750 fuse_vnode_open(*vpp, foo->open_flags, td);
752 * Purge the parent's attribute cache because the daemon should've
753 * updated its mtime and ctime
755 fuse_vnode_clear_attr_cache(dvp);
756 cache_purge_negative(dvp);
760 fdisp_destroy(fdip2);
766 struct vnop_fdatasync_args {
767 struct vop_generic_args a_gen;
769 struct thread * a_td;
773 fuse_vnop_fdatasync(struct vop_fdatasync_args *ap)
775 struct vnode *vp = ap->a_vp;
776 struct thread *td = ap->a_td;
777 int waitfor = MNT_WAIT;
781 if (fuse_isdeadfs(vp)) {
784 if ((err = vop_stdfdatasync_buf(ap)))
787 return fuse_internal_fsync(vp, td, waitfor, true);
791 struct vnop_fsync_args {
792 struct vop_generic_args a_gen;
795 struct thread * a_td;
799 fuse_vnop_fsync(struct vop_fsync_args *ap)
801 struct vnode *vp = ap->a_vp;
802 struct thread *td = ap->a_td;
803 int waitfor = ap->a_waitfor;
806 if (fuse_isdeadfs(vp)) {
809 if ((err = vop_stdfsync(ap)))
812 return fuse_internal_fsync(vp, td, waitfor, false);
816 struct vnop_getattr_args {
819 struct ucred *a_cred;
824 fuse_vnop_getattr(struct vop_getattr_args *ap)
826 struct vnode *vp = ap->a_vp;
827 struct vattr *vap = ap->a_vap;
828 struct ucred *cred = ap->a_cred;
829 struct thread *td = curthread;
834 dataflags = fuse_get_mpdata(vnode_mount(vp))->dataflags;
836 /* Note that we are not bailing out on a dead file system just yet. */
838 if (!(dataflags & FSESS_INITED)) {
839 if (!vnode_isvroot(vp)) {
840 fdata_set_dead(fuse_get_mpdata(vnode_mount(vp)));
847 err = fuse_internal_getattr(vp, vap, cred, td);
848 if (err == ENOTCONN && vnode_isvroot(vp)) {
849 /* see comment in fuse_vfsop_statfs() */
856 bzero(vap, sizeof(*vap));
857 vap->va_type = vnode_vtype(vp);
863 struct vnop_inactive_args {
869 fuse_vnop_inactive(struct vop_inactive_args *ap)
871 struct vnode *vp = ap->a_vp;
872 struct thread *td = ap->a_td;
874 struct fuse_vnode_data *fvdat = VTOFUD(vp);
875 struct fuse_filehandle *fufh, *fufh_tmp;
879 LIST_FOREACH_SAFE(fufh, &fvdat->handles, next, fufh_tmp) {
880 if (need_flush && vp->v_type == VREG) {
881 if ((VTOFUD(vp)->flag & FN_SIZECHANGE) != 0) {
882 fuse_vnode_savesize(vp, NULL, 0);
884 if ((fvdat->flag & FN_REVOKED) != 0)
885 fuse_io_invalbuf(vp, td);
887 fuse_io_flushbuf(vp, MNT_WAIT, td);
890 fuse_filehandle_close(vp, fufh, td, NULL);
893 if ((fvdat->flag & FN_REVOKED) != 0 && fuse_reclaim_revoked) {
900 struct vnop_link_args {
901 struct vnode *a_tdvp;
903 struct componentname *a_cnp;
907 fuse_vnop_link(struct vop_link_args *ap)
909 struct vnode *vp = ap->a_vp;
910 struct vnode *tdvp = ap->a_tdvp;
911 struct componentname *cnp = ap->a_cnp;
913 struct vattr *vap = VTOVA(vp);
915 struct fuse_dispatcher fdi;
916 struct fuse_entry_out *feo;
917 struct fuse_link_in fli;
921 if (fuse_isdeadfs(vp)) {
924 if (vnode_mount(tdvp) != vnode_mount(vp)) {
929 * This is a seatbelt check to protect naive userspace filesystems from
930 * themselves and the limitations of the FUSE IPC protocol. If a
931 * filesystem does not allow attribute caching, assume it is capable of
932 * validating that nlink does not overflow.
934 if (vap != NULL && vap->va_nlink >= FUSE_LINK_MAX)
936 fli.oldnodeid = VTOI(vp);
939 fuse_internal_newentry_makerequest(vnode_mount(tdvp), VTOI(tdvp), cnp,
940 FUSE_LINK, &fli, sizeof(fli), &fdi);
941 if ((err = fdisp_wait_answ(&fdi))) {
946 err = fuse_internal_checkentry(feo, vnode_vtype(vp));
949 * Purge the parent's attribute cache because the daemon
950 * should've updated its mtime and ctime
952 fuse_vnode_clear_attr_cache(tdvp);
953 fuse_internal_cache_attrs(vp, &feo->attr, feo->attr_valid,
954 feo->attr_valid_nsec, NULL);
961 struct fuse_lookup_alloc_arg {
962 struct fuse_entry_out *feo;
963 struct componentname *cnp;
968 /* Callback for vn_get_ino */
970 fuse_lookup_alloc(struct mount *mp, void *arg, int lkflags, struct vnode **vpp)
972 struct fuse_lookup_alloc_arg *flaa = arg;
974 return fuse_vnode_get(mp, flaa->feo, flaa->nid, NULL, vpp, flaa->cnp,
978 SDT_PROBE_DEFINE3(fusefs, , vnops, cache_lookup,
979 "int", "struct timespec*", "struct timespec*");
981 struct vnop_lookup_args {
982 struct vnodeop_desc *a_desc;
984 struct vnode **a_vpp;
985 struct componentname *a_cnp;
989 fuse_vnop_lookup(struct vop_lookup_args *ap)
991 struct vnode *dvp = ap->a_dvp;
992 struct vnode **vpp = ap->a_vpp;
993 struct componentname *cnp = ap->a_cnp;
994 struct thread *td = cnp->cn_thread;
995 struct ucred *cred = cnp->cn_cred;
997 int nameiop = cnp->cn_nameiop;
998 int flags = cnp->cn_flags;
999 int wantparent = flags & (LOCKPARENT | WANTPARENT);
1000 int islastcn = flags & ISLASTCN;
1001 struct mount *mp = vnode_mount(dvp);
1005 struct vnode *vp = NULL;
1007 struct fuse_dispatcher fdi;
1008 bool did_lookup = false;
1009 struct fuse_entry_out *feo = NULL;
1010 enum vtype vtyp; /* vnode type of target */
1011 off_t filesize; /* filesize of target */
1015 if (fuse_isdeadfs(dvp)) {
1019 if (!vnode_isdir(dvp))
1022 if (islastcn && vfs_isrdonly(mp) && (nameiop != LOOKUP))
1025 if ((err = fuse_internal_access(dvp, VEXEC, td, cred)))
1028 if (flags & ISDOTDOT) {
1029 KASSERT(VTOFUD(dvp)->flag & FN_PARENT_NID,
1030 ("Looking up .. is TODO"));
1031 nid = VTOFUD(dvp)->parent_nid;
1034 /* .. is obviously a directory */
1037 } else if (cnp->cn_namelen == 1 && *(cnp->cn_nameptr) == '.') {
1039 /* . is obviously a directory */
1043 struct timespec now, timeout;
1045 err = cache_lookup(dvp, vpp, cnp, &timeout, NULL);
1046 getnanouptime(&now);
1047 SDT_PROBE3(fusefs, , vnops, cache_lookup, err, &timeout, &now);
1049 case -1: /* positive match */
1050 if (timespeccmp(&timeout, &now, >)) {
1051 atomic_add_acq_long(&fuse_lookup_cache_hits, 1);
1054 atomic_add_acq_long(&fuse_lookup_cache_misses,
1057 &VTOFUD(*vpp)->entry_cache_timeout);
1068 case 0: /* no match in cache */
1069 atomic_add_acq_long(&fuse_lookup_cache_misses, 1);
1072 case ENOENT: /* negative match */
1073 getnanouptime(&now);
1074 if (timespeccmp(&timeout, &now, <=)) {
1076 cache_purge_negative(dvp);
1085 fdisp_init(&fdi, cnp->cn_namelen + 1);
1086 fdisp_make(&fdi, FUSE_LOOKUP, mp, nid, td, cred);
1088 memcpy(fdi.indata, cnp->cn_nameptr, cnp->cn_namelen);
1089 ((char *)fdi.indata)[cnp->cn_namelen] = '\0';
1090 lookup_err = fdisp_wait_answ(&fdi);
1094 /* lookup call succeeded */
1095 feo = (struct fuse_entry_out *)fdi.answ;
1098 /* zero nodeid means ENOENT and cache it */
1099 struct timespec timeout;
1101 fdi.answ_stat = ENOENT;
1102 lookup_err = ENOENT;
1103 if (cnp->cn_flags & MAKEENTRY) {
1104 fuse_validity_2_timespec(feo, &timeout);
1105 cache_enter_time(dvp, *vpp, cnp,
1108 } else if (nid == FUSE_ROOT_ID) {
1109 lookup_err = EINVAL;
1111 vtyp = IFTOVT(feo->attr.mode);
1112 filesize = feo->attr.size;
1114 if (lookup_err && (!fdi.answ_stat || lookup_err != ENOENT)) {
1115 fdisp_destroy(&fdi);
1119 /* lookup_err, if non-zero, must be ENOENT at this point */
1122 /* Entry not found */
1123 if ((nameiop == CREATE || nameiop == RENAME) && islastcn) {
1124 err = fuse_internal_access(dvp, VWRITE, td, cred);
1127 * Set the SAVENAME flag to hold onto the
1128 * pathname for use later in VOP_CREATE or
1131 cnp->cn_flags |= SAVENAME;
1139 /* Entry was found */
1140 if (flags & ISDOTDOT) {
1141 struct fuse_lookup_alloc_arg flaa;
1147 err = vn_vget_ino_gen(dvp, fuse_lookup_alloc, &flaa, 0,
1150 } else if (nid == VTOI(dvp)) {
1154 struct fuse_vnode_data *fvdat;
1156 err = fuse_vnode_get(vnode_mount(dvp), feo, nid, dvp,
1163 * In the case where we are looking up a FUSE node
1164 * represented by an existing cached vnode, and the
1165 * true size reported by FUSE_LOOKUP doesn't match
1166 * the vnode's cached size, then any cached writes
1167 * beyond the file's current size are lost.
1170 * * following attribute cache expiration, or
1171 * * due a bug in the daemon, or
1174 if (vnode_isreg(vp) &&
1175 filesize != fvdat->cached_attrs.va_size &&
1176 fvdat->flag & FN_SIZECHANGE) {
1178 * The FN_SIZECHANGE flag reflects a dirty
1179 * append. If userspace lets us know our cache
1180 * is invalid, that write was lost. (Dirty
1181 * writes that do not cause append are also
1182 * lost, but we don't detect them here.)
1184 * XXX: Maybe disable WB caching on this mount.
1186 printf("%s: WB cache incoherent on %s!\n",
1188 vnode_mount(vp)->mnt_stat.f_mntonname);
1190 fvdat->flag &= ~FN_SIZECHANGE;
1194 fuse_internal_cache_attrs(*vpp, &feo->attr,
1195 feo->attr_valid, feo->attr_valid_nsec, NULL);
1196 fuse_validity_2_bintime(feo->entry_valid,
1197 feo->entry_valid_nsec,
1198 &fvdat->entry_cache_timeout);
1200 if ((nameiop == DELETE || nameiop == RENAME) &&
1203 struct vattr dvattr;
1205 err = fuse_internal_access(dvp, VWRITE, td,
1210 * if the parent's sticky bit is set, check
1211 * whether we're allowed to remove the file.
1212 * Need to figure out the vnode locking to make
1215 fuse_internal_getattr(dvp, &dvattr, cred, td);
1216 if ((dvattr.va_mode & S_ISTXT) &&
1217 fuse_internal_access(dvp, VADMIN, td,
1219 fuse_internal_access(*vpp, VADMIN, td,
1227 (nameiop == DELETE) ||
1228 (nameiop == RENAME && wantparent))) {
1229 cnp->cn_flags |= SAVENAME;
1236 if (vp != NULL && dvp != vp)
1238 else if (vp != NULL)
1243 fdisp_destroy(&fdi);
1249 struct vnop_mkdir_args {
1250 struct vnode *a_dvp;
1251 struct vnode **a_vpp;
1252 struct componentname *a_cnp;
1253 struct vattr *a_vap;
1257 fuse_vnop_mkdir(struct vop_mkdir_args *ap)
1259 struct vnode *dvp = ap->a_dvp;
1260 struct vnode **vpp = ap->a_vpp;
1261 struct componentname *cnp = ap->a_cnp;
1262 struct vattr *vap = ap->a_vap;
1264 struct fuse_mkdir_in fmdi;
1266 if (fuse_isdeadfs(dvp)) {
1269 fmdi.mode = MAKEIMODE(vap->va_type, vap->va_mode);
1270 fmdi.umask = curthread->td_proc->p_fd->fd_cmask;
1272 return (fuse_internal_newentry(dvp, vpp, cnp, FUSE_MKDIR, &fmdi,
1273 sizeof(fmdi), VDIR));
1277 struct vnop_mknod_args {
1278 struct vnode *a_dvp;
1279 struct vnode **a_vpp;
1280 struct componentname *a_cnp;
1281 struct vattr *a_vap;
1285 fuse_vnop_mknod(struct vop_mknod_args *ap)
1288 struct vnode *dvp = ap->a_dvp;
1289 struct vnode **vpp = ap->a_vpp;
1290 struct componentname *cnp = ap->a_cnp;
1291 struct vattr *vap = ap->a_vap;
1293 if (fuse_isdeadfs(dvp))
1296 return fuse_internal_mknod(dvp, vpp, cnp, vap);
1300 struct vop_open_args {
1303 struct ucred *a_cred;
1304 struct thread *a_td;
1305 int a_fdidx; / struct file *a_fp;
1309 fuse_vnop_open(struct vop_open_args *ap)
1311 struct vnode *vp = ap->a_vp;
1312 int a_mode = ap->a_mode;
1313 struct thread *td = ap->a_td;
1314 struct ucred *cred = ap->a_cred;
1315 pid_t pid = td->td_proc->p_pid;
1316 struct fuse_vnode_data *fvdat;
1318 if (fuse_isdeadfs(vp))
1320 if (vp->v_type == VCHR || vp->v_type == VBLK || vp->v_type == VFIFO)
1321 return (EOPNOTSUPP);
1322 if ((a_mode & (FREAD | FWRITE | FEXEC)) == 0)
1327 if (fuse_filehandle_validrw(vp, a_mode, cred, pid)) {
1328 fuse_vnode_open(vp, 0, td);
1332 return fuse_filehandle_open(vp, a_mode, NULL, td, cred);
1336 fuse_vnop_pathconf(struct vop_pathconf_args *ap)
1339 switch (ap->a_name) {
1340 case _PC_FILESIZEBITS:
1344 *ap->a_retval = NAME_MAX;
1347 *ap->a_retval = MIN(LONG_MAX, FUSE_LINK_MAX);
1349 case _PC_SYMLINK_MAX:
1350 *ap->a_retval = MAXPATHLEN;
1356 return (vop_stdpathconf(ap));
1361 struct vnop_read_args {
1365 struct ucred *a_cred;
1369 fuse_vnop_read(struct vop_read_args *ap)
1371 struct vnode *vp = ap->a_vp;
1372 struct uio *uio = ap->a_uio;
1373 int ioflag = ap->a_ioflag;
1374 struct ucred *cred = ap->a_cred;
1375 pid_t pid = curthread->td_proc->p_pid;
1377 if (fuse_isdeadfs(vp)) {
1381 if (VTOFUD(vp)->flag & FN_DIRECTIO) {
1382 ioflag |= IO_DIRECT;
1385 return fuse_io_dispatch(vp, uio, ioflag, cred, pid);
1389 struct vnop_readdir_args {
1392 struct ucred *a_cred;
1399 fuse_vnop_readdir(struct vop_readdir_args *ap)
1401 struct vnode *vp = ap->a_vp;
1402 struct uio *uio = ap->a_uio;
1403 struct ucred *cred = ap->a_cred;
1404 struct fuse_filehandle *fufh = NULL;
1405 struct fuse_iov cookediov;
1411 bool closefufh = false;
1412 pid_t pid = curthread->td_proc->p_pid;
1416 if (fuse_isdeadfs(vp)) {
1419 if ( /* XXXIP ((uio_iovcnt(uio) > 1)) || */
1420 (uio_resid(uio) < sizeof(struct dirent))) {
1424 tresid = uio->uio_resid;
1425 startoff = uio->uio_offset;
1426 err = fuse_filehandle_get_dir(vp, &fufh, cred, pid);
1427 if (err == EBADF && vnode_mount(vp)->mnt_flag & MNT_EXPORTED) {
1429 * nfsd will do VOP_READDIR without first doing VOP_OPEN. We
1430 * must implicitly open the directory here
1432 err = fuse_filehandle_open(vp, FREAD, &fufh, curthread, cred);
1435 * When a directory is opened, it must be read from
1436 * the beginning. Hopefully, the "startoff" still
1437 * exists as an offset cookie for the directory.
1438 * If not, it will read the entire directory without
1439 * returning any entries and just return eof.
1441 uio->uio_offset = 0;
1447 if (ap->a_ncookies != NULL) {
1448 ncookies = uio->uio_resid /
1449 (offsetof(struct dirent, d_name) + 4) + 1;
1450 cookies = malloc(ncookies * sizeof(*cookies), M_TEMP, M_WAITOK);
1451 *ap->a_ncookies = ncookies;
1452 *ap->a_cookies = cookies;
1457 #define DIRCOOKEDSIZE FUSE_DIRENT_ALIGN(FUSE_NAME_OFFSET + MAXNAMLEN + 1)
1458 fiov_init(&cookediov, DIRCOOKEDSIZE);
1460 err = fuse_internal_readdir(vp, uio, startoff, fufh, &cookediov,
1461 &ncookies, cookies);
1463 fiov_teardown(&cookediov);
1465 fuse_filehandle_close(vp, fufh, curthread, cred);
1467 if (ap->a_ncookies != NULL) {
1469 *ap->a_ncookies -= ncookies;
1471 free(*ap->a_cookies, M_TEMP);
1472 *ap->a_ncookies = 0;
1473 *ap->a_cookies = NULL;
1476 if (err == 0 && tresid == uio->uio_resid)
1483 struct vnop_readlink_args {
1486 struct ucred *a_cred;
1490 fuse_vnop_readlink(struct vop_readlink_args *ap)
1492 struct vnode *vp = ap->a_vp;
1493 struct uio *uio = ap->a_uio;
1494 struct ucred *cred = ap->a_cred;
1496 struct fuse_dispatcher fdi;
1499 if (fuse_isdeadfs(vp)) {
1502 if (!vnode_islnk(vp)) {
1505 fdisp_init(&fdi, 0);
1506 err = fdisp_simple_putget_vp(&fdi, FUSE_READLINK, vp, curthread, cred);
1510 if (((char *)fdi.answ)[0] == '/' &&
1511 fuse_get_mpdata(vnode_mount(vp))->dataflags & FSESS_PUSH_SYMLINKS_IN) {
1512 char *mpth = vnode_mount(vp)->mnt_stat.f_mntonname;
1514 err = uiomove(mpth, strlen(mpth), uio);
1517 err = uiomove(fdi.answ, fdi.iosize, uio);
1520 fdisp_destroy(&fdi);
1525 struct vnop_reclaim_args {
1527 struct thread *a_td;
1531 fuse_vnop_reclaim(struct vop_reclaim_args *ap)
1533 struct vnode *vp = ap->a_vp;
1534 struct thread *td = ap->a_td;
1535 struct fuse_vnode_data *fvdat = VTOFUD(vp);
1536 struct fuse_filehandle *fufh, *fufh_tmp;
1539 panic("FUSE: no vnode data during recycling");
1541 LIST_FOREACH_SAFE(fufh, &fvdat->handles, next, fufh_tmp) {
1542 printf("FUSE: vnode being reclaimed with open fufh "
1543 "(type=%#x)", fufh->fufh_type);
1544 fuse_filehandle_close(vp, fufh, td, NULL);
1547 if ((!fuse_isdeadfs(vp)) && (fvdat->nlookup)) {
1548 fuse_internal_forget_send(vnode_mount(vp), td, NULL, VTOI(vp),
1551 fuse_vnode_setparent(vp, NULL);
1553 vfs_hash_remove(vp);
1554 vnode_destroy_vobject(vp);
1555 fuse_vnode_destroy(vp);
1561 struct vnop_remove_args {
1562 struct vnode *a_dvp;
1564 struct componentname *a_cnp;
1568 fuse_vnop_remove(struct vop_remove_args *ap)
1570 struct vnode *dvp = ap->a_dvp;
1571 struct vnode *vp = ap->a_vp;
1572 struct componentname *cnp = ap->a_cnp;
1576 if (fuse_isdeadfs(vp)) {
1579 if (vnode_isdir(vp)) {
1584 err = fuse_internal_remove(dvp, vp, cnp, FUSE_UNLINK);
1587 fuse_internal_vnode_disappear(vp);
1589 * Purge the parent's attribute cache because the daemon
1590 * should've updated its mtime and ctime
1592 fuse_vnode_clear_attr_cache(dvp);
1598 struct vnop_rename_args {
1599 struct vnode *a_fdvp;
1600 struct vnode *a_fvp;
1601 struct componentname *a_fcnp;
1602 struct vnode *a_tdvp;
1603 struct vnode *a_tvp;
1604 struct componentname *a_tcnp;
1608 fuse_vnop_rename(struct vop_rename_args *ap)
1610 struct vnode *fdvp = ap->a_fdvp;
1611 struct vnode *fvp = ap->a_fvp;
1612 struct componentname *fcnp = ap->a_fcnp;
1613 struct vnode *tdvp = ap->a_tdvp;
1614 struct vnode *tvp = ap->a_tvp;
1615 struct componentname *tcnp = ap->a_tcnp;
1616 struct fuse_data *data;
1617 bool newparent = fdvp != tdvp;
1618 bool isdir = fvp->v_type == VDIR;
1621 if (fuse_isdeadfs(fdvp)) {
1624 if (fvp->v_mount != tdvp->v_mount ||
1625 (tvp && fvp->v_mount != tvp->v_mount)) {
1626 SDT_PROBE2(fusefs, , vnops, trace, 1, "cross-device rename");
1633 * FUSE library is expected to check if target directory is not
1634 * under the source directory in the file system tree.
1635 * Linux performs this check at VFS level.
1638 * If source is a directory, and it will get a new parent, user must
1639 * have write permission to it, so ".." can be modified.
1641 data = fuse_get_mpdata(vnode_mount(tdvp));
1642 if (data->dataflags & FSESS_DEFAULT_PERMISSIONS && isdir && newparent) {
1643 err = fuse_internal_access(fvp, VWRITE,
1644 tcnp->cn_thread, tcnp->cn_cred);
1648 sx_xlock(&data->rename_lock);
1649 err = fuse_internal_rename(fdvp, fcnp, tdvp, tcnp);
1652 fuse_vnode_setparent(fvp, tdvp);
1654 fuse_vnode_setparent(tvp, NULL);
1656 sx_unlock(&data->rename_lock);
1658 if (tvp != NULL && tvp != fvp) {
1661 if (vnode_isdir(fvp)) {
1662 if ((tvp != NULL) && vnode_isdir(tvp)) {
1683 struct vnop_rmdir_args {
1684 struct vnode *a_dvp;
1686 struct componentname *a_cnp;
1690 fuse_vnop_rmdir(struct vop_rmdir_args *ap)
1692 struct vnode *dvp = ap->a_dvp;
1693 struct vnode *vp = ap->a_vp;
1697 if (fuse_isdeadfs(vp)) {
1700 if (VTOFUD(vp) == VTOFUD(dvp)) {
1703 err = fuse_internal_remove(dvp, vp, ap->a_cnp, FUSE_RMDIR);
1706 fuse_internal_vnode_disappear(vp);
1708 * Purge the parent's attribute cache because the daemon
1709 * should've updated its mtime and ctime
1711 fuse_vnode_clear_attr_cache(dvp);
1717 struct vnop_setattr_args {
1719 struct vattr *a_vap;
1720 struct ucred *a_cred;
1721 struct thread *a_td;
1725 fuse_vnop_setattr(struct vop_setattr_args *ap)
1727 struct vnode *vp = ap->a_vp;
1728 struct vattr *vap = ap->a_vap;
1729 struct ucred *cred = ap->a_cred;
1730 struct thread *td = curthread;
1732 struct fuse_data *data;
1733 struct vattr old_va;
1736 accmode_t accmode = 0;
1738 bool drop_suid = false;
1741 mp = vnode_mount(vp);
1742 data = fuse_get_mpdata(mp);
1743 dataflags = data->dataflags;
1744 checkperm = dataflags & FSESS_DEFAULT_PERMISSIONS;
1745 if (cred->cr_ngroups > 0)
1746 cr_gid = cred->cr_groups[0];
1750 if (fuse_isdeadfs(vp)) {
1754 if (vap->va_uid != (uid_t)VNOVAL) {
1756 /* Only root may change a file's owner */
1757 err = priv_check_cred(cred, PRIV_VFS_CHOWN);
1759 /* As a special case, allow the null chown */
1760 err2 = fuse_internal_getattr(vp, &old_va, cred,
1764 if (vap->va_uid != old_va.va_uid)
1774 if (vap->va_gid != (gid_t)VNOVAL) {
1775 if (checkperm && priv_check_cred(cred, PRIV_VFS_CHOWN))
1777 if (checkperm && !groupmember(vap->va_gid, cred))
1780 * Non-root users may only chgrp to one of their own
1783 err = priv_check_cred(cred, PRIV_VFS_CHOWN);
1785 /* As a special case, allow the null chgrp */
1786 err2 = fuse_internal_getattr(vp, &old_va, cred,
1790 if (vap->va_gid != old_va.va_gid)
1798 if (vap->va_size != VNOVAL) {
1799 switch (vp->v_type) {
1804 if (vfs_isrdonly(mp))
1809 * According to POSIX, the result is unspecified
1810 * for file types other than regular files,
1811 * directories and shared memory objects. We
1812 * don't support shared memory objects in the file
1813 * system, and have dubious support for truncating
1814 * symlinks. Just ignore the request in other cases.
1818 /* Don't set accmode. Permission to trunc is checked upstack */
1820 if (vap->va_atime.tv_sec != VNOVAL || vap->va_mtime.tv_sec != VNOVAL) {
1821 if (vap->va_vaflags & VA_UTIMES_NULL)
1827 if (vap->va_mode != (mode_t)VNOVAL)
1828 vap->va_mode &= ~(S_ISUID | S_ISGID);
1830 err = fuse_internal_getattr(vp, &old_va, cred, td);
1833 vap->va_mode = old_va.va_mode & ~(S_ISUID | S_ISGID);
1836 if (vap->va_mode != (mode_t)VNOVAL) {
1837 /* Only root may set the sticky bit on non-directories */
1838 if (checkperm && vp->v_type != VDIR && (vap->va_mode & S_ISTXT)
1839 && priv_check_cred(cred, PRIV_VFS_STICKYFILE))
1841 if (checkperm && (vap->va_mode & S_ISGID)) {
1842 err = fuse_internal_getattr(vp, &old_va, cred, td);
1845 if (!groupmember(old_va.va_gid, cred)) {
1846 err = priv_check_cred(cred, PRIV_VFS_SETGID);
1854 if (vfs_isrdonly(mp))
1857 err = fuse_internal_access(vp, accmode, td, cred);
1861 return fuse_internal_setattr(vp, vap, td, cred);
1865 struct vnop_strategy_args {
1871 fuse_vnop_strategy(struct vop_strategy_args *ap)
1873 struct vnode *vp = ap->a_vp;
1874 struct buf *bp = ap->a_bp;
1876 if (!vp || fuse_isdeadfs(vp)) {
1877 bp->b_ioflags |= BIO_ERROR;
1878 bp->b_error = ENXIO;
1884 * VOP_STRATEGY always returns zero and signals error via bp->b_ioflags.
1885 * fuse_io_strategy sets bp's error fields
1887 (void)fuse_io_strategy(vp, bp);
1894 struct vnop_symlink_args {
1895 struct vnode *a_dvp;
1896 struct vnode **a_vpp;
1897 struct componentname *a_cnp;
1898 struct vattr *a_vap;
1903 fuse_vnop_symlink(struct vop_symlink_args *ap)
1905 struct vnode *dvp = ap->a_dvp;
1906 struct vnode **vpp = ap->a_vpp;
1907 struct componentname *cnp = ap->a_cnp;
1908 const char *target = ap->a_target;
1910 struct fuse_dispatcher fdi;
1915 if (fuse_isdeadfs(dvp)) {
1919 * Unlike the other creator type calls, here we have to create a message
1920 * where the name of the new entry comes first, and the data describing
1921 * the entry comes second.
1922 * Hence we can't rely on our handy fuse_internal_newentry() routine,
1923 * but put together the message manually and just call the core part.
1926 len = strlen(target) + 1;
1927 fdisp_init(&fdi, len + cnp->cn_namelen + 1);
1928 fdisp_make_vp(&fdi, FUSE_SYMLINK, dvp, curthread, NULL);
1930 memcpy(fdi.indata, cnp->cn_nameptr, cnp->cn_namelen);
1931 ((char *)fdi.indata)[cnp->cn_namelen] = '\0';
1932 memcpy((char *)fdi.indata + cnp->cn_namelen + 1, target, len);
1934 err = fuse_internal_newentry_core(dvp, vpp, cnp, VLNK, &fdi);
1935 fdisp_destroy(&fdi);
1940 struct vnop_write_args {
1944 struct ucred *a_cred;
1948 fuse_vnop_write(struct vop_write_args *ap)
1950 struct vnode *vp = ap->a_vp;
1951 struct uio *uio = ap->a_uio;
1952 int ioflag = ap->a_ioflag;
1953 struct ucred *cred = ap->a_cred;
1954 pid_t pid = curthread->td_proc->p_pid;
1956 if (fuse_isdeadfs(vp)) {
1960 if (VTOFUD(vp)->flag & FN_DIRECTIO) {
1961 ioflag |= IO_DIRECT;
1964 return fuse_io_dispatch(vp, uio, ioflag, cred, pid);
1968 fuse_gbp_getblkno(struct vnode *vp, vm_ooffset_t off)
1970 const int biosize = fuse_iosize(vp);
1972 return (off / biosize);
1976 fuse_gbp_getblksz(struct vnode *vp, daddr_t lbn)
1980 const int biosize = fuse_iosize(vp);
1982 err = fuse_vnode_size(vp, &filesize, NULL, NULL);
1983 KASSERT(err == 0, ("vfs_bio_getpages can't handle errors here"));
1987 if ((off_t)lbn * biosize >= filesize) {
1989 } else if ((off_t)(lbn + 1) * biosize > filesize) {
1990 blksz = filesize - (off_t)lbn *biosize;
1998 struct vnop_getpages_args {
2006 fuse_vnop_getpages(struct vop_getpages_args *ap)
2008 struct vnode *vp = ap->a_vp;
2010 if (!fsess_opt_mmap(vnode_mount(vp))) {
2011 SDT_PROBE2(fusefs, , vnops, trace, 1,
2012 "called on non-cacheable vnode??\n");
2013 return (VM_PAGER_ERROR);
2016 return (vfs_bio_getpages(vp, ap->a_m, ap->a_count, ap->a_rbehind,
2017 ap->a_rahead, fuse_gbp_getblkno, fuse_gbp_getblksz));
2020 static const char extattr_namespace_separator = '.';
2023 struct vop_getextattr_args {
2024 struct vop_generic_args a_gen;
2026 int a_attrnamespace;
2030 struct ucred *a_cred;
2031 struct thread *a_td;
2035 fuse_vnop_getextattr(struct vop_getextattr_args *ap)
2037 struct vnode *vp = ap->a_vp;
2038 struct uio *uio = ap->a_uio;
2039 struct fuse_dispatcher fdi;
2040 struct fuse_getxattr_in *get_xattr_in;
2041 struct fuse_getxattr_out *get_xattr_out;
2042 struct mount *mp = vnode_mount(vp);
2043 struct thread *td = ap->a_td;
2044 struct ucred *cred = ap->a_cred;
2050 if (fuse_isdeadfs(vp))
2053 if (!fsess_isimpl(mp, FUSE_GETXATTR))
2056 err = fuse_extattr_check_cred(vp, ap->a_attrnamespace, cred, td, VREAD);
2060 /* Default to looking for user attributes. */
2061 if (ap->a_attrnamespace == EXTATTR_NAMESPACE_SYSTEM)
2062 prefix = EXTATTR_NAMESPACE_SYSTEM_STRING;
2064 prefix = EXTATTR_NAMESPACE_USER_STRING;
2066 len = strlen(prefix) + sizeof(extattr_namespace_separator) +
2067 strlen(ap->a_name) + 1;
2069 fdisp_init(&fdi, len + sizeof(*get_xattr_in));
2070 fdisp_make_vp(&fdi, FUSE_GETXATTR, vp, td, cred);
2072 get_xattr_in = fdi.indata;
2074 * Check to see whether we're querying the available size or
2075 * issuing the actual request. If we pass in 0, we get back struct
2076 * fuse_getxattr_out. If we pass in a non-zero size, we get back
2077 * that much data, without the struct fuse_getxattr_out header.
2080 get_xattr_in->size = 0;
2082 get_xattr_in->size = uio->uio_resid;
2084 attr_str = (char *)fdi.indata + sizeof(*get_xattr_in);
2085 snprintf(attr_str, len, "%s%c%s", prefix, extattr_namespace_separator,
2088 err = fdisp_wait_answ(&fdi);
2090 if (err == ENOSYS) {
2091 fsess_set_notimpl(mp, FUSE_GETXATTR);
2097 get_xattr_out = fdi.answ;
2099 if (ap->a_size != NULL)
2100 *ap->a_size = get_xattr_out->size;
2103 err = uiomove(fdi.answ, fdi.iosize, uio);
2106 fdisp_destroy(&fdi);
2111 struct vop_setextattr_args {
2112 struct vop_generic_args a_gen;
2114 int a_attrnamespace;
2117 struct ucred *a_cred;
2118 struct thread *a_td;
2122 fuse_vnop_setextattr(struct vop_setextattr_args *ap)
2124 struct vnode *vp = ap->a_vp;
2125 struct uio *uio = ap->a_uio;
2126 struct fuse_dispatcher fdi;
2127 struct fuse_setxattr_in *set_xattr_in;
2128 struct mount *mp = vnode_mount(vp);
2129 struct thread *td = ap->a_td;
2130 struct ucred *cred = ap->a_cred;
2136 if (fuse_isdeadfs(vp))
2139 if (!fsess_isimpl(mp, FUSE_SETXATTR))
2142 if (vfs_isrdonly(mp))
2145 /* Deleting xattrs must use VOP_DELETEEXTATTR instead */
2146 if (ap->a_uio == NULL) {
2148 * If we got here as fallback from VOP_DELETEEXTATTR, then
2149 * return EOPNOTSUPP.
2151 if (!fsess_isimpl(mp, FUSE_REMOVEXATTR))
2152 return (EOPNOTSUPP);
2157 err = fuse_extattr_check_cred(vp, ap->a_attrnamespace, cred, td,
2162 /* Default to looking for user attributes. */
2163 if (ap->a_attrnamespace == EXTATTR_NAMESPACE_SYSTEM)
2164 prefix = EXTATTR_NAMESPACE_SYSTEM_STRING;
2166 prefix = EXTATTR_NAMESPACE_USER_STRING;
2168 len = strlen(prefix) + sizeof(extattr_namespace_separator) +
2169 strlen(ap->a_name) + 1;
2171 fdisp_init(&fdi, len + sizeof(*set_xattr_in) + uio->uio_resid);
2172 fdisp_make_vp(&fdi, FUSE_SETXATTR, vp, td, cred);
2174 set_xattr_in = fdi.indata;
2175 set_xattr_in->size = uio->uio_resid;
2177 attr_str = (char *)fdi.indata + sizeof(*set_xattr_in);
2178 snprintf(attr_str, len, "%s%c%s", prefix, extattr_namespace_separator,
2181 err = uiomove((char *)fdi.indata + sizeof(*set_xattr_in) + len,
2182 uio->uio_resid, uio);
2187 err = fdisp_wait_answ(&fdi);
2189 if (err == ENOSYS) {
2190 fsess_set_notimpl(mp, FUSE_SETXATTR);
2193 if (err == ERESTART) {
2194 /* Can't restart after calling uiomove */
2199 fdisp_destroy(&fdi);
2204 * The Linux / FUSE extended attribute list is simply a collection of
2205 * NUL-terminated strings. The FreeBSD extended attribute list is a single
2206 * byte length followed by a non-NUL terminated string. So, this allows
2207 * conversion of the Linux / FUSE format to the FreeBSD format in place.
2208 * Linux attribute names are reported with the namespace as a prefix (e.g.
2209 * "user.attribute_name"), but in FreeBSD they are reported without the
2210 * namespace prefix (e.g. "attribute_name"). So, we're going from:
2212 * user.attr_name1\0user.attr_name2\0
2216 * <num>attr_name1<num>attr_name2
2218 * Where "<num>" is a single byte number of characters in the attribute name.
2221 * prefix - exattr namespace prefix string
2222 * list, list_len - input list with namespace prefixes
2223 * bsd_list, bsd_list_len - output list compatible with bsd vfs
2226 fuse_xattrlist_convert(char *prefix, const char *list, int list_len,
2227 char *bsd_list, int *bsd_list_len)
2229 int len, pos, dist_to_next, prefix_len;
2233 prefix_len = strlen(prefix);
2235 while (pos < list_len && list[pos] != '\0') {
2236 dist_to_next = strlen(&list[pos]) + 1;
2237 if (bcmp(&list[pos], prefix, prefix_len) == 0 &&
2238 list[pos + prefix_len] == extattr_namespace_separator) {
2239 len = dist_to_next -
2240 (prefix_len + sizeof(extattr_namespace_separator)) - 1;
2241 if (len >= EXTATTR_MAXNAMELEN)
2242 return (ENAMETOOLONG);
2244 bsd_list[*bsd_list_len] = len;
2245 memcpy(&bsd_list[*bsd_list_len + 1],
2246 &list[pos + prefix_len +
2247 sizeof(extattr_namespace_separator)], len);
2249 *bsd_list_len += len + 1;
2252 pos += dist_to_next;
2259 struct vop_listextattr_args {
2260 struct vop_generic_args a_gen;
2262 int a_attrnamespace;
2265 struct ucred *a_cred;
2266 struct thread *a_td;
2270 fuse_vnop_listextattr(struct vop_listextattr_args *ap)
2272 struct vnode *vp = ap->a_vp;
2273 struct uio *uio = ap->a_uio;
2274 struct fuse_dispatcher fdi;
2275 struct fuse_listxattr_in *list_xattr_in;
2276 struct fuse_listxattr_out *list_xattr_out;
2277 struct mount *mp = vnode_mount(vp);
2278 struct thread *td = ap->a_td;
2279 struct ucred *cred = ap->a_cred;
2283 char *bsd_list = NULL;
2289 if (fuse_isdeadfs(vp))
2292 if (!fsess_isimpl(mp, FUSE_LISTXATTR))
2295 err = fuse_extattr_check_cred(vp, ap->a_attrnamespace, cred, td, VREAD);
2300 * Add space for a NUL and the period separator if enabled.
2301 * Default to looking for user attributes.
2303 if (ap->a_attrnamespace == EXTATTR_NAMESPACE_SYSTEM)
2304 prefix = EXTATTR_NAMESPACE_SYSTEM_STRING;
2306 prefix = EXTATTR_NAMESPACE_USER_STRING;
2308 len = strlen(prefix) + sizeof(extattr_namespace_separator) + 1;
2310 fdisp_init(&fdi, sizeof(*list_xattr_in) + len);
2311 fdisp_make_vp(&fdi, FUSE_LISTXATTR, vp, td, cred);
2314 * Retrieve Linux / FUSE compatible list size.
2316 list_xattr_in = fdi.indata;
2317 list_xattr_in->size = 0;
2318 attr_str = (char *)fdi.indata + sizeof(*list_xattr_in);
2319 snprintf(attr_str, len, "%s%c", prefix, extattr_namespace_separator);
2321 err = fdisp_wait_answ(&fdi);
2323 if (err == ENOSYS) {
2324 fsess_set_notimpl(mp, FUSE_LISTXATTR);
2330 list_xattr_out = fdi.answ;
2331 linux_list_len = list_xattr_out->size;
2332 if (linux_list_len == 0) {
2333 if (ap->a_size != NULL)
2334 *ap->a_size = linux_list_len;
2339 * Retrieve Linux / FUSE compatible list values.
2341 fdisp_refresh_vp(&fdi, FUSE_LISTXATTR, vp, td, cred);
2342 list_xattr_in = fdi.indata;
2343 list_xattr_in->size = linux_list_len + sizeof(*list_xattr_out);
2344 attr_str = (char *)fdi.indata + sizeof(*list_xattr_in);
2345 snprintf(attr_str, len, "%s%c", prefix, extattr_namespace_separator);
2347 err = fdisp_wait_answ(&fdi);
2351 linux_list = fdi.answ;
2352 linux_list_len = fdi.iosize;
2355 * Retrieve the BSD compatible list values.
2356 * The Linux / FUSE attribute list format isn't the same
2357 * as FreeBSD's format. So we need to transform it into
2358 * FreeBSD's format before giving it to the user.
2360 bsd_list = malloc(linux_list_len, M_TEMP, M_WAITOK);
2361 err = fuse_xattrlist_convert(prefix, linux_list, linux_list_len,
2362 bsd_list, &bsd_list_len);
2366 if (ap->a_size != NULL)
2367 *ap->a_size = bsd_list_len;
2370 err = uiomove(bsd_list, bsd_list_len, uio);
2373 free(bsd_list, M_TEMP);
2374 fdisp_destroy(&fdi);
2379 struct vop_deleteextattr_args {
2380 struct vop_generic_args a_gen;
2382 int a_attrnamespace;
2384 struct ucred *a_cred;
2385 struct thread *a_td;
2389 fuse_vnop_deleteextattr(struct vop_deleteextattr_args *ap)
2391 struct vnode *vp = ap->a_vp;
2392 struct fuse_dispatcher fdi;
2393 struct mount *mp = vnode_mount(vp);
2394 struct thread *td = ap->a_td;
2395 struct ucred *cred = ap->a_cred;
2401 if (fuse_isdeadfs(vp))
2404 if (!fsess_isimpl(mp, FUSE_REMOVEXATTR))
2407 if (vfs_isrdonly(mp))
2410 err = fuse_extattr_check_cred(vp, ap->a_attrnamespace, cred, td,
2415 /* Default to looking for user attributes. */
2416 if (ap->a_attrnamespace == EXTATTR_NAMESPACE_SYSTEM)
2417 prefix = EXTATTR_NAMESPACE_SYSTEM_STRING;
2419 prefix = EXTATTR_NAMESPACE_USER_STRING;
2421 len = strlen(prefix) + sizeof(extattr_namespace_separator) +
2422 strlen(ap->a_name) + 1;
2424 fdisp_init(&fdi, len);
2425 fdisp_make_vp(&fdi, FUSE_REMOVEXATTR, vp, td, cred);
2427 attr_str = fdi.indata;
2428 snprintf(attr_str, len, "%s%c%s", prefix, extattr_namespace_separator,
2431 err = fdisp_wait_answ(&fdi);
2432 if (err == ENOSYS) {
2433 fsess_set_notimpl(mp, FUSE_REMOVEXATTR);
2437 fdisp_destroy(&fdi);
2442 struct vnop_print_args {
2447 fuse_vnop_print(struct vop_print_args *ap)
2449 struct fuse_vnode_data *fvdat = VTOFUD(ap->a_vp);
2451 printf("nodeid: %ju, parent nodeid: %ju, nlookup: %ju, flag: %#x\n",
2452 (uintmax_t)VTOILLU(ap->a_vp), (uintmax_t)fvdat->parent_nid,
2453 (uintmax_t)fvdat->nlookup,
2460 * Get an NFS filehandle for a FUSE file.
2462 * This will only work for FUSE file systems that guarantee the uniqueness of
2463 * nodeid:generation, which most don't.
2467 IN struct vnode *a_vp;
2468 IN struct fid *a_fhp;
2472 fuse_vnop_vptofh(struct vop_vptofh_args *ap)
2474 struct vnode *vp = ap->a_vp;
2475 struct fuse_vnode_data *fvdat = VTOFUD(vp);
2476 struct fuse_fid *fhp = (struct fuse_fid *)(ap->a_fhp);
2477 _Static_assert(sizeof(struct fuse_fid) <= sizeof(struct fid),
2478 "FUSE fid type is too big");
2479 struct mount *mp = vnode_mount(vp);
2480 struct fuse_data *data = fuse_get_mpdata(mp);
2484 if (!(data->dataflags & FSESS_EXPORT_SUPPORT))
2487 err = fuse_internal_getattr(vp, &va, curthread->td_ucred, curthread);
2491 /*ip = VTOI(ap->a_vp);*/
2492 /*ufhp = (struct ufid *)ap->a_fhp;*/
2493 fhp->len = sizeof(struct fuse_fid);
2494 fhp->nid = fvdat->nid;
2495 if (fvdat->generation <= UINT32_MAX)
2496 fhp->gen = fvdat->generation;