2 * Copyright (c) 2007-2009 Google Inc. and Amit Singh
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions are
9 * * Redistributions of source code must retain the above copyright
10 * notice, this list of conditions and the following disclaimer.
11 * * Redistributions in binary form must reproduce the above
12 * copyright notice, this list of conditions and the following disclaimer
13 * in the documentation and/or other materials provided with the
15 * * Neither the name of Google Inc. nor the names of its
16 * contributors may be used to endorse or promote products derived from
17 * this software without specific prior written permission.
19 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
20 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
21 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
22 * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
23 * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
24 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
25 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
26 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
27 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
28 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
29 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
31 * Copyright (C) 2005 Csaba Henk.
32 * All rights reserved.
34 * Redistribution and use in source and binary forms, with or without
35 * modification, are permitted provided that the following conditions
37 * 1. Redistributions of source code must retain the above copyright
38 * notice, this list of conditions and the following disclaimer.
39 * 2. Redistributions in binary form must reproduce the above copyright
40 * notice, this list of conditions and the following disclaimer in the
41 * documentation and/or other materials provided with the distribution.
43 * THIS SOFTWARE IS PROVIDED BY AUTHOR AND CONTRIBUTORS ``AS IS'' AND
44 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
45 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
46 * ARE DISCLAIMED. IN NO EVENT SHALL AUTHOR OR CONTRIBUTORS BE LIABLE
47 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
48 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
49 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
50 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
51 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
52 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
56 #include <sys/cdefs.h>
57 __FBSDID("$FreeBSD$");
59 #include <sys/types.h>
60 #include <sys/module.h>
61 #include <sys/systm.h>
62 #include <sys/errno.h>
63 #include <sys/param.h>
64 #include <sys/kernel.h>
67 #include <sys/malloc.h>
68 #include <sys/queue.h>
70 #include <sys/mutex.h>
73 #include <sys/mount.h>
74 #include <sys/vnode.h>
75 #include <sys/namei.h>
77 #include <sys/unistd.h>
78 #include <sys/filedesc.h>
80 #include <sys/fcntl.h>
81 #include <sys/dirent.h>
84 #include <sys/sysctl.h>
88 #include "fuse_file.h"
89 #include "fuse_internal.h"
91 #include "fuse_node.h"
92 #include "fuse_file.h"
93 #include "fuse_param.h"
95 #define FUSE_DEBUG_MODULE INTERNAL
96 #include "fuse_debug.h"
98 #ifdef ZERO_PAD_INCOMPLETE_BUFS
99 static int isbzero(void *buf, size_t len);
106 fuse_internal_access(struct vnode *vp,
108 struct fuse_access_param *facp,
117 struct fuse_dispatcher fdi;
118 struct fuse_access_in *fai;
119 struct fuse_data *data;
123 * If this vnop gives you trouble, just return 0 here for a lazy
128 fuse_trace_printf_func();
130 mp = vnode_mount(vp);
131 vtype = vnode_vtype(vp);
133 data = fuse_get_mpdata(mp);
134 dataflags = data->dataflags;
136 if ((mode & VWRITE) && vfs_isrdonly(mp)) {
139 /* Unless explicitly permitted, deny everyone except the fs owner. */
140 if (vnode_isvroot(vp) && !(facp->facc_flags & FACCESS_NOCHECKSPY)) {
141 if (!(dataflags & FSESS_DAEMON_CAN_SPY)) {
142 int denied = fuse_match_cred(data->daemoncred,
149 facp->facc_flags |= FACCESS_NOCHECKSPY;
151 if (!(facp->facc_flags & FACCESS_DO_ACCESS)) {
154 if (((vtype == VREG) && (mode & VEXEC))) {
155 #ifdef NEED_MOUNT_ARGUMENT_FOR_THIS
156 /* Let the kernel handle this through open / close heuristics.*/
159 /* Let the kernel handle this. */
163 if (!fsess_isimpl(mp, FUSE_ACCESS)) {
164 /* Let the kernel handle this. */
167 if (dataflags & FSESS_DEFAULT_PERMISSIONS) {
168 /* Let the kernel handle this. */
171 if ((mode & VADMIN) != 0) {
172 err = priv_check_cred(cred, PRIV_VFS_ADMIN, 0);
177 if ((mode & (VWRITE | VAPPEND | VADMIN)) != 0) {
180 if ((mode & VREAD) != 0) {
183 if ((mode & VEXEC) != 0) {
186 bzero(&fdi, sizeof(fdi));
188 fdisp_init(&fdi, sizeof(*fai));
189 fdisp_make_vp(&fdi, FUSE_ACCESS, vp, td, cred);
195 err = fdisp_wait_answ(&fdi);
199 fsess_set_notimpl(mp, FUSE_ACCESS);
208 fuse_internal_fsync_callback(struct fuse_ticket *tick, struct uio *uio)
210 fuse_trace_printf_func();
212 if (tick->tk_aw_ohead.error == ENOSYS) {
213 fsess_set_notimpl(tick->tk_data->mp, fticket_opcode(tick));
219 fuse_internal_fsync(struct vnode *vp,
222 struct fuse_filehandle *fufh)
225 struct fuse_fsync_in *ffsi;
226 struct fuse_dispatcher fdi;
228 fuse_trace_printf_func();
230 if (vnode_isdir(vp)) {
233 fdisp_init(&fdi, sizeof(*ffsi));
234 fdisp_make_vp(&fdi, op, vp, td, cred);
236 ffsi->fh = fufh->fh_id;
238 ffsi->fsync_flags = 1; /* datasync */
240 fuse_insert_callback(fdi.tick, fuse_internal_fsync_callback);
241 fuse_insert_message(fdi.tick);
252 fuse_internal_readdir(struct vnode *vp,
254 struct fuse_filehandle *fufh,
255 struct fuse_iov *cookediov)
258 struct fuse_dispatcher fdi;
259 struct fuse_read_in *fri;
261 if (uio_resid(uio) == 0) {
267 * Note that we DO NOT have a UIO_SYSSPACE here (so no need for p2p
271 while (uio_resid(uio) > 0) {
273 fdi.iosize = sizeof(*fri);
274 fdisp_make_vp(&fdi, FUSE_READDIR, vp, NULL, NULL);
277 fri->fh = fufh->fh_id;
278 fri->offset = uio_offset(uio);
279 fri->size = min(uio_resid(uio), FUSE_DEFAULT_IOSIZE);
282 if ((err = fdisp_wait_answ(&fdi))) {
285 if ((err = fuse_internal_readdir_processdata(uio, fri->size, fdi.answ,
286 fdi.iosize, cookediov))) {
292 return ((err == -1) ? 0 : err);
296 fuse_internal_readdir_processdata(struct uio *uio,
308 struct fuse_dirent *fudge;
309 struct fuse_iov *cookediov = param;
311 if (bufsize < FUSE_NAME_OFFSET) {
316 if (bufsize < FUSE_NAME_OFFSET) {
320 fudge = (struct fuse_dirent *)buf;
321 freclen = FUSE_DIRENT_SIZE(fudge);
325 if (bufsize < freclen) {
326 err = ((cou == 1) ? -1 : 0);
329 #ifdef ZERO_PAD_INCOMPLETE_BUFS
330 if (isbzero(buf, FUSE_NAME_OFFSET)) {
336 if (!fudge->namelen || fudge->namelen > MAXNAMLEN) {
340 bytesavail = GENERIC_DIRSIZ((struct pseudo_dirent *)
343 if (bytesavail > uio_resid(uio)) {
347 fiov_refresh(cookediov);
348 fiov_adjust(cookediov, bytesavail);
350 de = (struct dirent *)cookediov->base;
351 de->d_fileno = fudge->ino; /* XXX: truncation */
352 de->d_reclen = bytesavail;
353 de->d_type = fudge->type;
354 de->d_namlen = fudge->namelen;
355 memcpy((char *)cookediov->base + sizeof(struct dirent) -
357 (char *)buf + FUSE_NAME_OFFSET, fudge->namelen);
358 ((char *)cookediov->base)[bytesavail] = '\0';
360 err = uiomove(cookediov->base, cookediov->len, uio);
364 buf = (char *)buf + freclen;
366 uio_setoffset(uio, fudge->off);
374 #define INVALIDATE_CACHED_VATTRS_UPON_UNLINK 1
376 fuse_internal_remove(struct vnode *dvp,
378 struct componentname *cnp,
381 struct fuse_dispatcher fdi;
383 struct vattr *vap = VTOVA(vp);
385 #if INVALIDATE_CACHED_VATTRS_UPON_UNLINK
386 int need_invalidate = 0;
387 uint64_t target_nlink = 0;
392 debug_printf("dvp=%p, cnp=%p, op=%d\n", vp, cnp, op);
394 fdisp_init(&fdi, cnp->cn_namelen + 1);
395 fdisp_make_vp(&fdi, op, dvp, cnp->cn_thread, cnp->cn_cred);
397 memcpy(fdi.indata, cnp->cn_nameptr, cnp->cn_namelen);
398 ((char *)fdi.indata)[cnp->cn_namelen] = '\0';
400 #if INVALIDATE_CACHED_VATTRS_UPON_UNLINK
401 if (vap->va_nlink > 1) {
403 target_nlink = vap->va_nlink;
407 err = fdisp_wait_answ(&fdi);
415 fuse_internal_rename(struct vnode *fdvp,
416 struct componentname *fcnp,
418 struct componentname *tcnp)
420 struct fuse_dispatcher fdi;
421 struct fuse_rename_in *fri;
424 fdisp_init(&fdi, sizeof(*fri) + fcnp->cn_namelen + tcnp->cn_namelen + 2);
425 fdisp_make_vp(&fdi, FUSE_RENAME, fdvp, tcnp->cn_thread, tcnp->cn_cred);
428 fri->newdir = VTOI(tdvp);
429 memcpy((char *)fdi.indata + sizeof(*fri), fcnp->cn_nameptr,
431 ((char *)fdi.indata)[sizeof(*fri) + fcnp->cn_namelen] = '\0';
432 memcpy((char *)fdi.indata + sizeof(*fri) + fcnp->cn_namelen + 1,
433 tcnp->cn_nameptr, tcnp->cn_namelen);
434 ((char *)fdi.indata)[sizeof(*fri) + fcnp->cn_namelen +
435 tcnp->cn_namelen + 1] = '\0';
437 err = fdisp_wait_answ(&fdi);
444 /* entity creation */
447 fuse_internal_newentry_makerequest(struct mount *mp,
449 struct componentname *cnp,
453 struct fuse_dispatcher *fdip)
455 debug_printf("fdip=%p\n", fdip);
457 fdip->iosize = bufsize + cnp->cn_namelen + 1;
459 fdisp_make(fdip, op, mp, dnid, cnp->cn_thread, cnp->cn_cred);
460 memcpy(fdip->indata, buf, bufsize);
461 memcpy((char *)fdip->indata + bufsize, cnp->cn_nameptr, cnp->cn_namelen);
462 ((char *)fdip->indata)[bufsize + cnp->cn_namelen] = '\0';
466 fuse_internal_newentry_core(struct vnode *dvp,
468 struct componentname *cnp,
470 struct fuse_dispatcher *fdip)
473 struct fuse_entry_out *feo;
474 struct mount *mp = vnode_mount(dvp);
476 if ((err = fdisp_wait_answ(fdip))) {
481 if ((err = fuse_internal_checkentry(feo, vtyp))) {
484 err = fuse_vnode_get(mp, feo->nodeid, dvp, vpp, cnp, vtyp);
486 fuse_internal_forget_send(mp, cnp->cn_thread, cnp->cn_cred,
490 cache_attrs(*vpp, feo);
496 fuse_internal_newentry(struct vnode *dvp,
498 struct componentname *cnp,
505 struct fuse_dispatcher fdi;
506 struct mount *mp = vnode_mount(dvp);
509 fuse_internal_newentry_makerequest(mp, VTOI(dvp), cnp, op, buf,
511 err = fuse_internal_newentry_core(dvp, vpp, cnp, vtype, &fdi);
517 /* entity destruction */
520 fuse_internal_forget_callback(struct fuse_ticket *ftick, struct uio *uio)
522 fuse_internal_forget_send(ftick->tk_data->mp, curthread, NULL,
523 ((struct fuse_in_header *)ftick->tk_ms_fiov.base)->nodeid, 1);
529 fuse_internal_forget_send(struct mount *mp,
536 struct fuse_dispatcher fdi;
537 struct fuse_forget_in *ffi;
539 debug_printf("mp=%p, nodeid=%ju, nlookup=%ju\n",
540 mp, (uintmax_t)nodeid, (uintmax_t)nlookup);
543 * KASSERT(nlookup > 0, ("zero-times forget for vp #%llu",
544 * (long long unsigned) nodeid));
547 fdisp_init(&fdi, sizeof(*ffi));
548 fdisp_make(&fdi, FUSE_FORGET, mp, nodeid, td, cred);
551 ffi->nlookup = nlookup;
553 fuse_insert_message(fdi.tick);
558 fuse_internal_vnode_disappear(struct vnode *vp)
560 struct fuse_vnode_data *fvdat = VTOFUD(vp);
562 ASSERT_VOP_ELOCKED(vp, "fuse_internal_vnode_disappear");
563 fvdat->flag |= FN_REVOKED;
567 /* fuse start/stop */
570 fuse_internal_init_callback(struct fuse_ticket *tick, struct uio *uio)
573 struct fuse_data *data = tick->tk_data;
574 struct fuse_init_out *fiio;
576 if ((err = tick->tk_aw_ohead.error)) {
579 if ((err = fticket_pull(tick, uio))) {
582 fiio = fticket_resp(tick)->base;
584 /* XXX: Do we want to check anything further besides this? */
585 if (fiio->major < 7) {
586 debug_printf("userpace version too low\n");
587 err = EPROTONOSUPPORT;
590 data->fuse_libabi_major = fiio->major;
591 data->fuse_libabi_minor = fiio->minor;
593 if (fuse_libabi_geq(data, 7, 5)) {
594 if (fticket_resp(tick)->len == sizeof(struct fuse_init_out)) {
595 data->max_write = fiio->max_write;
601 data->max_write = 4096;
606 fdata_set_dead(data);
609 data->dataflags |= FSESS_INITED;
610 wakeup(&data->ticketer);
617 fuse_internal_send_init(struct fuse_data *data, struct thread *td)
619 struct fuse_init_in *fiii;
620 struct fuse_dispatcher fdi;
622 fdisp_init(&fdi, sizeof(*fiii));
623 fdisp_make(&fdi, FUSE_INIT, data->mp, 0, td, NULL);
625 fiii->major = FUSE_KERNEL_VERSION;
626 fiii->minor = FUSE_KERNEL_MINOR_VERSION;
627 fiii->max_readahead = FUSE_DEFAULT_IOSIZE * 16;
630 fuse_insert_callback(fdi.tick, fuse_internal_init_callback);
631 fuse_insert_message(fdi.tick);
635 #ifdef ZERO_PAD_INCOMPLETE_BUFS
637 isbzero(void *buf, size_t len)
641 for (i = 0; i < len; i++) {
642 if (((char *)buf)[i])