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