xref: /freebsd/sys/fs/fuse/fuse_ipc.h (revision e8d8bef961a50d4dc22501cde4fb9fb0be1b2532)
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  * $FreeBSD$
63  */
64 
65 #ifndef _FUSE_IPC_H_
66 #define _FUSE_IPC_H_
67 
68 #include <sys/param.h>
69 #include <sys/refcount.h>
70 
71 enum fuse_data_cache_mode {
72 	FUSE_CACHE_UC,
73 	FUSE_CACHE_WT,
74 	FUSE_CACHE_WB,
75 };
76 
77 struct fuse_iov {
78 	void   *base;
79 	size_t  len;
80 	size_t  allocated_size;
81 	int     credit;
82 };
83 
84 void fiov_init(struct fuse_iov *fiov, size_t size);
85 void fiov_teardown(struct fuse_iov *fiov);
86 void fiov_refresh(struct fuse_iov *fiov);
87 void fiov_adjust(struct fuse_iov *fiov, size_t size);
88 
89 #define FUSE_DIMALLOC(fiov, spc1, spc2, amnt) do {		\
90 	fiov_adjust(fiov, (sizeof(*(spc1)) + (amnt)));		\
91 	(spc1) = (fiov)->base;					\
92 	(spc2) = (char *)(fiov)->base + (sizeof(*(spc1)));	\
93 } while (0)
94 
95 #define FU_AT_LEAST(siz) max((siz), 160)
96 
97 #define FUSE_ASSERT_AW_DONE(ftick)					\
98 	KASSERT((ftick)->tk_aw_link.tqe_next == NULL &&			\
99 	    (ftick)->tk_aw_link.tqe_prev == NULL,			\
100 	    ("FUSE: ticket still on answer delivery list %p", (ftick)))
101 
102 #define FUSE_ASSERT_MS_DONE(ftick)				\
103 	KASSERT((ftick)->tk_ms_link.stqe_next == NULL,		\
104 	    ("FUSE: ticket still on message list %p", (ftick)))
105 
106 struct fuse_ticket;
107 struct fuse_data;
108 
109 typedef int fuse_handler_t(struct fuse_ticket *ftick, struct uio *uio);
110 
111 struct fuse_ticket {
112 	/* fields giving the identity of the ticket */
113 	uint64_t			tk_unique;
114 	struct fuse_data		*tk_data;
115 	int				tk_flag;
116 	u_int				tk_refcount;
117 	/*
118 	 * If this ticket's operation has been interrupted, this will hold the
119 	 * unique value of the FUSE_INTERRUPT operation.  Otherwise, it will be
120 	 * 0.
121 	 */
122 	uint64_t			irq_unique;
123 
124 	/* fields for initiating an upgoing message */
125 	struct fuse_iov			tk_ms_fiov;
126 	STAILQ_ENTRY(fuse_ticket)	tk_ms_link;
127 
128 	/* fields for handling answers coming from userspace */
129 	struct fuse_iov			tk_aw_fiov;
130 	struct fuse_out_header		tk_aw_ohead;
131 	int				tk_aw_errno;
132 	struct mtx			tk_aw_mtx;
133 	fuse_handler_t			*tk_aw_handler;
134 	TAILQ_ENTRY(fuse_ticket)	tk_aw_link;
135 };
136 
137 #define FT_ANSW  0x01  /* request of ticket has already been answered */
138 #define FT_DIRTY 0x04  /* ticket has been used */
139 
140 static inline struct fuse_iov *
141 fticket_resp(struct fuse_ticket *ftick)
142 {
143 	return (&ftick->tk_aw_fiov);
144 }
145 
146 static inline bool
147 fticket_answered(struct fuse_ticket *ftick)
148 {
149 	mtx_assert(&ftick->tk_aw_mtx, MA_OWNED);
150 	return (ftick->tk_flag & FT_ANSW);
151 }
152 
153 static inline void
154 fticket_set_answered(struct fuse_ticket *ftick)
155 {
156 	mtx_assert(&ftick->tk_aw_mtx, MA_OWNED);
157 	ftick->tk_flag |= FT_ANSW;
158 }
159 
160 static inline struct fuse_in_header*
161 fticket_in_header(struct fuse_ticket *ftick)
162 {
163 	return (struct fuse_in_header *)(ftick->tk_ms_fiov.base);
164 }
165 
166 static inline enum fuse_opcode
167 fticket_opcode(struct fuse_ticket *ftick)
168 {
169 	return fticket_in_header(ftick)->opcode;
170 }
171 
172 int fticket_pull(struct fuse_ticket *ftick, struct uio *uio);
173 
174 /*
175  * The data representing a FUSE session.
176  */
177 struct fuse_data {
178 	struct cdev			*fdev;
179 	struct mount			*mp;
180 	struct vnode			*vroot;
181 	struct ucred			*daemoncred;
182 	int				dataflags;
183 	int				ref;
184 
185 	struct mtx			ms_mtx;
186 	STAILQ_HEAD(, fuse_ticket)	ms_head;
187 	int				ms_count;
188 
189 	struct mtx			aw_mtx;
190 	TAILQ_HEAD(, fuse_ticket)	aw_head;
191 
192 	/*
193 	 * Holds the next value of the FUSE operation unique value.
194 	 * Also, serves as a wakeup channel to prevent any operations from
195 	 * being created before INIT completes.
196 	 */
197 	u_long				ticketer;
198 
199 	struct sx			rename_lock;
200 
201 	uint32_t			fuse_libabi_major;
202 	uint32_t			fuse_libabi_minor;
203 
204 	uint32_t			max_readahead_blocks;
205 	uint32_t			max_write;
206 	uint32_t			max_read;
207 	uint32_t			subtype;
208 	char				volname[MAXPATHLEN];
209 
210 	struct selinfo			ks_rsel;
211 
212 	int				daemon_timeout;
213 	int				linux_errnos;
214 	unsigned			time_gran;
215 	/* A bitmask of FUSE RPCs that are not implemented by the server */
216 	uint64_t			notimpl;
217 	/*
218 	 * A bitmask of FUSE RPCs that are implemented by the server.
219 	 * If an operation is not present in either notimpl or isimpl, then it
220 	 * may be implemented by the server, but the kernel doesn't know for
221 	 * sure.
222 	 */
223 	uint64_t			isimpl;
224 	uint64_t			mnt_flag;
225 	enum fuse_data_cache_mode	cache_mode;
226 };
227 
228 #define FSESS_DEAD                0x0001 /* session is to be closed */
229 #define FSESS_INITED              0x0004 /* session has been inited */
230 #define FSESS_DAEMON_CAN_SPY      0x0010 /* let non-owners access this fs */
231                                          /* (and being observed by the daemon) */
232 #define FSESS_PUSH_SYMLINKS_IN    0x0020 /* prefix absolute symlinks with mp */
233 #define FSESS_DEFAULT_PERMISSIONS 0x0040 /* kernel does permission checking */
234 #define FSESS_ASYNC_READ          0x1000 /* allow multiple reads of some file */
235 #define FSESS_POSIX_LOCKS         0x2000 /* daemon supports POSIX locks */
236 #define FSESS_EXPORT_SUPPORT      0x10000 /* daemon supports NFS-style lookups */
237 #define FSESS_INTR                0x20000 /* interruptible mounts */
238 #define FSESS_MNTOPTS_MASK	( \
239 	FSESS_DAEMON_CAN_SPY | FSESS_PUSH_SYMLINKS_IN | \
240 	FSESS_DEFAULT_PERMISSIONS | FSESS_INTR)
241 
242 extern int fuse_data_cache_mode;
243 
244 static inline struct fuse_data *
245 fuse_get_mpdata(struct mount *mp)
246 {
247 	return mp->mnt_data;
248 }
249 
250 static inline bool
251 fsess_is_impl(struct mount *mp, int opcode)
252 {
253 	struct fuse_data *data = fuse_get_mpdata(mp);
254 
255 	return ((data->isimpl & (1ULL << opcode)) != 0);
256 
257 }
258 
259 static inline bool
260 fsess_maybe_impl(struct mount *mp, int opcode)
261 {
262 	struct fuse_data *data = fuse_get_mpdata(mp);
263 
264 	return ((data->notimpl & (1ULL << opcode)) == 0);
265 
266 }
267 
268 static inline bool
269 fsess_not_impl(struct mount *mp, int opcode)
270 {
271 	struct fuse_data *data = fuse_get_mpdata(mp);
272 
273 	return ((data->notimpl & (1ULL << opcode)) != 0);
274 
275 }
276 
277 static inline void
278 fsess_set_impl(struct mount *mp, int opcode)
279 {
280 	struct fuse_data *data = fuse_get_mpdata(mp);
281 
282 	data->isimpl |= (1ULL << opcode);
283 }
284 
285 static inline void
286 fsess_set_notimpl(struct mount *mp, int opcode)
287 {
288 	struct fuse_data *data = fuse_get_mpdata(mp);
289 
290 	data->notimpl |= (1ULL << opcode);
291 }
292 
293 static inline bool
294 fsess_opt_datacache(struct mount *mp)
295 {
296 	struct fuse_data *data = fuse_get_mpdata(mp);
297 
298 	return (data->cache_mode != FUSE_CACHE_UC);
299 }
300 
301 static inline bool
302 fsess_opt_mmap(struct mount *mp)
303 {
304 	return (fsess_opt_datacache(mp));
305 }
306 
307 static inline bool
308 fsess_opt_writeback(struct mount *mp)
309 {
310 	struct fuse_data *data = fuse_get_mpdata(mp);
311 
312 	return (data->cache_mode == FUSE_CACHE_WB);
313 }
314 
315 /* Insert a new upgoing message */
316 static inline void
317 fuse_ms_push(struct fuse_ticket *ftick)
318 {
319 	mtx_assert(&ftick->tk_data->ms_mtx, MA_OWNED);
320 	refcount_acquire(&ftick->tk_refcount);
321 	STAILQ_INSERT_TAIL(&ftick->tk_data->ms_head, ftick, tk_ms_link);
322 	ftick->tk_data->ms_count++;
323 }
324 
325 /* Insert a new upgoing message to the front of the queue */
326 static inline void
327 fuse_ms_push_head(struct fuse_ticket *ftick)
328 {
329 	mtx_assert(&ftick->tk_data->ms_mtx, MA_OWNED);
330 	refcount_acquire(&ftick->tk_refcount);
331 	STAILQ_INSERT_HEAD(&ftick->tk_data->ms_head, ftick, tk_ms_link);
332 	ftick->tk_data->ms_count++;
333 }
334 
335 static inline struct fuse_ticket *
336 fuse_ms_pop(struct fuse_data *data)
337 {
338 	struct fuse_ticket *ftick = NULL;
339 
340 	mtx_assert(&data->ms_mtx, MA_OWNED);
341 
342 	if ((ftick = STAILQ_FIRST(&data->ms_head))) {
343 		STAILQ_REMOVE_HEAD(&data->ms_head, tk_ms_link);
344 		data->ms_count--;
345 #ifdef INVARIANTS
346 		MPASS(data->ms_count >= 0);
347 		ftick->tk_ms_link.stqe_next = NULL;
348 #endif
349 	}
350 
351 	return (ftick);
352 }
353 
354 static inline void
355 fuse_aw_push(struct fuse_ticket *ftick)
356 {
357 	mtx_assert(&ftick->tk_data->aw_mtx, MA_OWNED);
358 	refcount_acquire(&ftick->tk_refcount);
359 	TAILQ_INSERT_TAIL(&ftick->tk_data->aw_head, ftick, tk_aw_link);
360 }
361 
362 static inline void
363 fuse_aw_remove(struct fuse_ticket *ftick)
364 {
365 	mtx_assert(&ftick->tk_data->aw_mtx, MA_OWNED);
366 	TAILQ_REMOVE(&ftick->tk_data->aw_head, ftick, tk_aw_link);
367 #ifdef INVARIANTS
368 	ftick->tk_aw_link.tqe_next = NULL;
369 	ftick->tk_aw_link.tqe_prev = NULL;
370 #endif
371 }
372 
373 static inline struct fuse_ticket *
374 fuse_aw_pop(struct fuse_data *data)
375 {
376 	struct fuse_ticket *ftick;
377 
378 	mtx_assert(&data->aw_mtx, MA_OWNED);
379 
380 	if ((ftick = TAILQ_FIRST(&data->aw_head)) != NULL)
381 		fuse_aw_remove(ftick);
382 
383 	return (ftick);
384 }
385 
386 struct fuse_ticket *fuse_ticket_fetch(struct fuse_data *data);
387 int fuse_ticket_drop(struct fuse_ticket *ftick);
388 void fuse_insert_callback(struct fuse_ticket *ftick, fuse_handler_t *handler);
389 void fuse_insert_message(struct fuse_ticket *ftick, bool irq);
390 
391 static inline bool
392 fuse_libabi_geq(struct fuse_data *data, uint32_t abi_maj, uint32_t abi_min)
393 {
394 	return (data->fuse_libabi_major > abi_maj ||
395 	    (data->fuse_libabi_major == abi_maj &&
396 	     data->fuse_libabi_minor >= abi_min));
397 }
398 
399 struct fuse_data *fdata_alloc(struct cdev *dev, struct ucred *cred);
400 void fdata_trydestroy(struct fuse_data *data);
401 void fdata_set_dead(struct fuse_data *data);
402 
403 static inline bool
404 fdata_get_dead(struct fuse_data *data)
405 {
406 	return (data->dataflags & FSESS_DEAD);
407 }
408 
409 struct fuse_dispatcher {
410 	struct fuse_ticket    *tick;
411 	struct fuse_in_header *finh;
412 
413 	void    *indata;
414 	size_t   iosize;
415 	uint64_t nodeid;
416 	int      answ_stat;
417 	void    *answ;
418 };
419 
420 static inline void
421 fdisp_init(struct fuse_dispatcher *fdisp, size_t iosize)
422 {
423 	fdisp->iosize = iosize;
424 	fdisp->tick = NULL;
425 }
426 
427 static inline void
428 fdisp_destroy(struct fuse_dispatcher *fdisp)
429 {
430 	fuse_ticket_drop(fdisp->tick);
431 #ifdef INVARIANTS
432 	fdisp->tick = NULL;
433 #endif
434 }
435 
436 void fdisp_refresh(struct fuse_dispatcher *fdip);
437 
438 void fdisp_make(struct fuse_dispatcher *fdip, enum fuse_opcode op,
439     struct mount *mp, uint64_t nid, struct thread *td, struct ucred *cred);
440 
441 void fdisp_make_vp(struct fuse_dispatcher *fdip, enum fuse_opcode op,
442     struct vnode *vp, struct thread *td, struct ucred *cred);
443 
444 void fdisp_refresh_vp(struct fuse_dispatcher *fdip, enum fuse_opcode op,
445     struct vnode *vp, struct thread *td, struct ucred *cred);
446 
447 int fdisp_wait_answ(struct fuse_dispatcher *fdip);
448 
449 static inline int
450 fdisp_simple_putget_vp(struct fuse_dispatcher *fdip, enum fuse_opcode op,
451     struct vnode *vp, struct thread *td, struct ucred *cred)
452 {
453 	fdisp_make_vp(fdip, op, vp, td, cred);
454 	return (fdisp_wait_answ(fdip));
455 }
456 
457 #endif /* _FUSE_IPC_H_ */
458