xref: /freebsd/sys/fs/fuse/fuse_ipc.h (revision 988a9f7e39e1bea4eed03eb6f69df4cb0ec9289a)
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)          \
79 do {                                                   \
80     fiov_adjust(fiov, (sizeof(*(spc1)) + (amnt)));     \
81     (spc1) = (fiov)->base;                             \
82     (spc2) = (char *)(fiov)->base + (sizeof(*(spc1))); \
83 } while (0)
84 
85 #define FU_AT_LEAST(siz) max((siz), 160)
86 
87 #define FUSE_ASSERT_AW_DONE(ftick)                                      \
88     KASSERT((ftick)->tk_aw_link.tqe_next == NULL &&                     \
89         (ftick)->tk_aw_link.tqe_prev == NULL,                           \
90         ("FUSE: ticket still on answer delivery list %p", (ftick)))     \
91 
92 #define FUSE_ASSERT_MS_DONE(ftick)                                      \
93     KASSERT((ftick)->tk_ms_link.stqe_next == NULL,                      \
94         ("FUSE: ticket still on message list %p", (ftick)))
95 
96 struct fuse_ticket;
97 struct fuse_data;
98 
99 typedef int fuse_handler_t(struct fuse_ticket *ftick, struct uio *uio);
100 
101 struct fuse_ticket {
102     /* fields giving the identity of the ticket */
103     uint64_t                     tk_unique;
104     struct fuse_data            *tk_data;
105     int                          tk_flag;
106     u_int                        tk_refcount;
107 
108     /* fields for initiating an upgoing message */
109     struct fuse_iov              tk_ms_fiov;
110     void                        *tk_ms_bufdata;
111     size_t                       tk_ms_bufsize;
112     enum { FT_M_FIOV, FT_M_BUF } tk_ms_type;
113     STAILQ_ENTRY(fuse_ticket)    tk_ms_link;
114 
115     /* fields for handling answers coming from userspace */
116     struct fuse_iov              tk_aw_fiov;
117     void                        *tk_aw_bufdata;
118     size_t                       tk_aw_bufsize;
119     enum { FT_A_FIOV, FT_A_BUF } tk_aw_type;
120 
121     struct fuse_out_header       tk_aw_ohead;
122     int                          tk_aw_errno;
123     struct mtx                   tk_aw_mtx;
124     fuse_handler_t              *tk_aw_handler;
125     TAILQ_ENTRY(fuse_ticket)     tk_aw_link;
126 };
127 
128 #define FT_ANSW  0x01  /* request of ticket has already been answered */
129 #define FT_DIRTY 0x04  /* ticket has been used */
130 
131 static __inline__
132 struct fuse_iov *
133 fticket_resp(struct fuse_ticket *ftick)
134 {
135     return (&ftick->tk_aw_fiov);
136 }
137 
138 static __inline__
139 int
140 fticket_answered(struct fuse_ticket *ftick)
141 {
142     DEBUGX(FUSE_DEBUG_IPC, "-> ftick=%p\n", ftick);
143     mtx_assert(&ftick->tk_aw_mtx, MA_OWNED);
144     return (ftick->tk_flag & FT_ANSW);
145 }
146 
147 static __inline__
148 void
149 fticket_set_answered(struct fuse_ticket *ftick)
150 {
151     DEBUGX(FUSE_DEBUG_IPC, "-> ftick=%p\n", ftick);
152     mtx_assert(&ftick->tk_aw_mtx, MA_OWNED);
153     ftick->tk_flag |= FT_ANSW;
154 }
155 
156 static __inline__
157 enum fuse_opcode
158 fticket_opcode(struct fuse_ticket *ftick)
159 {
160     DEBUGX(FUSE_DEBUG_IPC, "-> ftick=%p\n", ftick);
161     return (((struct fuse_in_header *)(ftick->tk_ms_fiov.base))->opcode);
162 }
163 
164 int fticket_pull(struct fuse_ticket *ftick, struct uio *uio);
165 
166 enum mountpri { FM_NOMOUNTED, FM_PRIMARY, FM_SECONDARY };
167 
168 /*
169  * The data representing a FUSE session.
170  */
171 struct fuse_data {
172     struct cdev               *fdev;
173     struct mount              *mp;
174     struct vnode              *vroot;
175     struct ucred              *daemoncred;
176     int                        dataflags;
177     int                        ref;
178 
179     struct mtx                 ms_mtx;
180     STAILQ_HEAD(, fuse_ticket) ms_head;
181 
182     struct mtx                 aw_mtx;
183     TAILQ_HEAD(, fuse_ticket)  aw_head;
184 
185     u_long                     ticketer;
186 
187     struct sx                  rename_lock;
188 
189     uint32_t                   fuse_libabi_major;
190     uint32_t                   fuse_libabi_minor;
191 
192     uint32_t                   max_write;
193     uint32_t                   max_read;
194     uint32_t                   subtype;
195     char                       volname[MAXPATHLEN];
196 
197     struct selinfo ks_rsel;
198 
199     int                        daemon_timeout;
200     uint64_t                   notimpl;
201 };
202 
203 #define FSESS_DEAD                0x0001 /* session is to be closed */
204 #define FSESS_UNUSED0             0x0002 /* unused */
205 #define FSESS_INITED              0x0004 /* session has been inited */
206 #define FSESS_DAEMON_CAN_SPY      0x0010 /* let non-owners access this fs */
207                                          /* (and being observed by the daemon) */
208 #define FSESS_PUSH_SYMLINKS_IN    0x0020 /* prefix absolute symlinks with mp */
209 #define FSESS_DEFAULT_PERMISSIONS 0x0040 /* kernel does permission checking */
210 #define FSESS_NO_ATTRCACHE        0x0080 /* no attribute caching */
211 #define FSESS_NO_READAHEAD        0x0100 /* no readaheads */
212 #define FSESS_NO_DATACACHE        0x0200 /* disable buffer cache */
213 #define FSESS_NO_NAMECACHE        0x0400 /* disable name cache */
214 #define FSESS_NO_MMAP             0x0800 /* disable mmap */
215 #define FSESS_BROKENIO            0x1000 /* fix broken io */
216 
217 enum fuse_data_cache_mode {
218 	FUSE_CACHE_UC,
219 	FUSE_CACHE_WT,
220 	FUSE_CACHE_WB,
221 };
222 
223 extern int fuse_data_cache_mode;
224 extern int fuse_data_cache_invalidate;
225 extern int fuse_mmap_enable;
226 extern int fuse_sync_resize;
227 extern int fuse_fix_broken_io;
228 
229 static __inline__
230 struct fuse_data *
231 fuse_get_mpdata(struct mount *mp)
232 {
233     return mp->mnt_data;
234 }
235 
236 static __inline int
237 fsess_isimpl(struct mount *mp, int opcode)
238 {
239     struct fuse_data *data = fuse_get_mpdata(mp);
240 
241     return (data->notimpl & (1ULL << opcode)) == 0;
242 
243 }
244 static __inline void
245 fsess_set_notimpl(struct mount *mp, int opcode)
246 {
247     struct fuse_data *data = fuse_get_mpdata(mp);
248 
249     data->notimpl |= (1ULL << opcode);
250 }
251 
252 static __inline int
253 fsess_opt_datacache(struct mount *mp)
254 {
255     struct fuse_data *data = fuse_get_mpdata(mp);
256 
257     return (fuse_data_cache_mode != FUSE_CACHE_UC &&
258         (data->dataflags & FSESS_NO_DATACACHE) == 0);
259 }
260 
261 static __inline int
262 fsess_opt_mmap(struct mount *mp)
263 {
264     struct fuse_data *data = fuse_get_mpdata(mp);
265 
266     if (!fuse_mmap_enable || fuse_data_cache_mode == FUSE_CACHE_UC)
267         return 0;
268     return ((data->dataflags & (FSESS_NO_DATACACHE | FSESS_NO_MMAP)) == 0);
269 }
270 
271 static __inline int
272 fsess_opt_brokenio(struct mount *mp)
273 {
274     struct fuse_data *data = fuse_get_mpdata(mp);
275 
276     return (fuse_fix_broken_io || (data->dataflags & FSESS_BROKENIO));
277 }
278 
279 static __inline__
280 void
281 fuse_ms_push(struct fuse_ticket *ftick)
282 {
283     DEBUGX(FUSE_DEBUG_IPC, "ftick=%p refcount=%d\n",
284         ftick, ftick->tk_refcount + 1);
285     mtx_assert(&ftick->tk_data->ms_mtx, MA_OWNED);
286     refcount_acquire(&ftick->tk_refcount);
287     STAILQ_INSERT_TAIL(&ftick->tk_data->ms_head, ftick, tk_ms_link);
288 }
289 
290 static __inline__
291 struct fuse_ticket *
292 fuse_ms_pop(struct fuse_data *data)
293 {
294     struct fuse_ticket *ftick = NULL;
295 
296     mtx_assert(&data->ms_mtx, MA_OWNED);
297 
298     if ((ftick = STAILQ_FIRST(&data->ms_head))) {
299         STAILQ_REMOVE_HEAD(&data->ms_head, tk_ms_link);
300 #ifdef INVARIANTS
301         ftick->tk_ms_link.stqe_next = NULL;
302 #endif
303     }
304     DEBUGX(FUSE_DEBUG_IPC, "ftick=%p refcount=%d\n",
305         ftick, ftick ? ftick->tk_refcount : -1);
306 
307     return ftick;
308 }
309 
310 static __inline__
311 void
312 fuse_aw_push(struct fuse_ticket *ftick)
313 {
314     DEBUGX(FUSE_DEBUG_IPC, "ftick=%p refcount=%d\n",
315         ftick, ftick->tk_refcount + 1);
316     mtx_assert(&ftick->tk_data->aw_mtx, MA_OWNED);
317     refcount_acquire(&ftick->tk_refcount);
318     TAILQ_INSERT_TAIL(&ftick->tk_data->aw_head, ftick, tk_aw_link);
319 }
320 
321 static __inline__
322 void
323 fuse_aw_remove(struct fuse_ticket *ftick)
324 {
325     DEBUGX(FUSE_DEBUG_IPC, "ftick=%p refcount=%d\n",
326         ftick, ftick->tk_refcount);
327     mtx_assert(&ftick->tk_data->aw_mtx, MA_OWNED);
328     TAILQ_REMOVE(&ftick->tk_data->aw_head, ftick, tk_aw_link);
329 #ifdef INVARIANTS
330     ftick->tk_aw_link.tqe_next = NULL;
331     ftick->tk_aw_link.tqe_prev = NULL;
332 #endif
333 }
334 
335 static __inline__
336 struct fuse_ticket *
337 fuse_aw_pop(struct fuse_data *data)
338 {
339     struct fuse_ticket *ftick = NULL;
340 
341     mtx_assert(&data->aw_mtx, MA_OWNED);
342 
343     if ((ftick = TAILQ_FIRST(&data->aw_head))) {
344         fuse_aw_remove(ftick);
345     }
346     DEBUGX(FUSE_DEBUG_IPC, "ftick=%p refcount=%d\n",
347         ftick, ftick ? ftick->tk_refcount : -1);
348 
349     return ftick;
350 }
351 
352 struct fuse_ticket *fuse_ticket_fetch(struct fuse_data *data);
353 int fuse_ticket_drop(struct fuse_ticket *ftick);
354 void fuse_insert_callback(struct fuse_ticket *ftick, fuse_handler_t *handler);
355 void fuse_insert_message(struct fuse_ticket *ftick);
356 
357 static __inline__
358 int
359 fuse_libabi_geq(struct fuse_data *data, uint32_t abi_maj, uint32_t abi_min)
360 {
361     return (data->fuse_libabi_major > abi_maj ||
362             (data->fuse_libabi_major == abi_maj && data->fuse_libabi_minor >= abi_min));
363 }
364 
365 struct fuse_data *fdata_alloc(struct cdev *dev, struct ucred *cred);
366 void fdata_trydestroy(struct fuse_data *data);
367 void fdata_set_dead(struct fuse_data *data);
368 
369 static __inline__
370 int
371 fdata_get_dead(struct fuse_data *data)
372 {
373     return (data->dataflags & FSESS_DEAD);
374 }
375 
376 struct fuse_dispatcher {
377 
378     struct fuse_ticket    *tick;
379     struct fuse_in_header *finh;
380 
381     void    *indata;
382     size_t   iosize;
383     uint64_t nodeid;
384     int      answ_stat;
385     void    *answ;
386 };
387 
388 static __inline__
389 void
390 fdisp_init(struct fuse_dispatcher *fdisp, size_t iosize)
391 {
392     DEBUGX(FUSE_DEBUG_IPC, "-> fdisp=%p, iosize=%zx\n", fdisp, iosize);
393     fdisp->iosize = iosize;
394     fdisp->tick = NULL;
395 }
396 
397 static __inline__
398 void
399 fdisp_destroy(struct fuse_dispatcher *fdisp)
400 {
401     DEBUGX(FUSE_DEBUG_IPC, "-> fdisp=%p, ftick=%p\n", fdisp, fdisp->tick);
402     fuse_ticket_drop(fdisp->tick);
403 #ifdef INVARIANTS
404     fdisp->tick = NULL;
405 #endif
406 }
407 
408 void fdisp_make(struct fuse_dispatcher *fdip, enum fuse_opcode op,
409                 struct mount *mp, uint64_t nid, struct thread *td,
410                 struct ucred *cred);
411 
412 void fdisp_make_pid(struct fuse_dispatcher *fdip, enum fuse_opcode op,
413                     struct mount *mp, uint64_t nid, pid_t pid,
414                     struct ucred *cred);
415 
416 void fdisp_make_vp(struct fuse_dispatcher *fdip, enum fuse_opcode op,
417                    struct vnode *vp, struct thread *td, struct ucred *cred);
418 
419 int  fdisp_wait_answ(struct fuse_dispatcher *fdip);
420 
421 static __inline__
422 int
423 fdisp_simple_putget_vp(struct fuse_dispatcher *fdip, enum fuse_opcode op,
424                     struct vnode *vp, struct thread *td, struct ucred *cred)
425 {
426     DEBUGX(FUSE_DEBUG_IPC, "-> fdip=%p, opcode=%d, vp=%p\n", fdip, op, vp);
427     fdisp_make_vp(fdip, op, vp, td, cred);
428     return fdisp_wait_answ(fdip);
429 }
430 
431 #endif /* _FUSE_IPC_H_ */
432