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/mutex.h>
75 #include <sys/mount.h>
76 #include <sys/vnode.h>
77 #include <sys/namei.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>
90 #include "fuse_file.h"
91 #include "fuse_internal.h"
94 #include "fuse_node.h"
95 #include "fuse_file.h"
97 SDT_PROVIDER_DECLARE(fusefs);
100 * arg0: verbosity. Higher numbers give more verbose messages
101 * arg1: Textual message
103 SDT_PROBE_DEFINE2(fusefs, , internal, trace, "int", "char*");
105 #ifdef ZERO_PAD_INCOMPLETE_BUFS
106 static int isbzero(void *buf, size_t len);
111 fuse_internal_get_cached_vnode(struct mount* mp, ino_t ino, int flags,
115 struct thread *td = curthread;
116 uint64_t nodeid = ino;
121 error = vfs_hash_get(mp, fuse_vnode_hash(nodeid), flags, td, vpp,
122 fuse_vnode_cmp, &nodeid);
126 * Check the entry cache timeout. We have to do this within fusefs
127 * instead of by using cache_enter_time/cache_lookup because those
128 * routines are only intended to work with pathnames, not inodes
132 if (bintime_cmp(&(VTOFUD(*vpp)->entry_cache_timeout), &now, >)){
133 atomic_add_acq_long(&fuse_lookup_cache_hits, 1);
136 /* Entry cache timeout */
137 atomic_add_acq_long(&fuse_lookup_cache_misses, 1);
146 /* Synchronously send a FUSE_ACCESS operation */
148 fuse_internal_access(struct vnode *vp,
154 uint32_t mask = F_OK;
158 struct fuse_dispatcher fdi;
159 struct fuse_access_in *fai;
160 struct fuse_data *data;
162 mp = vnode_mount(vp);
163 vtype = vnode_vtype(vp);
165 data = fuse_get_mpdata(mp);
166 dataflags = data->dataflags;
171 if (mode & VMODIFY_PERMS && vfs_isrdonly(mp)) {
172 switch (vp->v_type) {
184 /* Unless explicitly permitted, deny everyone except the fs owner. */
185 if (!(dataflags & FSESS_DAEMON_CAN_SPY)) {
186 if (fuse_match_cred(data->daemoncred, cred))
190 if (dataflags & FSESS_DEFAULT_PERMISSIONS) {
193 fuse_internal_getattr(vp, &va, cred, td);
194 return vaccess(vp->v_type, va.va_mode, va.va_uid,
195 va.va_gid, mode, cred, NULL);
198 if (!fsess_isimpl(mp, FUSE_ACCESS))
201 if ((mode & (VWRITE | VAPPEND | VADMIN)) != 0)
203 if ((mode & VREAD) != 0)
205 if ((mode & VEXEC) != 0)
208 fdisp_init(&fdi, sizeof(*fai));
209 fdisp_make_vp(&fdi, FUSE_ACCESS, vp, td, cred);
214 err = fdisp_wait_answ(&fdi);
218 fsess_set_notimpl(mp, FUSE_ACCESS);
225 * Cache FUSE attributes from attr, in attribute cache associated with vnode
226 * 'vp'. Optionally, if argument 'vap' is not NULL, store a copy of the
227 * converted attributes there as well.
229 * If the nominal attribute cache TTL is zero, do not cache on the 'vp' (but do
230 * return the result to the caller).
233 fuse_internal_cache_attrs(struct vnode *vp, struct fuse_attr *attr,
234 uint64_t attr_valid, uint32_t attr_valid_nsec, struct vattr *vap)
237 struct fuse_vnode_data *fvdat;
238 struct fuse_data *data;
239 struct vattr *vp_cache_at;
241 mp = vnode_mount(vp);
243 data = fuse_get_mpdata(mp);
245 ASSERT_VOP_ELOCKED(vp, "fuse_internal_cache_attrs");
247 fuse_validity_2_bintime(attr_valid, attr_valid_nsec,
248 &fvdat->attr_cache_timeout);
250 /* Fix our buffers if the filesize changed without us knowing */
251 if (vnode_isreg(vp) && attr->size != fvdat->cached_attrs.va_size) {
252 (void)fuse_vnode_setsize(vp, attr->size);
253 fvdat->cached_attrs.va_size = attr->size;
256 if (attr_valid > 0 || attr_valid_nsec > 0)
257 vp_cache_at = &(fvdat->cached_attrs);
258 else if (vap != NULL)
263 vattr_null(vp_cache_at);
264 vp_cache_at->va_fsid = mp->mnt_stat.f_fsid.val[0];
265 vp_cache_at->va_fileid = attr->ino;
266 vp_cache_at->va_mode = attr->mode & ~S_IFMT;
267 vp_cache_at->va_nlink = attr->nlink;
268 vp_cache_at->va_uid = attr->uid;
269 vp_cache_at->va_gid = attr->gid;
270 vp_cache_at->va_rdev = attr->rdev;
271 vp_cache_at->va_size = attr->size;
272 /* XXX on i386, seconds are truncated to 32 bits */
273 vp_cache_at->va_atime.tv_sec = attr->atime;
274 vp_cache_at->va_atime.tv_nsec = attr->atimensec;
275 vp_cache_at->va_mtime.tv_sec = attr->mtime;
276 vp_cache_at->va_mtime.tv_nsec = attr->mtimensec;
277 vp_cache_at->va_ctime.tv_sec = attr->ctime;
278 vp_cache_at->va_ctime.tv_nsec = attr->ctimensec;
279 if (fuse_libabi_geq(data, 7, 9) && attr->blksize > 0)
280 vp_cache_at->va_blocksize = attr->blksize;
282 vp_cache_at->va_blocksize = PAGE_SIZE;
283 vp_cache_at->va_type = IFTOVT(attr->mode);
284 vp_cache_at->va_bytes = attr->blocks * S_BLKSIZE;
285 vp_cache_at->va_flags = 0;
287 if (vap != vp_cache_at && vap != NULL)
288 memcpy(vap, vp_cache_at, sizeof(*vap));
295 fuse_internal_fsync_callback(struct fuse_ticket *tick, struct uio *uio)
297 if (tick->tk_aw_ohead.error == ENOSYS) {
298 fsess_set_notimpl(tick->tk_data->mp, fticket_opcode(tick));
304 fuse_internal_fsync(struct vnode *vp,
309 struct fuse_fsync_in *ffsi = NULL;
310 struct fuse_dispatcher fdi;
311 struct fuse_filehandle *fufh;
312 struct fuse_vnode_data *fvdat = VTOFUD(vp);
313 struct mount *mp = vnode_mount(vp);
317 if (!fsess_isimpl(vnode_mount(vp),
318 (vnode_vtype(vp) == VDIR ? FUSE_FSYNCDIR : FUSE_FSYNC))) {
324 if (!fsess_isimpl(mp, op))
327 fdisp_init(&fdi, sizeof(*ffsi));
329 * fsync every open file handle for this file, because we can't be sure
330 * which file handle the caller is really referring to.
332 LIST_FOREACH(fufh, &fvdat->handles, next) {
334 fdisp_make_vp(&fdi, op, vp, td, NULL);
336 fdisp_refresh_vp(&fdi, op, vp, td, NULL);
338 ffsi->fh = fufh->fh_id;
339 ffsi->fsync_flags = 0;
342 ffsi->fsync_flags = 1;
344 if (waitfor == MNT_WAIT) {
345 err = fdisp_wait_answ(&fdi);
347 fuse_insert_callback(fdi.tick,
348 fuse_internal_fsync_callback);
349 fuse_insert_message(fdi.tick, false);
352 /* ENOSYS means "success, and don't call again" */
353 fsess_set_notimpl(mp, op);
363 /* Asynchronous invalidation */
364 SDT_PROBE_DEFINE2(fusefs, , internal, invalidate_cache_hit,
365 "struct vnode*", "struct vnode*");
367 fuse_internal_invalidate_entry(struct mount *mp, struct uio *uio)
369 struct fuse_notify_inval_entry_out fnieo;
370 struct componentname cn;
371 struct vnode *dvp, *vp;
375 if ((err = uiomove(&fnieo, sizeof(fnieo), uio)) != 0)
378 if ((err = uiomove(name, fnieo.namelen, uio)) != 0)
380 name[fnieo.namelen] = '\0';
381 /* fusefs does not cache "." or ".." entries */
382 if (strncmp(name, ".", sizeof(".")) == 0 ||
383 strncmp(name, "..", sizeof("..")) == 0)
386 if (fnieo.parent == FUSE_ROOT_ID)
387 err = VFS_ROOT(mp, LK_SHARED, &dvp);
389 err = fuse_internal_get_cached_vnode( mp, fnieo.parent,
392 * If dvp is not in the cache, then it must've been reclaimed. And
393 * since fuse_vnop_reclaim does a cache_purge, name's entry must've
394 * been invalidated already. So we can safely return if dvp == NULL
396 if (err != 0 || dvp == NULL)
399 * XXX we can't check dvp's generation because the FUSE invalidate
400 * entry message doesn't include it. Worse case is that we invalidate
401 * an entry that didn't need to be invalidated.
404 cn.cn_nameiop = LOOKUP;
405 cn.cn_flags = 0; /* !MAKEENTRY means free cached entry */
406 cn.cn_thread = curthread;
407 cn.cn_cred = curthread->td_ucred;
408 cn.cn_lkflags = LK_SHARED;
410 cn.cn_nameptr = name;
411 cn.cn_namelen = fnieo.namelen;
412 err = cache_lookup(dvp, &vp, &cn, NULL, NULL);
414 fuse_vnode_clear_attr_cache(dvp);
420 fuse_internal_invalidate_inode(struct mount *mp, struct uio *uio)
422 struct fuse_notify_inval_inode_out fniio;
426 if ((err = uiomove(&fniio, sizeof(fniio), uio)) != 0)
429 if (fniio.ino == FUSE_ROOT_ID)
430 err = VFS_ROOT(mp, LK_EXCLUSIVE, &vp);
432 err = fuse_internal_get_cached_vnode(mp, fniio.ino, LK_SHARED,
434 if (err != 0 || vp == NULL)
437 * XXX we can't check vp's generation because the FUSE invalidate
438 * entry message doesn't include it. Worse case is that we invalidate
439 * an inode that didn't need to be invalidated.
443 * Flush and invalidate buffers if off >= 0. Technically we only need
444 * to flush and invalidate the range of offsets [off, off + len), but
445 * for simplicity's sake we do everything.
448 fuse_io_invalbuf(vp, curthread);
449 fuse_vnode_clear_attr_cache(vp);
456 fuse_internal_mknod(struct vnode *dvp, struct vnode **vpp,
457 struct componentname *cnp, struct vattr *vap)
459 struct fuse_data *data;
460 struct fuse_mknod_in fmni;
463 data = fuse_get_mpdata(dvp->v_mount);
465 fmni.mode = MAKEIMODE(vap->va_type, vap->va_mode);
466 fmni.rdev = vap->va_rdev;
467 if (fuse_libabi_geq(data, 7, 12)) {
468 insize = sizeof(fmni);
469 fmni.umask = curthread->td_proc->p_fd->fd_cmask;
471 insize = FUSE_COMPAT_MKNOD_IN_SIZE;
473 return (fuse_internal_newentry(dvp, vpp, cnp, FUSE_MKNOD, &fmni,
474 insize, vap->va_type));
480 fuse_internal_readdir(struct vnode *vp,
483 struct fuse_filehandle *fufh,
484 struct fuse_iov *cookediov,
489 struct fuse_dispatcher fdi;
490 struct fuse_read_in *fri = NULL;
493 if (uio_resid(uio) == 0)
498 * Note that we DO NOT have a UIO_SYSSPACE here (so no need for p2p
503 * fnd_start is set non-zero once the offset in the directory gets
504 * to the startoff. This is done because directories must be read
505 * from the beginning (offset == 0) when fuse_vnop_readdir() needs
506 * to do an open of the directory.
507 * If it is not set non-zero here, it will be set non-zero in
508 * fuse_internal_readdir_processdata() when uio_offset == startoff.
511 if (uio->uio_offset == startoff)
513 while (uio_resid(uio) > 0) {
514 fdi.iosize = sizeof(*fri);
516 fdisp_make_vp(&fdi, FUSE_READDIR, vp, NULL, NULL);
518 fdisp_refresh_vp(&fdi, FUSE_READDIR, vp, NULL, NULL);
521 fri->fh = fufh->fh_id;
522 fri->offset = uio_offset(uio);
523 fri->size = MIN(uio->uio_resid,
524 fuse_get_mpdata(vp->v_mount)->max_read);
526 if ((err = fdisp_wait_answ(&fdi)))
528 if ((err = fuse_internal_readdir_processdata(uio, startoff,
529 &fnd_start, fri->size, fdi.answ, fdi.iosize, cookediov,
530 ncookies, &cookies)))
535 return ((err == -1) ? 0 : err);
539 * Return -1 to indicate that this readdir is finished, 0 if it copied
540 * all the directory data read in and it may be possible to read more
541 * and greater than 0 for a failure.
544 fuse_internal_readdir_processdata(struct uio *uio,
550 struct fuse_iov *cookediov,
559 struct fuse_dirent *fudge;
563 if (bufsize < FUSE_NAME_OFFSET)
566 if (bufsize < FUSE_NAME_OFFSET) {
570 fudge = (struct fuse_dirent *)buf;
571 freclen = FUSE_DIRENT_SIZE(fudge);
573 if (bufsize < freclen) {
575 * This indicates a partial directory entry at the
576 * end of the directory data.
581 #ifdef ZERO_PAD_INCOMPLETE_BUFS
582 if (isbzero(buf, FUSE_NAME_OFFSET)) {
588 if (!fudge->namelen || fudge->namelen > MAXNAMLEN) {
592 bytesavail = GENERIC_DIRSIZ((struct pseudo_dirent *)
595 if (bytesavail > uio_resid(uio)) {
596 /* Out of space for the dir so we are done. */
601 * Don't start to copy the directory entries out until
602 * the requested offset in the directory is found.
604 if (*fnd_start != 0) {
605 fiov_adjust(cookediov, bytesavail);
606 bzero(cookediov->base, bytesavail);
608 de = (struct dirent *)cookediov->base;
609 de->d_fileno = fudge->ino;
610 de->d_reclen = bytesavail;
611 de->d_type = fudge->type;
612 de->d_namlen = fudge->namelen;
613 memcpy((char *)cookediov->base + sizeof(struct dirent) -
615 (char *)buf + FUSE_NAME_OFFSET, fudge->namelen);
616 dirent_terminate(de);
618 err = uiomove(cookediov->base, cookediov->len, uio);
621 if (cookies != NULL) {
622 if (*ncookies == 0) {
626 *cookies = fudge->off;
630 } else if (startoff == fudge->off)
632 buf = (char *)buf + freclen;
634 uio_setoffset(uio, fudge->off);
644 fuse_internal_remove(struct vnode *dvp,
646 struct componentname *cnp,
649 struct fuse_dispatcher fdi;
652 fdisp_init(&fdi, cnp->cn_namelen + 1);
653 fdisp_make_vp(&fdi, op, dvp, cnp->cn_thread, cnp->cn_cred);
655 memcpy(fdi.indata, cnp->cn_nameptr, cnp->cn_namelen);
656 ((char *)fdi.indata)[cnp->cn_namelen] = '\0';
658 err = fdisp_wait_answ(&fdi);
666 fuse_internal_rename(struct vnode *fdvp,
667 struct componentname *fcnp,
669 struct componentname *tcnp)
671 struct fuse_dispatcher fdi;
672 struct fuse_rename_in *fri;
675 fdisp_init(&fdi, sizeof(*fri) + fcnp->cn_namelen + tcnp->cn_namelen + 2);
676 fdisp_make_vp(&fdi, FUSE_RENAME, fdvp, tcnp->cn_thread, tcnp->cn_cred);
679 fri->newdir = VTOI(tdvp);
680 memcpy((char *)fdi.indata + sizeof(*fri), fcnp->cn_nameptr,
682 ((char *)fdi.indata)[sizeof(*fri) + fcnp->cn_namelen] = '\0';
683 memcpy((char *)fdi.indata + sizeof(*fri) + fcnp->cn_namelen + 1,
684 tcnp->cn_nameptr, tcnp->cn_namelen);
685 ((char *)fdi.indata)[sizeof(*fri) + fcnp->cn_namelen +
686 tcnp->cn_namelen + 1] = '\0';
688 err = fdisp_wait_answ(&fdi);
695 /* entity creation */
698 fuse_internal_newentry_makerequest(struct mount *mp,
700 struct componentname *cnp,
704 struct fuse_dispatcher *fdip)
706 fdip->iosize = bufsize + cnp->cn_namelen + 1;
708 fdisp_make(fdip, op, mp, dnid, cnp->cn_thread, cnp->cn_cred);
709 memcpy(fdip->indata, buf, bufsize);
710 memcpy((char *)fdip->indata + bufsize, cnp->cn_nameptr, cnp->cn_namelen);
711 ((char *)fdip->indata)[bufsize + cnp->cn_namelen] = '\0';
715 fuse_internal_newentry_core(struct vnode *dvp,
717 struct componentname *cnp,
719 struct fuse_dispatcher *fdip)
722 struct fuse_entry_out *feo;
723 struct mount *mp = vnode_mount(dvp);
725 if ((err = fdisp_wait_answ(fdip))) {
730 if ((err = fuse_internal_checkentry(feo, vtyp))) {
733 err = fuse_vnode_get(mp, feo, feo->nodeid, dvp, vpp, cnp, vtyp);
735 fuse_internal_forget_send(mp, cnp->cn_thread, cnp->cn_cred,
741 * Purge the parent's attribute cache because the daemon should've
742 * updated its mtime and ctime
744 fuse_vnode_clear_attr_cache(dvp);
746 fuse_internal_cache_attrs(*vpp, &feo->attr, feo->attr_valid,
747 feo->attr_valid_nsec, NULL);
753 fuse_internal_newentry(struct vnode *dvp,
755 struct componentname *cnp,
762 struct fuse_dispatcher fdi;
763 struct mount *mp = vnode_mount(dvp);
766 fuse_internal_newentry_makerequest(mp, VTOI(dvp), cnp, op, buf,
768 err = fuse_internal_newentry_core(dvp, vpp, cnp, vtype, &fdi);
774 /* entity destruction */
777 fuse_internal_forget_callback(struct fuse_ticket *ftick, struct uio *uio)
779 fuse_internal_forget_send(ftick->tk_data->mp, curthread, NULL,
780 ((struct fuse_in_header *)ftick->tk_ms_fiov.base)->nodeid, 1);
786 fuse_internal_forget_send(struct mount *mp,
793 struct fuse_dispatcher fdi;
794 struct fuse_forget_in *ffi;
797 * KASSERT(nlookup > 0, ("zero-times forget for vp #%llu",
798 * (long long unsigned) nodeid));
801 fdisp_init(&fdi, sizeof(*ffi));
802 fdisp_make(&fdi, FUSE_FORGET, mp, nodeid, td, cred);
805 ffi->nlookup = nlookup;
807 fuse_insert_message(fdi.tick, false);
811 /* Fetch the vnode's attributes from the daemon*/
813 fuse_internal_do_getattr(struct vnode *vp, struct vattr *vap,
814 struct ucred *cred, struct thread *td)
816 struct fuse_dispatcher fdi;
817 struct fuse_vnode_data *fvdat = VTOFUD(vp);
818 struct fuse_getattr_in *fgai;
819 struct fuse_attr_out *fao;
820 off_t old_filesize = fvdat->cached_attrs.va_size;
825 fdisp_make_vp(&fdi, FUSE_GETATTR, vp, td, cred);
828 * We could look up a file handle and set it in fgai->fh, but that
829 * involves extra runtime work and I'm unaware of any file systems that
832 fgai->getattr_flags = 0;
833 if ((err = fdisp_simple_putget_vp(&fdi, FUSE_GETATTR, vp, td, cred))) {
835 fuse_internal_vnode_disappear(vp);
839 fao = (struct fuse_attr_out *)fdi.answ;
840 vtyp = IFTOVT(fao->attr.mode);
841 if (fvdat->flag & FN_SIZECHANGE)
842 fao->attr.size = old_filesize;
843 fuse_internal_cache_attrs(vp, &fao->attr, fao->attr_valid,
844 fao->attr_valid_nsec, vap);
845 if (vtyp != vnode_vtype(vp)) {
846 fuse_internal_vnode_disappear(vp);
855 /* Read a vnode's attributes from cache or fetch them from the fuse daemon */
857 fuse_internal_getattr(struct vnode *vp, struct vattr *vap, struct ucred *cred,
862 if ((attrs = VTOVA(vp)) != NULL) {
863 *vap = *attrs; /* struct copy */
867 return fuse_internal_do_getattr(vp, vap, cred, td);
871 fuse_internal_vnode_disappear(struct vnode *vp)
873 struct fuse_vnode_data *fvdat = VTOFUD(vp);
875 ASSERT_VOP_ELOCKED(vp, "fuse_internal_vnode_disappear");
876 fvdat->flag |= FN_REVOKED;
877 bintime_clear(&fvdat->attr_cache_timeout);
878 bintime_clear(&fvdat->entry_cache_timeout);
882 /* fuse start/stop */
885 fuse_internal_init_callback(struct fuse_ticket *tick, struct uio *uio)
888 struct fuse_data *data = tick->tk_data;
889 struct fuse_init_out *fiio;
891 if ((err = tick->tk_aw_ohead.error)) {
894 if ((err = fticket_pull(tick, uio))) {
897 fiio = fticket_resp(tick)->base;
899 /* XXX: Do we want to check anything further besides this? */
900 if (fiio->major < 7) {
901 SDT_PROBE2(fusefs, , internal, trace, 1,
902 "userpace version too low");
903 err = EPROTONOSUPPORT;
906 data->fuse_libabi_major = fiio->major;
907 data->fuse_libabi_minor = fiio->minor;
909 if (fuse_libabi_geq(data, 7, 5)) {
910 if (fticket_resp(tick)->len == sizeof(struct fuse_init_out)) {
911 data->max_readahead = fiio->max_readahead;
912 data->max_write = fiio->max_write;
913 if (fiio->flags & FUSE_ASYNC_READ)
914 data->dataflags |= FSESS_ASYNC_READ;
915 if (fiio->flags & FUSE_POSIX_LOCKS)
916 data->dataflags |= FSESS_POSIX_LOCKS;
917 if (fiio->flags & FUSE_EXPORT_SUPPORT)
918 data->dataflags |= FSESS_EXPORT_SUPPORT;
920 * Don't bother to check FUSE_BIG_WRITES, because it's
921 * redundant with max_write
928 data->max_write = 4096;
933 fdata_set_dead(data);
936 data->dataflags |= FSESS_INITED;
937 wakeup(&data->ticketer);
944 fuse_internal_send_init(struct fuse_data *data, struct thread *td)
946 struct fuse_init_in *fiii;
947 struct fuse_dispatcher fdi;
949 fdisp_init(&fdi, sizeof(*fiii));
950 fdisp_make(&fdi, FUSE_INIT, data->mp, 0, td, NULL);
952 fiii->major = FUSE_KERNEL_VERSION;
953 fiii->minor = FUSE_KERNEL_MINOR_VERSION;
955 * fusefs currently reads ahead no more than one cache block at a time.
956 * See fuse_read_biobackend
958 fiii->max_readahead = maxbcachebuf;
960 * Unsupported features:
961 * FUSE_FILE_OPS: No known FUSE server or client supports it
962 * FUSE_ATOMIC_O_TRUNC: our VFS cannot support it
963 * FUSE_DONT_MASK: unlike Linux, FreeBSD always applies the umask, even
964 * when default ACLs are in use.
966 fiii->flags = FUSE_ASYNC_READ | FUSE_POSIX_LOCKS | FUSE_EXPORT_SUPPORT
969 fuse_insert_callback(fdi.tick, fuse_internal_init_callback);
970 fuse_insert_message(fdi.tick, false);
975 * Send a FUSE_SETATTR operation with no permissions checks. If cred is NULL,
976 * send the request with root credentials
978 int fuse_internal_setattr(struct vnode *vp, struct vattr *vap,
979 struct thread *td, struct ucred *cred)
981 struct fuse_dispatcher fdi;
982 struct fuse_setattr_in *fsai;
984 pid_t pid = td->td_proc->p_pid;
985 struct fuse_data *data;
989 int sizechanged = -1;
990 uint64_t newsize = 0;
992 mp = vnode_mount(vp);
993 data = fuse_get_mpdata(mp);
994 dataflags = data->dataflags;
996 fdisp_init(&fdi, sizeof(*fsai));
997 fdisp_make_vp(&fdi, FUSE_SETATTR, vp, td, cred);
1005 if (vap->va_uid != (uid_t)VNOVAL) {
1006 fsai->uid = vap->va_uid;
1007 fsai->valid |= FATTR_UID;
1009 if (vap->va_gid != (gid_t)VNOVAL) {
1010 fsai->gid = vap->va_gid;
1011 fsai->valid |= FATTR_GID;
1013 if (vap->va_size != VNOVAL) {
1014 struct fuse_filehandle *fufh = NULL;
1016 /*Truncate to a new value. */
1017 fsai->size = vap->va_size;
1019 newsize = vap->va_size;
1020 fsai->valid |= FATTR_SIZE;
1022 fuse_filehandle_getrw(vp, FWRITE, &fufh, cred, pid);
1024 fsai->fh = fufh->fh_id;
1025 fsai->valid |= FATTR_FH;
1027 VTOFUD(vp)->flag &= ~FN_SIZECHANGE;
1029 if (vap->va_atime.tv_sec != VNOVAL) {
1030 fsai->atime = vap->va_atime.tv_sec;
1031 fsai->atimensec = vap->va_atime.tv_nsec;
1032 fsai->valid |= FATTR_ATIME;
1033 if (vap->va_vaflags & VA_UTIMES_NULL)
1034 fsai->valid |= FATTR_ATIME_NOW;
1036 if (vap->va_mtime.tv_sec != VNOVAL) {
1037 fsai->mtime = vap->va_mtime.tv_sec;
1038 fsai->mtimensec = vap->va_mtime.tv_nsec;
1039 fsai->valid |= FATTR_MTIME;
1040 if (vap->va_vaflags & VA_UTIMES_NULL)
1041 fsai->valid |= FATTR_MTIME_NOW;
1043 if (vap->va_mode != (mode_t)VNOVAL) {
1044 fsai->mode = vap->va_mode & ALLPERMS;
1045 fsai->valid |= FATTR_MODE;
1051 if ((err = fdisp_wait_answ(&fdi)))
1053 vtyp = IFTOVT(((struct fuse_attr_out *)fdi.answ)->attr.mode);
1055 if (vnode_vtype(vp) != vtyp) {
1056 if (vnode_vtype(vp) == VNON && vtyp != VNON) {
1057 SDT_PROBE2(fusefs, , internal, trace, 1, "FUSE: Dang! "
1058 "vnode_vtype is VNON and vtype isn't.");
1061 * STALE vnode, ditch
1063 * The vnode has changed its type "behind our back".
1064 * There's nothing really we can do, so let us just
1065 * force an internal revocation and tell the caller to
1066 * try again, if interested.
1068 fuse_internal_vnode_disappear(vp);
1073 struct fuse_attr_out *fao = (struct fuse_attr_out*)fdi.answ;
1074 fuse_internal_cache_attrs(vp, &fao->attr, fao->attr_valid,
1075 fao->attr_valid_nsec, NULL);
1079 fdisp_destroy(&fdi);
1083 #ifdef ZERO_PAD_INCOMPLETE_BUFS
1085 isbzero(void *buf, size_t len)
1089 for (i = 0; i < len; i++) {
1090 if (((char *)buf)[i])