2 * SPDX-License-Identifier: BSD-3-Clause
4 * Copyright (c) 2007-2009 Google Inc. and Amit Singh
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions are
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
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.
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.
33 * Copyright (C) 2005 Csaba Henk.
34 * All rights reserved.
36 * Redistribution and use in source and binary forms, with or without
37 * modification, are permitted provided that the following conditions
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.
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
63 #include <sys/param.h>
64 #include <sys/refcount.h>
69 size_t allocated_size;
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);
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))); \
84 #define FU_AT_LEAST(siz) max((siz), 160)
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)))
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)))
98 typedef int fuse_handler_t(struct fuse_ticket *ftick, struct uio *uio);
101 /* fields giving the identity of the ticket */
103 struct fuse_data *tk_data;
107 /* fields for initiating an upgoing message */
108 struct fuse_iov tk_ms_fiov;
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;
114 /* fields for handling answers coming from userspace */
115 struct fuse_iov tk_aw_fiov;
117 size_t tk_aw_bufsize;
118 enum { FT_A_FIOV, FT_A_BUF } tk_aw_type;
120 struct fuse_out_header tk_aw_ohead;
122 struct mtx tk_aw_mtx;
123 fuse_handler_t *tk_aw_handler;
124 TAILQ_ENTRY(fuse_ticket) tk_aw_link;
127 #define FT_ANSW 0x01 /* request of ticket has already been answered */
128 #define FT_DIRTY 0x04 /* ticket has been used */
130 static inline struct fuse_iov *
131 fticket_resp(struct fuse_ticket *ftick)
133 return (&ftick->tk_aw_fiov);
137 fticket_answered(struct fuse_ticket *ftick)
139 mtx_assert(&ftick->tk_aw_mtx, MA_OWNED);
140 return (ftick->tk_flag & FT_ANSW);
144 fticket_set_answered(struct fuse_ticket *ftick)
146 mtx_assert(&ftick->tk_aw_mtx, MA_OWNED);
147 ftick->tk_flag |= FT_ANSW;
150 static inline enum fuse_opcode
151 fticket_opcode(struct fuse_ticket *ftick)
153 return (((struct fuse_in_header *)(ftick->tk_ms_fiov.base))->opcode);
156 int fticket_pull(struct fuse_ticket *ftick, struct uio *uio);
158 enum mountpri { FM_NOMOUNTED, FM_PRIMARY, FM_SECONDARY };
161 * The data representing a FUSE session.
167 struct ucred *daemoncred;
172 STAILQ_HEAD(, fuse_ticket) ms_head;
175 TAILQ_HEAD(, fuse_ticket) aw_head;
179 struct sx rename_lock;
181 uint32_t fuse_libabi_major;
182 uint32_t fuse_libabi_minor;
187 char volname[MAXPATHLEN];
189 struct selinfo ks_rsel;
195 #define FSESS_DEAD 0x0001 /* session is to be closed */
196 #define FSESS_UNUSED0 0x0002 /* unused */
197 #define FSESS_INITED 0x0004 /* session has been inited */
198 #define FSESS_DAEMON_CAN_SPY 0x0010 /* let non-owners access this fs */
199 /* (and being observed by the daemon) */
200 #define FSESS_PUSH_SYMLINKS_IN 0x0020 /* prefix absolute symlinks with mp */
201 #define FSESS_DEFAULT_PERMISSIONS 0x0040 /* kernel does permission checking */
202 #define FSESS_NO_ATTRCACHE 0x0080 /* no attribute caching */
203 #define FSESS_NO_READAHEAD 0x0100 /* no readaheads */
204 #define FSESS_NO_DATACACHE 0x0200 /* disable buffer cache */
205 #define FSESS_NO_NAMECACHE 0x0400 /* disable name cache */
206 #define FSESS_NO_MMAP 0x0800 /* disable mmap */
207 #define FSESS_BROKENIO 0x1000 /* fix broken io */
209 enum fuse_data_cache_mode {
215 extern int fuse_data_cache_mode;
216 extern int fuse_data_cache_invalidate;
217 extern int fuse_mmap_enable;
218 extern int fuse_sync_resize;
219 extern int fuse_fix_broken_io;
221 static inline struct fuse_data *
222 fuse_get_mpdata(struct mount *mp)
228 fsess_isimpl(struct mount *mp, int opcode)
230 struct fuse_data *data = fuse_get_mpdata(mp);
232 return ((data->notimpl & (1ULL << opcode)) == 0);
236 fsess_set_notimpl(struct mount *mp, int opcode)
238 struct fuse_data *data = fuse_get_mpdata(mp);
240 data->notimpl |= (1ULL << opcode);
244 fsess_opt_datacache(struct mount *mp)
246 struct fuse_data *data = fuse_get_mpdata(mp);
248 return (fuse_data_cache_mode != FUSE_CACHE_UC &&
249 (data->dataflags & FSESS_NO_DATACACHE) == 0);
253 fsess_opt_mmap(struct mount *mp)
255 struct fuse_data *data = fuse_get_mpdata(mp);
257 if (!fuse_mmap_enable || fuse_data_cache_mode == FUSE_CACHE_UC)
259 return ((data->dataflags & (FSESS_NO_DATACACHE | FSESS_NO_MMAP)) == 0);
263 fsess_opt_brokenio(struct mount *mp)
265 struct fuse_data *data = fuse_get_mpdata(mp);
267 return (fuse_fix_broken_io || (data->dataflags & FSESS_BROKENIO));
271 fuse_ms_push(struct fuse_ticket *ftick)
273 mtx_assert(&ftick->tk_data->ms_mtx, MA_OWNED);
274 refcount_acquire(&ftick->tk_refcount);
275 STAILQ_INSERT_TAIL(&ftick->tk_data->ms_head, ftick, tk_ms_link);
278 static inline struct fuse_ticket *
279 fuse_ms_pop(struct fuse_data *data)
281 struct fuse_ticket *ftick = NULL;
283 mtx_assert(&data->ms_mtx, MA_OWNED);
285 if ((ftick = STAILQ_FIRST(&data->ms_head))) {
286 STAILQ_REMOVE_HEAD(&data->ms_head, tk_ms_link);
288 ftick->tk_ms_link.stqe_next = NULL;
296 fuse_aw_push(struct fuse_ticket *ftick)
298 mtx_assert(&ftick->tk_data->aw_mtx, MA_OWNED);
299 refcount_acquire(&ftick->tk_refcount);
300 TAILQ_INSERT_TAIL(&ftick->tk_data->aw_head, ftick, tk_aw_link);
304 fuse_aw_remove(struct fuse_ticket *ftick)
306 mtx_assert(&ftick->tk_data->aw_mtx, MA_OWNED);
307 TAILQ_REMOVE(&ftick->tk_data->aw_head, ftick, tk_aw_link);
309 ftick->tk_aw_link.tqe_next = NULL;
310 ftick->tk_aw_link.tqe_prev = NULL;
314 static inline struct fuse_ticket *
315 fuse_aw_pop(struct fuse_data *data)
317 struct fuse_ticket *ftick;
319 mtx_assert(&data->aw_mtx, MA_OWNED);
321 if ((ftick = TAILQ_FIRST(&data->aw_head)) != NULL)
322 fuse_aw_remove(ftick);
327 struct fuse_ticket *fuse_ticket_fetch(struct fuse_data *data);
328 int fuse_ticket_drop(struct fuse_ticket *ftick);
329 void fuse_insert_callback(struct fuse_ticket *ftick, fuse_handler_t *handler);
330 void fuse_insert_message(struct fuse_ticket *ftick);
333 fuse_libabi_geq(struct fuse_data *data, uint32_t abi_maj, uint32_t abi_min)
335 return (data->fuse_libabi_major > abi_maj ||
336 (data->fuse_libabi_major == abi_maj &&
337 data->fuse_libabi_minor >= abi_min));
340 struct fuse_data *fdata_alloc(struct cdev *dev, struct ucred *cred);
341 void fdata_trydestroy(struct fuse_data *data);
342 void fdata_set_dead(struct fuse_data *data);
345 fdata_get_dead(struct fuse_data *data)
347 return (data->dataflags & FSESS_DEAD);
350 struct fuse_dispatcher {
351 struct fuse_ticket *tick;
352 struct fuse_in_header *finh;
362 fdisp_init(struct fuse_dispatcher *fdisp, size_t iosize)
364 fdisp->iosize = iosize;
369 fdisp_destroy(struct fuse_dispatcher *fdisp)
371 fuse_ticket_drop(fdisp->tick);
377 void fdisp_refresh(struct fuse_dispatcher *fdip);
379 void fdisp_make(struct fuse_dispatcher *fdip, enum fuse_opcode op,
380 struct mount *mp, uint64_t nid, struct thread *td, struct ucred *cred);
382 void fdisp_make_vp(struct fuse_dispatcher *fdip, enum fuse_opcode op,
383 struct vnode *vp, struct thread *td, struct ucred *cred);
385 void fdisp_refresh_vp(struct fuse_dispatcher *fdip, enum fuse_opcode op,
386 struct vnode *vp, struct thread *td, struct ucred *cred);
388 int fdisp_wait_answ(struct fuse_dispatcher *fdip);
391 fdisp_simple_putget_vp(struct fuse_dispatcher *fdip, enum fuse_opcode op,
392 struct vnode *vp, struct thread *td, struct ucred *cred)
394 fdisp_make_vp(fdip, op, vp, td, cred);
395 return (fdisp_wait_answ(fdip));
398 #endif /* _FUSE_IPC_H_ */