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_data *data = fuse_get_mpdata(mp);
127 struct fuse_dispatcher fdi;
128 const struct fuse_open_out default_foo = {
129 .fh = 0,
130 .open_flags = FOPEN_KEEP_CACHE,
131 .padding = 0
132 };
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;
137 int err = 0;
138 int oflags = 0;
139 int op = FUSE_OPEN;
140 int relop = FUSE_RELEASE;
141 int fsess_no_op_support = FSESS_NO_OPEN_SUPPORT;
142
143 fufh_type = fflags_2_fufh_type(a_mode);
144 oflags = fufh_type_2_fflags(fufh_type);
145
146 if (vnode_isdir(vp)) {
147 op = FUSE_OPENDIR;
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);
152 }
153 fdisp_init(&fdi, sizeof(*foi));
154 if (fsess_not_impl(mp, op) && dataflags & fsess_no_op_support) {
155 /* The operation implicitly succeeds */
156 foo = &default_foo;
157 } else {
158 fdisp_make_vp(&fdi, op, vp, td, cred);
159
160 foi = fdi.indata;
161 foi->flags = oflags;
162
163 err = fdisp_wait_answ(&fdi);
164 if (err == ENOSYS && dataflags & fsess_no_op_support) {
165 /* The operation implicitly succeeds */
166 foo = &default_foo;
167 fsess_set_notimpl(mp, op);
168 fsess_set_notimpl(mp, relop);
169 err = 0;
170 } else if (err) {
171 SDT_PROBE2(fusefs, , file, trace, 1,
172 "OUCH ... daemon didn't give fh");
173 if (err == ENOENT)
174 fuse_internal_vnode_disappear(vp);
175 goto out;
176 } else {
177 foo = fdi.answ;
178 }
179 }
180
181 fuse_filehandle_init(vp, fufh_type, fufhp, td, cred, foo);
182 fuse_vnode_open(vp, foo->open_flags, td);
183
184 out:
185 if (foi)
186 fdisp_destroy(&fdi);
187 return err;
188 }
189
190 int
fuse_filehandle_close(struct vnode * vp,struct fuse_filehandle * fufh,struct thread * td,struct ucred * cred)191 fuse_filehandle_close(struct vnode *vp, struct fuse_filehandle *fufh,
192 struct thread *td, struct ucred *cred)
193 {
194 struct mount *mp = vnode_mount(vp);
195 struct fuse_dispatcher fdi;
196 struct fuse_release_in *fri;
197
198 int err = 0;
199 int op = FUSE_RELEASE;
200
201 if (fuse_isdeadfs(vp)) {
202 goto out;
203 }
204 if (vnode_isdir(vp))
205 op = FUSE_RELEASEDIR;
206
207 if (fsess_not_impl(mp, op))
208 goto out;
209
210 fdisp_init(&fdi, sizeof(*fri));
211 fdisp_make_vp(&fdi, op, vp, td, cred);
212 fri = fdi.indata;
213 fri->fh = fufh->fh_id;
214 fri->flags = fufh_type_2_fflags(fufh->fufh_type);
215 /*
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
218 * it.
219 */
220 fri->lock_owner = td->td_proc->p_pid;
221
222 err = fdisp_wait_answ(&fdi);
223 fdisp_destroy(&fdi);
224
225 out:
226 counter_u64_add(fuse_fh_count, -1);
227 LIST_REMOVE(fufh, next);
228 free(fufh, M_FUSE_FILEHANDLE);
229
230 return err;
231 }
232
233 /*
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"
239 */
240 bool
fuse_filehandle_validrw(struct vnode * vp,int mode,struct ucred * cred,pid_t pid)241 fuse_filehandle_validrw(struct vnode *vp, int mode,
242 struct ucred *cred, pid_t pid)
243 {
244 struct fuse_vnode_data *fvdat = VTOFUD(vp);
245 struct fuse_filehandle *fufh;
246 fufh_type_t fufh_type = fflags_2_fufh_type(mode);
247
248 /*
249 * Unlike fuse_filehandle_get, we want to search for a filehandle with
250 * the exact cred, and no fallback
251 */
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))
257 return true;
258 }
259
260 if (fufh_type == FUFH_EXEC)
261 return false;
262
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))
269 return true;
270 }
271
272 return false;
273 }
274
275 int
fuse_filehandle_get(struct vnode * vp,int fflag,struct fuse_filehandle ** fufhp,struct ucred * cred,pid_t pid)276 fuse_filehandle_get(struct vnode *vp, int fflag,
277 struct fuse_filehandle **fufhp, struct ucred *cred, pid_t pid)
278 {
279 struct fuse_vnode_data *fvdat = VTOFUD(vp);
280 struct fuse_filehandle *fufh;
281 fufh_type_t fufh_type;
282
283 fufh_type = fflags_2_fufh_type(fflag);
284 /* cred can be NULL for in-kernel clients */
285 if (cred == NULL)
286 goto fallback;
287
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))
293 goto found;
294 }
295
296 fallback:
297 /* Fallback: find a list entry with the right flags */
298 LIST_FOREACH(fufh, &fvdat->handles, next) {
299 if (fufh->fufh_type == fufh_type)
300 break;
301 }
302
303 if (fufh == NULL)
304 return EBADF;
305
306 found:
307 if (fufhp != NULL)
308 *fufhp = fufh;
309 return 0;
310 }
311
312 /* Get a file handle with any kind of flags */
313 int
fuse_filehandle_get_anyflags(struct vnode * vp,struct fuse_filehandle ** fufhp,struct ucred * cred,pid_t pid)314 fuse_filehandle_get_anyflags(struct vnode *vp,
315 struct fuse_filehandle **fufhp, struct ucred *cred, pid_t pid)
316 {
317 struct fuse_vnode_data *fvdat = VTOFUD(vp);
318 struct fuse_filehandle *fufh;
319
320 if (cred == NULL)
321 goto fallback;
322
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))
327 goto found;
328 }
329
330 fallback:
331 /* Fallback: find any list entry */
332 fufh = LIST_FIRST(&fvdat->handles);
333
334 if (fufh == NULL)
335 return EBADF;
336
337 found:
338 if (fufhp != NULL)
339 *fufhp = fufh;
340 return 0;
341 }
342
343 int
fuse_filehandle_getrw(struct vnode * vp,int fflag,struct fuse_filehandle ** fufhp,struct ucred * cred,pid_t pid)344 fuse_filehandle_getrw(struct vnode *vp, int fflag,
345 struct fuse_filehandle **fufhp, struct ucred *cred, pid_t pid)
346 {
347 int err;
348
349 err = fuse_filehandle_get(vp, fflag, fufhp, cred, pid);
350 if (err)
351 err = fuse_filehandle_get(vp, FREAD | FWRITE, fufhp, cred, pid);
352 return err;
353 }
354
355 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)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)
359 {
360 struct fuse_vnode_data *fvdat = VTOFUD(vp);
361 struct fuse_filehandle *fufh;
362
363 fufh = malloc(sizeof(struct fuse_filehandle), M_FUSE_FILEHANDLE,
364 M_WAITOK);
365 MPASS(fufh != NULL);
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);
374 }
375 LIST_INSERT_HEAD(&fvdat->handles, fufh, next);
376 if (fufhp != NULL)
377 *fufhp = fufh;
378
379 counter_u64_add(fuse_fh_count, 1);
380
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);
385 } else {
386 if ((foo->open_flags & FOPEN_KEEP_CACHE) == 0)
387 fuse_io_invalbuf(vp, td);
388 VTOFUD(vp)->flag &= ~FN_DIRECTIO;
389 }
390
391 }
392
393 void
fuse_file_init(void)394 fuse_file_init(void)
395 {
396 fuse_fh_count = counter_u64_alloc(M_WAITOK);
397 }
398
399 void
fuse_file_destroy(void)400 fuse_file_destroy(void)
401 {
402 counter_u64_free(fuse_fh_count);
403 }
404