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