xref: /freebsd/sys/fs/fuse/fuse_ipc.h (revision 0957b409)
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  * Redistribution and use in source and binary forms, with or without
37  * modification, are permitted provided that the following conditions
38  * are met:
39  * 1. Redistributions of source code must retain the above copyright
40  *    notice, this list of conditions and the following disclaimer.
41  * 2. Redistributions in binary form must reproduce the above copyright
42  *    notice, this list of conditions and the following disclaimer in the
43  *    documentation and/or other materials provided with the distribution.
44  *
45  * THIS SOFTWARE IS PROVIDED BY AUTHOR AND CONTRIBUTORS ``AS IS'' AND
46  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
47  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
48  * ARE DISCLAIMED.  IN NO EVENT SHALL AUTHOR OR CONTRIBUTORS BE LIABLE
49  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
50  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
51  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
52  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
53  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
54  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
55  * SUCH DAMAGE.
56  *
57  * $FreeBSD$
58  */
59 
60 #ifndef _FUSE_IPC_H_
61 #define _FUSE_IPC_H_
62 
63 #include <sys/param.h>
64 #include <sys/refcount.h>
65 
66 struct fuse_iov {
67 	void   *base;
68 	size_t  len;
69 	size_t  allocated_size;
70 	int     credit;
71 };
72 
73 void fiov_init(struct fuse_iov *fiov, size_t size);
74 void fiov_teardown(struct fuse_iov *fiov);
75 void fiov_refresh(struct fuse_iov *fiov);
76 void fiov_adjust(struct fuse_iov *fiov, size_t size);
77 
78 #define FUSE_DIMALLOC(fiov, spc1, spc2, amnt) do {		\
79 	fiov_adjust(fiov, (sizeof(*(spc1)) + (amnt)));		\
80 	(spc1) = (fiov)->base;					\
81 	(spc2) = (char *)(fiov)->base + (sizeof(*(spc1)));	\
82 } while (0)
83 
84 #define FU_AT_LEAST(siz) max((siz), 160)
85 
86 #define FUSE_ASSERT_AW_DONE(ftick)					\
87 	KASSERT((ftick)->tk_aw_link.tqe_next == NULL &&			\
88 	    (ftick)->tk_aw_link.tqe_prev == NULL,			\
89 	    ("FUSE: ticket still on answer delivery list %p", (ftick)))
90 
91 #define FUSE_ASSERT_MS_DONE(ftick)				\
92 	KASSERT((ftick)->tk_ms_link.stqe_next == NULL,		\
93 	    ("FUSE: ticket still on message list %p", (ftick)))
94 
95 struct fuse_ticket;
96 struct fuse_data;
97 
98 typedef int fuse_handler_t(struct fuse_ticket *ftick, struct uio *uio);
99 
100 struct fuse_ticket {
101 	/* fields giving the identity of the ticket */
102 	uint64_t			tk_unique;
103 	struct fuse_data		*tk_data;
104 	int				tk_flag;
105 	u_int				tk_refcount;
106 
107 	/* fields for initiating an upgoing message */
108 	struct fuse_iov			tk_ms_fiov;
109 	void				*tk_ms_bufdata;
110 	size_t				tk_ms_bufsize;
111 	enum { FT_M_FIOV, FT_M_BUF }	tk_ms_type;
112 	STAILQ_ENTRY(fuse_ticket)	tk_ms_link;
113 
114 	/* fields for handling answers coming from userspace */
115 	struct fuse_iov			tk_aw_fiov;
116 	void				*tk_aw_bufdata;
117 	size_t				tk_aw_bufsize;
118 	enum { FT_A_FIOV, FT_A_BUF }	tk_aw_type;
119 
120 	struct fuse_out_header		tk_aw_ohead;
121 	int				tk_aw_errno;
122 	struct mtx			tk_aw_mtx;
123 	fuse_handler_t			*tk_aw_handler;
124 	TAILQ_ENTRY(fuse_ticket)	tk_aw_link;
125 };
126 
127 #define FT_ANSW  0x01  /* request of ticket has already been answered */
128 #define FT_DIRTY 0x04  /* ticket has been used */
129 
130 static inline struct fuse_iov *
131 fticket_resp(struct fuse_ticket *ftick)
132 {
133 	return (&ftick->tk_aw_fiov);
134 }
135 
136 static inline bool
137 fticket_answered(struct fuse_ticket *ftick)
138 {
139 	DEBUGX(FUSE_DEBUG_IPC, "-> ftick=%p\n", ftick);
140 	mtx_assert(&ftick->tk_aw_mtx, MA_OWNED);
141 	return (ftick->tk_flag & FT_ANSW);
142 }
143 
144 static inline void
145 fticket_set_answered(struct fuse_ticket *ftick)
146 {
147 	DEBUGX(FUSE_DEBUG_IPC, "-> ftick=%p\n", ftick);
148 	mtx_assert(&ftick->tk_aw_mtx, MA_OWNED);
149 	ftick->tk_flag |= FT_ANSW;
150 }
151 
152 static inline enum fuse_opcode
153 fticket_opcode(struct fuse_ticket *ftick)
154 {
155 	DEBUGX(FUSE_DEBUG_IPC, "-> ftick=%p\n", ftick);
156 	return (((struct fuse_in_header *)(ftick->tk_ms_fiov.base))->opcode);
157 }
158 
159 int fticket_pull(struct fuse_ticket *ftick, struct uio *uio);
160 
161 enum mountpri { FM_NOMOUNTED, FM_PRIMARY, FM_SECONDARY };
162 
163 /*
164  * The data representing a FUSE session.
165  */
166 struct fuse_data {
167 	struct cdev			*fdev;
168 	struct mount			*mp;
169 	struct vnode			*vroot;
170 	struct ucred			*daemoncred;
171 	int				dataflags;
172 	int				ref;
173 
174 	struct mtx			ms_mtx;
175 	STAILQ_HEAD(, fuse_ticket)	ms_head;
176 
177 	struct mtx			aw_mtx;
178 	TAILQ_HEAD(, fuse_ticket)	aw_head;
179 
180 	u_long				ticketer;
181 
182 	struct sx			rename_lock;
183 
184 	uint32_t			fuse_libabi_major;
185 	uint32_t			fuse_libabi_minor;
186 
187 	uint32_t			max_write;
188 	uint32_t			max_read;
189 	uint32_t			subtype;
190 	char				volname[MAXPATHLEN];
191 
192 	struct selinfo			ks_rsel;
193 
194 	int				daemon_timeout;
195 	uint64_t			notimpl;
196 };
197 
198 #define FSESS_DEAD                0x0001 /* session is to be closed */
199 #define FSESS_UNUSED0             0x0002 /* unused */
200 #define FSESS_INITED              0x0004 /* session has been inited */
201 #define FSESS_DAEMON_CAN_SPY      0x0010 /* let non-owners access this fs */
202                                          /* (and being observed by the daemon) */
203 #define FSESS_PUSH_SYMLINKS_IN    0x0020 /* prefix absolute symlinks with mp */
204 #define FSESS_DEFAULT_PERMISSIONS 0x0040 /* kernel does permission checking */
205 #define FSESS_NO_ATTRCACHE        0x0080 /* no attribute caching */
206 #define FSESS_NO_READAHEAD        0x0100 /* no readaheads */
207 #define FSESS_NO_DATACACHE        0x0200 /* disable buffer cache */
208 #define FSESS_NO_NAMECACHE        0x0400 /* disable name cache */
209 #define FSESS_NO_MMAP             0x0800 /* disable mmap */
210 #define FSESS_BROKENIO            0x1000 /* fix broken io */
211 
212 enum fuse_data_cache_mode {
213 	FUSE_CACHE_UC,
214 	FUSE_CACHE_WT,
215 	FUSE_CACHE_WB,
216 };
217 
218 extern int fuse_data_cache_mode;
219 extern int fuse_data_cache_invalidate;
220 extern int fuse_mmap_enable;
221 extern int fuse_sync_resize;
222 extern int fuse_fix_broken_io;
223 
224 static inline struct fuse_data *
225 fuse_get_mpdata(struct mount *mp)
226 {
227 	return mp->mnt_data;
228 }
229 
230 static inline bool
231 fsess_isimpl(struct mount *mp, int opcode)
232 {
233 	struct fuse_data *data = fuse_get_mpdata(mp);
234 
235 	return ((data->notimpl & (1ULL << opcode)) == 0);
236 
237 }
238 static inline void
239 fsess_set_notimpl(struct mount *mp, int opcode)
240 {
241 	struct fuse_data *data = fuse_get_mpdata(mp);
242 
243 	data->notimpl |= (1ULL << opcode);
244 }
245 
246 static inline bool
247 fsess_opt_datacache(struct mount *mp)
248 {
249 	struct fuse_data *data = fuse_get_mpdata(mp);
250 
251 	return (fuse_data_cache_mode != FUSE_CACHE_UC &&
252 	    (data->dataflags & FSESS_NO_DATACACHE) == 0);
253 }
254 
255 static inline bool
256 fsess_opt_mmap(struct mount *mp)
257 {
258 	struct fuse_data *data = fuse_get_mpdata(mp);
259 
260 	if (!fuse_mmap_enable || fuse_data_cache_mode == FUSE_CACHE_UC)
261 		return (false);
262 	return ((data->dataflags & (FSESS_NO_DATACACHE | FSESS_NO_MMAP)) == 0);
263 }
264 
265 static inline bool
266 fsess_opt_brokenio(struct mount *mp)
267 {
268 	struct fuse_data *data = fuse_get_mpdata(mp);
269 
270 	return (fuse_fix_broken_io || (data->dataflags & FSESS_BROKENIO));
271 }
272 
273 static inline void
274 fuse_ms_push(struct fuse_ticket *ftick)
275 {
276 	DEBUGX(FUSE_DEBUG_IPC, "ftick=%p refcount=%d\n", ftick,
277 	    ftick->tk_refcount + 1);
278 	mtx_assert(&ftick->tk_data->ms_mtx, MA_OWNED);
279 	refcount_acquire(&ftick->tk_refcount);
280 	STAILQ_INSERT_TAIL(&ftick->tk_data->ms_head, ftick, tk_ms_link);
281 }
282 
283 static inline struct fuse_ticket *
284 fuse_ms_pop(struct fuse_data *data)
285 {
286 	struct fuse_ticket *ftick = NULL;
287 
288 	mtx_assert(&data->ms_mtx, MA_OWNED);
289 
290 	if ((ftick = STAILQ_FIRST(&data->ms_head))) {
291 		STAILQ_REMOVE_HEAD(&data->ms_head, tk_ms_link);
292 #ifdef INVARIANTS
293 		ftick->tk_ms_link.stqe_next = NULL;
294 #endif
295 	}
296 	DEBUGX(FUSE_DEBUG_IPC, "ftick=%p refcount=%d\n", ftick,
297 	    ftick ? ftick->tk_refcount : -1);
298 
299 	return (ftick);
300 }
301 
302 static inline void
303 fuse_aw_push(struct fuse_ticket *ftick)
304 {
305 	DEBUGX(FUSE_DEBUG_IPC, "ftick=%p refcount=%d\n", ftick,
306 	    ftick->tk_refcount + 1);
307 	mtx_assert(&ftick->tk_data->aw_mtx, MA_OWNED);
308 	refcount_acquire(&ftick->tk_refcount);
309 	TAILQ_INSERT_TAIL(&ftick->tk_data->aw_head, ftick, tk_aw_link);
310 }
311 
312 static inline void
313 fuse_aw_remove(struct fuse_ticket *ftick)
314 {
315 	DEBUGX(FUSE_DEBUG_IPC, "ftick=%p refcount=%d\n",
316 	    ftick, ftick->tk_refcount);
317 	mtx_assert(&ftick->tk_data->aw_mtx, MA_OWNED);
318 	TAILQ_REMOVE(&ftick->tk_data->aw_head, ftick, tk_aw_link);
319 #ifdef INVARIANTS
320 	ftick->tk_aw_link.tqe_next = NULL;
321 	ftick->tk_aw_link.tqe_prev = NULL;
322 #endif
323 }
324 
325 static inline struct fuse_ticket *
326 fuse_aw_pop(struct fuse_data *data)
327 {
328 	struct fuse_ticket *ftick;
329 
330 	mtx_assert(&data->aw_mtx, MA_OWNED);
331 
332 	if ((ftick = TAILQ_FIRST(&data->aw_head)) != NULL)
333 		fuse_aw_remove(ftick);
334 	DEBUGX(FUSE_DEBUG_IPC, "ftick=%p refcount=%d\n", ftick,
335 	    ftick ? ftick->tk_refcount : -1);
336 
337 	return (ftick);
338 }
339 
340 struct fuse_ticket *fuse_ticket_fetch(struct fuse_data *data);
341 int fuse_ticket_drop(struct fuse_ticket *ftick);
342 void fuse_insert_callback(struct fuse_ticket *ftick, fuse_handler_t *handler);
343 void fuse_insert_message(struct fuse_ticket *ftick);
344 
345 static inline bool
346 fuse_libabi_geq(struct fuse_data *data, uint32_t abi_maj, uint32_t abi_min)
347 {
348 	return (data->fuse_libabi_major > abi_maj ||
349 	    (data->fuse_libabi_major == abi_maj &&
350 	     data->fuse_libabi_minor >= abi_min));
351 }
352 
353 struct fuse_data *fdata_alloc(struct cdev *dev, struct ucred *cred);
354 void fdata_trydestroy(struct fuse_data *data);
355 void fdata_set_dead(struct fuse_data *data);
356 
357 static inline bool
358 fdata_get_dead(struct fuse_data *data)
359 {
360 	return (data->dataflags & FSESS_DEAD);
361 }
362 
363 struct fuse_dispatcher {
364 	struct fuse_ticket    *tick;
365 	struct fuse_in_header *finh;
366 
367 	void    *indata;
368 	size_t   iosize;
369 	uint64_t nodeid;
370 	int      answ_stat;
371 	void    *answ;
372 };
373 
374 static inline void
375 fdisp_init(struct fuse_dispatcher *fdisp, size_t iosize)
376 {
377 	DEBUGX(FUSE_DEBUG_IPC, "-> fdisp=%p, iosize=%zx\n", fdisp, iosize);
378 	fdisp->iosize = iosize;
379 	fdisp->tick = NULL;
380 }
381 
382 static inline void
383 fdisp_destroy(struct fuse_dispatcher *fdisp)
384 {
385 	DEBUGX(FUSE_DEBUG_IPC, "-> fdisp=%p, ftick=%p\n", fdisp, fdisp->tick);
386 	fuse_ticket_drop(fdisp->tick);
387 #ifdef INVARIANTS
388 	fdisp->tick = NULL;
389 #endif
390 }
391 
392 void fdisp_make(struct fuse_dispatcher *fdip, enum fuse_opcode op,
393     struct mount *mp, uint64_t nid, struct thread *td, struct ucred *cred);
394 
395 void fdisp_make_pid(struct fuse_dispatcher *fdip, enum fuse_opcode op,
396     struct mount *mp, uint64_t nid, pid_t pid, struct ucred *cred);
397 
398 void fdisp_make_vp(struct fuse_dispatcher *fdip, enum fuse_opcode op,
399     struct vnode *vp, struct thread *td, struct ucred *cred);
400 
401 int fdisp_wait_answ(struct fuse_dispatcher *fdip);
402 
403 static inline int
404 fdisp_simple_putget_vp(struct fuse_dispatcher *fdip, enum fuse_opcode op,
405     struct vnode *vp, struct thread *td, struct ucred *cred)
406 {
407 	DEBUGX(FUSE_DEBUG_IPC, "-> fdip=%p, opcode=%d, vp=%p\n", fdip, op, vp);
408 	fdisp_make_vp(fdip, op, vp, td, cred);
409 	return (fdisp_wait_answ(fdip));
410 }
411 
412 #endif /* _FUSE_IPC_H_ */
413