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/param.h>
67 #include <sys/systm.h>
68 #include <sys/counter.h>
69 #include <sys/module.h>
70 #include <sys/errno.h>
71 #include <sys/kernel.h>
74 #include <sys/malloc.h>
75 #include <sys/queue.h>
78 #include <sys/mutex.h>
80 #include <sys/mount.h>
81 #include <sys/vnode.h>
83 #include <sys/sysctl.h>
86 #include "fuse_file.h"
87 #include "fuse_internal.h"
90 #include "fuse_node.h"
92 MALLOC_DEFINE(M_FUSE_FILEHANDLE, "fuse_filefilehandle", "FUSE file handle");
94 SDT_PROVIDER_DECLARE(fusefs);
97 * arg0: verbosity. Higher numbers give more verbose messages
98 * arg1: Textual message
100 SDT_PROBE_DEFINE2(fusefs, , file, trace, "int", "char*");
102 static counter_u64_t fuse_fh_count;
104 SYSCTL_COUNTER_U64(_vfs_fusefs_stats, OID_AUTO, filehandle_count, CTLFLAG_RD,
105 &fuse_fh_count, "number of open FUSE filehandles");
107 /* Get the FUFH type for a particular access mode */
108 static inline fufh_type_t
109 fflags_2_fufh_type(int fflags)
111 if ((fflags & FREAD) && (fflags & FWRITE))
113 else if (fflags & (FWRITE))
115 else if (fflags & (FREAD))
117 else if (fflags & (FEXEC))
120 panic("FUSE: What kind of a flag is this (%x)?", fflags);
124 fuse_filehandle_open(struct vnode *vp, int a_mode,
125 struct fuse_filehandle **fufhp, struct thread *td, struct ucred *cred)
127 struct mount *mp = vnode_mount(vp);
128 struct fuse_data *data = fuse_get_mpdata(mp);
129 struct fuse_dispatcher fdi;
130 const struct fuse_open_out default_foo = {
132 .open_flags = FOPEN_KEEP_CACHE,
135 struct fuse_open_in *foi = NULL;
136 const struct fuse_open_out *foo;
137 fufh_type_t fufh_type;
138 int dataflags = data->dataflags;
142 int relop = FUSE_RELEASE;
143 int fsess_no_op_support = FSESS_NO_OPEN_SUPPORT;
145 fufh_type = fflags_2_fufh_type(a_mode);
146 oflags = fufh_type_2_fflags(fufh_type);
148 if (vnode_isdir(vp)) {
150 relop = FUSE_RELEASEDIR;
151 fsess_no_op_support = FSESS_NO_OPENDIR_SUPPORT;
152 /* vn_open_vnode already rejects FWRITE on directories */
153 MPASS(fufh_type == FUFH_RDONLY || fufh_type == FUFH_EXEC);
155 fdisp_init(&fdi, sizeof(*foi));
156 if (fsess_not_impl(mp, op) && dataflags & fsess_no_op_support) {
157 /* The operation implicitly succeeds */
160 fdisp_make_vp(&fdi, op, vp, td, cred);
165 err = fdisp_wait_answ(&fdi);
166 if (err == ENOSYS && dataflags & fsess_no_op_support) {
167 /* The operation implicitly succeeds */
169 fsess_set_notimpl(mp, op);
170 fsess_set_notimpl(mp, relop);
173 SDT_PROBE2(fusefs, , file, trace, 1,
174 "OUCH ... daemon didn't give fh");
176 fuse_internal_vnode_disappear(vp);
183 fuse_filehandle_init(vp, fufh_type, fufhp, td, cred, foo);
184 fuse_vnode_open(vp, foo->open_flags, td);
193 fuse_filehandle_close(struct vnode *vp, struct fuse_filehandle *fufh,
194 struct thread *td, struct ucred *cred)
196 struct mount *mp = vnode_mount(vp);
197 struct fuse_dispatcher fdi;
198 struct fuse_release_in *fri;
201 int op = FUSE_RELEASE;
203 if (fuse_isdeadfs(vp)) {
207 op = FUSE_RELEASEDIR;
209 if (fsess_not_impl(mp, op))
212 fdisp_init(&fdi, sizeof(*fri));
213 fdisp_make_vp(&fdi, op, vp, td, cred);
215 fri->fh = fufh->fh_id;
216 fri->flags = fufh_type_2_fflags(fufh->fufh_type);
218 * If the file has a POSIX lock then we're supposed to set lock_owner.
219 * If not, then lock_owner is undefined. So we may as well always set
222 fri->lock_owner = td->td_proc->p_pid;
224 err = fdisp_wait_answ(&fdi);
228 counter_u64_add(fuse_fh_count, -1);
229 LIST_REMOVE(fufh, next);
230 free(fufh, M_FUSE_FILEHANDLE);
236 * Check for a valid file handle, first the type requested, but if that
237 * isn't valid, try for FUFH_RDWR.
238 * Return true if there is any file handle with the correct credentials and
239 * a fufh type that includes the provided one.
240 * A pid of 0 means "don't care"
243 fuse_filehandle_validrw(struct vnode *vp, int mode,
244 struct ucred *cred, pid_t pid)
246 struct fuse_vnode_data *fvdat = VTOFUD(vp);
247 struct fuse_filehandle *fufh;
248 fufh_type_t fufh_type = fflags_2_fufh_type(mode);
251 * Unlike fuse_filehandle_get, we want to search for a filehandle with
252 * the exact cred, and no fallback
254 LIST_FOREACH(fufh, &fvdat->handles, next) {
255 if (fufh->fufh_type == fufh_type &&
256 fufh->uid == cred->cr_uid &&
257 fufh->gid == cred->cr_rgid &&
258 (pid == 0 || fufh->pid == pid))
262 if (fufh_type == FUFH_EXEC)
265 /* Fallback: find a RDWR list entry with the right cred */
266 LIST_FOREACH(fufh, &fvdat->handles, next) {
267 if (fufh->fufh_type == FUFH_RDWR &&
268 fufh->uid == cred->cr_uid &&
269 fufh->gid == cred->cr_rgid &&
270 (pid == 0 || fufh->pid == pid))
278 fuse_filehandle_get(struct vnode *vp, int fflag,
279 struct fuse_filehandle **fufhp, struct ucred *cred, pid_t pid)
281 struct fuse_vnode_data *fvdat = VTOFUD(vp);
282 struct fuse_filehandle *fufh;
283 fufh_type_t fufh_type;
285 fufh_type = fflags_2_fufh_type(fflag);
286 /* cred can be NULL for in-kernel clients */
290 LIST_FOREACH(fufh, &fvdat->handles, next) {
291 if (fufh->fufh_type == fufh_type &&
292 fufh->uid == cred->cr_uid &&
293 fufh->gid == cred->cr_rgid &&
294 (pid == 0 || fufh->pid == pid))
299 /* Fallback: find a list entry with the right flags */
300 LIST_FOREACH(fufh, &fvdat->handles, next) {
301 if (fufh->fufh_type == fufh_type)
314 /* Get a file handle with any kind of flags */
316 fuse_filehandle_get_anyflags(struct vnode *vp,
317 struct fuse_filehandle **fufhp, struct ucred *cred, pid_t pid)
319 struct fuse_vnode_data *fvdat = VTOFUD(vp);
320 struct fuse_filehandle *fufh;
325 LIST_FOREACH(fufh, &fvdat->handles, next) {
326 if (fufh->uid == cred->cr_uid &&
327 fufh->gid == cred->cr_rgid &&
328 (pid == 0 || fufh->pid == pid))
333 /* Fallback: find any list entry */
334 fufh = LIST_FIRST(&fvdat->handles);
346 fuse_filehandle_getrw(struct vnode *vp, int fflag,
347 struct fuse_filehandle **fufhp, struct ucred *cred, pid_t pid)
351 err = fuse_filehandle_get(vp, fflag, fufhp, cred, pid);
353 err = fuse_filehandle_get(vp, FREAD | FWRITE, fufhp, cred, pid);
358 fuse_filehandle_init(struct vnode *vp, fufh_type_t fufh_type,
359 struct fuse_filehandle **fufhp, struct thread *td, const struct ucred *cred,
360 const struct fuse_open_out *foo)
362 struct fuse_vnode_data *fvdat = VTOFUD(vp);
363 struct fuse_filehandle *fufh;
365 fufh = malloc(sizeof(struct fuse_filehandle), M_FUSE_FILEHANDLE,
368 fufh->fh_id = foo->fh;
369 fufh->fufh_type = fufh_type;
370 fufh->gid = cred->cr_rgid;
371 fufh->uid = cred->cr_uid;
372 fufh->pid = td->td_proc->p_pid;
373 fufh->fuse_open_flags = foo->open_flags;
374 if (!FUFH_IS_VALID(fufh)) {
375 panic("FUSE: init: invalid filehandle id (type=%d)", fufh_type);
377 LIST_INSERT_HEAD(&fvdat->handles, fufh, next);
381 counter_u64_add(fuse_fh_count, 1);
383 if (foo->open_flags & FOPEN_DIRECT_IO) {
384 ASSERT_VOP_ELOCKED(vp, __func__);
385 VTOFUD(vp)->flag |= FN_DIRECTIO;
386 fuse_io_invalbuf(vp, td);
388 if ((foo->open_flags & FOPEN_KEEP_CACHE) == 0)
389 fuse_io_invalbuf(vp, td);
390 VTOFUD(vp)->flag &= ~FN_DIRECTIO;
398 fuse_fh_count = counter_u64_alloc(M_WAITOK);
402 fuse_file_destroy(void)
404 counter_u64_free(fuse_fh_count);