Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
freebsd
GitHub Repository: freebsd/freebsd-src
Path: blob/main/sys/fs/fuse/fuse_ipc.h
105900 views
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
* Copyright (c) 2019 The FreeBSD Foundation
37
*
38
* Portions of this software were developed by BFF Storage Systems, LLC under
39
* sponsorship from the FreeBSD Foundation.
40
*
41
* Redistribution and use in source and binary forms, with or without
42
* modification, are permitted provided that the following conditions
43
* are met:
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.
49
*
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
60
* SUCH DAMAGE.
61
*/
62
63
#ifndef _FUSE_IPC_H_
64
#define _FUSE_IPC_H_
65
66
#include <sys/param.h>
67
#include <sys/refcount.h>
68
69
enum fuse_data_cache_mode {
70
FUSE_CACHE_UC,
71
FUSE_CACHE_WT,
72
FUSE_CACHE_WB,
73
};
74
75
struct fuse_iov {
76
void *base;
77
size_t len;
78
size_t allocated_size;
79
int credit;
80
};
81
82
void fiov_init(struct fuse_iov *fiov, size_t size);
83
void fiov_teardown(struct fuse_iov *fiov);
84
void fiov_refresh(struct fuse_iov *fiov);
85
void fiov_adjust(struct fuse_iov *fiov, size_t size);
86
87
#define FUSE_DIMALLOC(fiov, spc1, spc2, amnt) do { \
88
fiov_adjust(fiov, (sizeof(*(spc1)) + (amnt))); \
89
(spc1) = (fiov)->base; \
90
(spc2) = (char *)(fiov)->base + (sizeof(*(spc1))); \
91
} while (0)
92
93
#define FU_AT_LEAST(siz) max((siz), 160)
94
95
#define FUSE_ASSERT_AW_DONE(ftick) \
96
KASSERT((ftick)->tk_aw_link.tqe_next == NULL && \
97
(ftick)->tk_aw_link.tqe_prev == NULL, \
98
("FUSE: ticket still on answer delivery list %p", (ftick)))
99
100
#define FUSE_ASSERT_MS_DONE(ftick) \
101
KASSERT((ftick)->tk_ms_link.stqe_next == NULL, \
102
("FUSE: ticket still on message list %p", (ftick)))
103
104
struct fuse_ticket;
105
struct fuse_data;
106
107
typedef int fuse_handler_t(struct fuse_ticket *ftick, struct uio *uio);
108
109
struct fuse_ticket {
110
/* fields giving the identity of the ticket */
111
uint64_t tk_unique;
112
struct fuse_data *tk_data;
113
int tk_flag;
114
u_int tk_refcount;
115
/*
116
* If this ticket's operation has been interrupted, this will hold the
117
* unique value of the FUSE_INTERRUPT operation. Otherwise, it will be
118
* 0.
119
*/
120
uint64_t irq_unique;
121
122
/* fields for initiating an upgoing message */
123
struct fuse_iov tk_ms_fiov;
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
struct fuse_out_header tk_aw_ohead;
129
int tk_aw_errno;
130
struct mtx tk_aw_mtx;
131
fuse_handler_t *tk_aw_handler;
132
TAILQ_ENTRY(fuse_ticket) tk_aw_link;
133
};
134
135
#define FT_ANSW 0x01 /* request of ticket has already been answered */
136
#define FT_DIRTY 0x04 /* ticket has been used */
137
138
static inline struct fuse_iov *
139
fticket_resp(struct fuse_ticket *ftick)
140
{
141
return (&ftick->tk_aw_fiov);
142
}
143
144
static inline bool
145
fticket_answered(struct fuse_ticket *ftick)
146
{
147
mtx_assert(&ftick->tk_aw_mtx, MA_OWNED);
148
return (ftick->tk_flag & FT_ANSW);
149
}
150
151
static inline void
152
fticket_set_answered(struct fuse_ticket *ftick)
153
{
154
mtx_assert(&ftick->tk_aw_mtx, MA_OWNED);
155
ftick->tk_flag |= FT_ANSW;
156
}
157
158
static inline struct fuse_in_header*
159
fticket_in_header(struct fuse_ticket *ftick)
160
{
161
return (struct fuse_in_header *)(ftick->tk_ms_fiov.base);
162
}
163
164
static inline enum fuse_opcode
165
fticket_opcode(struct fuse_ticket *ftick)
166
{
167
return fticket_in_header(ftick)->opcode;
168
}
169
170
int fticket_pull(struct fuse_ticket *ftick, struct uio *uio);
171
172
/*
173
* The data representing a FUSE session.
174
*/
175
struct fuse_data {
176
struct cdev *fdev;
177
struct mount *mp;
178
struct vnode *vroot;
179
struct ucred *daemoncred;
180
int dataflags;
181
int ref;
182
183
struct mtx ms_mtx;
184
STAILQ_HEAD(, fuse_ticket) ms_head;
185
int ms_count;
186
187
struct mtx aw_mtx;
188
TAILQ_HEAD(, fuse_ticket) aw_head;
189
190
/*
191
* Holds the next value of the FUSE operation unique value.
192
* Also, serves as a wakeup channel to prevent any operations from
193
* being created before INIT completes.
194
*/
195
u_long ticketer;
196
197
struct sx rename_lock;
198
199
uint32_t fuse_libabi_major;
200
uint32_t fuse_libabi_minor;
201
202
uint32_t max_readahead_blocks;
203
uint32_t max_write;
204
uint32_t max_read;
205
206
struct selinfo ks_rsel;
207
208
int daemon_timeout;
209
int linux_errnos;
210
unsigned time_gran;
211
/* A bitmask of FUSE RPCs that are not implemented by the server */
212
uint64_t notimpl;
213
/*
214
* A bitmask of FUSE RPCs that are implemented by the server.
215
* If an operation is not present in either notimpl or isimpl, then it
216
* may be implemented by the server, but the kernel doesn't know for
217
* sure.
218
*/
219
uint64_t isimpl;
220
uint64_t mnt_flag;
221
enum fuse_data_cache_mode cache_mode;
222
};
223
224
#define FSESS_DEAD 0x0001 /* session is to be closed */
225
#define FSESS_INITED 0x0004 /* session has been inited */
226
#define FSESS_DAEMON_CAN_SPY 0x0010 /* let non-owners access this fs */
227
/* (and being observed by the daemon) */
228
#define FSESS_PUSH_SYMLINKS_IN 0x0020 /* prefix absolute symlinks with mp */
229
#define FSESS_DEFAULT_PERMISSIONS 0x0040 /* kernel does permission checking */
230
#define FSESS_ASYNC_READ 0x1000 /* allow multiple reads of some file */
231
#define FSESS_POSIX_LOCKS 0x2000 /* daemon supports POSIX locks */
232
#define FSESS_EXPORT_SUPPORT 0x10000 /* daemon supports NFS-style lookups */
233
#define FSESS_INTR 0x20000 /* interruptible mounts */
234
#define FSESS_WARN_SHORT_WRITE 0x40000 /* Short write without direct_io */
235
#define FSESS_WARN_WROTE_LONG 0x80000 /* Wrote more data than provided */
236
#define FSESS_WARN_LSEXTATTR_LONG 0x100000 /* Returned too many extattrs */
237
#define FSESS_WARN_CACHE_INCOHERENT 0x200000 /* Read cache incoherent */
238
#define FSESS_WARN_WB_CACHE_INCOHERENT 0x400000 /* WB cache incoherent */
239
#define FSESS_WARN_ILLEGAL_INODE 0x800000 /* Illegal inode for new file */
240
#define FSESS_WARN_READLINK_EMBEDDED_NUL 0x1000000 /* corrupt READLINK output */
241
#define FSESS_WARN_DOT_LOOKUP 0x2000000 /* Inconsistent . LOOKUP response */
242
#define FSESS_WARN_INODE_MISMATCH 0x4000000 /* ino != nodeid */
243
#define FSESS_SETXATTR_EXT 0x8000000 /* extended fuse_setxattr_in */
244
#define FSESS_AUTO_UNMOUNT 0x10000000 /* perform unmount when server dies */
245
#define FSESS_MNTOPTS_MASK ( \
246
FSESS_DAEMON_CAN_SPY | FSESS_PUSH_SYMLINKS_IN | \
247
FSESS_DEFAULT_PERMISSIONS | FSESS_INTR | FSESS_AUTO_UNMOUNT)
248
249
extern int fuse_data_cache_mode;
250
251
static inline struct fuse_data *
252
fuse_get_mpdata(struct mount *mp)
253
{
254
return mp->mnt_data;
255
}
256
257
static inline bool
258
fsess_is_impl(struct mount *mp, int opcode)
259
{
260
struct fuse_data *data = fuse_get_mpdata(mp);
261
262
return ((data->isimpl & (1ULL << opcode)) != 0);
263
264
}
265
266
static inline bool
267
fsess_maybe_impl(struct mount *mp, int opcode)
268
{
269
struct fuse_data *data = fuse_get_mpdata(mp);
270
271
return ((data->notimpl & (1ULL << opcode)) == 0);
272
273
}
274
275
static inline bool
276
fsess_not_impl(struct mount *mp, int opcode)
277
{
278
struct fuse_data *data = fuse_get_mpdata(mp);
279
280
return ((data->notimpl & (1ULL << opcode)) != 0);
281
282
}
283
284
static inline void
285
fsess_set_impl(struct mount *mp, int opcode)
286
{
287
struct fuse_data *data = fuse_get_mpdata(mp);
288
289
data->isimpl |= (1ULL << opcode);
290
}
291
292
static inline void
293
fsess_set_notimpl(struct mount *mp, int opcode)
294
{
295
struct fuse_data *data = fuse_get_mpdata(mp);
296
297
data->notimpl |= (1ULL << opcode);
298
}
299
300
static inline bool
301
fsess_opt_datacache(struct mount *mp)
302
{
303
struct fuse_data *data = fuse_get_mpdata(mp);
304
305
return (data->cache_mode != FUSE_CACHE_UC);
306
}
307
308
static inline bool
309
fsess_opt_mmap(struct mount *mp)
310
{
311
return (fsess_opt_datacache(mp));
312
}
313
314
static inline bool
315
fsess_opt_writeback(struct mount *mp)
316
{
317
struct fuse_data *data = fuse_get_mpdata(mp);
318
319
return (data->cache_mode == FUSE_CACHE_WB);
320
}
321
322
/* Insert a new upgoing message */
323
static inline void
324
fuse_ms_push(struct fuse_ticket *ftick)
325
{
326
mtx_assert(&ftick->tk_data->ms_mtx, MA_OWNED);
327
refcount_acquire(&ftick->tk_refcount);
328
STAILQ_INSERT_TAIL(&ftick->tk_data->ms_head, ftick, tk_ms_link);
329
ftick->tk_data->ms_count++;
330
}
331
332
/* Insert a new upgoing message to the front of the queue */
333
static inline void
334
fuse_ms_push_head(struct fuse_ticket *ftick)
335
{
336
mtx_assert(&ftick->tk_data->ms_mtx, MA_OWNED);
337
refcount_acquire(&ftick->tk_refcount);
338
STAILQ_INSERT_HEAD(&ftick->tk_data->ms_head, ftick, tk_ms_link);
339
ftick->tk_data->ms_count++;
340
}
341
342
static inline struct fuse_ticket *
343
fuse_ms_pop(struct fuse_data *data)
344
{
345
struct fuse_ticket *ftick = NULL;
346
347
mtx_assert(&data->ms_mtx, MA_OWNED);
348
349
if ((ftick = STAILQ_FIRST(&data->ms_head))) {
350
STAILQ_REMOVE_HEAD(&data->ms_head, tk_ms_link);
351
data->ms_count--;
352
#ifdef INVARIANTS
353
MPASS(data->ms_count >= 0);
354
ftick->tk_ms_link.stqe_next = NULL;
355
#endif
356
}
357
358
return (ftick);
359
}
360
361
static inline void
362
fuse_aw_push(struct fuse_ticket *ftick)
363
{
364
mtx_assert(&ftick->tk_data->aw_mtx, MA_OWNED);
365
refcount_acquire(&ftick->tk_refcount);
366
TAILQ_INSERT_TAIL(&ftick->tk_data->aw_head, ftick, tk_aw_link);
367
}
368
369
static inline void
370
fuse_aw_remove(struct fuse_ticket *ftick)
371
{
372
mtx_assert(&ftick->tk_data->aw_mtx, MA_OWNED);
373
TAILQ_REMOVE(&ftick->tk_data->aw_head, ftick, tk_aw_link);
374
#ifdef INVARIANTS
375
ftick->tk_aw_link.tqe_next = NULL;
376
ftick->tk_aw_link.tqe_prev = NULL;
377
#endif
378
}
379
380
static inline struct fuse_ticket *
381
fuse_aw_pop(struct fuse_data *data)
382
{
383
struct fuse_ticket *ftick;
384
385
mtx_assert(&data->aw_mtx, MA_OWNED);
386
387
if ((ftick = TAILQ_FIRST(&data->aw_head)) != NULL)
388
fuse_aw_remove(ftick);
389
390
return (ftick);
391
}
392
393
struct fuse_ticket *fuse_ticket_fetch(struct fuse_data *data);
394
int fuse_ticket_drop(struct fuse_ticket *ftick);
395
void fuse_insert_callback(struct fuse_ticket *ftick, fuse_handler_t *handler);
396
void fuse_insert_message(struct fuse_ticket *ftick, bool irq);
397
398
static inline bool
399
fuse_libabi_geq(struct fuse_data *data, uint32_t abi_maj, uint32_t abi_min)
400
{
401
return (data->fuse_libabi_major > abi_maj ||
402
(data->fuse_libabi_major == abi_maj &&
403
data->fuse_libabi_minor >= abi_min));
404
}
405
406
/* Print msg as a warning to the console, but no more than once per session */
407
void fuse_warn(struct fuse_data *data, unsigned flag, const char *msg);
408
409
struct fuse_data *fdata_alloc(struct cdev *dev, struct ucred *cred);
410
void fdata_trydestroy(struct fuse_data *data);
411
void fdata_set_dead(struct fuse_data *data);
412
413
static inline bool
414
fdata_get_dead(struct fuse_data *data)
415
{
416
return (data->dataflags & FSESS_DEAD);
417
}
418
419
struct fuse_dispatcher {
420
struct fuse_ticket *tick;
421
struct fuse_in_header *finh;
422
423
void *indata;
424
size_t iosize;
425
uint64_t nodeid;
426
int answ_stat;
427
void *answ;
428
};
429
430
static inline void
431
fdisp_init(struct fuse_dispatcher *fdisp, size_t iosize)
432
{
433
fdisp->iosize = iosize;
434
fdisp->tick = NULL;
435
}
436
437
static inline void
438
fdisp_destroy(struct fuse_dispatcher *fdisp)
439
{
440
fuse_ticket_drop(fdisp->tick);
441
#ifdef INVARIANTS
442
fdisp->tick = NULL;
443
#endif
444
}
445
446
void fdisp_make(struct fuse_dispatcher *fdip, enum fuse_opcode op,
447
struct mount *mp, uint64_t nid, struct thread *td, struct ucred *cred);
448
449
void fdisp_make_vp(struct fuse_dispatcher *fdip, enum fuse_opcode op,
450
struct vnode *vp, struct thread *td, struct ucred *cred);
451
452
void fdisp_refresh_vp(struct fuse_dispatcher *fdip, enum fuse_opcode op,
453
struct vnode *vp, struct thread *td, struct ucred *cred);
454
455
int fdisp_wait_answ(struct fuse_dispatcher *fdip);
456
457
static inline int
458
fdisp_simple_putget_vp(struct fuse_dispatcher *fdip, enum fuse_opcode op,
459
struct vnode *vp, struct thread *td, struct ucred *cred)
460
{
461
fdisp_make_vp(fdip, op, vp, td, cred);
462
return (fdisp_wait_answ(fdip));
463
}
464
465
#endif /* _FUSE_IPC_H_ */
466
467