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 #include <sys/param.h>
65 #include <sys/systm.h>
66 #include <sys/counter.h>
67 #include <sys/module.h>
68 #include <sys/errno.h>
69 #include <sys/kernel.h>
72 #include <sys/malloc.h>
73 #include <sys/queue.h>
76 #include <sys/mutex.h>
78 #include <sys/mount.h>
79 #include <sys/vnode.h>
81 #include <sys/sysctl.h>
84 #include "fuse_file.h"
85 #include "fuse_internal.h"
88 #include "fuse_node.h"
90 MALLOC_DEFINE(M_FUSE_FILEHANDLE, "fuse_filefilehandle", "FUSE file handle");
92 SDT_PROVIDER_DECLARE(fusefs);
95 * arg0: verbosity. Higher numbers give more verbose messages
96 * arg1: Textual message
98 SDT_PROBE_DEFINE2(fusefs, , file, trace, "int", "char*");
100 static counter_u64_t fuse_fh_count;
102 SYSCTL_COUNTER_U64(_vfs_fusefs_stats, OID_AUTO, filehandle_count, CTLFLAG_RD,
103 &fuse_fh_count, "number of open FUSE filehandles");
105 /* Get the FUFH type for a particular access mode */
106 static inline fufh_type_t
107 fflags_2_fufh_type(int fflags)
109 if ((fflags & FREAD) && (fflags & FWRITE))
111 else if (fflags & (FWRITE))
113 else if (fflags & (FREAD))
115 else if (fflags & (FEXEC))
118 panic("FUSE: What kind of a flag is this (%x)?", fflags);
122 fuse_filehandle_open(struct vnode *vp, int a_mode,
123 struct fuse_filehandle **fufhp, struct thread *td, struct ucred *cred)
125 struct mount *mp = vnode_mount(vp);
126 struct fuse_data *data = fuse_get_mpdata(mp);
127 struct fuse_dispatcher fdi;
128 const struct fuse_open_out default_foo = {
130 .open_flags = FOPEN_KEEP_CACHE,
133 struct fuse_open_in *foi = NULL;
134 const struct fuse_open_out *foo;
135 fufh_type_t fufh_type;
136 int dataflags = data->dataflags;
140 int relop = FUSE_RELEASE;
141 int fsess_no_op_support = FSESS_NO_OPEN_SUPPORT;
143 fufh_type = fflags_2_fufh_type(a_mode);
144 oflags = fufh_type_2_fflags(fufh_type);
146 if (vnode_isdir(vp)) {
148 relop = FUSE_RELEASEDIR;
149 fsess_no_op_support = FSESS_NO_OPENDIR_SUPPORT;
150 /* vn_open_vnode already rejects FWRITE on directories */
151 MPASS(fufh_type == FUFH_RDONLY || fufh_type == FUFH_EXEC);
153 fdisp_init(&fdi, sizeof(*foi));
154 if (fsess_not_impl(mp, op) && dataflags & fsess_no_op_support) {
155 /* The operation implicitly succeeds */
158 fdisp_make_vp(&fdi, op, vp, td, cred);
163 err = fdisp_wait_answ(&fdi);
164 if (err == ENOSYS && dataflags & fsess_no_op_support) {
165 /* The operation implicitly succeeds */
167 fsess_set_notimpl(mp, op);
168 fsess_set_notimpl(mp, relop);
171 SDT_PROBE2(fusefs, , file, trace, 1,
172 "OUCH ... daemon didn't give fh");
174 fuse_internal_vnode_disappear(vp);
181 fuse_filehandle_init(vp, fufh_type, fufhp, td, cred, foo);
182 fuse_vnode_open(vp, foo->open_flags, td);
191 fuse_filehandle_close(struct vnode *vp, struct fuse_filehandle *fufh,
192 struct thread *td, struct ucred *cred)
194 struct mount *mp = vnode_mount(vp);
195 struct fuse_dispatcher fdi;
196 struct fuse_release_in *fri;
199 int op = FUSE_RELEASE;
201 if (fuse_isdeadfs(vp)) {
205 op = FUSE_RELEASEDIR;
207 if (fsess_not_impl(mp, op))
210 fdisp_init(&fdi, sizeof(*fri));
211 fdisp_make_vp(&fdi, op, vp, td, cred);
213 fri->fh = fufh->fh_id;
214 fri->flags = fufh_type_2_fflags(fufh->fufh_type);
216 * If the file has a POSIX lock then we're supposed to set lock_owner.
217 * If not, then lock_owner is undefined. So we may as well always set
220 fri->lock_owner = td->td_proc->p_pid;
222 err = fdisp_wait_answ(&fdi);
226 counter_u64_add(fuse_fh_count, -1);
227 LIST_REMOVE(fufh, next);
228 free(fufh, M_FUSE_FILEHANDLE);
234 * Check for a valid file handle, first the type requested, but if that
235 * isn't valid, try for FUFH_RDWR.
236 * Return true if there is any file handle with the correct credentials and
237 * a fufh type that includes the provided one.
238 * A pid of 0 means "don't care"
241 fuse_filehandle_validrw(struct vnode *vp, int mode,
242 struct ucred *cred, pid_t pid)
244 struct fuse_vnode_data *fvdat = VTOFUD(vp);
245 struct fuse_filehandle *fufh;
246 fufh_type_t fufh_type = fflags_2_fufh_type(mode);
249 * Unlike fuse_filehandle_get, we want to search for a filehandle with
250 * the exact cred, and no fallback
252 LIST_FOREACH(fufh, &fvdat->handles, next) {
253 if (fufh->fufh_type == fufh_type &&
254 fufh->uid == cred->cr_uid &&
255 fufh->gid == cred->cr_rgid &&
256 (pid == 0 || fufh->pid == pid))
260 if (fufh_type == FUFH_EXEC)
263 /* Fallback: find a RDWR list entry with the right cred */
264 LIST_FOREACH(fufh, &fvdat->handles, next) {
265 if (fufh->fufh_type == FUFH_RDWR &&
266 fufh->uid == cred->cr_uid &&
267 fufh->gid == cred->cr_rgid &&
268 (pid == 0 || fufh->pid == pid))
276 fuse_filehandle_get(struct vnode *vp, int fflag,
277 struct fuse_filehandle **fufhp, struct ucred *cred, pid_t pid)
279 struct fuse_vnode_data *fvdat = VTOFUD(vp);
280 struct fuse_filehandle *fufh;
281 fufh_type_t fufh_type;
283 fufh_type = fflags_2_fufh_type(fflag);
284 /* cred can be NULL for in-kernel clients */
288 LIST_FOREACH(fufh, &fvdat->handles, next) {
289 if (fufh->fufh_type == fufh_type &&
290 fufh->uid == cred->cr_uid &&
291 fufh->gid == cred->cr_rgid &&
292 (pid == 0 || fufh->pid == pid))
297 /* Fallback: find a list entry with the right flags */
298 LIST_FOREACH(fufh, &fvdat->handles, next) {
299 if (fufh->fufh_type == fufh_type)
312 /* Get a file handle with any kind of flags */
314 fuse_filehandle_get_anyflags(struct vnode *vp,
315 struct fuse_filehandle **fufhp, struct ucred *cred, pid_t pid)
317 struct fuse_vnode_data *fvdat = VTOFUD(vp);
318 struct fuse_filehandle *fufh;
323 LIST_FOREACH(fufh, &fvdat->handles, next) {
324 if (fufh->uid == cred->cr_uid &&
325 fufh->gid == cred->cr_rgid &&
326 (pid == 0 || fufh->pid == pid))
331 /* Fallback: find any list entry */
332 fufh = LIST_FIRST(&fvdat->handles);
344 fuse_filehandle_getrw(struct vnode *vp, int fflag,
345 struct fuse_filehandle **fufhp, struct ucred *cred, pid_t pid)
349 err = fuse_filehandle_get(vp, fflag, fufhp, cred, pid);
351 err = fuse_filehandle_get(vp, FREAD | FWRITE, fufhp, cred, pid);
356 fuse_filehandle_init(struct vnode *vp, fufh_type_t fufh_type,
357 struct fuse_filehandle **fufhp, struct thread *td, const struct ucred *cred,
358 const struct fuse_open_out *foo)
360 struct fuse_vnode_data *fvdat = VTOFUD(vp);
361 struct fuse_filehandle *fufh;
363 fufh = malloc(sizeof(struct fuse_filehandle), M_FUSE_FILEHANDLE,
366 fufh->fh_id = foo->fh;
367 fufh->fufh_type = fufh_type;
368 fufh->gid = cred->cr_rgid;
369 fufh->uid = cred->cr_uid;
370 fufh->pid = td->td_proc->p_pid;
371 fufh->fuse_open_flags = foo->open_flags;
372 if (!FUFH_IS_VALID(fufh)) {
373 panic("FUSE: init: invalid filehandle id (type=%d)", fufh_type);
375 LIST_INSERT_HEAD(&fvdat->handles, fufh, next);
379 counter_u64_add(fuse_fh_count, 1);
381 if (foo->open_flags & FOPEN_DIRECT_IO) {
382 ASSERT_VOP_ELOCKED(vp, __func__);
383 VTOFUD(vp)->flag |= FN_DIRECTIO;
384 fuse_io_invalbuf(vp, td);
386 if ((foo->open_flags & FOPEN_KEEP_CACHE) == 0)
387 fuse_io_invalbuf(vp, td);
388 VTOFUD(vp)->flag &= ~FN_DIRECTIO;
396 fuse_fh_count = counter_u64_alloc(M_WAITOK);
400 fuse_file_destroy(void)
402 counter_u64_free(fuse_fh_count);