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, 0);
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);
376 #define INVALIDATE_CACHED_VATTRS_UPON_UNLINK 1
378 fuse_internal_remove(struct vnode *dvp,
380 struct componentname *cnp,
383 struct fuse_dispatcher fdi;
385 struct vattr *vap = VTOVA(vp);
387 #if INVALIDATE_CACHED_VATTRS_UPON_UNLINK
388 int need_invalidate = 0;
389 uint64_t target_nlink = 0;
394 debug_printf("dvp=%p, cnp=%p, op=%d\n", vp, cnp, op);
396 fdisp_init(&fdi, cnp->cn_namelen + 1);
397 fdisp_make_vp(&fdi, op, dvp, cnp->cn_thread, cnp->cn_cred);
399 memcpy(fdi.indata, cnp->cn_nameptr, cnp->cn_namelen);
400 ((char *)fdi.indata)[cnp->cn_namelen] = '\0';
402 #if INVALIDATE_CACHED_VATTRS_UPON_UNLINK
403 if (vap->va_nlink > 1) {
405 target_nlink = vap->va_nlink;
409 err = fdisp_wait_answ(&fdi);
417 fuse_internal_rename(struct vnode *fdvp,
418 struct componentname *fcnp,
420 struct componentname *tcnp)
422 struct fuse_dispatcher fdi;
423 struct fuse_rename_in *fri;
426 fdisp_init(&fdi, sizeof(*fri) + fcnp->cn_namelen + tcnp->cn_namelen + 2);
427 fdisp_make_vp(&fdi, FUSE_RENAME, fdvp, tcnp->cn_thread, tcnp->cn_cred);
430 fri->newdir = VTOI(tdvp);
431 memcpy((char *)fdi.indata + sizeof(*fri), fcnp->cn_nameptr,
433 ((char *)fdi.indata)[sizeof(*fri) + fcnp->cn_namelen] = '\0';
434 memcpy((char *)fdi.indata + sizeof(*fri) + fcnp->cn_namelen + 1,
435 tcnp->cn_nameptr, tcnp->cn_namelen);
436 ((char *)fdi.indata)[sizeof(*fri) + fcnp->cn_namelen +
437 tcnp->cn_namelen + 1] = '\0';
439 err = fdisp_wait_answ(&fdi);
446 /* entity creation */
449 fuse_internal_newentry_makerequest(struct mount *mp,
451 struct componentname *cnp,
455 struct fuse_dispatcher *fdip)
457 debug_printf("fdip=%p\n", fdip);
459 fdip->iosize = bufsize + cnp->cn_namelen + 1;
461 fdisp_make(fdip, op, mp, dnid, cnp->cn_thread, cnp->cn_cred);
462 memcpy(fdip->indata, buf, bufsize);
463 memcpy((char *)fdip->indata + bufsize, cnp->cn_nameptr, cnp->cn_namelen);
464 ((char *)fdip->indata)[bufsize + cnp->cn_namelen] = '\0';
468 fuse_internal_newentry_core(struct vnode *dvp,
470 struct componentname *cnp,
472 struct fuse_dispatcher *fdip)
475 struct fuse_entry_out *feo;
476 struct mount *mp = vnode_mount(dvp);
478 if ((err = fdisp_wait_answ(fdip))) {
483 if ((err = fuse_internal_checkentry(feo, vtyp))) {
486 err = fuse_vnode_get(mp, feo->nodeid, dvp, vpp, cnp, vtyp);
488 fuse_internal_forget_send(mp, cnp->cn_thread, cnp->cn_cred,
492 cache_attrs(*vpp, feo);
498 fuse_internal_newentry(struct vnode *dvp,
500 struct componentname *cnp,
507 struct fuse_dispatcher fdi;
508 struct mount *mp = vnode_mount(dvp);
511 fuse_internal_newentry_makerequest(mp, VTOI(dvp), cnp, op, buf,
513 err = fuse_internal_newentry_core(dvp, vpp, cnp, vtype, &fdi);
519 /* entity destruction */
522 fuse_internal_forget_callback(struct fuse_ticket *ftick, struct uio *uio)
524 fuse_internal_forget_send(ftick->tk_data->mp, curthread, NULL,
525 ((struct fuse_in_header *)ftick->tk_ms_fiov.base)->nodeid, 1);
531 fuse_internal_forget_send(struct mount *mp,
538 struct fuse_dispatcher fdi;
539 struct fuse_forget_in *ffi;
541 debug_printf("mp=%p, nodeid=%ju, nlookup=%ju\n",
542 mp, (uintmax_t)nodeid, (uintmax_t)nlookup);
545 * KASSERT(nlookup > 0, ("zero-times forget for vp #%llu",
546 * (long long unsigned) nodeid));
549 fdisp_init(&fdi, sizeof(*ffi));
550 fdisp_make(&fdi, FUSE_FORGET, mp, nodeid, td, cred);
553 ffi->nlookup = nlookup;
555 fuse_insert_message(fdi.tick);
560 fuse_internal_vnode_disappear(struct vnode *vp)
562 struct fuse_vnode_data *fvdat = VTOFUD(vp);
564 ASSERT_VOP_ELOCKED(vp, "fuse_internal_vnode_disappear");
565 fvdat->flag |= FN_REVOKED;
569 /* fuse start/stop */
572 fuse_internal_init_callback(struct fuse_ticket *tick, struct uio *uio)
575 struct fuse_data *data = tick->tk_data;
576 struct fuse_init_out *fiio;
578 if ((err = tick->tk_aw_ohead.error)) {
581 if ((err = fticket_pull(tick, uio))) {
584 fiio = fticket_resp(tick)->base;
586 /* XXX: Do we want to check anything further besides this? */
587 if (fiio->major < 7) {
588 debug_printf("userpace version too low\n");
589 err = EPROTONOSUPPORT;
592 data->fuse_libabi_major = fiio->major;
593 data->fuse_libabi_minor = fiio->minor;
595 if (fuse_libabi_geq(data, 7, 5)) {
596 if (fticket_resp(tick)->len == sizeof(struct fuse_init_out)) {
597 data->max_write = fiio->max_write;
603 data->max_write = 4096;
608 fdata_set_dead(data);
611 data->dataflags |= FSESS_INITED;
612 wakeup(&data->ticketer);
619 fuse_internal_send_init(struct fuse_data *data, struct thread *td)
621 struct fuse_init_in *fiii;
622 struct fuse_dispatcher fdi;
624 fdisp_init(&fdi, sizeof(*fiii));
625 fdisp_make(&fdi, FUSE_INIT, data->mp, 0, td, NULL);
627 fiii->major = FUSE_KERNEL_VERSION;
628 fiii->minor = FUSE_KERNEL_MINOR_VERSION;
629 fiii->max_readahead = FUSE_DEFAULT_IOSIZE * 16;
632 fuse_insert_callback(fdi.tick, fuse_internal_init_callback);
633 fuse_insert_message(fdi.tick);
637 #ifdef ZERO_PAD_INCOMPLETE_BUFS
639 isbzero(void *buf, size_t len)
643 for (i = 0; i < len; i++) {
644 if (((char *)buf)[i])