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 * Copyright (c) 2019 The FreeBSD Foundation
38 * Portions of this software were developed by BFF Storage Systems, LLC under
39 * sponsorship from the FreeBSD Foundation.
41 * Redistribution and use in source and binary forms, with or without
42 * modification, are permitted provided that the following conditions
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.
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
68 #include <sys/param.h>
69 #include <sys/refcount.h>
71 enum fuse_data_cache_mode {
80 size_t allocated_size;
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);
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))); \
95 #define FU_AT_LEAST(siz) max((siz), 160)
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)))
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)))
109 typedef int fuse_handler_t(struct fuse_ticket *ftick, struct uio *uio);
112 /* fields giving the identity of the ticket */
114 struct fuse_data *tk_data;
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
124 /* fields for initiating an upgoing message */
125 struct fuse_iov tk_ms_fiov;
127 size_t tk_ms_bufsize;
128 enum { FT_M_FIOV, FT_M_BUF } tk_ms_type;
129 STAILQ_ENTRY(fuse_ticket) tk_ms_link;
131 /* fields for handling answers coming from userspace */
132 struct fuse_iov tk_aw_fiov;
134 size_t tk_aw_bufsize;
135 enum { FT_A_FIOV, FT_A_BUF } tk_aw_type;
137 struct fuse_out_header tk_aw_ohead;
139 struct mtx tk_aw_mtx;
140 fuse_handler_t *tk_aw_handler;
141 TAILQ_ENTRY(fuse_ticket) tk_aw_link;
144 #define FT_ANSW 0x01 /* request of ticket has already been answered */
145 #define FT_DIRTY 0x04 /* ticket has been used */
147 static inline struct fuse_iov *
148 fticket_resp(struct fuse_ticket *ftick)
150 return (&ftick->tk_aw_fiov);
154 fticket_answered(struct fuse_ticket *ftick)
156 mtx_assert(&ftick->tk_aw_mtx, MA_OWNED);
157 return (ftick->tk_flag & FT_ANSW);
161 fticket_set_answered(struct fuse_ticket *ftick)
163 mtx_assert(&ftick->tk_aw_mtx, MA_OWNED);
164 ftick->tk_flag |= FT_ANSW;
167 static inline struct fuse_in_header*
168 fticket_in_header(struct fuse_ticket *ftick)
170 return (struct fuse_in_header *)(ftick->tk_ms_fiov.base);
173 static inline enum fuse_opcode
174 fticket_opcode(struct fuse_ticket *ftick)
176 return fticket_in_header(ftick)->opcode;
179 int fticket_pull(struct fuse_ticket *ftick, struct uio *uio);
182 * The data representing a FUSE session.
188 struct ucred *daemoncred;
193 STAILQ_HEAD(, fuse_ticket) ms_head;
197 TAILQ_HEAD(, fuse_ticket) aw_head;
200 * Holds the next value of the FUSE operation unique value.
201 * Also, serves as a wakeup channel to prevent any operations from
202 * being created before INIT completes.
206 struct sx rename_lock;
208 uint32_t fuse_libabi_major;
209 uint32_t fuse_libabi_minor;
211 uint32_t max_readahead_blocks;
215 char volname[MAXPATHLEN];
217 struct selinfo ks_rsel;
223 enum fuse_data_cache_mode cache_mode;
226 #define FSESS_DEAD 0x0001 /* session is to be closed */
227 #define FSESS_INITED 0x0004 /* session has been inited */
228 #define FSESS_DAEMON_CAN_SPY 0x0010 /* let non-owners access this fs */
229 /* (and being observed by the daemon) */
230 #define FSESS_PUSH_SYMLINKS_IN 0x0020 /* prefix absolute symlinks with mp */
231 #define FSESS_DEFAULT_PERMISSIONS 0x0040 /* kernel does permission checking */
232 #define FSESS_ASYNC_READ 0x1000 /* allow multiple reads of some file */
233 #define FSESS_POSIX_LOCKS 0x2000 /* daemon supports POSIX locks */
234 #define FSESS_EXPORT_SUPPORT 0x10000 /* daemon supports NFS-style lookups */
235 #define FSESS_INTR 0x20000 /* interruptible mounts */
236 #define FSESS_MNTOPTS_MASK ( \
237 FSESS_DAEMON_CAN_SPY | FSESS_PUSH_SYMLINKS_IN | \
238 FSESS_DEFAULT_PERMISSIONS | FSESS_INTR)
240 extern int fuse_data_cache_mode;
242 static inline struct fuse_data *
243 fuse_get_mpdata(struct mount *mp)
249 fsess_isimpl(struct mount *mp, int opcode)
251 struct fuse_data *data = fuse_get_mpdata(mp);
253 return ((data->notimpl & (1ULL << opcode)) == 0);
257 fsess_set_notimpl(struct mount *mp, int opcode)
259 struct fuse_data *data = fuse_get_mpdata(mp);
261 data->notimpl |= (1ULL << opcode);
265 fsess_opt_datacache(struct mount *mp)
267 struct fuse_data *data = fuse_get_mpdata(mp);
269 return (data->cache_mode != FUSE_CACHE_UC);
273 fsess_opt_mmap(struct mount *mp)
275 return (fsess_opt_datacache(mp));
279 fsess_opt_writeback(struct mount *mp)
281 struct fuse_data *data = fuse_get_mpdata(mp);
283 return (data->cache_mode == FUSE_CACHE_WB);
286 /* Insert a new upgoing message */
288 fuse_ms_push(struct fuse_ticket *ftick)
290 mtx_assert(&ftick->tk_data->ms_mtx, MA_OWNED);
291 refcount_acquire(&ftick->tk_refcount);
292 STAILQ_INSERT_TAIL(&ftick->tk_data->ms_head, ftick, tk_ms_link);
293 ftick->tk_data->ms_count++;
296 /* Insert a new upgoing message to the front of the queue */
298 fuse_ms_push_head(struct fuse_ticket *ftick)
300 mtx_assert(&ftick->tk_data->ms_mtx, MA_OWNED);
301 refcount_acquire(&ftick->tk_refcount);
302 STAILQ_INSERT_HEAD(&ftick->tk_data->ms_head, ftick, tk_ms_link);
303 ftick->tk_data->ms_count++;
306 static inline struct fuse_ticket *
307 fuse_ms_pop(struct fuse_data *data)
309 struct fuse_ticket *ftick = NULL;
311 mtx_assert(&data->ms_mtx, MA_OWNED);
313 if ((ftick = STAILQ_FIRST(&data->ms_head))) {
314 STAILQ_REMOVE_HEAD(&data->ms_head, tk_ms_link);
317 MPASS(data->ms_count >= 0);
318 ftick->tk_ms_link.stqe_next = NULL;
326 fuse_aw_push(struct fuse_ticket *ftick)
328 mtx_assert(&ftick->tk_data->aw_mtx, MA_OWNED);
329 refcount_acquire(&ftick->tk_refcount);
330 TAILQ_INSERT_TAIL(&ftick->tk_data->aw_head, ftick, tk_aw_link);
334 fuse_aw_remove(struct fuse_ticket *ftick)
336 mtx_assert(&ftick->tk_data->aw_mtx, MA_OWNED);
337 TAILQ_REMOVE(&ftick->tk_data->aw_head, ftick, tk_aw_link);
339 ftick->tk_aw_link.tqe_next = NULL;
340 ftick->tk_aw_link.tqe_prev = NULL;
344 static inline struct fuse_ticket *
345 fuse_aw_pop(struct fuse_data *data)
347 struct fuse_ticket *ftick;
349 mtx_assert(&data->aw_mtx, MA_OWNED);
351 if ((ftick = TAILQ_FIRST(&data->aw_head)) != NULL)
352 fuse_aw_remove(ftick);
357 struct fuse_ticket *fuse_ticket_fetch(struct fuse_data *data);
358 int fuse_ticket_drop(struct fuse_ticket *ftick);
359 void fuse_insert_callback(struct fuse_ticket *ftick, fuse_handler_t *handler);
360 void fuse_insert_message(struct fuse_ticket *ftick, bool irq);
363 fuse_libabi_geq(struct fuse_data *data, uint32_t abi_maj, uint32_t abi_min)
365 return (data->fuse_libabi_major > abi_maj ||
366 (data->fuse_libabi_major == abi_maj &&
367 data->fuse_libabi_minor >= abi_min));
370 struct fuse_data *fdata_alloc(struct cdev *dev, struct ucred *cred);
371 void fdata_trydestroy(struct fuse_data *data);
372 void fdata_set_dead(struct fuse_data *data);
375 fdata_get_dead(struct fuse_data *data)
377 return (data->dataflags & FSESS_DEAD);
380 struct fuse_dispatcher {
381 struct fuse_ticket *tick;
382 struct fuse_in_header *finh;
392 fdisp_init(struct fuse_dispatcher *fdisp, size_t iosize)
394 fdisp->iosize = iosize;
399 fdisp_destroy(struct fuse_dispatcher *fdisp)
401 fuse_ticket_drop(fdisp->tick);
407 void fdisp_refresh(struct fuse_dispatcher *fdip);
409 void fdisp_make(struct fuse_dispatcher *fdip, enum fuse_opcode op,
410 struct mount *mp, uint64_t nid, struct thread *td, struct ucred *cred);
412 void fdisp_make_vp(struct fuse_dispatcher *fdip, enum fuse_opcode op,
413 struct vnode *vp, struct thread *td, struct ucred *cred);
415 void fdisp_refresh_vp(struct fuse_dispatcher *fdip, enum fuse_opcode op,
416 struct vnode *vp, struct thread *td, struct ucred *cred);
418 int fdisp_wait_answ(struct fuse_dispatcher *fdip);
421 fdisp_simple_putget_vp(struct fuse_dispatcher *fdip, enum fuse_opcode op,
422 struct vnode *vp, struct thread *td, struct ucred *cred)
424 fdisp_make_vp(fdip, op, vp, td, cred);
425 return (fdisp_wait_answ(fdip));
428 #endif /* _FUSE_IPC_H_ */