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"
95 #include "fuse_param.h"
97 SDT_PROVIDER_DECLARE(fuse);
100 * arg0: verbosity. Higher numbers give more verbose messages
101 * arg1: Textual message
103 SDT_PROBE_DEFINE2(fuse, , internal, trace, "int", "char*");
105 #ifdef ZERO_PAD_INCOMPLETE_BUFS
106 static int isbzero(void *buf, size_t len);
113 fuse_internal_access(struct vnode *vp,
115 struct fuse_access_param *facp,
124 struct fuse_dispatcher fdi;
125 struct fuse_access_in *fai;
126 struct fuse_data *data;
130 * If this vnop gives you trouble, just return 0 here for a lazy
135 mp = vnode_mount(vp);
136 vtype = vnode_vtype(vp);
138 data = fuse_get_mpdata(mp);
139 dataflags = data->dataflags;
141 if ((mode & VWRITE) && vfs_isrdonly(mp)) {
144 /* Unless explicitly permitted, deny everyone except the fs owner. */
145 if (vnode_isvroot(vp) && !(facp->facc_flags & FACCESS_NOCHECKSPY)) {
146 if (!(dataflags & FSESS_DAEMON_CAN_SPY)) {
147 int denied = fuse_match_cred(data->daemoncred,
154 facp->facc_flags |= FACCESS_NOCHECKSPY;
156 if (!(facp->facc_flags & FACCESS_DO_ACCESS)) {
159 if (((vtype == VREG) && (mode & VEXEC))) {
160 #ifdef NEED_MOUNT_ARGUMENT_FOR_THIS
161 /* Let the kernel handle this through open / close heuristics.*/
164 /* Let the kernel handle this. */
168 if (!fsess_isimpl(mp, FUSE_ACCESS)) {
169 /* Let the kernel handle this. */
172 if (dataflags & FSESS_DEFAULT_PERMISSIONS) {
173 /* Let the kernel handle this. */
176 if ((mode & VADMIN) != 0) {
177 err = priv_check_cred(cred, PRIV_VFS_ADMIN);
182 if ((mode & (VWRITE | VAPPEND | VADMIN)) != 0) {
185 if ((mode & VREAD) != 0) {
188 if ((mode & VEXEC) != 0) {
191 bzero(&fdi, sizeof(fdi));
193 fdisp_init(&fdi, sizeof(*fai));
194 fdisp_make_vp(&fdi, FUSE_ACCESS, vp, td, cred);
200 err = fdisp_wait_answ(&fdi);
204 fsess_set_notimpl(mp, FUSE_ACCESS);
211 * Cache FUSE attributes from feo, in attr cache associated with vnode 'vp'.
212 * Optionally, if argument 'vap' is not NULL, store a copy of the converted
213 * attributes there as well.
215 * If the nominal attribute cache TTL is zero, do not cache on the 'vp' (but do
216 * return the result to the caller).
219 fuse_internal_cache_attrs(struct vnode *vp, struct fuse_attr *attr,
220 uint64_t attr_valid, uint32_t attr_valid_nsec, struct vattr *vap)
223 struct fuse_vnode_data *fvdat;
224 struct vattr *vp_cache_at;
226 mp = vnode_mount(vp);
229 /* Honor explicit do-not-cache requests from user filesystems. */
230 if (attr_valid == 0 && attr_valid_nsec == 0)
231 fvdat->valid_attr_cache = false;
233 fvdat->valid_attr_cache = true;
235 vp_cache_at = VTOVA(vp);
237 if (vap == NULL && vp_cache_at == NULL)
245 vap->va_fsid = mp->mnt_stat.f_fsid.val[0];
246 vap->va_fileid = attr->ino;
247 vap->va_mode = attr->mode & ~S_IFMT;
248 vap->va_nlink = attr->nlink;
249 vap->va_uid = attr->uid;
250 vap->va_gid = attr->gid;
251 vap->va_rdev = attr->rdev;
252 vap->va_size = attr->size;
253 /* XXX on i386, seconds are truncated to 32 bits */
254 vap->va_atime.tv_sec = attr->atime;
255 vap->va_atime.tv_nsec = attr->atimensec;
256 vap->va_mtime.tv_sec = attr->mtime;
257 vap->va_mtime.tv_nsec = attr->mtimensec;
258 vap->va_ctime.tv_sec = attr->ctime;
259 vap->va_ctime.tv_nsec = attr->ctimensec;
260 vap->va_blocksize = PAGE_SIZE;
261 vap->va_type = IFTOVT(attr->mode);
262 vap->va_bytes = attr->blocks * S_BLKSIZE;
265 if (vap != vp_cache_at && vp_cache_at != NULL)
266 memcpy(vp_cache_at, vap, sizeof(*vap));
273 fuse_internal_fsync_callback(struct fuse_ticket *tick, struct uio *uio)
275 if (tick->tk_aw_ohead.error == ENOSYS) {
276 fsess_set_notimpl(tick->tk_data->mp, fticket_opcode(tick));
282 fuse_internal_fsync(struct vnode *vp,
285 struct fuse_filehandle *fufh)
288 struct fuse_fsync_in *ffsi;
289 struct fuse_dispatcher fdi;
291 if (vnode_isdir(vp)) {
294 fdisp_init(&fdi, sizeof(*ffsi));
295 fdisp_make_vp(&fdi, op, vp, td, cred);
297 ffsi->fh = fufh->fh_id;
299 ffsi->fsync_flags = 1; /* datasync */
301 fuse_insert_callback(fdi.tick, fuse_internal_fsync_callback);
302 fuse_insert_message(fdi.tick);
313 fuse_internal_readdir(struct vnode *vp,
315 struct fuse_filehandle *fufh,
316 struct fuse_iov *cookediov)
319 struct fuse_dispatcher fdi;
320 struct fuse_read_in *fri;
322 if (uio_resid(uio) == 0) {
328 * Note that we DO NOT have a UIO_SYSSPACE here (so no need for p2p
332 while (uio_resid(uio) > 0) {
334 fdi.iosize = sizeof(*fri);
335 fdisp_make_vp(&fdi, FUSE_READDIR, vp, NULL, NULL);
338 fri->fh = fufh->fh_id;
339 fri->offset = uio_offset(uio);
340 fri->size = min(uio_resid(uio), FUSE_DEFAULT_IOSIZE);
343 if ((err = fdisp_wait_answ(&fdi))) {
346 if ((err = fuse_internal_readdir_processdata(uio, fri->size, fdi.answ,
347 fdi.iosize, cookediov))) {
353 return ((err == -1) ? 0 : err);
357 fuse_internal_readdir_processdata(struct uio *uio,
369 struct fuse_dirent *fudge;
370 struct fuse_iov *cookediov = param;
372 if (bufsize < FUSE_NAME_OFFSET) {
377 if (bufsize < FUSE_NAME_OFFSET) {
381 fudge = (struct fuse_dirent *)buf;
382 freclen = FUSE_DIRENT_SIZE(fudge);
386 if (bufsize < freclen) {
387 err = ((cou == 1) ? -1 : 0);
390 #ifdef ZERO_PAD_INCOMPLETE_BUFS
391 if (isbzero(buf, FUSE_NAME_OFFSET)) {
397 if (!fudge->namelen || fudge->namelen > MAXNAMLEN) {
401 bytesavail = GENERIC_DIRSIZ((struct pseudo_dirent *)
404 if (bytesavail > uio_resid(uio)) {
408 fiov_refresh(cookediov);
409 fiov_adjust(cookediov, bytesavail);
411 de = (struct dirent *)cookediov->base;
412 de->d_fileno = fudge->ino;
413 de->d_reclen = bytesavail;
414 de->d_type = fudge->type;
415 de->d_namlen = fudge->namelen;
416 memcpy((char *)cookediov->base + sizeof(struct dirent) -
418 (char *)buf + FUSE_NAME_OFFSET, fudge->namelen);
419 dirent_terminate(de);
421 err = uiomove(cookediov->base, cookediov->len, uio);
425 buf = (char *)buf + freclen;
427 uio_setoffset(uio, fudge->off);
436 fuse_internal_remove(struct vnode *dvp,
438 struct componentname *cnp,
441 struct fuse_dispatcher fdi;
442 struct fuse_vnode_data *fvdat;
448 fdisp_init(&fdi, cnp->cn_namelen + 1);
449 fdisp_make_vp(&fdi, op, dvp, cnp->cn_thread, cnp->cn_cred);
451 memcpy(fdi.indata, cnp->cn_nameptr, cnp->cn_namelen);
452 ((char *)fdi.indata)[cnp->cn_namelen] = '\0';
454 err = fdisp_wait_answ(&fdi);
462 fuse_internal_rename(struct vnode *fdvp,
463 struct componentname *fcnp,
465 struct componentname *tcnp)
467 struct fuse_dispatcher fdi;
468 struct fuse_rename_in *fri;
471 fdisp_init(&fdi, sizeof(*fri) + fcnp->cn_namelen + tcnp->cn_namelen + 2);
472 fdisp_make_vp(&fdi, FUSE_RENAME, fdvp, tcnp->cn_thread, tcnp->cn_cred);
475 fri->newdir = VTOI(tdvp);
476 memcpy((char *)fdi.indata + sizeof(*fri), fcnp->cn_nameptr,
478 ((char *)fdi.indata)[sizeof(*fri) + fcnp->cn_namelen] = '\0';
479 memcpy((char *)fdi.indata + sizeof(*fri) + fcnp->cn_namelen + 1,
480 tcnp->cn_nameptr, tcnp->cn_namelen);
481 ((char *)fdi.indata)[sizeof(*fri) + fcnp->cn_namelen +
482 tcnp->cn_namelen + 1] = '\0';
484 err = fdisp_wait_answ(&fdi);
491 /* entity creation */
494 fuse_internal_newentry_makerequest(struct mount *mp,
496 struct componentname *cnp,
500 struct fuse_dispatcher *fdip)
502 fdip->iosize = bufsize + cnp->cn_namelen + 1;
504 fdisp_make(fdip, op, mp, dnid, cnp->cn_thread, cnp->cn_cred);
505 memcpy(fdip->indata, buf, bufsize);
506 memcpy((char *)fdip->indata + bufsize, cnp->cn_nameptr, cnp->cn_namelen);
507 ((char *)fdip->indata)[bufsize + cnp->cn_namelen] = '\0';
511 fuse_internal_newentry_core(struct vnode *dvp,
513 struct componentname *cnp,
515 struct fuse_dispatcher *fdip)
518 struct fuse_entry_out *feo;
519 struct mount *mp = vnode_mount(dvp);
521 if ((err = fdisp_wait_answ(fdip))) {
526 if ((err = fuse_internal_checkentry(feo, vtyp))) {
529 err = fuse_vnode_get(mp, feo, feo->nodeid, dvp, vpp, cnp, vtyp);
531 fuse_internal_forget_send(mp, cnp->cn_thread, cnp->cn_cred,
535 fuse_internal_cache_attrs(*vpp, &feo->attr, feo->attr_valid,
536 feo->attr_valid_nsec, NULL);
542 fuse_internal_newentry(struct vnode *dvp,
544 struct componentname *cnp,
551 struct fuse_dispatcher fdi;
552 struct mount *mp = vnode_mount(dvp);
555 fuse_internal_newentry_makerequest(mp, VTOI(dvp), cnp, op, buf,
557 err = fuse_internal_newentry_core(dvp, vpp, cnp, vtype, &fdi);
563 /* entity destruction */
566 fuse_internal_forget_callback(struct fuse_ticket *ftick, struct uio *uio)
568 fuse_internal_forget_send(ftick->tk_data->mp, curthread, NULL,
569 ((struct fuse_in_header *)ftick->tk_ms_fiov.base)->nodeid, 1);
575 fuse_internal_forget_send(struct mount *mp,
582 struct fuse_dispatcher fdi;
583 struct fuse_forget_in *ffi;
586 * KASSERT(nlookup > 0, ("zero-times forget for vp #%llu",
587 * (long long unsigned) nodeid));
590 fdisp_init(&fdi, sizeof(*ffi));
591 fdisp_make(&fdi, FUSE_FORGET, mp, nodeid, td, cred);
594 ffi->nlookup = nlookup;
596 fuse_insert_message(fdi.tick);
601 fuse_internal_vnode_disappear(struct vnode *vp)
603 struct fuse_vnode_data *fvdat = VTOFUD(vp);
605 ASSERT_VOP_ELOCKED(vp, "fuse_internal_vnode_disappear");
606 fvdat->flag |= FN_REVOKED;
607 fvdat->valid_attr_cache = false;
611 /* fuse start/stop */
614 fuse_internal_init_callback(struct fuse_ticket *tick, struct uio *uio)
617 struct fuse_data *data = tick->tk_data;
618 struct fuse_init_out *fiio;
620 if ((err = tick->tk_aw_ohead.error)) {
623 if ((err = fticket_pull(tick, uio))) {
626 fiio = fticket_resp(tick)->base;
628 /* XXX: Do we want to check anything further besides this? */
629 if (fiio->major < 7) {
630 SDT_PROBE2(fuse, , internal, trace, 1,
631 "userpace version too low");
632 err = EPROTONOSUPPORT;
635 data->fuse_libabi_major = fiio->major;
636 data->fuse_libabi_minor = fiio->minor;
638 if (fuse_libabi_geq(data, 7, 5)) {
639 if (fticket_resp(tick)->len == sizeof(struct fuse_init_out)) {
640 data->max_write = fiio->max_write;
646 data->max_write = 4096;
651 fdata_set_dead(data);
654 data->dataflags |= FSESS_INITED;
655 wakeup(&data->ticketer);
662 fuse_internal_send_init(struct fuse_data *data, struct thread *td)
664 struct fuse_init_in *fiii;
665 struct fuse_dispatcher fdi;
667 fdisp_init(&fdi, sizeof(*fiii));
668 fdisp_make(&fdi, FUSE_INIT, data->mp, 0, td, NULL);
670 fiii->major = FUSE_KERNEL_VERSION;
671 fiii->minor = FUSE_KERNEL_MINOR_VERSION;
672 fiii->max_readahead = FUSE_DEFAULT_IOSIZE * 16;
675 fuse_insert_callback(fdi.tick, fuse_internal_init_callback);
676 fuse_insert_message(fdi.tick);
680 #ifdef ZERO_PAD_INCOMPLETE_BUFS
682 isbzero(void *buf, size_t len)
686 for (i = 0; i < len; i++) {
687 if (((char *)buf)[i])