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 * Copyright (c) 2019 The FreeBSD Foundation
38 * Portions of this software were developed by BFF Storage Systems, LLC under
39 * sponsorship from the FreeBSD Foundation.
41 * Redistribution and use in source and binary forms, with or without
42 * modification, are permitted provided that the following conditions
44 * 1. Redistributions of source code must retain the above copyright
45 * notice, this list of conditions and the following disclaimer.
46 * 2. Redistributions in binary form must reproduce the above copyright
47 * notice, this list of conditions and the following disclaimer in the
48 * documentation and/or other materials provided with the distribution.
50 * THIS SOFTWARE IS PROVIDED BY AUTHOR AND CONTRIBUTORS ``AS IS'' AND
51 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
52 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
53 * ARE DISCLAIMED. IN NO EVENT SHALL AUTHOR OR CONTRIBUTORS BE LIABLE
54 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
55 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
56 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
57 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
58 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
59 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
63 #include <sys/cdefs.h>
64 __FBSDID("$FreeBSD$");
66 #include <sys/types.h>
67 #include <sys/systm.h>
68 #include <sys/counter.h>
69 #include <sys/module.h>
70 #include <sys/errno.h>
71 #include <sys/param.h>
72 #include <sys/kernel.h>
75 #include <sys/malloc.h>
76 #include <sys/queue.h>
79 #include <sys/mutex.h>
81 #include <sys/vnode.h>
82 #include <sys/namei.h>
83 #include <sys/mount.h>
84 #include <sys/sysctl.h>
85 #include <sys/fcntl.h>
88 #include <security/mac/mac_framework.h>
90 #include <vm/vm_extern.h>
93 #include "fuse_node.h"
94 #include "fuse_internal.h"
98 SDT_PROVIDER_DECLARE(fusefs);
101 * arg0: verbosity. Higher numbers give more verbose messages
102 * arg1: Textual message
104 SDT_PROBE_DEFINE2(fusefs, , node, trace, "int", "char*");
106 MALLOC_DEFINE(M_FUSEVN, "fuse_vnode", "fuse vnode private data");
108 static int sysctl_fuse_cache_mode(SYSCTL_HANDLER_ARGS);
110 static counter_u64_t fuse_node_count;
112 SYSCTL_COUNTER_U64(_vfs_fusefs_stats, OID_AUTO, node_count, CTLFLAG_RD,
113 &fuse_node_count, "Count of FUSE vnodes");
115 int fuse_data_cache_mode = FUSE_CACHE_WT;
119 * This sysctl is no longer needed as of fuse protocol 7.23. Individual
120 * servers can select the cache behavior they need for each mountpoint:
121 * - writethrough: the default
122 * - writeback: set FUSE_WRITEBACK_CACHE in fuse_init_out.flags
123 * - uncached: set FOPEN_DIRECT_IO for every file
124 * The sysctl is retained primarily for use by jails supporting older FUSE
125 * protocols. It may be removed entirely once FreeBSD 11.3 and 12.0 are EOL.
127 SYSCTL_PROC(_vfs_fusefs, OID_AUTO, data_cache_mode,
128 CTLTYPE_INT | CTLFLAG_MPSAFE | CTLFLAG_RW,
129 &fuse_data_cache_mode, 0, sysctl_fuse_cache_mode, "I",
130 "Zero: disable caching of FUSE file data; One: write-through caching "
131 "(default); Two: write-back caching (generally unsafe)");
134 sysctl_fuse_cache_mode(SYSCTL_HANDLER_ARGS)
139 error = sysctl_handle_int(oidp, &val, 0, req);
140 if (error || !req->newptr)
156 fuse_vnode_init(struct vnode *vp, struct fuse_vnode_data *fvdat,
157 uint64_t nodeid, enum vtype vtyp)
160 LIST_INIT(&fvdat->handles);
161 vattr_null(&fvdat->cached_attrs);
162 if (nodeid == FUSE_ROOT_ID) {
163 vp->v_vflag |= VV_ROOT;
167 cluster_init_vn(&fvdat->clusterw);
169 counter_u64_add(fuse_node_count, 1);
173 fuse_vnode_destroy(struct vnode *vp)
175 struct fuse_vnode_data *fvdat = vp->v_data;
178 KASSERT(LIST_EMPTY(&fvdat->handles),
179 ("Destroying fuse vnode with open files!"));
180 free(fvdat, M_FUSEVN);
182 counter_u64_add(fuse_node_count, -1);
186 fuse_vnode_cmp(struct vnode *vp, void *nidp)
188 return (VTOI(vp) != *((uint64_t *)nidp));
191 SDT_PROBE_DEFINE3(fusefs, , node, stale_vnode, "struct vnode*", "enum vtype",
194 fuse_vnode_alloc(struct mount *mp,
200 struct fuse_data *data;
201 struct fuse_vnode_data *fvdat;
205 data = fuse_get_mpdata(mp);
210 err = vfs_hash_get(mp, fuse_vnode_hash(nodeid), LK_EXCLUSIVE, td, vpp,
211 fuse_vnode_cmp, &nodeid);
216 if ((*vpp)->v_type != vtyp) {
218 * STALE vnode! This probably indicates a buggy
219 * server, but it could also be the result of a race
220 * between FUSE_LOOKUP and another client's
221 * FUSE_UNLINK/FUSE_CREATE
223 SDT_PROBE3(fusefs, , node, stale_vnode, *vpp, vtyp,
225 fuse_internal_vnode_disappear(*vpp);
226 lockmgr((*vpp)->v_vnlock, LK_RELEASE, NULL);
230 MPASS((*vpp)->v_data != NULL);
231 MPASS(VTOFUD(*vpp)->nid == nodeid);
232 SDT_PROBE2(fusefs, , node, trace, 1, "vnode taken from hash");
235 fvdat = malloc(sizeof(*fvdat), M_FUSEVN, M_WAITOK | M_ZERO);
238 err = getnewvnode("fuse", mp, &fuse_fifoops, vpp);
241 err = getnewvnode("fuse", mp, &fuse_vnops, vpp);
245 free(fvdat, M_FUSEVN);
248 lockmgr((*vpp)->v_vnlock, LK_EXCLUSIVE, NULL);
249 fuse_vnode_init(*vpp, fvdat, nodeid, vtyp);
250 err = insmntque(*vpp, mp);
251 ASSERT_VOP_ELOCKED(*vpp, "fuse_vnode_alloc");
253 lockmgr((*vpp)->v_vnlock, LK_RELEASE, NULL);
254 free(fvdat, M_FUSEVN);
258 /* Disallow async reads for fifos because UFS does. I don't know why */
259 if (data->dataflags & FSESS_ASYNC_READ && vtyp != VFIFO)
260 VN_LOCK_ASHARE(*vpp);
262 err = vfs_hash_insert(*vpp, fuse_vnode_hash(nodeid), LK_EXCLUSIVE,
263 td, &vp2, fuse_vnode_cmp, &nodeid);
265 lockmgr((*vpp)->v_vnlock, LK_RELEASE, NULL);
266 free(fvdat, M_FUSEVN);
275 ASSERT_VOP_ELOCKED(*vpp, "fuse_vnode_alloc");
281 fuse_vnode_get(struct mount *mp,
282 struct fuse_entry_out *feo,
286 struct componentname *cnp,
289 struct thread *td = (cnp != NULL ? cnp->cn_thread : curthread);
291 * feo should only be NULL for the root directory, which (when libfuse
292 * is used) always has generation 0
294 uint64_t generation = feo ? feo->generation : 0;
297 err = fuse_vnode_alloc(mp, td, nodeid, vtyp, vpp);
302 MPASS(cnp && (cnp->cn_flags & ISDOTDOT) == 0);
304 !(cnp->cn_namelen == 1 && cnp->cn_nameptr[0] == '.'));
305 fuse_vnode_setparent(*vpp, dvp);
307 if (dvp != NULL && cnp != NULL && (cnp->cn_flags & MAKEENTRY) != 0 &&
309 (feo->entry_valid != 0 || feo->entry_valid_nsec != 0)) {
310 struct timespec timeout;
312 ASSERT_VOP_LOCKED(*vpp, "fuse_vnode_get");
313 ASSERT_VOP_LOCKED(dvp, "fuse_vnode_get");
315 fuse_validity_2_timespec(feo, &timeout);
316 cache_enter_time(dvp, *vpp, cnp, &timeout, NULL);
319 VTOFUD(*vpp)->generation = generation;
321 * In userland, libfuse uses cached lookups for dot and dotdot entries,
322 * thus it does not really bump the nlookup counter for forget.
323 * Follow the same semantic and avoid the bump in order to keep
324 * nlookup counters consistent.
326 if (cnp == NULL || ((cnp->cn_flags & ISDOTDOT) == 0 &&
327 (cnp->cn_namelen != 1 || cnp->cn_nameptr[0] != '.')))
328 VTOFUD(*vpp)->nlookup++;
334 * Called for every fusefs vnode open to initialize the vnode (not
335 * fuse_filehandle) for use
338 fuse_vnode_open(struct vnode *vp, int32_t fuse_open_flags, struct thread *td)
340 if (vnode_vtype(vp) == VREG)
341 vnode_create_vobject(vp, 0, td);
345 fuse_vnode_savesize(struct vnode *vp, struct ucred *cred, pid_t pid)
347 struct fuse_vnode_data *fvdat = VTOFUD(vp);
348 struct thread *td = curthread;
349 struct fuse_filehandle *fufh = NULL;
350 struct fuse_dispatcher fdi;
351 struct fuse_setattr_in *fsai;
354 ASSERT_VOP_ELOCKED(vp, "fuse_io_extend");
356 if (fuse_isdeadfs(vp)) {
359 if (vnode_vtype(vp) == VDIR) {
362 if (vfs_isrdonly(vnode_mount(vp))) {
368 fdisp_init(&fdi, sizeof(*fsai));
369 fdisp_make_vp(&fdi, FUSE_SETATTR, vp, td, cred);
373 /* Truncate to a new value. */
374 MPASS((fvdat->flag & FN_SIZECHANGE) != 0);
375 fsai->size = fvdat->cached_attrs.va_size;
376 fsai->valid |= FATTR_SIZE;
378 fuse_filehandle_getrw(vp, FWRITE, &fufh, cred, pid);
380 fsai->fh = fufh->fh_id;
381 fsai->valid |= FATTR_FH;
383 err = fdisp_wait_answ(&fdi);
386 fvdat->flag &= ~FN_SIZECHANGE;
392 * Adjust the vnode's size to a new value.
394 * If the new value came from the server, such as from a FUSE_GETATTR
395 * operation, set `from_server` true. But if it came from a local operation,
396 * such as write(2) or truncate(2), set `from_server` false.
399 fuse_vnode_setsize(struct vnode *vp, off_t newsize, bool from_server)
401 struct fuse_vnode_data *fvdat = VTOFUD(vp);
405 struct buf *bp = NULL;
408 ASSERT_VOP_ELOCKED(vp, "fuse_vnode_setsize");
410 iosize = fuse_iosize(vp);
411 oldsize = fvdat->cached_attrs.va_size;
412 fvdat->cached_attrs.va_size = newsize;
413 if ((attrs = VTOVA(vp)) != NULL)
414 attrs->va_size = newsize;
416 if (newsize < oldsize) {
419 err = vtruncbuf(vp, newsize, fuse_iosize(vp));
422 if (newsize % iosize == 0)
425 * Zero the contents of the last partial block.
426 * Sure seems like vtruncbuf should do this for us.
429 lbn = newsize / iosize;
430 bp = getblk(vp, lbn, iosize, PCATCH, 0, 0);
435 if (!(bp->b_flags & B_CACHE))
436 goto out; /* Nothing to do */
437 MPASS(bp->b_flags & B_VMIO);
439 bp->b_dirtyend = MIN(bp->b_dirtyend, newsize - lbn * iosize);
440 } else if (from_server && newsize > oldsize && oldsize != VNOVAL) {
442 * The FUSE server changed the file size behind our back. We
443 * should invalidate the entire cache.
445 daddr_t left_lbn, end_lbn;
447 left_lbn = oldsize / iosize;
448 end_lbn = howmany(newsize, iosize);
449 v_inval_buf_range(vp, 0, end_lbn, iosize);
454 vnode_pager_setsize(vp, newsize);
458 /* Get the current, possibly dirty, size of the file */
460 fuse_vnode_size(struct vnode *vp, off_t *filesize, struct ucred *cred,
463 struct fuse_vnode_data *fvdat = VTOFUD(vp);
466 if (!(fvdat->flag & FN_SIZECHANGE) &&
467 (!fuse_vnode_attr_cache_valid(vp) ||
468 fvdat->cached_attrs.va_size == VNOVAL))
469 error = fuse_internal_do_getattr(vp, NULL, cred, td);
472 *filesize = fvdat->cached_attrs.va_size;
478 fuse_vnode_undirty_cached_timestamps(struct vnode *vp)
480 struct fuse_vnode_data *fvdat = VTOFUD(vp);
482 fvdat->flag &= ~(FN_MTIMECHANGE | FN_CTIMECHANGE);
485 /* Update a fuse file's cached timestamps */
487 fuse_vnode_update(struct vnode *vp, int flags)
489 struct fuse_vnode_data *fvdat = VTOFUD(vp);
490 struct fuse_data *data = fuse_get_mpdata(vnode_mount(vp));
495 if (data->time_gran > 1)
496 ts.tv_nsec = rounddown(ts.tv_nsec, data->time_gran);
498 if (flags & FN_MTIMECHANGE)
499 fvdat->cached_attrs.va_mtime = ts;
500 if (flags & FN_CTIMECHANGE)
501 fvdat->cached_attrs.va_ctime = ts;
503 fvdat->flag |= flags;
509 fuse_node_count = counter_u64_alloc(M_WAITOK);
513 fuse_node_destroy(void)
515 counter_u64_free(fuse_node_count);