xref: /freebsd/sys/fs/fuse/fuse_file.c (revision f81cdf24ba5436367377f7c8e8f51f6df2a75ca7)
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
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
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_data *data = fuse_get_mpdata(mp);
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 dataflags = data->dataflags;
136 	int err = 0;
137 	int oflags = 0;
138 	int op = FUSE_OPEN;
139 	int relop = FUSE_RELEASE;
140 	int fsess_no_op_support = FSESS_NO_OPEN_SUPPORT;
141 
142 	fufh_type = fflags_2_fufh_type(a_mode);
143 	oflags = fufh_type_2_fflags(fufh_type);
144 
145 	if (vnode_isdir(vp)) {
146 		op = FUSE_OPENDIR;
147 		relop = FUSE_RELEASEDIR;
148 		fsess_no_op_support = FSESS_NO_OPENDIR_SUPPORT;
149 		/* vn_open_vnode already rejects FWRITE on directories */
150 		MPASS(fufh_type == FUFH_RDONLY || fufh_type == FUFH_EXEC);
151 	}
152 	fdisp_init(&fdi, sizeof(*foi));
153 	if (fsess_not_impl(mp, op) && dataflags & fsess_no_op_support) {
154 		/* The operation implicitly succeeds */
155 		foo = &default_foo;
156 	} else {
157 		fdisp_make_vp(&fdi, op, vp, td, cred);
158 
159 		foi = fdi.indata;
160 		foi->flags = oflags;
161 
162 		err = fdisp_wait_answ(&fdi);
163 		if (err == ENOSYS && dataflags & fsess_no_op_support) {
164 			/* The operation implicitly succeeds */
165 			foo = &default_foo;
166 			fsess_set_notimpl(mp, op);
167 			fsess_set_notimpl(mp, relop);
168 			err = 0;
169 		} else if (err) {
170 			SDT_PROBE2(fusefs, , file, trace, 1,
171 				"OUCH ... daemon didn't give fh");
172 			if (err == ENOENT)
173 				fuse_internal_vnode_disappear(vp);
174 			goto out;
175 		} else {
176 			foo = fdi.answ;
177 		}
178 	}
179 
180 	fuse_filehandle_init(vp, fufh_type, fufhp, td, cred, foo);
181 	fuse_vnode_open(vp, foo->open_flags, td);
182 
183 out:
184 	if (foi)
185 		fdisp_destroy(&fdi);
186 	return err;
187 }
188 
189 int
190 fuse_filehandle_close(struct vnode *vp, struct fuse_filehandle *fufh,
191     struct thread *td, struct ucred *cred)
192 {
193 	struct mount *mp = vnode_mount(vp);
194 	struct fuse_dispatcher fdi;
195 	struct fuse_release_in *fri;
196 
197 	int err = 0;
198 	int op = FUSE_RELEASE;
199 
200 	if (fuse_isdeadfs(vp)) {
201 		goto out;
202 	}
203 	if (vnode_isdir(vp))
204 		op = FUSE_RELEASEDIR;
205 
206 	if (fsess_not_impl(mp, op))
207 		goto out;
208 
209 	fdisp_init(&fdi, sizeof(*fri));
210 	fdisp_make_vp(&fdi, op, vp, td, cred);
211 	fri = fdi.indata;
212 	fri->fh = fufh->fh_id;
213 	fri->flags = fufh_type_2_fflags(fufh->fufh_type);
214 	/*
215 	 * If the file has a POSIX lock then we're supposed to set lock_owner.
216 	 * If not, then lock_owner is undefined.  So we may as well always set
217 	 * it.
218 	 */
219 	fri->lock_owner = td->td_proc->p_pid;
220 
221 	err = fdisp_wait_answ(&fdi);
222 	fdisp_destroy(&fdi);
223 
224 out:
225 	counter_u64_add(fuse_fh_count, -1);
226 	LIST_REMOVE(fufh, next);
227 	free(fufh, M_FUSE_FILEHANDLE);
228 
229 	return err;
230 }
231 
232 /*
233  * Check for a valid file handle, first the type requested, but if that
234  * isn't valid, try for FUFH_RDWR.
235  * Return true if there is any file handle with the correct credentials and
236  * a fufh type that includes the provided one.
237  * A pid of 0 means "don't care"
238  */
239 bool
240 fuse_filehandle_validrw(struct vnode *vp, int mode,
241 	struct ucred *cred, pid_t pid)
242 {
243 	struct fuse_vnode_data *fvdat = VTOFUD(vp);
244 	struct fuse_filehandle *fufh;
245 	fufh_type_t fufh_type = fflags_2_fufh_type(mode);
246 
247 	/*
248 	 * Unlike fuse_filehandle_get, we want to search for a filehandle with
249 	 * the exact cred, and no fallback
250 	 */
251 	LIST_FOREACH(fufh, &fvdat->handles, next) {
252 		if (fufh->fufh_type == fufh_type &&
253 		    fufh->uid == cred->cr_uid &&
254 		    fufh->gid == cred->cr_rgid &&
255 		    (pid == 0 || fufh->pid == pid))
256 			return true;
257 	}
258 
259 	if (fufh_type == FUFH_EXEC)
260 		return false;
261 
262 	/* Fallback: find a RDWR list entry with the right cred */
263 	LIST_FOREACH(fufh, &fvdat->handles, next) {
264 		if (fufh->fufh_type == FUFH_RDWR &&
265 		    fufh->uid == cred->cr_uid &&
266 		    fufh->gid == cred->cr_rgid &&
267 		    (pid == 0 || fufh->pid == pid))
268 			return true;
269 	}
270 
271 	return false;
272 }
273 
274 int
275 fuse_filehandle_get(struct vnode *vp, int fflag,
276     struct fuse_filehandle **fufhp, struct ucred *cred, pid_t pid)
277 {
278 	struct fuse_vnode_data *fvdat = VTOFUD(vp);
279 	struct fuse_filehandle *fufh;
280 	fufh_type_t fufh_type;
281 
282 	fufh_type = fflags_2_fufh_type(fflag);
283 	/* cred can be NULL for in-kernel clients */
284 	if (cred == NULL)
285 		goto fallback;
286 
287 	LIST_FOREACH(fufh, &fvdat->handles, next) {
288 		if (fufh->fufh_type == fufh_type &&
289 		    fufh->uid == cred->cr_uid &&
290 		    fufh->gid == cred->cr_rgid &&
291 		    (pid == 0 || fufh->pid == pid))
292 			goto found;
293 	}
294 
295 fallback:
296 	/* Fallback: find a list entry with the right flags */
297 	LIST_FOREACH(fufh, &fvdat->handles, next) {
298 		if (fufh->fufh_type == fufh_type)
299 			break;
300 	}
301 
302 	if (fufh == NULL)
303 		return EBADF;
304 
305 found:
306 	if (fufhp != NULL)
307 		*fufhp = fufh;
308 	return 0;
309 }
310 
311 /* Get a file handle with any kind of flags */
312 int
313 fuse_filehandle_get_anyflags(struct vnode *vp,
314     struct fuse_filehandle **fufhp, struct ucred *cred, pid_t pid)
315 {
316 	struct fuse_vnode_data *fvdat = VTOFUD(vp);
317 	struct fuse_filehandle *fufh;
318 
319 	if (cred == NULL)
320 		goto fallback;
321 
322 	LIST_FOREACH(fufh, &fvdat->handles, next) {
323 		if (fufh->uid == cred->cr_uid &&
324 		    fufh->gid == cred->cr_rgid &&
325 		    (pid == 0 || fufh->pid == pid))
326 			goto found;
327 	}
328 
329 fallback:
330 	/* Fallback: find any list entry */
331 	fufh = LIST_FIRST(&fvdat->handles);
332 
333 	if (fufh == NULL)
334 		return EBADF;
335 
336 found:
337 	if (fufhp != NULL)
338 		*fufhp = fufh;
339 	return 0;
340 }
341 
342 int
343 fuse_filehandle_getrw(struct vnode *vp, int fflag,
344     struct fuse_filehandle **fufhp, struct ucred *cred, pid_t pid)
345 {
346 	int err;
347 
348 	err = fuse_filehandle_get(vp, fflag, fufhp, cred, pid);
349 	if (err)
350 		err = fuse_filehandle_get(vp, FREAD | FWRITE, fufhp, cred, pid);
351 	return err;
352 }
353 
354 void
355 fuse_filehandle_init(struct vnode *vp, fufh_type_t fufh_type,
356     struct fuse_filehandle **fufhp, struct thread *td, const struct ucred *cred,
357     const struct fuse_open_out *foo)
358 {
359 	struct fuse_vnode_data *fvdat = VTOFUD(vp);
360 	struct fuse_filehandle *fufh;
361 
362 	fufh = malloc(sizeof(struct fuse_filehandle), M_FUSE_FILEHANDLE,
363 		M_WAITOK);
364 	MPASS(fufh != NULL);
365 	fufh->fh_id = foo->fh;
366 	fufh->fufh_type = fufh_type;
367 	fufh->gid = cred->cr_rgid;
368 	fufh->uid = cred->cr_uid;
369 	fufh->pid = td->td_proc->p_pid;
370 	fufh->fuse_open_flags = foo->open_flags;
371 	if (!FUFH_IS_VALID(fufh)) {
372 		panic("FUSE: init: invalid filehandle id (type=%d)", fufh_type);
373 	}
374 	LIST_INSERT_HEAD(&fvdat->handles, fufh, next);
375 	if (fufhp != NULL)
376 		*fufhp = fufh;
377 
378 	counter_u64_add(fuse_fh_count, 1);
379 
380 	if (foo->open_flags & FOPEN_DIRECT_IO) {
381 		ASSERT_VOP_ELOCKED(vp, __func__);
382 		VTOFUD(vp)->flag |= FN_DIRECTIO;
383 		fuse_io_invalbuf(vp, td);
384 	} else {
385 		if ((foo->open_flags & FOPEN_KEEP_CACHE) == 0)
386 			fuse_io_invalbuf(vp, td);
387 	        VTOFUD(vp)->flag &= ~FN_DIRECTIO;
388 	}
389 
390 }
391 
392 void
393 fuse_file_init(void)
394 {
395 	fuse_fh_count = counter_u64_alloc(M_WAITOK);
396 }
397 
398 void
399 fuse_file_destroy(void)
400 {
401 	counter_u64_free(fuse_fh_count);
402 }
403