Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
freebsd
GitHub Repository: freebsd/freebsd-src
Path: blob/main/sys/fs/fuse/fuse_file.c
39586 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
#include <sys/param.h>
64
#include <sys/systm.h>
65
#include <sys/counter.h>
66
#include <sys/module.h>
67
#include <sys/errno.h>
68
#include <sys/kernel.h>
69
#include <sys/conf.h>
70
#include <sys/uio.h>
71
#include <sys/malloc.h>
72
#include <sys/queue.h>
73
#include <sys/lock.h>
74
#include <sys/sx.h>
75
#include <sys/mutex.h>
76
#include <sys/proc.h>
77
#include <sys/mount.h>
78
#include <sys/vnode.h>
79
#include <sys/sdt.h>
80
#include <sys/sysctl.h>
81
82
#include "fuse.h"
83
#include "fuse_file.h"
84
#include "fuse_internal.h"
85
#include "fuse_io.h"
86
#include "fuse_ipc.h"
87
#include "fuse_node.h"
88
89
MALLOC_DEFINE(M_FUSE_FILEHANDLE, "fuse_filefilehandle", "FUSE file handle");
90
91
SDT_PROVIDER_DECLARE(fusefs);
92
/*
93
* Fuse trace probe:
94
* arg0: verbosity. Higher numbers give more verbose messages
95
* arg1: Textual message
96
*/
97
SDT_PROBE_DEFINE2(fusefs, , file, trace, "int", "char*");
98
99
static counter_u64_t fuse_fh_count;
100
101
SYSCTL_COUNTER_U64(_vfs_fusefs_stats, OID_AUTO, filehandle_count, CTLFLAG_RD,
102
&fuse_fh_count, "number of open FUSE filehandles");
103
104
/* Get the FUFH type for a particular access mode */
105
static inline fufh_type_t
106
fflags_2_fufh_type(int fflags)
107
{
108
if ((fflags & FREAD) && (fflags & FWRITE))
109
return FUFH_RDWR;
110
else if (fflags & (FWRITE))
111
return FUFH_WRONLY;
112
else if (fflags & (FREAD))
113
return FUFH_RDONLY;
114
else if (fflags & (FEXEC))
115
return FUFH_EXEC;
116
else
117
panic("FUSE: What kind of a flag is this (%x)?", fflags);
118
}
119
120
int
121
fuse_filehandle_open(struct vnode *vp, int a_mode,
122
struct fuse_filehandle **fufhp, struct thread *td, struct ucred *cred)
123
{
124
struct mount *mp = vnode_mount(vp);
125
struct fuse_dispatcher fdi;
126
const struct fuse_open_out default_foo = {
127
.fh = 0,
128
.open_flags = FOPEN_KEEP_CACHE,
129
.padding = 0
130
};
131
struct fuse_open_in *foi = NULL;
132
const struct fuse_open_out *foo;
133
fufh_type_t fufh_type;
134
int err = 0;
135
int oflags = 0;
136
int op = FUSE_OPEN;
137
int relop = FUSE_RELEASE;
138
139
fufh_type = fflags_2_fufh_type(a_mode);
140
oflags = fufh_type_2_fflags(fufh_type);
141
142
if (vnode_isdir(vp)) {
143
op = FUSE_OPENDIR;
144
relop = FUSE_RELEASEDIR;
145
/* vn_open_vnode already rejects FWRITE on directories */
146
MPASS(fufh_type == FUFH_RDONLY || fufh_type == FUFH_EXEC);
147
}
148
fdisp_init(&fdi, sizeof(*foi));
149
if (fsess_not_impl(mp, op)) {
150
/* The operation implicitly succeeds */
151
foo = &default_foo;
152
} else {
153
fdisp_make_vp(&fdi, op, vp, td, cred);
154
155
foi = fdi.indata;
156
foi->flags = oflags;
157
158
err = fdisp_wait_answ(&fdi);
159
if (err == ENOSYS) {
160
/* The operation implicitly succeeds */
161
foo = &default_foo;
162
fsess_set_notimpl(mp, op);
163
fsess_set_notimpl(mp, relop);
164
err = 0;
165
} else if (err) {
166
SDT_PROBE2(fusefs, , file, trace, 1,
167
"OUCH ... daemon didn't give fh");
168
if (err == ENOENT)
169
fuse_internal_vnode_disappear(vp);
170
goto out;
171
} else {
172
foo = fdi.answ;
173
fsess_set_impl(mp, op);
174
}
175
}
176
177
fuse_filehandle_init(vp, fufh_type, fufhp, td, cred, foo);
178
fuse_vnode_open(vp, foo->open_flags, td);
179
180
out:
181
if (foi)
182
fdisp_destroy(&fdi);
183
return err;
184
}
185
186
int
187
fuse_filehandle_close(struct vnode *vp, struct fuse_filehandle *fufh,
188
struct thread *td, struct ucred *cred)
189
{
190
struct mount *mp = vnode_mount(vp);
191
struct fuse_dispatcher fdi;
192
struct fuse_release_in *fri;
193
194
int err = 0;
195
int op = FUSE_RELEASE;
196
197
if (fuse_isdeadfs(vp)) {
198
goto out;
199
}
200
if (vnode_isdir(vp))
201
op = FUSE_RELEASEDIR;
202
203
if (fsess_not_impl(mp, op))
204
goto out;
205
206
fdisp_init(&fdi, sizeof(*fri));
207
fdisp_make_vp(&fdi, op, vp, td, cred);
208
fri = fdi.indata;
209
fri->fh = fufh->fh_id;
210
fri->flags = fufh_type_2_fflags(fufh->fufh_type);
211
/*
212
* If the file has a POSIX lock then we're supposed to set lock_owner.
213
* If not, then lock_owner is undefined. So we may as well always set
214
* it.
215
*/
216
fri->lock_owner = td->td_proc->p_pid;
217
218
err = fdisp_wait_answ(&fdi);
219
fdisp_destroy(&fdi);
220
221
out:
222
counter_u64_add(fuse_fh_count, -1);
223
LIST_REMOVE(fufh, next);
224
free(fufh, M_FUSE_FILEHANDLE);
225
226
return err;
227
}
228
229
/*
230
* Check for a valid file handle, first the type requested, but if that
231
* isn't valid, try for FUFH_RDWR.
232
* Return true if there is any file handle with the correct credentials and
233
* a fufh type that includes the provided one.
234
* A pid of 0 means "don't care"
235
*/
236
bool
237
fuse_filehandle_validrw(struct vnode *vp, int mode,
238
struct ucred *cred, pid_t pid)
239
{
240
struct fuse_vnode_data *fvdat = VTOFUD(vp);
241
struct fuse_filehandle *fufh;
242
fufh_type_t fufh_type = fflags_2_fufh_type(mode);
243
244
/*
245
* Unlike fuse_filehandle_get, we want to search for a filehandle with
246
* the exact cred, and no fallback
247
*/
248
LIST_FOREACH(fufh, &fvdat->handles, next) {
249
if (fufh->fufh_type == fufh_type &&
250
fufh->uid == cred->cr_uid &&
251
fufh->gid == cred->cr_rgid &&
252
(pid == 0 || fufh->pid == pid))
253
return true;
254
}
255
256
if (fufh_type == FUFH_EXEC)
257
return false;
258
259
/* Fallback: find a RDWR list entry with the right cred */
260
LIST_FOREACH(fufh, &fvdat->handles, next) {
261
if (fufh->fufh_type == FUFH_RDWR &&
262
fufh->uid == cred->cr_uid &&
263
fufh->gid == cred->cr_rgid &&
264
(pid == 0 || fufh->pid == pid))
265
return true;
266
}
267
268
return false;
269
}
270
271
int
272
fuse_filehandle_get(struct vnode *vp, int fflag,
273
struct fuse_filehandle **fufhp, struct ucred *cred, pid_t pid)
274
{
275
struct fuse_vnode_data *fvdat = VTOFUD(vp);
276
struct fuse_filehandle *fufh;
277
fufh_type_t fufh_type;
278
279
fufh_type = fflags_2_fufh_type(fflag);
280
/* cred can be NULL for in-kernel clients */
281
if (cred == NULL)
282
goto fallback;
283
284
LIST_FOREACH(fufh, &fvdat->handles, next) {
285
if (fufh->fufh_type == fufh_type &&
286
fufh->uid == cred->cr_uid &&
287
fufh->gid == cred->cr_rgid &&
288
(pid == 0 || fufh->pid == pid))
289
goto found;
290
}
291
292
fallback:
293
/* Fallback: find a list entry with the right flags */
294
LIST_FOREACH(fufh, &fvdat->handles, next) {
295
if (fufh->fufh_type == fufh_type)
296
break;
297
}
298
299
if (fufh == NULL)
300
return EBADF;
301
302
found:
303
if (fufhp != NULL)
304
*fufhp = fufh;
305
return 0;
306
}
307
308
/* Get a file handle with any kind of flags */
309
int
310
fuse_filehandle_get_anyflags(struct vnode *vp,
311
struct fuse_filehandle **fufhp, struct ucred *cred, pid_t pid)
312
{
313
struct fuse_vnode_data *fvdat = VTOFUD(vp);
314
struct fuse_filehandle *fufh;
315
316
if (cred == NULL)
317
goto fallback;
318
319
LIST_FOREACH(fufh, &fvdat->handles, next) {
320
if (fufh->uid == cred->cr_uid &&
321
fufh->gid == cred->cr_rgid &&
322
(pid == 0 || fufh->pid == pid))
323
goto found;
324
}
325
326
fallback:
327
/* Fallback: find any list entry */
328
fufh = LIST_FIRST(&fvdat->handles);
329
330
if (fufh == NULL)
331
return EBADF;
332
333
found:
334
if (fufhp != NULL)
335
*fufhp = fufh;
336
return 0;
337
}
338
339
int
340
fuse_filehandle_getrw(struct vnode *vp, int fflag,
341
struct fuse_filehandle **fufhp, struct ucred *cred, pid_t pid)
342
{
343
int err;
344
345
err = fuse_filehandle_get(vp, fflag, fufhp, cred, pid);
346
if (err)
347
err = fuse_filehandle_get(vp, FREAD | FWRITE, fufhp, cred, pid);
348
return err;
349
}
350
351
void
352
fuse_filehandle_init(struct vnode *vp, fufh_type_t fufh_type,
353
struct fuse_filehandle **fufhp, struct thread *td, const struct ucred *cred,
354
const struct fuse_open_out *foo)
355
{
356
struct fuse_vnode_data *fvdat = VTOFUD(vp);
357
struct fuse_filehandle *fufh;
358
359
fufh = malloc(sizeof(struct fuse_filehandle), M_FUSE_FILEHANDLE,
360
M_WAITOK);
361
MPASS(fufh != NULL);
362
fufh->fh_id = foo->fh;
363
fufh->fufh_type = fufh_type;
364
fufh->gid = cred->cr_rgid;
365
fufh->uid = cred->cr_uid;
366
fufh->pid = td->td_proc->p_pid;
367
fufh->fuse_open_flags = foo->open_flags;
368
if (!FUFH_IS_VALID(fufh)) {
369
panic("FUSE: init: invalid filehandle id (type=%d)", fufh_type);
370
}
371
LIST_INSERT_HEAD(&fvdat->handles, fufh, next);
372
if (fufhp != NULL)
373
*fufhp = fufh;
374
375
counter_u64_add(fuse_fh_count, 1);
376
377
if (foo->open_flags & FOPEN_DIRECT_IO) {
378
ASSERT_VOP_ELOCKED(vp, __func__);
379
VTOFUD(vp)->flag |= FN_DIRECTIO;
380
fuse_io_invalbuf(vp, td);
381
} else {
382
if ((foo->open_flags & FOPEN_KEEP_CACHE) == 0)
383
fuse_io_invalbuf(vp, td);
384
VTOFUD(vp)->flag &= ~FN_DIRECTIO;
385
}
386
387
}
388
389
void
390
fuse_file_init(void)
391
{
392
fuse_fh_count = counter_u64_alloc(M_WAITOK);
393
}
394
395
void
396
fuse_file_destroy(void)
397
{
398
counter_u64_free(fuse_fh_count);
399
}
400
401