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>
73 #include <sys/mutex.h>
75 #include <sys/vnode.h>
76 #include <sys/namei.h>
77 #include <sys/mount.h>
78 #include <sys/sysctl.h>
79 #include <sys/fcntl.h>
80 #include <sys/fnv_hash.h>
82 #include <security/mac/mac_framework.h>
84 #include <vm/vm_extern.h>
87 #include "fuse_node.h"
88 #include "fuse_internal.h"
92 #define FUSE_DEBUG_MODULE VNOPS
93 #include "fuse_debug.h"
95 MALLOC_DEFINE(M_FUSEVN, "fuse_vnode", "fuse vnode private data");
97 static int fuse_node_count = 0;
99 SYSCTL_INT(_vfs_fuse, OID_AUTO, node_count, CTLFLAG_RD,
100 &fuse_node_count, 0, "Count of FUSE vnodes");
102 int fuse_data_cache_enable = 1;
104 SYSCTL_INT(_vfs_fuse, OID_AUTO, data_cache_enable, CTLFLAG_RW,
105 &fuse_data_cache_enable, 0,
106 "enable caching of FUSE file data (including dirty data)");
108 int fuse_data_cache_invalidate = 0;
110 SYSCTL_INT(_vfs_fuse, OID_AUTO, data_cache_invalidate, CTLFLAG_RW,
111 &fuse_data_cache_invalidate, 0,
112 "If non-zero, discard cached clean file data when there are no active file"
115 int fuse_mmap_enable = 1;
117 SYSCTL_INT(_vfs_fuse, OID_AUTO, mmap_enable, CTLFLAG_RW,
118 &fuse_mmap_enable, 0,
119 "If non-zero, and data_cache_enable is also non-zero, enable mmap(2) of "
122 int fuse_refresh_size = 0;
124 SYSCTL_INT(_vfs_fuse, OID_AUTO, refresh_size, CTLFLAG_RW,
125 &fuse_refresh_size, 0,
126 "If non-zero, and no dirty file extension data is buffered, fetch file "
127 "size before write operations");
129 int fuse_sync_resize = 1;
131 SYSCTL_INT(_vfs_fuse, OID_AUTO, sync_resize, CTLFLAG_RW,
132 &fuse_sync_resize, 0,
133 "If a cached write extended a file, inform FUSE filesystem of the changed"
134 "size immediately subsequent to the issued writes");
136 int fuse_fix_broken_io = 0;
138 SYSCTL_INT(_vfs_fuse, OID_AUTO, fix_broken_io, CTLFLAG_RW,
139 &fuse_fix_broken_io, 0,
140 "If non-zero, print a diagnostic warning if a userspace filesystem returns"
141 " EIO on reads of recently extended portions of files");
144 fuse_vnode_init(struct vnode *vp, struct fuse_vnode_data *fvdat,
145 uint64_t nodeid, enum vtype vtyp)
150 if (nodeid == FUSE_ROOT_ID) {
151 vp->v_vflag |= VV_ROOT;
156 for (i = 0; i < FUFH_MAXTYPE; i++)
157 fvdat->fufh[i].fh_type = FUFH_INVALID;
159 atomic_add_acq_int(&fuse_node_count, 1);
163 fuse_vnode_destroy(struct vnode *vp)
165 struct fuse_vnode_data *fvdat = vp->v_data;
168 free(fvdat, M_FUSEVN);
170 atomic_subtract_acq_int(&fuse_node_count, 1);
174 fuse_vnode_cmp(struct vnode *vp, void *nidp)
176 return (VTOI(vp) != *((uint64_t *)nidp));
179 static uint32_t __inline
180 fuse_vnode_hash(uint64_t id)
182 return (fnv_32_buf(&id, sizeof(id), FNV1_32_INIT));
186 fuse_vnode_alloc(struct mount *mp,
192 struct fuse_vnode_data *fvdat;
196 FS_DEBUG("been asked for vno #%ju\n", (uintmax_t)nodeid);
202 err = vfs_hash_get(mp, fuse_vnode_hash(nodeid), LK_EXCLUSIVE, td, vpp,
203 fuse_vnode_cmp, &nodeid);
208 MPASS((*vpp)->v_type == vtyp && (*vpp)->v_data != NULL);
209 FS_DEBUG("vnode taken from hash\n");
212 fvdat = malloc(sizeof(*fvdat), M_FUSEVN, M_WAITOK | M_ZERO);
213 err = getnewvnode("fuse", mp, &fuse_vnops, vpp);
215 free(fvdat, M_FUSEVN);
218 lockmgr((*vpp)->v_vnlock, LK_EXCLUSIVE, NULL);
219 fuse_vnode_init(*vpp, fvdat, nodeid, vtyp);
220 err = insmntque(*vpp, mp);
221 ASSERT_VOP_ELOCKED(*vpp, "fuse_vnode_alloc");
223 free(fvdat, M_FUSEVN);
227 err = vfs_hash_insert(*vpp, fuse_vnode_hash(nodeid), LK_EXCLUSIVE,
228 td, &vp2, fuse_vnode_cmp, &nodeid);
236 ASSERT_VOP_ELOCKED(*vpp, "fuse_vnode_alloc");
242 fuse_vnode_get(struct mount *mp,
246 struct componentname *cnp,
249 struct thread *td = (cnp != NULL ? cnp->cn_thread : curthread);
252 debug_printf("dvp=%p\n", dvp);
254 err = fuse_vnode_alloc(mp, td, nodeid, vtyp, vpp);
259 MPASS((cnp->cn_flags & ISDOTDOT) == 0);
260 MPASS(!(cnp->cn_namelen == 1 && cnp->cn_nameptr[0] == '.'));
261 fuse_vnode_setparent(*vpp, dvp);
263 if (dvp != NULL && cnp != NULL && (cnp->cn_flags & MAKEENTRY) != 0) {
264 ASSERT_VOP_LOCKED(*vpp, "fuse_vnode_get");
265 ASSERT_VOP_LOCKED(dvp, "fuse_vnode_get");
266 cache_enter(dvp, *vpp, cnp);
270 * In userland, libfuse uses cached lookups for dot and dotdot entries,
271 * thus it does not really bump the nlookup counter for forget.
272 * Follow the same semantic and avoid tu bump it in order to keep
273 * nlookup counters consistent.
275 if (cnp == NULL || ((cnp->cn_flags & ISDOTDOT) == 0 &&
276 (cnp->cn_namelen != 1 || cnp->cn_nameptr[0] != '.')))
277 VTOFUD(*vpp)->nlookup++;
283 fuse_vnode_open(struct vnode *vp, int32_t fuse_open_flags, struct thread *td)
286 * Funcation is called for every vnode open.
287 * Merge fuse_open_flags it may be 0
290 * Ideally speaking, direct io should be enabled on
291 * fd's but do not see of any way of providing that
292 * this implementation.
294 * Also cannot think of a reason why would two
295 * different fd's on same vnode would like
296 * have DIRECT_IO turned on and off. But linux
297 * based implementation works on an fd not an
298 * inode and provides such a feature.
300 * XXXIP: Handle fd based DIRECT_IO
302 if (fuse_open_flags & FOPEN_DIRECT_IO) {
303 ASSERT_VOP_ELOCKED(vp, __func__);
304 VTOFUD(vp)->flag |= FN_DIRECTIO;
305 fuse_io_invalbuf(vp, td);
307 if ((fuse_open_flags & FOPEN_KEEP_CACHE) == 0)
308 fuse_io_invalbuf(vp, td);
309 VTOFUD(vp)->flag &= ~FN_DIRECTIO;
312 if (vnode_vtype(vp) == VREG) {
313 /* XXXIP prevent getattr, by using cached node size */
314 vnode_create_vobject(vp, 0, td);
319 fuse_vnode_savesize(struct vnode *vp, struct ucred *cred)
321 struct fuse_vnode_data *fvdat = VTOFUD(vp);
322 struct thread *td = curthread;
323 struct fuse_filehandle *fufh = NULL;
324 struct fuse_dispatcher fdi;
325 struct fuse_setattr_in *fsai;
328 FS_DEBUG("inode=%ju size=%ju\n", (uintmax_t)VTOI(vp),
329 (uintmax_t)fvdat->filesize);
330 ASSERT_VOP_ELOCKED(vp, "fuse_io_extend");
332 if (fuse_isdeadfs(vp)) {
335 if (vnode_vtype(vp) == VDIR) {
338 if (vfs_isrdonly(vnode_mount(vp))) {
344 fdisp_init(&fdi, sizeof(*fsai));
345 fdisp_make_vp(&fdi, FUSE_SETATTR, vp, td, cred);
349 /* Truncate to a new value. */
350 fsai->size = fvdat->filesize;
351 fsai->valid |= FATTR_SIZE;
353 fuse_filehandle_getrw(vp, FUFH_WRONLY, &fufh);
355 fsai->fh = fufh->fh_id;
356 fsai->valid |= FATTR_FH;
358 err = fdisp_wait_answ(&fdi);
361 fvdat->flag &= ~FN_SIZECHANGE;
367 fuse_vnode_refreshsize(struct vnode *vp, struct ucred *cred)
370 struct fuse_vnode_data *fvdat = VTOFUD(vp);
373 if ((fvdat->flag & FN_SIZECHANGE) != 0 ||
374 (fuse_refresh_size == 0 && fvdat->filesize != 0))
377 VOP_GETATTR(vp, &va, cred);
378 FS_DEBUG("refreshed file size: %jd\n", (intmax_t)VTOFUD(vp)->filesize);
382 fuse_vnode_setsize(struct vnode *vp, struct ucred *cred, off_t newsize)
384 struct fuse_vnode_data *fvdat = VTOFUD(vp);
388 FS_DEBUG("inode=%ju oldsize=%ju newsize=%ju\n",
389 (uintmax_t)VTOI(vp), (uintmax_t)fvdat->filesize,
391 ASSERT_VOP_ELOCKED(vp, "fuse_vnode_setsize");
393 oldsize = fvdat->filesize;
394 fvdat->filesize = newsize;
395 fvdat->flag |= FN_SIZECHANGE;
397 if (newsize < oldsize) {
398 err = vtruncbuf(vp, cred, newsize, fuse_iosize(vp));
400 vnode_pager_setsize(vp, newsize);