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