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;
821 struct timespec old_ctime = fvdat->cached_attrs.va_ctime;
822 struct timespec old_mtime = fvdat->cached_attrs.va_mtime;
827 fdisp_make_vp(&fdi, FUSE_GETATTR, vp, td, cred);
830 * We could look up a file handle and set it in fgai->fh, but that
831 * involves extra runtime work and I'm unaware of any file systems that
834 fgai->getattr_flags = 0;
835 if ((err = fdisp_simple_putget_vp(&fdi, FUSE_GETATTR, vp, td, cred))) {
837 fuse_internal_vnode_disappear(vp);
841 fao = (struct fuse_attr_out *)fdi.answ;
842 vtyp = IFTOVT(fao->attr.mode);
843 if (fvdat->flag & FN_SIZECHANGE)
844 fao->attr.size = old_filesize;
845 if (fvdat->flag & FN_CTIMECHANGE) {
846 fao->attr.ctime = old_ctime.tv_sec;
847 fao->attr.ctimensec = old_ctime.tv_nsec;
849 if (fvdat->flag & FN_MTIMECHANGE) {
850 fao->attr.mtime = old_mtime.tv_sec;
851 fao->attr.mtimensec = old_mtime.tv_nsec;
853 fuse_internal_cache_attrs(vp, &fao->attr, fao->attr_valid,
854 fao->attr_valid_nsec, vap);
855 if (vtyp != vnode_vtype(vp)) {
856 fuse_internal_vnode_disappear(vp);
865 /* Read a vnode's attributes from cache or fetch them from the fuse daemon */
867 fuse_internal_getattr(struct vnode *vp, struct vattr *vap, struct ucred *cred,
872 if ((attrs = VTOVA(vp)) != NULL) {
873 *vap = *attrs; /* struct copy */
877 return fuse_internal_do_getattr(vp, vap, cred, td);
881 fuse_internal_vnode_disappear(struct vnode *vp)
883 struct fuse_vnode_data *fvdat = VTOFUD(vp);
885 ASSERT_VOP_ELOCKED(vp, "fuse_internal_vnode_disappear");
886 fvdat->flag |= FN_REVOKED;
887 bintime_clear(&fvdat->attr_cache_timeout);
888 bintime_clear(&fvdat->entry_cache_timeout);
892 /* fuse start/stop */
895 fuse_internal_init_callback(struct fuse_ticket *tick, struct uio *uio)
898 struct fuse_data *data = tick->tk_data;
899 struct fuse_init_out *fiio;
901 if ((err = tick->tk_aw_ohead.error)) {
904 if ((err = fticket_pull(tick, uio))) {
907 fiio = fticket_resp(tick)->base;
909 data->fuse_libabi_major = fiio->major;
910 data->fuse_libabi_minor = fiio->minor;
911 if (!fuse_libabi_geq(data, 7, 4)) {
913 * With a little work we could support servers as old as 7.1.
914 * But there would be little payoff.
916 SDT_PROBE2(fusefs, , internal, trace, 1,
917 "userpace version too low");
918 err = EPROTONOSUPPORT;
922 if (fuse_libabi_geq(data, 7, 5)) {
923 if (fticket_resp(tick)->len == sizeof(struct fuse_init_out) ||
924 fticket_resp(tick)->len == FUSE_COMPAT_22_INIT_OUT_SIZE) {
925 data->max_write = fiio->max_write;
926 if (fiio->flags & FUSE_ASYNC_READ)
927 data->dataflags |= FSESS_ASYNC_READ;
928 if (fiio->flags & FUSE_POSIX_LOCKS)
929 data->dataflags |= FSESS_POSIX_LOCKS;
930 if (fiio->flags & FUSE_EXPORT_SUPPORT)
931 data->dataflags |= FSESS_EXPORT_SUPPORT;
933 * Don't bother to check FUSE_BIG_WRITES, because it's
934 * redundant with max_write
937 * max_background and congestion_threshold are not
944 /* Old fixed values */
945 data->max_write = 4096;
948 if (fuse_libabi_geq(data, 7, 6))
949 data->max_readahead_blocks = fiio->max_readahead / maxbcachebuf;
951 if (!fuse_libabi_geq(data, 7, 7))
952 fsess_set_notimpl(data->mp, FUSE_INTERRUPT);
954 if (!fuse_libabi_geq(data, 7, 8)) {
955 fsess_set_notimpl(data->mp, FUSE_BMAP);
956 fsess_set_notimpl(data->mp, FUSE_DESTROY);
959 if (fuse_libabi_geq(data, 7, 23) && fiio->time_gran >= 1 &&
960 fiio->time_gran <= 1000000000)
961 data->time_gran = fiio->time_gran;
967 fdata_set_dead(data);
970 data->dataflags |= FSESS_INITED;
971 wakeup(&data->ticketer);
978 fuse_internal_send_init(struct fuse_data *data, struct thread *td)
980 struct fuse_init_in *fiii;
981 struct fuse_dispatcher fdi;
983 fdisp_init(&fdi, sizeof(*fiii));
984 fdisp_make(&fdi, FUSE_INIT, data->mp, 0, td, NULL);
986 fiii->major = FUSE_KERNEL_VERSION;
987 fiii->minor = FUSE_KERNEL_MINOR_VERSION;
989 * fusefs currently reads ahead no more than one cache block at a time.
990 * See fuse_read_biobackend
992 fiii->max_readahead = maxbcachebuf;
994 * Unsupported features:
995 * FUSE_FILE_OPS: No known FUSE server or client supports it
996 * FUSE_ATOMIC_O_TRUNC: our VFS cannot support it
997 * FUSE_DONT_MASK: unlike Linux, FreeBSD always applies the umask, even
998 * when default ACLs are in use.
1000 fiii->flags = FUSE_ASYNC_READ | FUSE_POSIX_LOCKS | FUSE_EXPORT_SUPPORT
1003 fuse_insert_callback(fdi.tick, fuse_internal_init_callback);
1004 fuse_insert_message(fdi.tick, false);
1005 fdisp_destroy(&fdi);
1009 * Send a FUSE_SETATTR operation with no permissions checks. If cred is NULL,
1010 * send the request with root credentials
1012 int fuse_internal_setattr(struct vnode *vp, struct vattr *vap,
1013 struct thread *td, struct ucred *cred)
1015 struct fuse_vnode_data *fvdat;
1016 struct fuse_dispatcher fdi;
1017 struct fuse_setattr_in *fsai;
1019 pid_t pid = td->td_proc->p_pid;
1020 struct fuse_data *data;
1024 int sizechanged = -1;
1025 uint64_t newsize = 0;
1027 mp = vnode_mount(vp);
1029 data = fuse_get_mpdata(mp);
1030 dataflags = data->dataflags;
1032 fdisp_init(&fdi, sizeof(*fsai));
1033 fdisp_make_vp(&fdi, FUSE_SETATTR, vp, td, cred);
1041 if (vap->va_uid != (uid_t)VNOVAL) {
1042 fsai->uid = vap->va_uid;
1043 fsai->valid |= FATTR_UID;
1045 if (vap->va_gid != (gid_t)VNOVAL) {
1046 fsai->gid = vap->va_gid;
1047 fsai->valid |= FATTR_GID;
1049 if (vap->va_size != VNOVAL) {
1050 struct fuse_filehandle *fufh = NULL;
1052 /*Truncate to a new value. */
1053 fsai->size = vap->va_size;
1055 newsize = vap->va_size;
1056 fsai->valid |= FATTR_SIZE;
1058 fuse_filehandle_getrw(vp, FWRITE, &fufh, cred, pid);
1060 fsai->fh = fufh->fh_id;
1061 fsai->valid |= FATTR_FH;
1063 VTOFUD(vp)->flag &= ~FN_SIZECHANGE;
1065 if (vap->va_atime.tv_sec != VNOVAL) {
1066 fsai->atime = vap->va_atime.tv_sec;
1067 fsai->atimensec = vap->va_atime.tv_nsec;
1068 fsai->valid |= FATTR_ATIME;
1069 if (vap->va_vaflags & VA_UTIMES_NULL)
1070 fsai->valid |= FATTR_ATIME_NOW;
1072 if (vap->va_mtime.tv_sec != VNOVAL) {
1073 fsai->mtime = vap->va_mtime.tv_sec;
1074 fsai->mtimensec = vap->va_mtime.tv_nsec;
1075 fsai->valid |= FATTR_MTIME;
1076 if (vap->va_vaflags & VA_UTIMES_NULL)
1077 fsai->valid |= FATTR_MTIME_NOW;
1078 } else if (fvdat->flag & FN_MTIMECHANGE) {
1079 fsai->mtime = fvdat->cached_attrs.va_mtime.tv_sec;
1080 fsai->mtimensec = fvdat->cached_attrs.va_mtime.tv_nsec;
1081 fsai->valid |= FATTR_MTIME;
1083 if (fuse_libabi_geq(data, 7, 23) && fvdat->flag & FN_CTIMECHANGE) {
1084 fsai->ctime = fvdat->cached_attrs.va_ctime.tv_sec;
1085 fsai->ctimensec = fvdat->cached_attrs.va_ctime.tv_nsec;
1086 fsai->valid |= FATTR_CTIME;
1088 if (vap->va_mode != (mode_t)VNOVAL) {
1089 fsai->mode = vap->va_mode & ALLPERMS;
1090 fsai->valid |= FATTR_MODE;
1096 if ((err = fdisp_wait_answ(&fdi)))
1098 vtyp = IFTOVT(((struct fuse_attr_out *)fdi.answ)->attr.mode);
1100 if (vnode_vtype(vp) != vtyp) {
1101 if (vnode_vtype(vp) == VNON && vtyp != VNON) {
1102 SDT_PROBE2(fusefs, , internal, trace, 1, "FUSE: Dang! "
1103 "vnode_vtype is VNON and vtype isn't.");
1106 * STALE vnode, ditch
1108 * The vnode has changed its type "behind our back".
1109 * There's nothing really we can do, so let us just
1110 * force an internal revocation and tell the caller to
1111 * try again, if interested.
1113 fuse_internal_vnode_disappear(vp);
1118 struct fuse_attr_out *fao = (struct fuse_attr_out*)fdi.answ;
1119 fuse_vnode_undirty_cached_timestamps(vp);
1120 fuse_internal_cache_attrs(vp, &fao->attr, fao->attr_valid,
1121 fao->attr_valid_nsec, NULL);
1125 fdisp_destroy(&fdi);
1129 #ifdef ZERO_PAD_INCOMPLETE_BUFS
1131 isbzero(void *buf, size_t len)
1135 for (i = 0; i < len; i++) {
1136 if (((char *)buf)[i])