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/mount.h>
76 #include <sys/vnode.h>
77 #include <sys/sysctl.h>
80 #include "fuse_file.h"
81 #include "fuse_internal.h"
83 #include "fuse_node.h"
85 #define FUSE_DEBUG_MODULE FILE
86 #include "fuse_debug.h"
88 static int fuse_fh_count = 0;
90 SYSCTL_INT(_vfs_fuse, OID_AUTO, filehandle_count, CTLFLAG_RD,
91 &fuse_fh_count, 0, "");
94 fuse_filehandle_open(struct vnode *vp,
95 fufh_type_t fufh_type,
96 struct fuse_filehandle **fufhp,
100 struct fuse_dispatcher fdi;
101 struct fuse_open_in *foi;
102 struct fuse_open_out *foo;
109 fuse_trace_printf("fuse_filehandle_open(vp=%p, fufh_type=%d)\n",
112 if (fuse_filehandle_valid(vp, fufh_type)) {
113 panic("FUSE: filehandle_open called despite valid fufh (type=%d)",
118 * Note that this means we are effectively FILTERING OUT open() flags.
120 oflags = fuse_filehandle_xlate_to_oflags(fufh_type);
122 if (vnode_isdir(vp)) {
125 if (fufh_type != FUFH_RDONLY) {
126 printf("FUSE:non-rdonly fh requested for a directory?\n");
127 fufh_type = FUFH_RDONLY;
130 fdisp_init(&fdi, sizeof(*foi));
131 fdisp_make_vp(&fdi, op, vp, td, cred);
136 if ((err = fdisp_wait_answ(&fdi))) {
137 debug_printf("OUCH ... daemon didn't give fh (err = %d)\n", err);
139 fuse_internal_vnode_disappear(vp);
145 fuse_filehandle_init(vp, fufh_type, fufhp, foo->fh);
148 * For WRONLY opens, force DIRECT_IO. This is necessary
149 * since writing a partial block through the buffer cache
150 * will result in a read of the block and that read won't
151 * be allowed by the WRONLY open.
153 if (fufh_type == FUFH_WRONLY)
154 fuse_vnode_open(vp, foo->open_flags | FOPEN_DIRECT_IO, td);
156 fuse_vnode_open(vp, foo->open_flags, td);
164 fuse_filehandle_close(struct vnode *vp,
165 fufh_type_t fufh_type,
169 struct fuse_dispatcher fdi;
170 struct fuse_release_in *fri;
171 struct fuse_vnode_data *fvdat = VTOFUD(vp);
172 struct fuse_filehandle *fufh = NULL;
176 int op = FUSE_RELEASE;
178 fuse_trace_printf("fuse_filehandle_put(vp=%p, fufh_type=%d)\n",
181 fufh = &(fvdat->fufh[fufh_type]);
182 if (!FUFH_IS_VALID(fufh)) {
183 panic("FUSE: filehandle_put called on invalid fufh (type=%d)",
187 if (fuse_isdeadfs(vp)) {
190 if (vnode_isdir(vp)) {
191 op = FUSE_RELEASEDIR;
194 fdisp_init(&fdi, sizeof(*fri));
195 fdisp_make_vp(&fdi, op, vp, td, cred);
197 fri->fh = fufh->fh_id;
198 fri->flags = fuse_filehandle_xlate_to_oflags(fufh_type);
200 err = fdisp_wait_answ(&fdi);
204 atomic_subtract_acq_int(&fuse_fh_count, 1);
205 fufh->fh_id = (uint64_t)-1;
206 fufh->fh_type = FUFH_INVALID;
212 fuse_filehandle_valid(struct vnode *vp, fufh_type_t fufh_type)
214 struct fuse_vnode_data *fvdat = VTOFUD(vp);
215 struct fuse_filehandle *fufh;
217 fufh = &(fvdat->fufh[fufh_type]);
218 return FUFH_IS_VALID(fufh);
222 * Check for a valid file handle, first the type requested, but if that
223 * isn't valid, try for FUFH_RDWR.
224 * Return the FUFH type that is valid or FUFH_INVALID if there are none.
225 * This is a variant of fuse_filehandle_vaild() analogous to
226 * fuse_filehandle_getrw().
229 fuse_filehandle_validrw(struct vnode *vp, fufh_type_t fufh_type)
231 struct fuse_vnode_data *fvdat = VTOFUD(vp);
232 struct fuse_filehandle *fufh;
234 fufh = &fvdat->fufh[fufh_type];
235 if (FUFH_IS_VALID(fufh) != 0)
237 fufh = &fvdat->fufh[FUFH_RDWR];
238 if (FUFH_IS_VALID(fufh) != 0)
240 return (FUFH_INVALID);
244 fuse_filehandle_get(struct vnode *vp, fufh_type_t fufh_type,
245 struct fuse_filehandle **fufhp)
247 struct fuse_vnode_data *fvdat = VTOFUD(vp);
248 struct fuse_filehandle *fufh;
250 fufh = &(fvdat->fufh[fufh_type]);
251 if (!FUFH_IS_VALID(fufh))
259 fuse_filehandle_getrw(struct vnode *vp, fufh_type_t fufh_type,
260 struct fuse_filehandle **fufhp)
262 struct fuse_vnode_data *fvdat = VTOFUD(vp);
263 struct fuse_filehandle *fufh;
265 fufh = &(fvdat->fufh[fufh_type]);
266 if (!FUFH_IS_VALID(fufh)) {
267 fufh_type = FUFH_RDWR;
269 return fuse_filehandle_get(vp, fufh_type, fufhp);
273 fuse_filehandle_init(struct vnode *vp,
274 fufh_type_t fufh_type,
275 struct fuse_filehandle **fufhp,
278 struct fuse_vnode_data *fvdat = VTOFUD(vp);
279 struct fuse_filehandle *fufh;
281 FS_DEBUG("id=%jd type=%d\n", (intmax_t)fh_id, fufh_type);
282 fufh = &(fvdat->fufh[fufh_type]);
283 MPASS(!FUFH_IS_VALID(fufh));
285 fufh->fh_type = fufh_type;
286 if (!FUFH_IS_VALID(fufh)) {
287 panic("FUSE: init: invalid filehandle id (type=%d)", fufh_type);
292 atomic_add_acq_int(&fuse_fh_count, 1);