1 /*-
2 * SPDX-License-Identifier: BSD-3-Clause
3 *
4 * Copyright (c) 2007-2009 Google Inc. and Amit Singh
5 * All rights reserved.
6 *
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions are
9 * met:
10 *
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
16 * distribution.
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.
20 *
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.
32 *
33 * Copyright (C) 2005 Csaba Henk.
34 * All rights reserved.
35 *
36 * Copyright (c) 2019 The FreeBSD Foundation
37 *
38 * Portions of this software were developed by BFF Storage Systems, LLC under
39 * sponsorship from the FreeBSD Foundation.
40 *
41 * Redistribution and use in source and binary forms, with or without
42 * modification, are permitted provided that the following conditions
43 * are met:
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.
49 *
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
60 * SUCH DAMAGE.
61 */
62
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>
70 #include <sys/conf.h>
71 #include <sys/uio.h>
72 #include <sys/malloc.h>
73 #include <sys/queue.h>
74 #include <sys/lock.h>
75 #include <sys/sx.h>
76 #include <sys/mutex.h>
77 #include <sys/proc.h>
78 #include <sys/mount.h>
79 #include <sys/vnode.h>
80 #include <sys/sdt.h>
81 #include <sys/sysctl.h>
82
83 #include "fuse.h"
84 #include "fuse_file.h"
85 #include "fuse_internal.h"
86 #include "fuse_io.h"
87 #include "fuse_ipc.h"
88 #include "fuse_node.h"
89
90 MALLOC_DEFINE(M_FUSE_FILEHANDLE, "fuse_filefilehandle", "FUSE file handle");
91
92 SDT_PROVIDER_DECLARE(fusefs);
93 /*
94 * Fuse trace probe:
95 * arg0: verbosity. Higher numbers give more verbose messages
96 * arg1: Textual message
97 */
98 SDT_PROBE_DEFINE2(fusefs, , file, trace, "int", "char*");
99
100 static counter_u64_t fuse_fh_count;
101
102 SYSCTL_COUNTER_U64(_vfs_fusefs_stats, OID_AUTO, filehandle_count, CTLFLAG_RD,
103 &fuse_fh_count, "number of open FUSE filehandles");
104
105 /* Get the FUFH type for a particular access mode */
106 static inline fufh_type_t
fflags_2_fufh_type(int fflags)107 fflags_2_fufh_type(int fflags)
108 {
109 if ((fflags & FREAD) && (fflags & FWRITE))
110 return FUFH_RDWR;
111 else if (fflags & (FWRITE))
112 return FUFH_WRONLY;
113 else if (fflags & (FREAD))
114 return FUFH_RDONLY;
115 else if (fflags & (FEXEC))
116 return FUFH_EXEC;
117 else
118 panic("FUSE: What kind of a flag is this (%x)?", fflags);
119 }
120
121 int
fuse_filehandle_open(struct vnode * vp,int a_mode,struct fuse_filehandle ** fufhp,struct thread * td,struct ucred * cred)122 fuse_filehandle_open(struct vnode *vp, int a_mode,
123 struct fuse_filehandle **fufhp, struct thread *td, struct ucred *cred)
124 {
125 struct mount *mp = vnode_mount(vp);
126 struct fuse_dispatcher fdi;
127 const struct fuse_open_out default_foo = {
128 .fh = 0,
129 .open_flags = FOPEN_KEEP_CACHE,
130 .padding = 0
131 };
132 struct fuse_open_in *foi = NULL;
133 const struct fuse_open_out *foo;
134 fufh_type_t fufh_type;
135 int err = 0;
136 int oflags = 0;
137 int op = FUSE_OPEN;
138 int relop = FUSE_RELEASE;
139
140 fufh_type = fflags_2_fufh_type(a_mode);
141 oflags = fufh_type_2_fflags(fufh_type);
142
143 if (vnode_isdir(vp)) {
144 op = FUSE_OPENDIR;
145 relop = FUSE_RELEASEDIR;
146 /* vn_open_vnode already rejects FWRITE on directories */
147 MPASS(fufh_type == FUFH_RDONLY || fufh_type == FUFH_EXEC);
148 }
149 fdisp_init(&fdi, sizeof(*foi));
150 if (fsess_not_impl(mp, op)) {
151 /* The operation implicitly succeeds */
152 foo = &default_foo;
153 } else {
154 fdisp_make_vp(&fdi, op, vp, td, cred);
155
156 foi = fdi.indata;
157 foi->flags = oflags;
158
159 err = fdisp_wait_answ(&fdi);
160 if (err == ENOSYS) {
161 /* The operation implicitly succeeds */
162 foo = &default_foo;
163 fsess_set_notimpl(mp, op);
164 fsess_set_notimpl(mp, relop);
165 err = 0;
166 } else if (err) {
167 SDT_PROBE2(fusefs, , file, trace, 1,
168 "OUCH ... daemon didn't give fh");
169 if (err == ENOENT)
170 fuse_internal_vnode_disappear(vp);
171 goto out;
172 } else {
173 foo = fdi.answ;
174 fsess_set_impl(mp, op);
175 }
176 }
177
178 fuse_filehandle_init(vp, fufh_type, fufhp, td, cred, foo);
179 fuse_vnode_open(vp, foo->open_flags, td);
180
181 out:
182 if (foi)
183 fdisp_destroy(&fdi);
184 return err;
185 }
186
187 int
fuse_filehandle_close(struct vnode * vp,struct fuse_filehandle * fufh,struct thread * td,struct ucred * cred)188 fuse_filehandle_close(struct vnode *vp, struct fuse_filehandle *fufh,
189 struct thread *td, struct ucred *cred)
190 {
191 struct mount *mp = vnode_mount(vp);
192 struct fuse_dispatcher fdi;
193 struct fuse_release_in *fri;
194
195 int err = 0;
196 int op = FUSE_RELEASE;
197
198 if (fuse_isdeadfs(vp)) {
199 goto out;
200 }
201 if (vnode_isdir(vp))
202 op = FUSE_RELEASEDIR;
203
204 if (fsess_not_impl(mp, op))
205 goto out;
206
207 fdisp_init(&fdi, sizeof(*fri));
208 fdisp_make_vp(&fdi, op, vp, td, cred);
209 fri = fdi.indata;
210 fri->fh = fufh->fh_id;
211 fri->flags = fufh_type_2_fflags(fufh->fufh_type);
212 /*
213 * If the file has a POSIX lock then we're supposed to set lock_owner.
214 * If not, then lock_owner is undefined. So we may as well always set
215 * it.
216 */
217 fri->lock_owner = td->td_proc->p_pid;
218
219 err = fdisp_wait_answ(&fdi);
220 fdisp_destroy(&fdi);
221
222 out:
223 counter_u64_add(fuse_fh_count, -1);
224 LIST_REMOVE(fufh, next);
225 free(fufh, M_FUSE_FILEHANDLE);
226
227 return err;
228 }
229
230 /*
231 * Check for a valid file handle, first the type requested, but if that
232 * isn't valid, try for FUFH_RDWR.
233 * Return true if there is any file handle with the correct credentials and
234 * a fufh type that includes the provided one.
235 * A pid of 0 means "don't care"
236 */
237 bool
fuse_filehandle_validrw(struct vnode * vp,int mode,struct ucred * cred,pid_t pid)238 fuse_filehandle_validrw(struct vnode *vp, int mode,
239 struct ucred *cred, pid_t pid)
240 {
241 struct fuse_vnode_data *fvdat = VTOFUD(vp);
242 struct fuse_filehandle *fufh;
243 fufh_type_t fufh_type = fflags_2_fufh_type(mode);
244
245 /*
246 * Unlike fuse_filehandle_get, we want to search for a filehandle with
247 * the exact cred, and no fallback
248 */
249 LIST_FOREACH(fufh, &fvdat->handles, next) {
250 if (fufh->fufh_type == fufh_type &&
251 fufh->uid == cred->cr_uid &&
252 fufh->gid == cred->cr_rgid &&
253 (pid == 0 || fufh->pid == pid))
254 return true;
255 }
256
257 if (fufh_type == FUFH_EXEC)
258 return false;
259
260 /* Fallback: find a RDWR list entry with the right cred */
261 LIST_FOREACH(fufh, &fvdat->handles, next) {
262 if (fufh->fufh_type == FUFH_RDWR &&
263 fufh->uid == cred->cr_uid &&
264 fufh->gid == cred->cr_rgid &&
265 (pid == 0 || fufh->pid == pid))
266 return true;
267 }
268
269 return false;
270 }
271
272 int
fuse_filehandle_get(struct vnode * vp,int fflag,struct fuse_filehandle ** fufhp,struct ucred * cred,pid_t pid)273 fuse_filehandle_get(struct vnode *vp, int fflag,
274 struct fuse_filehandle **fufhp, struct ucred *cred, pid_t pid)
275 {
276 struct fuse_vnode_data *fvdat = VTOFUD(vp);
277 struct fuse_filehandle *fufh;
278 fufh_type_t fufh_type;
279
280 fufh_type = fflags_2_fufh_type(fflag);
281 /* cred can be NULL for in-kernel clients */
282 if (cred == NULL)
283 goto fallback;
284
285 LIST_FOREACH(fufh, &fvdat->handles, next) {
286 if (fufh->fufh_type == fufh_type &&
287 fufh->uid == cred->cr_uid &&
288 fufh->gid == cred->cr_rgid &&
289 (pid == 0 || fufh->pid == pid))
290 goto found;
291 }
292
293 fallback:
294 /* Fallback: find a list entry with the right flags */
295 LIST_FOREACH(fufh, &fvdat->handles, next) {
296 if (fufh->fufh_type == fufh_type)
297 break;
298 }
299
300 if (fufh == NULL)
301 return EBADF;
302
303 found:
304 if (fufhp != NULL)
305 *fufhp = fufh;
306 return 0;
307 }
308
309 /* Get a file handle with any kind of flags */
310 int
fuse_filehandle_get_anyflags(struct vnode * vp,struct fuse_filehandle ** fufhp,struct ucred * cred,pid_t pid)311 fuse_filehandle_get_anyflags(struct vnode *vp,
312 struct fuse_filehandle **fufhp, struct ucred *cred, pid_t pid)
313 {
314 struct fuse_vnode_data *fvdat = VTOFUD(vp);
315 struct fuse_filehandle *fufh;
316
317 if (cred == NULL)
318 goto fallback;
319
320 LIST_FOREACH(fufh, &fvdat->handles, next) {
321 if (fufh->uid == cred->cr_uid &&
322 fufh->gid == cred->cr_rgid &&
323 (pid == 0 || fufh->pid == pid))
324 goto found;
325 }
326
327 fallback:
328 /* Fallback: find any list entry */
329 fufh = LIST_FIRST(&fvdat->handles);
330
331 if (fufh == NULL)
332 return EBADF;
333
334 found:
335 if (fufhp != NULL)
336 *fufhp = fufh;
337 return 0;
338 }
339
340 int
fuse_filehandle_getrw(struct vnode * vp,int fflag,struct fuse_filehandle ** fufhp,struct ucred * cred,pid_t pid)341 fuse_filehandle_getrw(struct vnode *vp, int fflag,
342 struct fuse_filehandle **fufhp, struct ucred *cred, pid_t pid)
343 {
344 int err;
345
346 err = fuse_filehandle_get(vp, fflag, fufhp, cred, pid);
347 if (err)
348 err = fuse_filehandle_get(vp, FREAD | FWRITE, fufhp, cred, pid);
349 return err;
350 }
351
352 void
fuse_filehandle_init(struct vnode * vp,fufh_type_t fufh_type,struct fuse_filehandle ** fufhp,struct thread * td,const struct ucred * cred,const struct fuse_open_out * foo)353 fuse_filehandle_init(struct vnode *vp, fufh_type_t fufh_type,
354 struct fuse_filehandle **fufhp, struct thread *td, const struct ucred *cred,
355 const struct fuse_open_out *foo)
356 {
357 struct fuse_vnode_data *fvdat = VTOFUD(vp);
358 struct fuse_filehandle *fufh;
359
360 fufh = malloc(sizeof(struct fuse_filehandle), M_FUSE_FILEHANDLE,
361 M_WAITOK);
362 MPASS(fufh != NULL);
363 fufh->fh_id = foo->fh;
364 fufh->fufh_type = fufh_type;
365 fufh->gid = cred->cr_rgid;
366 fufh->uid = cred->cr_uid;
367 fufh->pid = td->td_proc->p_pid;
368 fufh->fuse_open_flags = foo->open_flags;
369 if (!FUFH_IS_VALID(fufh)) {
370 panic("FUSE: init: invalid filehandle id (type=%d)", fufh_type);
371 }
372 LIST_INSERT_HEAD(&fvdat->handles, fufh, next);
373 if (fufhp != NULL)
374 *fufhp = fufh;
375
376 counter_u64_add(fuse_fh_count, 1);
377
378 if (foo->open_flags & FOPEN_DIRECT_IO) {
379 ASSERT_VOP_ELOCKED(vp, __func__);
380 VTOFUD(vp)->flag |= FN_DIRECTIO;
381 fuse_io_invalbuf(vp, td);
382 } else {
383 if ((foo->open_flags & FOPEN_KEEP_CACHE) == 0)
384 fuse_io_invalbuf(vp, td);
385 VTOFUD(vp)->flag &= ~FN_DIRECTIO;
386 }
387
388 }
389
390 void
fuse_file_init(void)391 fuse_file_init(void)
392 {
393 fuse_fh_count = counter_u64_alloc(M_WAITOK);
394 }
395
396 void
fuse_file_destroy(void)397 fuse_file_destroy(void)
398 {
399 counter_u64_free(fuse_fh_count);
400 }
401