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"
93 #include "fuse_node.h"
94 #include "fuse_file.h"
96 SDT_PROVIDER_DECLARE(fuse);
99 * arg0: verbosity. Higher numbers give more verbose messages
100 * arg1: Textual message
102 SDT_PROBE_DEFINE2(fuse, , internal, trace, "int", "char*");
104 #ifdef ZERO_PAD_INCOMPLETE_BUFS
105 static int isbzero(void *buf, size_t len);
109 /* Synchronously send a FUSE_ACCESS operation */
111 fuse_internal_access(struct vnode *vp,
113 struct fuse_access_param *facp,
118 uint32_t mask = F_OK;
122 struct fuse_dispatcher fdi;
123 struct fuse_access_in *fai;
124 struct fuse_data *data;
126 mp = vnode_mount(vp);
127 vtype = vnode_vtype(vp);
129 data = fuse_get_mpdata(mp);
130 dataflags = data->dataflags;
132 if ((mode & VWRITE) && vfs_isrdonly(mp)) {
136 /* Unless explicitly permitted, deny everyone except the fs owner. */
137 if (!(facp->facc_flags)) {
138 if (!(dataflags & FSESS_DAEMON_CAN_SPY)) {
139 int denied = fuse_match_cred(data->daemoncred, cred);
146 if (dataflags & FSESS_DEFAULT_PERMISSIONS) {
147 /* TODO: Implement me! Bug 216391 */
151 if (!fsess_isimpl(mp, FUSE_ACCESS))
154 if ((mode & (VWRITE | VAPPEND | VADMIN)) != 0)
156 if ((mode & VREAD) != 0)
158 if ((mode & VEXEC) != 0)
161 fdisp_init(&fdi, sizeof(*fai));
162 fdisp_make_vp(&fdi, FUSE_ACCESS, vp, td, cred);
167 err = fdisp_wait_answ(&fdi);
171 fsess_set_notimpl(mp, FUSE_ACCESS);
178 * Cache FUSE attributes from attr, in attribute cache associated with vnode
179 * 'vp'. Optionally, if argument 'vap' is not NULL, store a copy of the
180 * converted attributes there as well.
182 * If the nominal attribute cache TTL is zero, do not cache on the 'vp' (but do
183 * return the result to the caller).
186 fuse_internal_cache_attrs(struct vnode *vp, struct fuse_attr *attr,
187 uint64_t attr_valid, uint32_t attr_valid_nsec, struct vattr *vap)
190 struct fuse_vnode_data *fvdat;
191 struct vattr *vp_cache_at;
193 mp = vnode_mount(vp);
196 fuse_validity_2_bintime(attr_valid, attr_valid_nsec,
197 &fvdat->attr_cache_timeout);
199 vp_cache_at = VTOVA(vp);
201 if (vap == NULL && vp_cache_at == NULL)
209 vap->va_fsid = mp->mnt_stat.f_fsid.val[0];
210 vap->va_fileid = attr->ino;
211 vap->va_mode = attr->mode & ~S_IFMT;
212 vap->va_nlink = attr->nlink;
213 vap->va_uid = attr->uid;
214 vap->va_gid = attr->gid;
215 vap->va_rdev = attr->rdev;
216 vap->va_size = attr->size;
217 /* XXX on i386, seconds are truncated to 32 bits */
218 vap->va_atime.tv_sec = attr->atime;
219 vap->va_atime.tv_nsec = attr->atimensec;
220 vap->va_mtime.tv_sec = attr->mtime;
221 vap->va_mtime.tv_nsec = attr->mtimensec;
222 vap->va_ctime.tv_sec = attr->ctime;
223 vap->va_ctime.tv_nsec = attr->ctimensec;
224 vap->va_blocksize = PAGE_SIZE;
225 vap->va_type = IFTOVT(attr->mode);
226 vap->va_bytes = attr->blocks * S_BLKSIZE;
229 if (vap != vp_cache_at && vp_cache_at != NULL)
230 memcpy(vp_cache_at, vap, sizeof(*vap));
237 fuse_internal_fsync_callback(struct fuse_ticket *tick, struct uio *uio)
239 if (tick->tk_aw_ohead.error == ENOSYS) {
240 fsess_set_notimpl(tick->tk_data->mp, fticket_opcode(tick));
246 fuse_internal_fsync(struct vnode *vp,
251 struct fuse_fsync_in *ffsi = NULL;
252 struct fuse_dispatcher fdi;
253 struct fuse_filehandle *fufh;
254 struct fuse_vnode_data *fvdat = VTOFUD(vp);
258 if (!fsess_isimpl(vnode_mount(vp),
259 (vnode_vtype(vp) == VDIR ? FUSE_FSYNCDIR : FUSE_FSYNC))) {
265 fdisp_init(&fdi, sizeof(*ffsi));
267 * fsync every open file handle for this file, because we can't be sure
268 * which file handle the caller is really referring to.
270 LIST_FOREACH(fufh, &fvdat->handles, next) {
272 fdisp_make_vp(&fdi, op, vp, td, NULL);
274 fdisp_refresh_vp(&fdi, op, vp, td, NULL);
276 ffsi->fh = fufh->fh_id;
277 ffsi->fsync_flags = 0;
280 ffsi->fsync_flags = 1;
282 if (waitfor == MNT_WAIT) {
283 err = fdisp_wait_answ(&fdi);
285 fuse_insert_callback(fdi.tick,
286 fuse_internal_fsync_callback);
287 fuse_insert_message(fdi.tick);
298 fuse_internal_readdir(struct vnode *vp,
300 struct fuse_filehandle *fufh,
301 struct fuse_iov *cookediov)
304 struct fuse_dispatcher fdi;
305 struct fuse_read_in *fri = NULL;
307 if (uio_resid(uio) == 0) {
313 * Note that we DO NOT have a UIO_SYSSPACE here (so no need for p2p
317 while (uio_resid(uio) > 0) {
318 fdi.iosize = sizeof(*fri);
320 fdisp_make_vp(&fdi, FUSE_READDIR, vp, NULL, NULL);
322 fdisp_refresh_vp(&fdi, FUSE_READDIR, vp, NULL, NULL);
325 fri->fh = fufh->fh_id;
326 fri->offset = uio_offset(uio);
327 fri->size = MIN(uio->uio_resid,
328 fuse_get_mpdata(vp->v_mount)->max_read);
330 if ((err = fdisp_wait_answ(&fdi))) {
333 if ((err = fuse_internal_readdir_processdata(uio, fri->size, fdi.answ,
334 fdi.iosize, cookediov))) {
340 return ((err == -1) ? 0 : err);
344 fuse_internal_readdir_processdata(struct uio *uio,
356 struct fuse_dirent *fudge;
357 struct fuse_iov *cookediov = param;
359 if (bufsize < FUSE_NAME_OFFSET) {
364 if (bufsize < FUSE_NAME_OFFSET) {
368 fudge = (struct fuse_dirent *)buf;
369 freclen = FUSE_DIRENT_SIZE(fudge);
373 if (bufsize < freclen) {
374 err = ((cou == 1) ? -1 : 0);
377 #ifdef ZERO_PAD_INCOMPLETE_BUFS
378 if (isbzero(buf, FUSE_NAME_OFFSET)) {
384 if (!fudge->namelen || fudge->namelen > MAXNAMLEN) {
388 bytesavail = GENERIC_DIRSIZ((struct pseudo_dirent *)
391 if (bytesavail > uio_resid(uio)) {
395 fiov_adjust(cookediov, bytesavail);
396 bzero(cookediov->base, bytesavail);
398 de = (struct dirent *)cookediov->base;
399 de->d_fileno = fudge->ino;
400 de->d_reclen = bytesavail;
401 de->d_type = fudge->type;
402 de->d_namlen = fudge->namelen;
403 memcpy((char *)cookediov->base + sizeof(struct dirent) -
405 (char *)buf + FUSE_NAME_OFFSET, fudge->namelen);
406 dirent_terminate(de);
408 err = uiomove(cookediov->base, cookediov->len, uio);
412 buf = (char *)buf + freclen;
414 uio_setoffset(uio, fudge->off);
423 fuse_internal_remove(struct vnode *dvp,
425 struct componentname *cnp,
428 struct fuse_dispatcher fdi;
431 fdisp_init(&fdi, cnp->cn_namelen + 1);
432 fdisp_make_vp(&fdi, op, dvp, cnp->cn_thread, cnp->cn_cred);
434 memcpy(fdi.indata, cnp->cn_nameptr, cnp->cn_namelen);
435 ((char *)fdi.indata)[cnp->cn_namelen] = '\0';
437 err = fdisp_wait_answ(&fdi);
445 fuse_internal_rename(struct vnode *fdvp,
446 struct componentname *fcnp,
448 struct componentname *tcnp)
450 struct fuse_dispatcher fdi;
451 struct fuse_rename_in *fri;
454 fdisp_init(&fdi, sizeof(*fri) + fcnp->cn_namelen + tcnp->cn_namelen + 2);
455 fdisp_make_vp(&fdi, FUSE_RENAME, fdvp, tcnp->cn_thread, tcnp->cn_cred);
458 fri->newdir = VTOI(tdvp);
459 memcpy((char *)fdi.indata + sizeof(*fri), fcnp->cn_nameptr,
461 ((char *)fdi.indata)[sizeof(*fri) + fcnp->cn_namelen] = '\0';
462 memcpy((char *)fdi.indata + sizeof(*fri) + fcnp->cn_namelen + 1,
463 tcnp->cn_nameptr, tcnp->cn_namelen);
464 ((char *)fdi.indata)[sizeof(*fri) + fcnp->cn_namelen +
465 tcnp->cn_namelen + 1] = '\0';
467 err = fdisp_wait_answ(&fdi);
474 /* entity creation */
477 fuse_internal_newentry_makerequest(struct mount *mp,
479 struct componentname *cnp,
483 struct fuse_dispatcher *fdip)
485 fdip->iosize = bufsize + cnp->cn_namelen + 1;
487 fdisp_make(fdip, op, mp, dnid, cnp->cn_thread, cnp->cn_cred);
488 memcpy(fdip->indata, buf, bufsize);
489 memcpy((char *)fdip->indata + bufsize, cnp->cn_nameptr, cnp->cn_namelen);
490 ((char *)fdip->indata)[bufsize + cnp->cn_namelen] = '\0';
494 fuse_internal_newentry_core(struct vnode *dvp,
496 struct componentname *cnp,
498 struct fuse_dispatcher *fdip)
501 struct fuse_entry_out *feo;
502 struct mount *mp = vnode_mount(dvp);
504 if ((err = fdisp_wait_answ(fdip))) {
509 if ((err = fuse_internal_checkentry(feo, vtyp))) {
512 err = fuse_vnode_get(mp, feo, feo->nodeid, dvp, vpp, cnp, vtyp);
514 fuse_internal_forget_send(mp, cnp->cn_thread, cnp->cn_cred,
518 fuse_internal_cache_attrs(*vpp, &feo->attr, feo->attr_valid,
519 feo->attr_valid_nsec, NULL);
525 fuse_internal_newentry(struct vnode *dvp,
527 struct componentname *cnp,
534 struct fuse_dispatcher fdi;
535 struct mount *mp = vnode_mount(dvp);
538 fuse_internal_newentry_makerequest(mp, VTOI(dvp), cnp, op, buf,
540 err = fuse_internal_newentry_core(dvp, vpp, cnp, vtype, &fdi);
546 /* entity destruction */
549 fuse_internal_forget_callback(struct fuse_ticket *ftick, struct uio *uio)
551 fuse_internal_forget_send(ftick->tk_data->mp, curthread, NULL,
552 ((struct fuse_in_header *)ftick->tk_ms_fiov.base)->nodeid, 1);
558 fuse_internal_forget_send(struct mount *mp,
565 struct fuse_dispatcher fdi;
566 struct fuse_forget_in *ffi;
569 * KASSERT(nlookup > 0, ("zero-times forget for vp #%llu",
570 * (long long unsigned) nodeid));
573 fdisp_init(&fdi, sizeof(*ffi));
574 fdisp_make(&fdi, FUSE_FORGET, mp, nodeid, td, cred);
577 ffi->nlookup = nlookup;
579 fuse_insert_message(fdi.tick);
583 /* Read a vnode's attributes from cache or fetch them from the fuse daemon */
585 fuse_internal_getattr(struct vnode *vp, struct vattr *vap, struct ucred *cred,
588 struct fuse_dispatcher fdi;
589 struct fuse_vnode_data *fvdat = VTOFUD(vp);
591 struct fuse_attr_out *fao;
594 if ((attrs = VTOVA(vp)) != NULL) {
597 if ((fvdat->flag & FN_SIZECHANGE) != 0)
598 vap->va_size = fvdat->filesize;
603 if ((err = fdisp_simple_putget_vp(&fdi, FUSE_GETATTR, vp, td, cred))) {
605 fuse_internal_vnode_disappear(vp);
610 fao = (struct fuse_attr_out *)fdi.answ;
611 fuse_internal_cache_attrs(vp, &fao->attr, fao->attr_valid,
612 fao->attr_valid_nsec, vap);
613 if (vap->va_type != vnode_vtype(vp)) {
614 fuse_internal_vnode_disappear(vp);
618 if ((fvdat->flag & FN_SIZECHANGE) != 0)
619 vap->va_size = fvdat->filesize;
621 if (vnode_isreg(vp) && (fvdat->flag & FN_SIZECHANGE) == 0) {
623 * This is for those cases when the file size changed without us
624 * knowing, and we want to catch up.
626 off_t new_filesize = fao->attr.size;
628 if (fvdat->filesize != new_filesize) {
629 fuse_vnode_setsize(vp, cred, new_filesize);
630 fvdat->flag &= ~FN_SIZECHANGE;
640 fuse_internal_vnode_disappear(struct vnode *vp)
642 struct fuse_vnode_data *fvdat = VTOFUD(vp);
644 ASSERT_VOP_ELOCKED(vp, "fuse_internal_vnode_disappear");
645 fvdat->flag |= FN_REVOKED;
646 bintime_clear(&fvdat->attr_cache_timeout);
650 /* fuse start/stop */
653 fuse_internal_init_callback(struct fuse_ticket *tick, struct uio *uio)
656 struct fuse_data *data = tick->tk_data;
657 struct fuse_init_out *fiio;
659 if ((err = tick->tk_aw_ohead.error)) {
662 if ((err = fticket_pull(tick, uio))) {
665 fiio = fticket_resp(tick)->base;
667 /* XXX: Do we want to check anything further besides this? */
668 if (fiio->major < 7) {
669 SDT_PROBE2(fuse, , internal, trace, 1,
670 "userpace version too low");
671 err = EPROTONOSUPPORT;
674 data->fuse_libabi_major = fiio->major;
675 data->fuse_libabi_minor = fiio->minor;
677 if (fuse_libabi_geq(data, 7, 5)) {
678 if (fticket_resp(tick)->len == sizeof(struct fuse_init_out)) {
679 data->max_write = fiio->max_write;
685 data->max_write = 4096;
690 fdata_set_dead(data);
693 data->dataflags |= FSESS_INITED;
694 wakeup(&data->ticketer);
701 fuse_internal_send_init(struct fuse_data *data, struct thread *td)
703 struct fuse_init_in *fiii;
704 struct fuse_dispatcher fdi;
706 fdisp_init(&fdi, sizeof(*fiii));
707 fdisp_make(&fdi, FUSE_INIT, data->mp, 0, td, NULL);
709 fiii->major = FUSE_KERNEL_VERSION;
710 fiii->minor = FUSE_KERNEL_MINOR_VERSION;
712 * fusefs currently doesn't do any readahead other than fetching whole
713 * buffer cache block sized regions at once. So the max readahead is
714 * the size of a buffer cache block.
716 fiii->max_readahead = maxbcachebuf;
719 fuse_insert_callback(fdi.tick, fuse_internal_init_callback);
720 fuse_insert_message(fdi.tick);
724 #ifdef ZERO_PAD_INCOMPLETE_BUFS
726 isbzero(void *buf, size_t len)
730 for (i = 0; i < len; i++) {
731 if (((char *)buf)[i])