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/types.h>
62 #include <sys/module.h>
63 #include <sys/systm.h>
64 #include <sys/errno.h>
65 #include <sys/param.h>
66 #include <sys/kernel.h>
69 #include <sys/malloc.h>
70 #include <sys/queue.h>
72 #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 #define FUSE_DEBUG_MODULE INTERNAL
98 #include "fuse_debug.h"
100 #ifdef ZERO_PAD_INCOMPLETE_BUFS
101 static int isbzero(void *buf, size_t len);
108 fuse_internal_access(struct vnode *vp,
110 struct fuse_access_param *facp,
119 struct fuse_dispatcher fdi;
120 struct fuse_access_in *fai;
121 struct fuse_data *data;
125 * If this vnop gives you trouble, just return 0 here for a lazy
130 fuse_trace_printf_func();
132 mp = vnode_mount(vp);
133 vtype = vnode_vtype(vp);
135 data = fuse_get_mpdata(mp);
136 dataflags = data->dataflags;
138 if ((mode & VWRITE) && vfs_isrdonly(mp)) {
141 /* Unless explicitly permitted, deny everyone except the fs owner. */
142 if (vnode_isvroot(vp) && !(facp->facc_flags & FACCESS_NOCHECKSPY)) {
143 if (!(dataflags & FSESS_DAEMON_CAN_SPY)) {
144 int denied = fuse_match_cred(data->daemoncred,
151 facp->facc_flags |= FACCESS_NOCHECKSPY;
153 if (!(facp->facc_flags & FACCESS_DO_ACCESS)) {
156 if (((vtype == VREG) && (mode & VEXEC))) {
157 #ifdef NEED_MOUNT_ARGUMENT_FOR_THIS
158 /* Let the kernel handle this through open / close heuristics.*/
161 /* Let the kernel handle this. */
165 if (!fsess_isimpl(mp, FUSE_ACCESS)) {
166 /* Let the kernel handle this. */
169 if (dataflags & FSESS_DEFAULT_PERMISSIONS) {
170 /* Let the kernel handle this. */
173 if ((mode & VADMIN) != 0) {
174 err = priv_check_cred(cred, PRIV_VFS_ADMIN);
179 if ((mode & (VWRITE | VAPPEND | VADMIN)) != 0) {
182 if ((mode & VREAD) != 0) {
185 if ((mode & VEXEC) != 0) {
188 bzero(&fdi, sizeof(fdi));
190 fdisp_init(&fdi, sizeof(*fai));
191 fdisp_make_vp(&fdi, FUSE_ACCESS, vp, td, cred);
197 err = fdisp_wait_answ(&fdi);
201 fsess_set_notimpl(mp, FUSE_ACCESS);
210 fuse_internal_fsync_callback(struct fuse_ticket *tick, struct uio *uio)
212 fuse_trace_printf_func();
214 if (tick->tk_aw_ohead.error == ENOSYS) {
215 fsess_set_notimpl(tick->tk_data->mp, fticket_opcode(tick));
221 fuse_internal_fsync(struct vnode *vp,
224 struct fuse_filehandle *fufh)
227 struct fuse_fsync_in *ffsi;
228 struct fuse_dispatcher fdi;
230 fuse_trace_printf_func();
232 if (vnode_isdir(vp)) {
235 fdisp_init(&fdi, sizeof(*ffsi));
236 fdisp_make_vp(&fdi, op, vp, td, cred);
238 ffsi->fh = fufh->fh_id;
240 ffsi->fsync_flags = 1; /* datasync */
242 fuse_insert_callback(fdi.tick, fuse_internal_fsync_callback);
243 fuse_insert_message(fdi.tick);
254 fuse_internal_readdir(struct vnode *vp,
256 struct fuse_filehandle *fufh,
257 struct fuse_iov *cookediov)
260 struct fuse_dispatcher fdi;
261 struct fuse_read_in *fri;
263 if (uio_resid(uio) == 0) {
269 * Note that we DO NOT have a UIO_SYSSPACE here (so no need for p2p
273 while (uio_resid(uio) > 0) {
275 fdi.iosize = sizeof(*fri);
276 fdisp_make_vp(&fdi, FUSE_READDIR, vp, NULL, NULL);
279 fri->fh = fufh->fh_id;
280 fri->offset = uio_offset(uio);
281 fri->size = min(uio_resid(uio), FUSE_DEFAULT_IOSIZE);
284 if ((err = fdisp_wait_answ(&fdi))) {
287 if ((err = fuse_internal_readdir_processdata(uio, fri->size, fdi.answ,
288 fdi.iosize, cookediov))) {
294 return ((err == -1) ? 0 : err);
298 fuse_internal_readdir_processdata(struct uio *uio,
310 struct fuse_dirent *fudge;
311 struct fuse_iov *cookediov = param;
313 if (bufsize < FUSE_NAME_OFFSET) {
318 if (bufsize < FUSE_NAME_OFFSET) {
322 fudge = (struct fuse_dirent *)buf;
323 freclen = FUSE_DIRENT_SIZE(fudge);
327 if (bufsize < freclen) {
328 err = ((cou == 1) ? -1 : 0);
331 #ifdef ZERO_PAD_INCOMPLETE_BUFS
332 if (isbzero(buf, FUSE_NAME_OFFSET)) {
338 if (!fudge->namelen || fudge->namelen > MAXNAMLEN) {
342 bytesavail = GENERIC_DIRSIZ((struct pseudo_dirent *)
345 if (bytesavail > uio_resid(uio)) {
349 fiov_refresh(cookediov);
350 fiov_adjust(cookediov, bytesavail);
352 de = (struct dirent *)cookediov->base;
353 de->d_fileno = fudge->ino;
354 de->d_reclen = bytesavail;
355 de->d_type = fudge->type;
356 de->d_namlen = fudge->namelen;
357 memcpy((char *)cookediov->base + sizeof(struct dirent) -
359 (char *)buf + FUSE_NAME_OFFSET, fudge->namelen);
360 dirent_terminate(de);
362 err = uiomove(cookediov->base, cookediov->len, uio);
366 buf = (char *)buf + freclen;
368 uio_setoffset(uio, fudge->off);
377 fuse_internal_remove(struct vnode *dvp,
379 struct componentname *cnp,
382 struct fuse_dispatcher fdi;
383 struct fuse_vnode_data *fvdat;
389 debug_printf("dvp=%p, cnp=%p, op=%d\n", vp, cnp, op);
391 fdisp_init(&fdi, cnp->cn_namelen + 1);
392 fdisp_make_vp(&fdi, op, dvp, cnp->cn_thread, cnp->cn_cred);
394 memcpy(fdi.indata, cnp->cn_nameptr, cnp->cn_namelen);
395 ((char *)fdi.indata)[cnp->cn_namelen] = '\0';
397 err = fdisp_wait_answ(&fdi);
405 fuse_internal_rename(struct vnode *fdvp,
406 struct componentname *fcnp,
408 struct componentname *tcnp)
410 struct fuse_dispatcher fdi;
411 struct fuse_rename_in *fri;
414 fdisp_init(&fdi, sizeof(*fri) + fcnp->cn_namelen + tcnp->cn_namelen + 2);
415 fdisp_make_vp(&fdi, FUSE_RENAME, fdvp, tcnp->cn_thread, tcnp->cn_cred);
418 fri->newdir = VTOI(tdvp);
419 memcpy((char *)fdi.indata + sizeof(*fri), fcnp->cn_nameptr,
421 ((char *)fdi.indata)[sizeof(*fri) + fcnp->cn_namelen] = '\0';
422 memcpy((char *)fdi.indata + sizeof(*fri) + fcnp->cn_namelen + 1,
423 tcnp->cn_nameptr, tcnp->cn_namelen);
424 ((char *)fdi.indata)[sizeof(*fri) + fcnp->cn_namelen +
425 tcnp->cn_namelen + 1] = '\0';
427 err = fdisp_wait_answ(&fdi);
434 /* entity creation */
437 fuse_internal_newentry_makerequest(struct mount *mp,
439 struct componentname *cnp,
443 struct fuse_dispatcher *fdip)
445 debug_printf("fdip=%p\n", fdip);
447 fdip->iosize = bufsize + cnp->cn_namelen + 1;
449 fdisp_make(fdip, op, mp, dnid, cnp->cn_thread, cnp->cn_cred);
450 memcpy(fdip->indata, buf, bufsize);
451 memcpy((char *)fdip->indata + bufsize, cnp->cn_nameptr, cnp->cn_namelen);
452 ((char *)fdip->indata)[bufsize + cnp->cn_namelen] = '\0';
456 fuse_internal_newentry_core(struct vnode *dvp,
458 struct componentname *cnp,
460 struct fuse_dispatcher *fdip)
463 struct fuse_entry_out *feo;
464 struct mount *mp = vnode_mount(dvp);
466 if ((err = fdisp_wait_answ(fdip))) {
471 if ((err = fuse_internal_checkentry(feo, vtyp))) {
474 err = fuse_vnode_get(mp, feo, feo->nodeid, dvp, vpp, cnp, vtyp);
476 fuse_internal_forget_send(mp, cnp->cn_thread, cnp->cn_cred,
480 cache_attrs(*vpp, feo, NULL);
486 fuse_internal_newentry(struct vnode *dvp,
488 struct componentname *cnp,
495 struct fuse_dispatcher fdi;
496 struct mount *mp = vnode_mount(dvp);
499 fuse_internal_newentry_makerequest(mp, VTOI(dvp), cnp, op, buf,
501 err = fuse_internal_newentry_core(dvp, vpp, cnp, vtype, &fdi);
507 /* entity destruction */
510 fuse_internal_forget_callback(struct fuse_ticket *ftick, struct uio *uio)
512 fuse_internal_forget_send(ftick->tk_data->mp, curthread, NULL,
513 ((struct fuse_in_header *)ftick->tk_ms_fiov.base)->nodeid, 1);
519 fuse_internal_forget_send(struct mount *mp,
526 struct fuse_dispatcher fdi;
527 struct fuse_forget_in *ffi;
529 debug_printf("mp=%p, nodeid=%ju, nlookup=%ju\n",
530 mp, (uintmax_t)nodeid, (uintmax_t)nlookup);
533 * KASSERT(nlookup > 0, ("zero-times forget for vp #%llu",
534 * (long long unsigned) nodeid));
537 fdisp_init(&fdi, sizeof(*ffi));
538 fdisp_make(&fdi, FUSE_FORGET, mp, nodeid, td, cred);
541 ffi->nlookup = nlookup;
543 fuse_insert_message(fdi.tick);
548 fuse_internal_vnode_disappear(struct vnode *vp)
550 struct fuse_vnode_data *fvdat = VTOFUD(vp);
552 ASSERT_VOP_ELOCKED(vp, "fuse_internal_vnode_disappear");
553 fvdat->flag |= FN_REVOKED;
554 fvdat->valid_attr_cache = false;
558 /* fuse start/stop */
561 fuse_internal_init_callback(struct fuse_ticket *tick, struct uio *uio)
564 struct fuse_data *data = tick->tk_data;
565 struct fuse_init_out *fiio;
567 if ((err = tick->tk_aw_ohead.error)) {
570 if ((err = fticket_pull(tick, uio))) {
573 fiio = fticket_resp(tick)->base;
575 /* XXX: Do we want to check anything further besides this? */
576 if (fiio->major < 7) {
577 debug_printf("userpace version too low\n");
578 err = EPROTONOSUPPORT;
581 data->fuse_libabi_major = fiio->major;
582 data->fuse_libabi_minor = fiio->minor;
584 if (fuse_libabi_geq(data, 7, 5)) {
585 if (fticket_resp(tick)->len == sizeof(struct fuse_init_out)) {
586 data->max_write = fiio->max_write;
592 data->max_write = 4096;
597 fdata_set_dead(data);
600 data->dataflags |= FSESS_INITED;
601 wakeup(&data->ticketer);
608 fuse_internal_send_init(struct fuse_data *data, struct thread *td)
610 struct fuse_init_in *fiii;
611 struct fuse_dispatcher fdi;
613 fdisp_init(&fdi, sizeof(*fiii));
614 fdisp_make(&fdi, FUSE_INIT, data->mp, 0, td, NULL);
616 fiii->major = FUSE_KERNEL_VERSION;
617 fiii->minor = FUSE_KERNEL_MINOR_VERSION;
618 fiii->max_readahead = FUSE_DEFAULT_IOSIZE * 16;
621 fuse_insert_callback(fdi.tick, fuse_internal_init_callback);
622 fuse_insert_message(fdi.tick);
626 #ifdef ZERO_PAD_INCOMPLETE_BUFS
628 isbzero(void *buf, size_t len)
632 for (i = 0; i < len; i++) {
633 if (((char *)buf)[i])