Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
freebsd
GitHub Repository: freebsd/freebsd-src
Path: blob/main/sys/fs/fuse/fuse_vnops.c
104875 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/module.h>
65
#include <sys/systm.h>
66
#include <sys/errno.h>
67
#include <sys/kernel.h>
68
#include <sys/conf.h>
69
#include <sys/filio.h>
70
#include <sys/uio.h>
71
#include <sys/malloc.h>
72
#include <sys/queue.h>
73
#include <sys/limits.h>
74
#include <sys/lock.h>
75
#include <sys/rwlock.h>
76
#include <sys/sx.h>
77
#include <sys/proc.h>
78
#include <sys/mount.h>
79
#include <sys/vnode.h>
80
#include <sys/namei.h>
81
#include <sys/extattr.h>
82
#include <sys/stat.h>
83
#include <sys/unistd.h>
84
#include <sys/filedesc.h>
85
#include <sys/file.h>
86
#include <sys/fcntl.h>
87
#include <sys/dirent.h>
88
#include <sys/bio.h>
89
#include <sys/buf.h>
90
#include <sys/sysctl.h>
91
#include <sys/vmmeter.h>
92
#define EXTERR_CATEGORY EXTERR_CAT_FUSE_VNOPS
93
#include <sys/exterrvar.h>
94
#include <sys/sysent.h>
95
96
#include <vm/vm.h>
97
#include <vm/vm_extern.h>
98
#include <vm/pmap.h>
99
#include <vm/vm_map.h>
100
#include <vm/vm_page.h>
101
#include <vm/vm_param.h>
102
#include <vm/vm_object.h>
103
#include <vm/vm_pager.h>
104
#include <vm/vnode_pager.h>
105
#include <vm/vm_object.h>
106
107
#include "fuse.h"
108
#include "fuse_file.h"
109
#include "fuse_internal.h"
110
#include "fuse_ipc.h"
111
#include "fuse_node.h"
112
#include "fuse_io.h"
113
114
#include <sys/priv.h>
115
116
/* Maximum number of hardlinks to a single FUSE file */
117
#define FUSE_LINK_MAX UINT32_MAX
118
119
SDT_PROVIDER_DECLARE(fusefs);
120
/*
121
* Fuse trace probe:
122
* arg0: verbosity. Higher numbers give more verbose messages
123
* arg1: Textual message
124
*/
125
SDT_PROBE_DEFINE2(fusefs, , vnops, trace, "int", "char*");
126
127
/* vnode ops */
128
static vop_access_t fuse_vnop_access;
129
static vop_advlock_t fuse_vnop_advlock;
130
static vop_allocate_t fuse_vnop_allocate;
131
static vop_bmap_t fuse_vnop_bmap;
132
static vop_close_t fuse_fifo_close;
133
static vop_close_t fuse_vnop_close;
134
static vop_copy_file_range_t fuse_vnop_copy_file_range;
135
static vop_create_t fuse_vnop_create;
136
static vop_deallocate_t fuse_vnop_deallocate;
137
static vop_deleteextattr_t fuse_vnop_deleteextattr;
138
static vop_fdatasync_t fuse_vnop_fdatasync;
139
static vop_fsync_t fuse_vnop_fsync;
140
static vop_getattr_t fuse_vnop_getattr;
141
static vop_getextattr_t fuse_vnop_getextattr;
142
static vop_inactive_t fuse_vnop_inactive;
143
static vop_ioctl_t fuse_vnop_ioctl;
144
static vop_link_t fuse_vnop_link;
145
static vop_listextattr_t fuse_vnop_listextattr;
146
static vop_lookup_t fuse_vnop_lookup;
147
static vop_mkdir_t fuse_vnop_mkdir;
148
static vop_mknod_t fuse_vnop_mknod;
149
static vop_open_t fuse_vnop_open;
150
static vop_pathconf_t fuse_vnop_pathconf;
151
static vop_read_t fuse_vnop_read;
152
static vop_readdir_t fuse_vnop_readdir;
153
static vop_readlink_t fuse_vnop_readlink;
154
static vop_reclaim_t fuse_vnop_reclaim;
155
static vop_remove_t fuse_vnop_remove;
156
static vop_rename_t fuse_vnop_rename;
157
static vop_rmdir_t fuse_vnop_rmdir;
158
static vop_setattr_t fuse_vnop_setattr;
159
static vop_setextattr_t fuse_vnop_setextattr;
160
static vop_strategy_t fuse_vnop_strategy;
161
static vop_symlink_t fuse_vnop_symlink;
162
static vop_write_t fuse_vnop_write;
163
static vop_getpages_t fuse_vnop_getpages;
164
static vop_print_t fuse_vnop_print;
165
static vop_vptofh_t fuse_vnop_vptofh;
166
167
struct vop_vector fuse_fifoops = {
168
.vop_default = &fifo_specops,
169
.vop_access = fuse_vnop_access,
170
.vop_close = fuse_fifo_close,
171
.vop_fsync = fuse_vnop_fsync,
172
.vop_getattr = fuse_vnop_getattr,
173
.vop_inactive = fuse_vnop_inactive,
174
.vop_pathconf = fuse_vnop_pathconf,
175
.vop_print = fuse_vnop_print,
176
.vop_read = VOP_PANIC,
177
.vop_reclaim = fuse_vnop_reclaim,
178
.vop_setattr = fuse_vnop_setattr,
179
.vop_write = VOP_PANIC,
180
.vop_vptofh = fuse_vnop_vptofh,
181
};
182
VFS_VOP_VECTOR_REGISTER(fuse_fifoops);
183
184
struct vop_vector fuse_vnops = {
185
.vop_allocate = fuse_vnop_allocate,
186
.vop_default = &default_vnodeops,
187
.vop_access = fuse_vnop_access,
188
.vop_advlock = fuse_vnop_advlock,
189
.vop_bmap = fuse_vnop_bmap,
190
.vop_close = fuse_vnop_close,
191
.vop_copy_file_range = fuse_vnop_copy_file_range,
192
.vop_create = fuse_vnop_create,
193
.vop_deallocate = fuse_vnop_deallocate,
194
.vop_deleteextattr = fuse_vnop_deleteextattr,
195
.vop_fsync = fuse_vnop_fsync,
196
.vop_fdatasync = fuse_vnop_fdatasync,
197
.vop_getattr = fuse_vnop_getattr,
198
.vop_getextattr = fuse_vnop_getextattr,
199
.vop_inactive = fuse_vnop_inactive,
200
.vop_ioctl = fuse_vnop_ioctl,
201
.vop_link = fuse_vnop_link,
202
.vop_listextattr = fuse_vnop_listextattr,
203
.vop_lookup = fuse_vnop_lookup,
204
.vop_mkdir = fuse_vnop_mkdir,
205
.vop_mknod = fuse_vnop_mknod,
206
.vop_open = fuse_vnop_open,
207
.vop_pathconf = fuse_vnop_pathconf,
208
/*
209
* TODO: implement vop_poll after upgrading to protocol 7.21.
210
* FUSE_POLL was added in protocol 7.11, but it's kind of broken until
211
* 7.21, which adds the ability for the client to choose which poll
212
* events it wants, and for a client to deregister a file handle
213
*/
214
.vop_read = fuse_vnop_read,
215
.vop_readdir = fuse_vnop_readdir,
216
.vop_readlink = fuse_vnop_readlink,
217
.vop_reclaim = fuse_vnop_reclaim,
218
.vop_remove = fuse_vnop_remove,
219
.vop_rename = fuse_vnop_rename,
220
.vop_rmdir = fuse_vnop_rmdir,
221
.vop_setattr = fuse_vnop_setattr,
222
.vop_setextattr = fuse_vnop_setextattr,
223
.vop_strategy = fuse_vnop_strategy,
224
.vop_symlink = fuse_vnop_symlink,
225
.vop_write = fuse_vnop_write,
226
.vop_getpages = fuse_vnop_getpages,
227
.vop_print = fuse_vnop_print,
228
.vop_vptofh = fuse_vnop_vptofh,
229
};
230
VFS_VOP_VECTOR_REGISTER(fuse_vnops);
231
232
/* Check permission for extattr operations, much like extattr_check_cred */
233
static int
234
fuse_extattr_check_cred(struct vnode *vp, int ns, struct ucred *cred,
235
struct thread *td, accmode_t accmode)
236
{
237
struct mount *mp = vnode_mount(vp);
238
struct fuse_data *data = fuse_get_mpdata(mp);
239
int default_permissions = data->dataflags & FSESS_DEFAULT_PERMISSIONS;
240
241
/*
242
* Kernel-invoked always succeeds.
243
*/
244
if (cred == NOCRED)
245
return (0);
246
247
/*
248
* Do not allow privileged processes in jail to directly manipulate
249
* system attributes.
250
*/
251
switch (ns) {
252
case EXTATTR_NAMESPACE_SYSTEM:
253
if (default_permissions) {
254
return (priv_check_cred(cred, PRIV_VFS_EXTATTR_SYSTEM));
255
}
256
return (0);
257
case EXTATTR_NAMESPACE_USER:
258
if (default_permissions) {
259
return (fuse_internal_access(vp, accmode, td, cred));
260
}
261
return (0);
262
default:
263
return (EPERM);
264
}
265
}
266
267
/* Get a filehandle for a directory */
268
static int
269
fuse_filehandle_get_dir(struct vnode *vp, struct fuse_filehandle **fufhp,
270
struct ucred *cred, pid_t pid)
271
{
272
if (fuse_filehandle_get(vp, FREAD, fufhp, cred, pid) == 0)
273
return 0;
274
return fuse_filehandle_get(vp, FEXEC, fufhp, cred, pid);
275
}
276
277
/* Send FUSE_FLUSH for this vnode */
278
static int
279
fuse_flush(struct vnode *vp, struct ucred *cred, pid_t pid, int fflag)
280
{
281
struct fuse_flush_in *ffi;
282
struct fuse_filehandle *fufh;
283
struct fuse_dispatcher fdi;
284
struct thread *td = curthread;
285
struct mount *mp = vnode_mount(vp);
286
int err;
287
288
if (fsess_not_impl(mp, FUSE_FLUSH))
289
return 0;
290
291
err = fuse_filehandle_getrw(vp, fflag, &fufh, cred, pid);
292
if (err)
293
return err;
294
295
if (fufh->fuse_open_flags & FOPEN_NOFLUSH &&
296
(!fsess_opt_writeback(mp)))
297
return (0);
298
299
fdisp_init(&fdi, sizeof(*ffi));
300
fdisp_make_vp(&fdi, FUSE_FLUSH, vp, td, cred);
301
ffi = fdi.indata;
302
ffi->fh = fufh->fh_id;
303
/*
304
* If the file has a POSIX lock then we're supposed to set lock_owner.
305
* If not, then lock_owner is undefined. So we may as well always set
306
* it.
307
*/
308
ffi->lock_owner = td->td_proc->p_pid;
309
310
err = fdisp_wait_answ(&fdi);
311
if (err == ENOSYS) {
312
fsess_set_notimpl(mp, FUSE_FLUSH);
313
err = 0;
314
}
315
fdisp_destroy(&fdi);
316
return err;
317
}
318
319
/* Close wrapper for fifos. */
320
static int
321
fuse_fifo_close(struct vop_close_args *ap)
322
{
323
return (fifo_specops.vop_close(ap));
324
}
325
326
/* Invalidate a range of cached data, whether dirty of not */
327
static int
328
fuse_inval_buf_range(struct vnode *vp, off_t filesize, off_t start, off_t end)
329
{
330
struct buf *bp;
331
daddr_t left_lbn, end_lbn, right_lbn;
332
off_t new_filesize;
333
int iosize, left_on, right_on, right_blksize;
334
335
iosize = fuse_iosize(vp);
336
left_lbn = start / iosize;
337
end_lbn = howmany(end, iosize);
338
left_on = start & (iosize - 1);
339
if (left_on != 0) {
340
bp = getblk(vp, left_lbn, iosize, PCATCH, 0, 0);
341
if ((bp->b_flags & B_CACHE) != 0 && bp->b_dirtyend >= left_on) {
342
/*
343
* Flush the dirty buffer, because we don't have a
344
* byte-granular way to record which parts of the
345
* buffer are valid.
346
*/
347
bwrite(bp);
348
if (bp->b_error)
349
return (bp->b_error);
350
} else {
351
brelse(bp);
352
}
353
}
354
right_on = end & (iosize - 1);
355
if (right_on != 0) {
356
right_lbn = end / iosize;
357
new_filesize = MAX(filesize, end);
358
right_blksize = MIN(iosize, new_filesize - iosize * right_lbn);
359
bp = getblk(vp, right_lbn, right_blksize, PCATCH, 0, 0);
360
if ((bp->b_flags & B_CACHE) != 0 && bp->b_dirtyoff < right_on) {
361
/*
362
* Flush the dirty buffer, because we don't have a
363
* byte-granular way to record which parts of the
364
* buffer are valid.
365
*/
366
bwrite(bp);
367
if (bp->b_error)
368
return (bp->b_error);
369
} else {
370
brelse(bp);
371
}
372
}
373
374
v_inval_buf_range(vp, left_lbn, end_lbn, iosize);
375
return (0);
376
}
377
378
/* Send FUSE_IOCTL for this node */
379
static int
380
fuse_vnop_do_ioctl(struct vnode *vp, u_long cmd, void *arg, int fflag,
381
struct ucred *cred, struct thread *td)
382
{
383
struct fuse_dispatcher fdi;
384
struct fuse_ioctl_in *fii;
385
struct fuse_ioctl_out *fio;
386
struct fuse_filehandle *fufh;
387
uint32_t flags = 0;
388
uint32_t insize = 0;
389
uint32_t outsize = 0;
390
int err;
391
392
err = fuse_filehandle_getrw(vp, fflag, &fufh, cred, td->td_proc->p_pid);
393
if (err != 0)
394
return (err);
395
396
if (vnode_isdir(vp)) {
397
struct fuse_data *data = fuse_get_mpdata(vnode_mount(vp));
398
399
if (!fuse_libabi_geq(data, 7, 18))
400
return (ENOTTY);
401
flags |= FUSE_IOCTL_DIR;
402
}
403
#ifdef __LP64__
404
#ifdef COMPAT_FREEBSD32
405
if (SV_PROC_FLAG(td->td_proc, SV_ILP32))
406
flags |= FUSE_IOCTL_32BIT;
407
#endif
408
#else /* !defined(__LP64__) */
409
flags |= FUSE_IOCTL_32BIT;
410
#endif
411
412
if ((cmd & IOC_OUT) != 0)
413
outsize = IOCPARM_LEN(cmd);
414
/* _IOWINT() sets IOC_VOID */
415
if ((cmd & (IOC_VOID | IOC_IN)) != 0)
416
insize = IOCPARM_LEN(cmd);
417
418
fdisp_init(&fdi, sizeof(*fii) + insize);
419
fdisp_make_vp(&fdi, FUSE_IOCTL, vp, td, cred);
420
fii = fdi.indata;
421
fii->fh = fufh->fh_id;
422
fii->flags = flags;
423
fii->cmd = cmd;
424
fii->arg = (uintptr_t)arg;
425
fii->in_size = insize;
426
fii->out_size = outsize;
427
if (insize > 0)
428
memcpy((char *)fii + sizeof(*fii), arg, insize);
429
430
err = fdisp_wait_answ(&fdi);
431
if (err != 0) {
432
if (err == ENOSYS)
433
err = ENOTTY;
434
goto out;
435
}
436
437
fio = fdi.answ;
438
if (fdi.iosize > sizeof(*fio)) {
439
size_t realoutsize = fdi.iosize - sizeof(*fio);
440
441
if (realoutsize > outsize) {
442
err = EIO;
443
goto out;
444
}
445
memcpy(arg, (char *)fio + sizeof(*fio), realoutsize);
446
}
447
if (fio->result > 0)
448
td->td_retval[0] = fio->result;
449
else
450
err = -fio->result;
451
452
out:
453
fdisp_destroy(&fdi);
454
return (err);
455
}
456
457
/* Send FUSE_LSEEK for this node */
458
static int
459
fuse_vnop_do_lseek(struct vnode *vp, struct thread *td, struct ucred *cred,
460
pid_t pid, off_t *offp, int whence)
461
{
462
struct fuse_dispatcher fdi;
463
struct fuse_filehandle *fufh;
464
struct fuse_lseek_in *flsi;
465
struct fuse_lseek_out *flso;
466
struct mount *mp = vnode_mount(vp);
467
int err;
468
469
ASSERT_VOP_LOCKED(vp, __func__);
470
471
err = fuse_filehandle_getrw(vp, FREAD, &fufh, cred, pid);
472
if (err)
473
return (err);
474
fdisp_init(&fdi, sizeof(*flsi));
475
fdisp_make_vp(&fdi, FUSE_LSEEK, vp, td, cred);
476
flsi = fdi.indata;
477
flsi->fh = fufh->fh_id;
478
flsi->offset = *offp;
479
flsi->whence = whence;
480
err = fdisp_wait_answ(&fdi);
481
if (err == ENOSYS) {
482
fsess_set_notimpl(mp, FUSE_LSEEK);
483
} else if (err == ENXIO) {
484
/* Note: ENXIO means "no more hole/data regions until EOF" */
485
fsess_set_impl(mp, FUSE_LSEEK);
486
} else if (err == 0) {
487
fsess_set_impl(mp, FUSE_LSEEK);
488
flso = fdi.answ;
489
*offp = flso->offset;
490
}
491
fdisp_destroy(&fdi);
492
493
return (err);
494
}
495
496
/*
497
struct vnop_access_args {
498
struct vnode *a_vp;
499
#if VOP_ACCESS_TAKES_ACCMODE_T
500
accmode_t a_accmode;
501
#else
502
int a_mode;
503
#endif
504
struct ucred *a_cred;
505
struct thread *a_td;
506
};
507
*/
508
static int
509
fuse_vnop_access(struct vop_access_args *ap)
510
{
511
struct vnode *vp = ap->a_vp;
512
int accmode = ap->a_accmode;
513
struct ucred *cred = ap->a_cred;
514
515
struct fuse_data *data = fuse_get_mpdata(vnode_mount(vp));
516
517
int err;
518
519
if (fuse_isdeadfs(vp)) {
520
if (vnode_isvroot(vp)) {
521
return 0;
522
}
523
return (EXTERROR(ENXIO, "This FUSE session is about "
524
"to be closed"));
525
}
526
if (!(data->dataflags & FSESS_INITED)) {
527
if (vnode_isvroot(vp)) {
528
if (priv_check_cred(cred, PRIV_VFS_ADMIN) ||
529
(fuse_match_cred(data->daemoncred, cred) == 0)) {
530
return 0;
531
}
532
}
533
return (EXTERROR(EBADF, "Access denied until FUSE session "
534
"is initialized"));
535
}
536
if (vnode_islnk(vp)) {
537
return 0;
538
}
539
540
err = fuse_internal_access(vp, accmode, ap->a_td, ap->a_cred);
541
return err;
542
}
543
544
/*
545
* struct vop_advlock_args {
546
* struct vop_generic_args a_gen;
547
* struct vnode *a_vp;
548
* void *a_id;
549
* int a_op;
550
* struct flock *a_fl;
551
* int a_flags;
552
* }
553
*/
554
static int
555
fuse_vnop_advlock(struct vop_advlock_args *ap)
556
{
557
struct vnode *vp = ap->a_vp;
558
struct flock *fl = ap->a_fl;
559
struct thread *td = curthread;
560
struct ucred *cred = td->td_ucred;
561
pid_t pid = td->td_proc->p_pid;
562
struct fuse_filehandle *fufh;
563
struct fuse_dispatcher fdi;
564
struct fuse_lk_in *fli;
565
struct fuse_lk_out *flo;
566
struct vattr vattr;
567
enum fuse_opcode op;
568
off_t size, start;
569
int dataflags, err;
570
int flags = ap->a_flags;
571
572
dataflags = fuse_get_mpdata(vnode_mount(vp))->dataflags;
573
574
if (fuse_isdeadfs(vp)) {
575
return (EXTERROR(ENXIO, "This FUSE session is about "
576
"to be closed"));
577
}
578
579
switch(ap->a_op) {
580
case F_GETLK:
581
op = FUSE_GETLK;
582
break;
583
case F_SETLK:
584
if (flags & F_WAIT)
585
op = FUSE_SETLKW;
586
else
587
op = FUSE_SETLK;
588
break;
589
case F_UNLCK:
590
op = FUSE_SETLK;
591
break;
592
default:
593
return (EXTERROR(EINVAL, "Unsupported lock flags"));
594
}
595
596
if (!(dataflags & FSESS_POSIX_LOCKS))
597
return vop_stdadvlock(ap);
598
/* FUSE doesn't properly support flock until protocol 7.17 */
599
if (flags & F_FLOCK)
600
return vop_stdadvlock(ap);
601
602
vn_lock(vp, LK_SHARED | LK_RETRY);
603
604
switch (fl->l_whence) {
605
case SEEK_SET:
606
case SEEK_CUR:
607
/*
608
* Caller is responsible for adding any necessary offset
609
* when SEEK_CUR is used.
610
*/
611
start = fl->l_start;
612
break;
613
614
case SEEK_END:
615
err = fuse_internal_getattr(vp, &vattr, cred, td);
616
if (err)
617
goto out;
618
size = vattr.va_size;
619
if (size > OFF_MAX ||
620
(fl->l_start > 0 && size > OFF_MAX - fl->l_start)) {
621
err = EXTERROR(EOVERFLOW, "Offset is too large");
622
goto out;
623
}
624
start = size + fl->l_start;
625
break;
626
627
default:
628
return (EXTERROR(EINVAL, "Unsupported offset type"));
629
}
630
631
err = fuse_filehandle_get_anyflags(vp, &fufh, cred, pid);
632
if (err)
633
goto out;
634
635
fdisp_init(&fdi, sizeof(*fli));
636
637
fdisp_make_vp(&fdi, op, vp, td, cred);
638
fli = fdi.indata;
639
fli->fh = fufh->fh_id;
640
fli->owner = td->td_proc->p_pid;
641
fli->lk.start = start;
642
if (fl->l_len != 0)
643
fli->lk.end = start + fl->l_len - 1;
644
else
645
fli->lk.end = INT64_MAX;
646
fli->lk.type = fl->l_type;
647
fli->lk.pid = td->td_proc->p_pid;
648
649
err = fdisp_wait_answ(&fdi);
650
fdisp_destroy(&fdi);
651
652
if (err == 0 && op == FUSE_GETLK) {
653
flo = fdi.answ;
654
fl->l_type = flo->lk.type;
655
fl->l_whence = SEEK_SET;
656
if (flo->lk.type != F_UNLCK) {
657
fl->l_pid = flo->lk.pid;
658
fl->l_start = flo->lk.start;
659
if (flo->lk.end == INT64_MAX)
660
fl->l_len = 0;
661
else
662
fl->l_len = flo->lk.end - flo->lk.start + 1;
663
fl->l_start = flo->lk.start;
664
}
665
}
666
667
out:
668
VOP_UNLOCK(vp);
669
return err;
670
}
671
672
static int
673
fuse_vnop_allocate(struct vop_allocate_args *ap)
674
{
675
struct vnode *vp = ap->a_vp;
676
off_t *len = ap->a_len;
677
off_t *offset = ap->a_offset;
678
struct ucred *cred = ap->a_cred;
679
struct fuse_filehandle *fufh;
680
struct mount *mp = vnode_mount(vp);
681
struct fuse_dispatcher fdi;
682
struct fuse_fallocate_in *ffi;
683
struct uio io;
684
pid_t pid = curthread->td_proc->p_pid;
685
struct fuse_vnode_data *fvdat = VTOFUD(vp);
686
off_t filesize;
687
int err;
688
689
if (fuse_isdeadfs(vp))
690
return (EXTERROR(ENXIO, "This FUSE session is about "
691
"to be closed"));
692
693
switch (vp->v_type) {
694
case VFIFO:
695
return (ESPIPE);
696
case VLNK:
697
case VREG:
698
break;
699
default:
700
return (ENODEV);
701
}
702
703
if (vfs_isrdonly(mp))
704
return (EROFS);
705
706
if (fsess_not_impl(mp, FUSE_FALLOCATE))
707
return (EXTERROR(EOPNOTSUPP, "This server does not implement "
708
"FUSE_FALLOCATE"));
709
710
io.uio_offset = *offset;
711
io.uio_resid = *len;
712
err = vn_rlimit_fsize(vp, &io, curthread);
713
if (err)
714
return (err);
715
716
err = fuse_filehandle_getrw(vp, FWRITE, &fufh, cred, pid);
717
if (err)
718
return (err);
719
720
fuse_vnode_update(vp, FN_MTIMECHANGE | FN_CTIMECHANGE);
721
722
err = fuse_vnode_size(vp, &filesize, cred, curthread);
723
if (err)
724
return (err);
725
fuse_inval_buf_range(vp, filesize, *offset, *offset + *len);
726
727
fdisp_init(&fdi, sizeof(*ffi));
728
fdisp_make_vp(&fdi, FUSE_FALLOCATE, vp, curthread, cred);
729
ffi = fdi.indata;
730
ffi->fh = fufh->fh_id;
731
ffi->offset = *offset;
732
ffi->length = *len;
733
ffi->mode = 0;
734
err = fdisp_wait_answ(&fdi);
735
736
if (err == ENOSYS) {
737
fsess_set_notimpl(mp, FUSE_FALLOCATE);
738
err = EXTERROR(EOPNOTSUPP, "This server does not implement "
739
"FUSE_ALLOCATE");
740
} else if (err == EOPNOTSUPP) {
741
/*
742
* The file system server does not support FUSE_FALLOCATE with
743
* the supplied mode for this particular file.
744
*/
745
err = EXTERROR(EOPNOTSUPP, "This file can't be pre-allocated");
746
} else if (!err) {
747
*offset += *len;
748
*len = 0;
749
fuse_vnode_undirty_cached_timestamps(vp, false);
750
fuse_internal_clear_suid_on_write(vp, cred, curthread);
751
if (*offset > fvdat->cached_attrs.va_size) {
752
fuse_vnode_setsize(vp, *offset, false);
753
getnanouptime(&fvdat->last_local_modify);
754
}
755
}
756
757
fdisp_destroy(&fdi);
758
return (err);
759
}
760
761
/* {
762
struct vnode *a_vp;
763
daddr_t a_bn;
764
struct bufobj **a_bop;
765
daddr_t *a_bnp;
766
int *a_runp;
767
int *a_runb;
768
} */
769
static int
770
fuse_vnop_bmap(struct vop_bmap_args *ap)
771
{
772
struct vnode *vp = ap->a_vp;
773
struct bufobj **bo = ap->a_bop;
774
struct thread *td = curthread;
775
struct mount *mp;
776
struct fuse_dispatcher fdi;
777
struct fuse_bmap_in *fbi;
778
struct fuse_bmap_out *fbo;
779
struct fuse_data *data;
780
struct fuse_vnode_data *fvdat = VTOFUD(vp);
781
uint64_t biosize;
782
off_t fsize;
783
daddr_t lbn = ap->a_bn;
784
daddr_t *pbn = ap->a_bnp;
785
int *runp = ap->a_runp;
786
int *runb = ap->a_runb;
787
int error = 0;
788
int maxrun;
789
790
if (fuse_isdeadfs(vp)) {
791
return (EXTERROR(ENXIO, "This FUSE session is about "
792
"to be closed"));
793
}
794
795
mp = vnode_mount(vp);
796
data = fuse_get_mpdata(mp);
797
biosize = fuse_iosize(vp);
798
maxrun = MIN(vp->v_mount->mnt_iosize_max / biosize - 1,
799
data->max_readahead_blocks);
800
801
if (bo != NULL)
802
*bo = &vp->v_bufobj;
803
804
/*
805
* The FUSE_BMAP operation does not include the runp and runb
806
* variables, so we must guess. Report nonzero contiguous runs so
807
* cluster_read will combine adjacent reads. It's worthwhile to reduce
808
* upcalls even if we don't know the true physical layout of the file.
809
*
810
* FUSE file systems may opt out of read clustering in two ways:
811
* * mounting with -onoclusterr
812
* * Setting max_readahead <= maxbcachebuf during FUSE_INIT
813
*/
814
if (runb != NULL)
815
*runb = MIN(lbn, maxrun);
816
if (runp != NULL && maxrun == 0)
817
*runp = 0;
818
else if (runp != NULL) {
819
/*
820
* If the file's size is cached, use that value to calculate
821
* runp, even if the cache is expired. runp is only advisory,
822
* and the risk of getting it wrong is not worth the cost of
823
* another upcall.
824
*/
825
if (fvdat->cached_attrs.va_size != VNOVAL)
826
fsize = fvdat->cached_attrs.va_size;
827
else
828
error = fuse_vnode_size(vp, &fsize, td->td_ucred, td);
829
if (error == 0)
830
*runp = MIN(MAX(0, fsize / (off_t)biosize - lbn - 1),
831
maxrun);
832
else
833
*runp = 0;
834
}
835
836
if (fsess_maybe_impl(mp, FUSE_BMAP)) {
837
fdisp_init(&fdi, sizeof(*fbi));
838
fdisp_make_vp(&fdi, FUSE_BMAP, vp, td, td->td_ucred);
839
fbi = fdi.indata;
840
fbi->block = lbn;
841
fbi->blocksize = biosize;
842
error = fdisp_wait_answ(&fdi);
843
if (error == ENOSYS) {
844
fdisp_destroy(&fdi);
845
fsess_set_notimpl(mp, FUSE_BMAP);
846
error = 0;
847
} else {
848
fbo = fdi.answ;
849
if (error == 0 && pbn != NULL)
850
*pbn = fbo->block;
851
fdisp_destroy(&fdi);
852
return error;
853
}
854
}
855
856
/* If the daemon doesn't support BMAP, make up a sensible default */
857
if (pbn != NULL)
858
*pbn = lbn * btodb(biosize);
859
return (error);
860
}
861
862
/*
863
struct vop_close_args {
864
struct vnode *a_vp;
865
int a_fflag;
866
struct ucred *a_cred;
867
struct thread *a_td;
868
};
869
*/
870
static int
871
fuse_vnop_close(struct vop_close_args *ap)
872
{
873
struct vnode *vp = ap->a_vp;
874
struct mount *mp = vnode_mount(vp);
875
struct ucred *cred = ap->a_cred;
876
int fflag = ap->a_fflag;
877
struct thread *td;
878
struct fuse_vnode_data *fvdat = VTOFUD(vp);
879
pid_t pid;
880
int err = 0;
881
882
/* NB: a_td will be NULL from some async kernel contexts */
883
td = ap->a_td ? ap->a_td : curthread;
884
pid = td->td_proc->p_pid;
885
886
if (fuse_isdeadfs(vp))
887
return 0;
888
if (vnode_isdir(vp))
889
return 0;
890
if (fflag & IO_NDELAY)
891
return 0;
892
893
if (cred == NULL)
894
cred = td->td_ucred;
895
896
err = fuse_flush(vp, cred, pid, fflag);
897
if (err == 0 && (fvdat->flag & FN_ATIMECHANGE) && !vfs_isrdonly(mp)) {
898
struct vattr vap;
899
struct fuse_data *data;
900
int dataflags;
901
int access_e = 0;
902
903
data = fuse_get_mpdata(mp);
904
dataflags = data->dataflags;
905
if (dataflags & FSESS_DEFAULT_PERMISSIONS) {
906
struct vattr va;
907
908
fuse_internal_getattr(vp, &va, cred, td);
909
access_e = vaccess(vp->v_type, va.va_mode, va.va_uid,
910
va.va_gid, VWRITE, cred);
911
}
912
if (access_e == 0) {
913
VATTR_NULL(&vap);
914
vap.va_atime = fvdat->cached_attrs.va_atime;
915
/*
916
* Ignore errors setting when setting atime. That
917
* should not cause close(2) to fail.
918
*/
919
fuse_internal_setattr(vp, &vap, td, NULL);
920
}
921
}
922
/* TODO: close the file handle, if we're sure it's no longer used */
923
if ((fvdat->flag & FN_SIZECHANGE) != 0) {
924
fuse_vnode_savesize(vp, cred, pid);
925
}
926
return err;
927
}
928
929
/*
930
struct vop_copy_file_range_args {
931
struct vop_generic_args a_gen;
932
struct vnode *a_invp;
933
off_t *a_inoffp;
934
struct vnode *a_outvp;
935
off_t *a_outoffp;
936
size_t *a_lenp;
937
unsigned int a_flags;
938
struct ucred *a_incred;
939
struct ucred *a_outcred;
940
struct thread *a_fsizetd;
941
}
942
*/
943
static int
944
fuse_vnop_copy_file_range(struct vop_copy_file_range_args *ap)
945
{
946
struct vnode *invp = ap->a_invp;
947
struct vnode *outvp = ap->a_outvp;
948
struct mount *mp = vnode_mount(invp);
949
struct fuse_vnode_data *outfvdat = VTOFUD(outvp);
950
struct fuse_dispatcher fdi;
951
struct fuse_filehandle *infufh, *outfufh;
952
struct fuse_copy_file_range_in *fcfri;
953
struct ucred *incred = ap->a_incred;
954
struct ucred *outcred = ap->a_outcred;
955
struct fuse_write_out *fwo;
956
struct thread *td;
957
struct uio io;
958
off_t outfilesize;
959
ssize_t r = 0;
960
pid_t pid;
961
int err;
962
963
if ((ap->a_flags & COPY_FILE_RANGE_CLONE) != 0)
964
return (EXTERROR(ENOSYS, "Cannot clone"));
965
966
if (mp == NULL || mp != vnode_mount(outvp))
967
return (EXTERROR(ENOSYS, "Mount points do not match"));
968
969
if (incred->cr_uid != outcred->cr_uid)
970
return (EXTERROR(ENOSYS, "FUSE_COPY_FILE_RANGE does not "
971
"support different credentials for infd and outfd"));
972
973
if (incred->cr_gid != outcred->cr_gid)
974
return (EXTERROR(ENOSYS, "FUSE_COPY_FILE_RANGE does not "
975
"support different credentials for infd and outfd"));
976
977
/* Caller busied mp, mnt_data can be safely accessed. */
978
if (fsess_not_impl(mp, FUSE_COPY_FILE_RANGE))
979
return (EXTERROR(ENOSYS, "This daemon does not "
980
"implement COPY_FILE_RANGE"));
981
982
if (ap->a_fsizetd == NULL)
983
td = curthread;
984
else
985
td = ap->a_fsizetd;
986
pid = td->td_proc->p_pid;
987
988
vn_lock_pair(invp, false, LK_SHARED, outvp, false, LK_EXCLUSIVE);
989
if (invp->v_data == NULL || outvp->v_data == NULL) {
990
err = EXTERROR(EBADF, "vnode got reclaimed");
991
goto unlock;
992
}
993
994
err = fuse_filehandle_getrw(invp, FREAD, &infufh, incred, pid);
995
if (err)
996
goto unlock;
997
998
err = fuse_filehandle_getrw(outvp, FWRITE, &outfufh, outcred, pid);
999
if (err)
1000
goto unlock;
1001
1002
io.uio_resid = *ap->a_lenp;
1003
if (ap->a_fsizetd) {
1004
io.uio_offset = *ap->a_outoffp;
1005
err = vn_rlimit_fsizex(outvp, &io, 0, &r, ap->a_fsizetd);
1006
if (err != 0)
1007
goto unlock;
1008
}
1009
1010
err = fuse_vnode_size(outvp, &outfilesize, outcred, curthread);
1011
if (err)
1012
goto unlock;
1013
1014
vnode_pager_clean_sync(invp);
1015
err = fuse_inval_buf_range(outvp, outfilesize, *ap->a_outoffp,
1016
*ap->a_outoffp + io.uio_resid);
1017
if (err)
1018
goto unlock;
1019
1020
fdisp_init(&fdi, sizeof(*fcfri));
1021
fdisp_make_vp(&fdi, FUSE_COPY_FILE_RANGE, invp, td, incred);
1022
fcfri = fdi.indata;
1023
fcfri->fh_in = infufh->fh_id;
1024
fcfri->off_in = *ap->a_inoffp;
1025
fcfri->nodeid_out = VTOI(outvp);
1026
fcfri->fh_out = outfufh->fh_id;
1027
fcfri->off_out = *ap->a_outoffp;
1028
fcfri->len = io.uio_resid;
1029
fcfri->flags = 0;
1030
1031
err = fdisp_wait_answ(&fdi);
1032
if (err == 0) {
1033
fwo = fdi.answ;
1034
*ap->a_lenp = fwo->size;
1035
*ap->a_inoffp += fwo->size;
1036
*ap->a_outoffp += fwo->size;
1037
fuse_internal_clear_suid_on_write(outvp, outcred, td);
1038
if (*ap->a_outoffp > outfvdat->cached_attrs.va_size) {
1039
fuse_vnode_setsize(outvp, *ap->a_outoffp, false);
1040
getnanouptime(&outfvdat->last_local_modify);
1041
}
1042
fuse_vnode_update(invp, FN_ATIMECHANGE);
1043
fuse_vnode_update(outvp, FN_MTIMECHANGE | FN_CTIMECHANGE);
1044
}
1045
fdisp_destroy(&fdi);
1046
1047
unlock:
1048
if (invp != outvp)
1049
VOP_UNLOCK(invp);
1050
VOP_UNLOCK(outvp);
1051
1052
if (err == ENOSYS)
1053
fsess_set_notimpl(mp, FUSE_COPY_FILE_RANGE);
1054
1055
/*
1056
* No need to call vn_rlimit_fsizex_res before return, since the uio is
1057
* local.
1058
*/
1059
return (err);
1060
}
1061
1062
static void
1063
fdisp_make_mknod_for_fallback(
1064
struct fuse_dispatcher *fdip,
1065
struct componentname *cnp,
1066
struct vnode *dvp,
1067
uint64_t parentnid,
1068
struct thread *td,
1069
struct ucred *cred,
1070
mode_t mode,
1071
enum fuse_opcode *op)
1072
{
1073
struct fuse_mknod_in *fmni;
1074
1075
fdisp_init(fdip, sizeof(*fmni) + cnp->cn_namelen + 1);
1076
*op = FUSE_MKNOD;
1077
fdisp_make(fdip, *op, vnode_mount(dvp), parentnid, td, cred);
1078
fmni = fdip->indata;
1079
fmni->mode = mode;
1080
fmni->rdev = 0;
1081
memcpy((char *)fdip->indata + sizeof(*fmni), cnp->cn_nameptr,
1082
cnp->cn_namelen);
1083
((char *)fdip->indata)[sizeof(*fmni) + cnp->cn_namelen] = '\0';
1084
}
1085
/*
1086
struct vnop_create_args {
1087
struct vnode *a_dvp;
1088
struct vnode **a_vpp;
1089
struct componentname *a_cnp;
1090
struct vattr *a_vap;
1091
};
1092
*/
1093
static int
1094
fuse_vnop_create(struct vop_create_args *ap)
1095
{
1096
struct vnode *dvp = ap->a_dvp;
1097
struct vnode **vpp = ap->a_vpp;
1098
struct componentname *cnp = ap->a_cnp;
1099
struct vattr *vap = ap->a_vap;
1100
struct thread *td = curthread;
1101
struct ucred *cred = cnp->cn_cred;
1102
1103
struct fuse_data *data;
1104
struct fuse_create_in *fci;
1105
struct fuse_entry_out *feo;
1106
struct fuse_open_out *foo;
1107
struct fuse_dispatcher fdi, fdi2;
1108
struct fuse_dispatcher *fdip = &fdi;
1109
struct fuse_dispatcher *fdip2 = NULL;
1110
1111
int err;
1112
1113
struct mount *mp = vnode_mount(dvp);
1114
data = fuse_get_mpdata(mp);
1115
uint64_t parentnid = VTOFUD(dvp)->nid;
1116
mode_t mode = MAKEIMODE(vap->va_type, vap->va_mode);
1117
enum fuse_opcode op;
1118
int flags;
1119
1120
if (fuse_isdeadfs(dvp))
1121
return (EXTERROR(ENXIO, "This FUSE session is about "
1122
"to be closed"));
1123
1124
/* FUSE expects sockets to be created with FUSE_MKNOD */
1125
if (vap->va_type == VSOCK)
1126
return fuse_internal_mknod(dvp, vpp, cnp, vap);
1127
1128
/*
1129
* VOP_CREATE doesn't tell us the open(2) flags, so we guess. Only a
1130
* writable mode makes sense, and we might as well include readability
1131
* too.
1132
*/
1133
flags = O_RDWR;
1134
1135
bzero(&fdi, sizeof(fdi));
1136
1137
if (vap->va_type != VREG)
1138
return (EXTERROR(EINVAL, "Only regular files can be created"));
1139
1140
if (fsess_not_impl(mp, FUSE_CREATE) || vap->va_type == VSOCK) {
1141
/* Fallback to FUSE_MKNOD/FUSE_OPEN */
1142
fdisp_make_mknod_for_fallback(fdip, cnp, dvp, parentnid, td,
1143
cred, mode, &op);
1144
} else {
1145
/* Use FUSE_CREATE */
1146
size_t insize;
1147
1148
op = FUSE_CREATE;
1149
fdisp_init(fdip, sizeof(*fci) + cnp->cn_namelen + 1);
1150
fdisp_make(fdip, op, vnode_mount(dvp), parentnid, td, cred);
1151
fci = fdip->indata;
1152
fci->mode = mode;
1153
fci->flags = O_CREAT | flags;
1154
if (fuse_libabi_geq(data, 7, 12)) {
1155
insize = sizeof(*fci);
1156
fci->umask = td->td_proc->p_pd->pd_cmask;
1157
} else {
1158
insize = sizeof(struct fuse_open_in);
1159
}
1160
1161
memcpy((char *)fdip->indata + insize, cnp->cn_nameptr,
1162
cnp->cn_namelen);
1163
((char *)fdip->indata)[insize + cnp->cn_namelen] = '\0';
1164
}
1165
1166
err = fdisp_wait_answ(fdip);
1167
1168
if (err) {
1169
if (err == ENOSYS && op == FUSE_CREATE) {
1170
fsess_set_notimpl(mp, FUSE_CREATE);
1171
fdisp_destroy(fdip);
1172
fdisp_make_mknod_for_fallback(fdip, cnp, dvp,
1173
parentnid, td, cred, mode, &op);
1174
err = fdisp_wait_answ(fdip);
1175
}
1176
if (err)
1177
goto out;
1178
}
1179
1180
feo = fdip->answ;
1181
1182
if ((err = fuse_internal_checkentry(feo, vap->va_type))) {
1183
goto out;
1184
}
1185
1186
if (op == FUSE_CREATE) {
1187
if (fuse_libabi_geq(data, 7, 9))
1188
foo = (struct fuse_open_out*)(feo + 1);
1189
else
1190
foo = (struct fuse_open_out*)((char*)feo +
1191
FUSE_COMPAT_ENTRY_OUT_SIZE);
1192
} else {
1193
/* Issue a separate FUSE_OPEN */
1194
struct fuse_open_in *foi;
1195
1196
fdip2 = &fdi2;
1197
fdisp_init(fdip2, sizeof(*foi));
1198
fdisp_make(fdip2, FUSE_OPEN, vnode_mount(dvp), feo->nodeid, td,
1199
cred);
1200
foi = fdip2->indata;
1201
foi->flags = flags;
1202
err = fdisp_wait_answ(fdip2);
1203
if (err)
1204
goto out;
1205
foo = fdip2->answ;
1206
}
1207
err = fuse_vnode_get(mp, feo, feo->nodeid, dvp, vpp, cnp, vap->va_type);
1208
if (err) {
1209
struct fuse_release_in *fri;
1210
uint64_t nodeid = feo->nodeid;
1211
uint64_t fh_id = foo->fh;
1212
1213
fdisp_destroy(fdip);
1214
fdisp_init(fdip, sizeof(*fri));
1215
fdisp_make(fdip, FUSE_RELEASE, mp, nodeid, td, cred);
1216
fri = fdip->indata;
1217
fri->fh = fh_id;
1218
fri->flags = flags;
1219
fuse_insert_callback(fdip->tick, fuse_internal_forget_callback);
1220
fuse_insert_message(fdip->tick, false);
1221
goto out;
1222
}
1223
ASSERT_VOP_ELOCKED(*vpp, "fuse_vnop_create");
1224
fuse_internal_cache_attrs(*vpp, &feo->attr, feo->attr_valid,
1225
feo->attr_valid_nsec, NULL, true);
1226
1227
fuse_filehandle_init(*vpp, FUFH_RDWR, NULL, td, cred, foo);
1228
fuse_vnode_open(*vpp, foo->open_flags, td);
1229
/*
1230
* Purge the parent's attribute cache because the daemon should've
1231
* updated its mtime and ctime
1232
*/
1233
fuse_vnode_clear_attr_cache(dvp);
1234
cache_purge_negative(dvp);
1235
1236
out:
1237
if (fdip2)
1238
fdisp_destroy(fdip2);
1239
fdisp_destroy(fdip);
1240
return err;
1241
}
1242
1243
/*
1244
struct vnop_fdatasync_args {
1245
struct vop_generic_args a_gen;
1246
struct vnode * a_vp;
1247
struct thread * a_td;
1248
};
1249
*/
1250
static int
1251
fuse_vnop_fdatasync(struct vop_fdatasync_args *ap)
1252
{
1253
struct vnode *vp = ap->a_vp;
1254
struct thread *td = ap->a_td;
1255
int waitfor = MNT_WAIT;
1256
1257
int err = 0;
1258
1259
if (fuse_isdeadfs(vp)) {
1260
return 0;
1261
}
1262
if ((err = vop_stdfdatasync_buf(ap)))
1263
return err;
1264
1265
return fuse_internal_fsync(vp, td, waitfor, true);
1266
}
1267
1268
/*
1269
struct vnop_fsync_args {
1270
struct vop_generic_args a_gen;
1271
struct vnode * a_vp;
1272
int a_waitfor;
1273
struct thread * a_td;
1274
};
1275
*/
1276
static int
1277
fuse_vnop_fsync(struct vop_fsync_args *ap)
1278
{
1279
struct vnode *vp = ap->a_vp;
1280
struct thread *td = ap->a_td;
1281
int waitfor = ap->a_waitfor;
1282
int err = 0;
1283
1284
if (fuse_isdeadfs(vp)) {
1285
return 0;
1286
}
1287
if ((err = vop_stdfsync(ap)))
1288
return err;
1289
1290
return fuse_internal_fsync(vp, td, waitfor, false);
1291
}
1292
1293
/*
1294
struct vnop_getattr_args {
1295
struct vnode *a_vp;
1296
struct vattr *a_vap;
1297
struct ucred *a_cred;
1298
struct thread *a_td;
1299
};
1300
*/
1301
static int
1302
fuse_vnop_getattr(struct vop_getattr_args *ap)
1303
{
1304
struct vnode *vp = ap->a_vp;
1305
struct vattr *vap = ap->a_vap;
1306
struct ucred *cred = ap->a_cred;
1307
struct thread *td = curthread;
1308
int err = 0;
1309
1310
err = fuse_internal_getattr(vp, vap, cred, td);
1311
if (err == ENOTCONN && vnode_isvroot(vp)) {
1312
/*
1313
* We want to seem a legitimate fs even if the daemon is dead,
1314
* so that, eg., we can still do path based unmounting after
1315
* the daemon dies.
1316
*/
1317
err = 0;
1318
bzero(vap, sizeof(*vap));
1319
vap->va_type = vnode_vtype(vp);
1320
}
1321
return err;
1322
}
1323
1324
/*
1325
struct vnop_inactive_args {
1326
struct vnode *a_vp;
1327
};
1328
*/
1329
static int
1330
fuse_vnop_inactive(struct vop_inactive_args *ap)
1331
{
1332
struct vnode *vp = ap->a_vp;
1333
struct thread *td = curthread;
1334
1335
struct fuse_vnode_data *fvdat = VTOFUD(vp);
1336
struct fuse_filehandle *fufh, *fufh_tmp;
1337
1338
int need_flush = 1;
1339
1340
LIST_FOREACH_SAFE(fufh, &fvdat->handles, next, fufh_tmp) {
1341
if (need_flush && vp->v_type == VREG) {
1342
if ((VTOFUD(vp)->flag & FN_SIZECHANGE) != 0) {
1343
fuse_vnode_savesize(vp, NULL, 0);
1344
}
1345
if ((fvdat->flag & FN_REVOKED) != 0)
1346
fuse_io_invalbuf(vp, td);
1347
else
1348
fuse_io_flushbuf(vp, MNT_WAIT, td);
1349
need_flush = 0;
1350
}
1351
fuse_filehandle_close(vp, fufh, td, NULL);
1352
}
1353
1354
if ((fvdat->flag & FN_REVOKED) != 0)
1355
vrecycle(vp);
1356
1357
return 0;
1358
}
1359
1360
/*
1361
struct vnop_ioctl_args {
1362
struct vnode *a_vp;
1363
u_long a_command;
1364
caddr_t a_data;
1365
int a_fflag;
1366
struct ucred *a_cred;
1367
struct thread *a_td;
1368
};
1369
*/
1370
static int
1371
fuse_vnop_ioctl(struct vop_ioctl_args *ap)
1372
{
1373
struct vnode *vp = ap->a_vp;
1374
struct mount *mp = vnode_mount(vp);
1375
struct ucred *cred = ap->a_cred;
1376
struct thread *td = ap->a_td;
1377
int err;
1378
1379
if (fuse_isdeadfs(vp)) {
1380
return (ENXIO);
1381
}
1382
1383
switch (ap->a_command) {
1384
case FIOSEEKDATA:
1385
case FIOSEEKHOLE:
1386
/* Call FUSE_LSEEK, if we can, or fall back to vop_stdioctl */
1387
if (fsess_maybe_impl(mp, FUSE_LSEEK)) {
1388
off_t *offp = ap->a_data;
1389
pid_t pid = td->td_proc->p_pid;
1390
int whence;
1391
1392
if (ap->a_command == FIOSEEKDATA)
1393
whence = SEEK_DATA;
1394
else
1395
whence = SEEK_HOLE;
1396
1397
vn_lock(vp, LK_SHARED | LK_RETRY);
1398
err = fuse_vnop_do_lseek(vp, td, cred, pid, offp,
1399
whence);
1400
VOP_UNLOCK(vp);
1401
}
1402
if (fsess_not_impl(mp, FUSE_LSEEK))
1403
err = vop_stdioctl(ap);
1404
break;
1405
default:
1406
err = fuse_vnop_do_ioctl(vp, ap->a_command, ap->a_data,
1407
ap->a_fflag, cred, td);
1408
break;
1409
}
1410
return (err);
1411
}
1412
1413
1414
/*
1415
struct vnop_link_args {
1416
struct vnode *a_tdvp;
1417
struct vnode *a_vp;
1418
struct componentname *a_cnp;
1419
};
1420
*/
1421
static int
1422
fuse_vnop_link(struct vop_link_args *ap)
1423
{
1424
struct vnode *vp = ap->a_vp;
1425
struct vnode *tdvp = ap->a_tdvp;
1426
struct componentname *cnp = ap->a_cnp;
1427
1428
struct vattr *vap = VTOVA(vp);
1429
1430
struct fuse_dispatcher fdi;
1431
struct fuse_entry_out *feo;
1432
struct fuse_link_in fli;
1433
1434
int err;
1435
1436
if (fuse_isdeadfs(vp)) {
1437
return (EXTERROR(ENXIO, "This FUSE session is about "
1438
"to be closed"));
1439
}
1440
if (vnode_mount(tdvp) != vnode_mount(vp)) {
1441
return (EXDEV);
1442
}
1443
1444
/*
1445
* This is a seatbelt check to protect naive userspace filesystems from
1446
* themselves and the limitations of the FUSE IPC protocol. If a
1447
* filesystem does not allow attribute caching, assume it is capable of
1448
* validating that nlink does not overflow.
1449
*/
1450
if (vap != NULL && vap->va_nlink >= FUSE_LINK_MAX)
1451
return (EMLINK);
1452
fli.oldnodeid = VTOI(vp);
1453
1454
fdisp_init(&fdi, 0);
1455
fuse_internal_newentry_makerequest(vnode_mount(tdvp), VTOI(tdvp), cnp,
1456
FUSE_LINK, &fli, sizeof(fli), &fdi);
1457
if ((err = fdisp_wait_answ(&fdi))) {
1458
goto out;
1459
}
1460
feo = fdi.answ;
1461
1462
if (fli.oldnodeid != feo->nodeid) {
1463
static const char exterr[] = "Server assigned wrong inode "
1464
"for a hard link.";
1465
struct fuse_data *data = fuse_get_mpdata(vnode_mount(vp));
1466
fuse_warn(data, FSESS_WARN_ILLEGAL_INODE, exterr);
1467
fuse_vnode_clear_attr_cache(vp);
1468
fuse_vnode_clear_attr_cache(tdvp);
1469
err = EXTERROR(EIO, exterr);
1470
goto out;
1471
}
1472
1473
err = fuse_internal_checkentry(feo, vnode_vtype(vp));
1474
if (!err) {
1475
/*
1476
* Purge the parent's attribute cache because the daemon
1477
* should've updated its mtime and ctime
1478
*/
1479
fuse_vnode_clear_attr_cache(tdvp);
1480
fuse_internal_cache_attrs(vp, &feo->attr, feo->attr_valid,
1481
feo->attr_valid_nsec, NULL, true);
1482
}
1483
out:
1484
fdisp_destroy(&fdi);
1485
return err;
1486
}
1487
1488
struct fuse_lookup_alloc_arg {
1489
struct fuse_entry_out *feo;
1490
struct componentname *cnp;
1491
uint64_t nid;
1492
__enum_uint8(vtype) vtyp;
1493
};
1494
1495
/* Callback for vn_get_ino */
1496
static int
1497
fuse_lookup_alloc(struct mount *mp, void *arg, int lkflags, struct vnode **vpp)
1498
{
1499
struct fuse_lookup_alloc_arg *flaa = arg;
1500
1501
return fuse_vnode_get(mp, flaa->feo, flaa->nid, NULL, vpp, flaa->cnp,
1502
flaa->vtyp);
1503
}
1504
1505
SDT_PROBE_DEFINE3(fusefs, , vnops, cache_lookup,
1506
"int", "struct timespec*", "struct timespec*");
1507
/*
1508
struct vnop_lookup_args {
1509
struct vnodeop_desc *a_desc;
1510
struct vnode *a_dvp;
1511
struct vnode **a_vpp;
1512
struct componentname *a_cnp;
1513
};
1514
*/
1515
int
1516
fuse_vnop_lookup(struct vop_lookup_args *ap)
1517
{
1518
struct vnode *dvp = ap->a_dvp;
1519
struct vnode **vpp = ap->a_vpp;
1520
struct componentname *cnp = ap->a_cnp;
1521
struct thread *td = curthread;
1522
struct ucred *cred = cnp->cn_cred;
1523
struct timespec now;
1524
1525
int nameiop = cnp->cn_nameiop;
1526
bool isdotdot = cnp->cn_flags & ISDOTDOT;
1527
bool islastcn = cnp->cn_flags & ISLASTCN;
1528
struct mount *mp = vnode_mount(dvp);
1529
struct fuse_data *data = fuse_get_mpdata(mp);
1530
int default_permissions = data->dataflags & FSESS_DEFAULT_PERMISSIONS;
1531
bool is_dot;
1532
1533
int err = 0;
1534
int lookup_err = 0;
1535
struct vnode *vp = NULL;
1536
1537
struct fuse_dispatcher fdi;
1538
bool did_lookup = false;
1539
struct fuse_entry_out *feo = NULL;
1540
__enum_uint8(vtype) vtyp; /* vnode type of target */
1541
1542
uint64_t nid;
1543
1544
if (fuse_isdeadfs(dvp)) {
1545
*vpp = NULL;
1546
return (EXTERROR(ENXIO, "This FUSE session is about "
1547
"to be closed"));
1548
}
1549
if (!vnode_isdir(dvp))
1550
return ENOTDIR;
1551
1552
if (islastcn && vfs_isrdonly(mp) && (nameiop != LOOKUP))
1553
return EROFS;
1554
1555
if ((cnp->cn_flags & NOEXECCHECK) != 0)
1556
cnp->cn_flags &= ~NOEXECCHECK;
1557
else if ((err = fuse_internal_access(dvp, VEXEC, td, cred)))
1558
return err;
1559
1560
is_dot = cnp->cn_namelen == 1 && *(cnp->cn_nameptr) == '.';
1561
if (isdotdot && !(data->dataflags & FSESS_EXPORT_SUPPORT)) {
1562
if (!(VTOFUD(dvp)->flag & FN_PARENT_NID)) {
1563
/*
1564
* Since the file system doesn't support ".." lookups,
1565
* we have no way to find this entry.
1566
*/
1567
return (EXTERROR(ESTALE, "This server does not support "
1568
"'..' lookups"));
1569
}
1570
nid = VTOFUD(dvp)->parent_nid;
1571
if (nid == 0)
1572
return ENOENT;
1573
/* .. is obviously a directory */
1574
vtyp = VDIR;
1575
} else if (is_dot) {
1576
nid = VTOI(dvp);
1577
/* . is obviously a directory */
1578
vtyp = VDIR;
1579
} else {
1580
struct timespec timeout;
1581
int ncpticks; /* here to accommodate for API contract */
1582
1583
err = cache_lookup(dvp, vpp, cnp, &timeout, &ncpticks);
1584
getnanouptime(&now);
1585
SDT_PROBE3(fusefs, , vnops, cache_lookup, err, &timeout, &now);
1586
switch (err) {
1587
case -1: /* positive match */
1588
if (timespeccmp(&timeout, &now, >)) {
1589
counter_u64_add(fuse_lookup_cache_hits, 1);
1590
} else {
1591
/* Cache timeout */
1592
counter_u64_add(fuse_lookup_cache_misses, 1);
1593
bintime_clear(
1594
&VTOFUD(*vpp)->entry_cache_timeout);
1595
cache_purge(*vpp);
1596
if (dvp != *vpp)
1597
vput(*vpp);
1598
else
1599
vrele(*vpp);
1600
*vpp = NULL;
1601
break;
1602
}
1603
return 0;
1604
1605
case 0: /* no match in cache */
1606
counter_u64_add(fuse_lookup_cache_misses, 1);
1607
break;
1608
1609
case ENOENT: /* negative match */
1610
if (timespeccmp(&timeout, &now, <=)) {
1611
/* Cache timeout */
1612
cache_purge_negative(dvp);
1613
break;
1614
}
1615
/* fall through */
1616
default:
1617
return err;
1618
}
1619
1620
fdisp_init(&fdi, cnp->cn_namelen + 1);
1621
fdisp_make(&fdi, FUSE_LOOKUP, mp, VTOI(dvp), td, cred);
1622
1623
memcpy(fdi.indata, cnp->cn_nameptr, cnp->cn_namelen);
1624
((char *)fdi.indata)[cnp->cn_namelen] = '\0';
1625
lookup_err = fdisp_wait_answ(&fdi);
1626
did_lookup = true;
1627
1628
if (!lookup_err) {
1629
/* lookup call succeeded */
1630
feo = (struct fuse_entry_out *)fdi.answ;
1631
nid = feo->nodeid;
1632
if (nid == 0) {
1633
/* zero nodeid means ENOENT and cache it */
1634
struct timespec timeout;
1635
1636
fdi.answ_stat = ENOENT;
1637
lookup_err = ENOENT;
1638
if (cnp->cn_flags & MAKEENTRY) {
1639
fuse_validity_2_timespec(feo, &timeout);
1640
/* Use the same entry_time for .. as for
1641
* the file itself. That doesn't honor
1642
* exactly what the fuse server tells
1643
* us, but to do otherwise would require
1644
* another cache lookup at this point.
1645
*/
1646
struct timespec *dtsp = NULL;
1647
cache_enter_time(dvp, *vpp, cnp,
1648
&timeout, dtsp);
1649
}
1650
}
1651
vtyp = IFTOVT(feo->attr.mode);
1652
}
1653
if (lookup_err && (!fdi.answ_stat || lookup_err != ENOENT)) {
1654
fdisp_destroy(&fdi);
1655
return lookup_err;
1656
}
1657
}
1658
/* lookup_err, if non-zero, must be ENOENT at this point */
1659
1660
if (lookup_err) {
1661
/* Entry not found */
1662
if ((nameiop == CREATE || nameiop == RENAME) && islastcn) {
1663
if (default_permissions)
1664
err = fuse_internal_access(dvp, VWRITE, td,
1665
cred);
1666
else
1667
err = 0;
1668
if (!err) {
1669
err = EJUSTRETURN;
1670
}
1671
} else {
1672
err = ENOENT;
1673
}
1674
} else {
1675
/* Entry was found */
1676
if (isdotdot) {
1677
struct fuse_lookup_alloc_arg flaa;
1678
1679
flaa.nid = nid;
1680
flaa.feo = feo;
1681
flaa.cnp = cnp;
1682
flaa.vtyp = vtyp;
1683
err = vn_vget_ino_gen(dvp, fuse_lookup_alloc, &flaa, 0,
1684
&vp);
1685
*vpp = vp;
1686
} else if (nid == VTOI(dvp)) {
1687
if (is_dot) {
1688
vref(dvp);
1689
*vpp = dvp;
1690
} else {
1691
static const char exterr[] = "Server assigned "
1692
"same inode to both parent and child.";
1693
fuse_warn(fuse_get_mpdata(mp),
1694
FSESS_WARN_ILLEGAL_INODE, exterr);
1695
err = EXTERROR(EIO, exterr);
1696
}
1697
1698
} else {
1699
struct fuse_vnode_data *fvdat;
1700
1701
err = fuse_vnode_get(vnode_mount(dvp), feo, nid, dvp,
1702
&vp, cnp, vtyp);
1703
if (err)
1704
goto out;
1705
*vpp = vp;
1706
fvdat = VTOFUD(vp);
1707
1708
MPASS(feo != NULL);
1709
if (timespeccmp(&now, &fvdat->last_local_modify, >)) {
1710
/*
1711
* Attributes from the server are definitely
1712
* newer than the last attributes we sent to
1713
* the server, so cache them.
1714
*/
1715
fuse_internal_cache_attrs(*vpp, &feo->attr,
1716
feo->attr_valid, feo->attr_valid_nsec,
1717
NULL, true);
1718
}
1719
fuse_validity_2_bintime(feo->entry_valid,
1720
feo->entry_valid_nsec,
1721
&fvdat->entry_cache_timeout);
1722
1723
if ((nameiop == DELETE || nameiop == RENAME) &&
1724
islastcn && default_permissions)
1725
{
1726
struct vattr dvattr;
1727
1728
err = fuse_internal_access(dvp, VWRITE, td,
1729
cred);
1730
if (err != 0)
1731
goto out;
1732
/*
1733
* if the parent's sticky bit is set, check
1734
* whether we're allowed to remove the file.
1735
* Need to figure out the vnode locking to make
1736
* this work.
1737
*/
1738
fuse_internal_getattr(dvp, &dvattr, cred, td);
1739
if ((dvattr.va_mode & S_ISTXT) &&
1740
fuse_internal_access(dvp, VADMIN, td,
1741
cred) &&
1742
fuse_internal_access(*vpp, VADMIN, td,
1743
cred)) {
1744
err = EPERM;
1745
goto out;
1746
}
1747
}
1748
}
1749
}
1750
out:
1751
if (err) {
1752
if (vp != NULL && dvp != vp)
1753
vput(vp);
1754
else if (vp != NULL)
1755
vrele(vp);
1756
*vpp = NULL;
1757
}
1758
if (did_lookup)
1759
fdisp_destroy(&fdi);
1760
1761
return err;
1762
}
1763
1764
/*
1765
struct vnop_mkdir_args {
1766
struct vnode *a_dvp;
1767
struct vnode **a_vpp;
1768
struct componentname *a_cnp;
1769
struct vattr *a_vap;
1770
};
1771
*/
1772
static int
1773
fuse_vnop_mkdir(struct vop_mkdir_args *ap)
1774
{
1775
struct vnode *dvp = ap->a_dvp;
1776
struct vnode **vpp = ap->a_vpp;
1777
struct componentname *cnp = ap->a_cnp;
1778
struct vattr *vap = ap->a_vap;
1779
1780
struct fuse_mkdir_in fmdi;
1781
1782
if (fuse_isdeadfs(dvp)) {
1783
return (EXTERROR(ENXIO, "This FUSE session is about "
1784
"to be closed"));
1785
}
1786
fmdi.mode = MAKEIMODE(vap->va_type, vap->va_mode);
1787
fmdi.umask = curthread->td_proc->p_pd->pd_cmask;
1788
1789
return (fuse_internal_newentry(dvp, vpp, cnp, FUSE_MKDIR, &fmdi,
1790
sizeof(fmdi), VDIR));
1791
}
1792
1793
/*
1794
struct vnop_mknod_args {
1795
struct vnode *a_dvp;
1796
struct vnode **a_vpp;
1797
struct componentname *a_cnp;
1798
struct vattr *a_vap;
1799
};
1800
*/
1801
static int
1802
fuse_vnop_mknod(struct vop_mknod_args *ap)
1803
{
1804
1805
struct vnode *dvp = ap->a_dvp;
1806
struct vnode **vpp = ap->a_vpp;
1807
struct componentname *cnp = ap->a_cnp;
1808
struct vattr *vap = ap->a_vap;
1809
1810
if (fuse_isdeadfs(dvp))
1811
return (EXTERROR(ENXIO, "This FUSE session is about "
1812
"to be closed"));
1813
1814
return fuse_internal_mknod(dvp, vpp, cnp, vap);
1815
}
1816
1817
/*
1818
struct vop_open_args {
1819
struct vnode *a_vp;
1820
int a_mode;
1821
struct ucred *a_cred;
1822
struct thread *a_td;
1823
int a_fdidx; / struct file *a_fp;
1824
};
1825
*/
1826
static int
1827
fuse_vnop_open(struct vop_open_args *ap)
1828
{
1829
struct vnode *vp = ap->a_vp;
1830
int a_mode = ap->a_mode;
1831
struct thread *td = ap->a_td;
1832
struct ucred *cred = ap->a_cred;
1833
pid_t pid = td->td_proc->p_pid;
1834
1835
if (fuse_isdeadfs(vp))
1836
return (EXTERROR(ENXIO, "This FUSE session is about "
1837
"to be closed"));
1838
if (VN_ISDEV(vp) || vp->v_type == VFIFO)
1839
return (EXTERROR(EOPNOTSUPP, "Unsupported vnode type",
1840
vp->v_type));
1841
if ((a_mode & (FREAD | FWRITE | FEXEC)) == 0)
1842
return (EXTERROR(EINVAL, "Illegal mode", a_mode));
1843
1844
if (fuse_filehandle_validrw(vp, a_mode, cred, pid)) {
1845
fuse_vnode_open(vp, 0, td);
1846
return 0;
1847
}
1848
1849
return fuse_filehandle_open(vp, a_mode, NULL, td, cred);
1850
}
1851
1852
static int
1853
fuse_vnop_pathconf(struct vop_pathconf_args *ap)
1854
{
1855
struct vnode *vp = ap->a_vp;
1856
struct mount *mp;
1857
struct fuse_filehandle *fufh;
1858
int err;
1859
bool closefufh = false;
1860
1861
switch (ap->a_name) {
1862
case _PC_FILESIZEBITS:
1863
*ap->a_retval = 64;
1864
return (0);
1865
case _PC_NAME_MAX:
1866
*ap->a_retval = NAME_MAX;
1867
return (0);
1868
case _PC_LINK_MAX:
1869
*ap->a_retval = MIN(LONG_MAX, FUSE_LINK_MAX);
1870
return (0);
1871
case _PC_SYMLINK_MAX:
1872
*ap->a_retval = MAXPATHLEN;
1873
return (0);
1874
case _PC_NO_TRUNC:
1875
*ap->a_retval = 1;
1876
return (0);
1877
case _PC_MIN_HOLE_SIZE:
1878
/*
1879
* The FUSE protocol provides no mechanism for a server to
1880
* report _PC_MIN_HOLE_SIZE. It's a protocol bug. Instead,
1881
* return EINVAL if the server does not support FUSE_LSEEK, or
1882
* 1 if it does.
1883
*/
1884
mp = vnode_mount(vp);
1885
if (!fsess_is_impl(mp, FUSE_LSEEK) &&
1886
!fsess_not_impl(mp, FUSE_LSEEK)) {
1887
off_t offset = 0;
1888
1889
/*
1890
* Issue a FUSE_LSEEK to find out if it's supported.
1891
* Use SEEK_DATA instead of SEEK_HOLE, because the
1892
* latter generally requires sequential scans of file
1893
* metadata, which can be slow.
1894
*/
1895
err = fuse_vnop_do_lseek(vp, curthread,
1896
curthread->td_ucred, curthread->td_proc->p_pid,
1897
&offset, SEEK_DATA);
1898
if (err == EBADF) {
1899
/*
1900
* pathconf() doesn't necessarily open the
1901
* file. So we may need to do it here.
1902
*/
1903
err = fuse_filehandle_open(vp, FREAD, &fufh,
1904
curthread, curthread->td_ucred);
1905
if (err == 0) {
1906
closefufh = true;
1907
err = fuse_vnop_do_lseek(vp, curthread,
1908
curthread->td_ucred,
1909
curthread->td_proc->p_pid, &offset,
1910
SEEK_DATA);
1911
}
1912
if (closefufh)
1913
fuse_filehandle_close(vp, fufh,
1914
curthread, curthread->td_ucred);
1915
}
1916
1917
}
1918
1919
if (fsess_is_impl(mp, FUSE_LSEEK)) {
1920
*ap->a_retval = 1;
1921
return (0);
1922
} else if (fsess_not_impl(mp, FUSE_LSEEK)) {
1923
/* FUSE_LSEEK is not implemented */
1924
return (EXTERROR(EINVAL, "This server does not "
1925
"implement FUSE_LSEEK"));
1926
} else {
1927
return (err);
1928
}
1929
default:
1930
return (vop_stdpathconf(ap));
1931
}
1932
}
1933
1934
SDT_PROBE_DEFINE3(fusefs, , vnops, filehandles_closed, "struct vnode*",
1935
"struct uio*", "struct ucred*");
1936
/*
1937
struct vnop_read_args {
1938
struct vnode *a_vp;
1939
struct uio *a_uio;
1940
int a_ioflag;
1941
struct ucred *a_cred;
1942
};
1943
*/
1944
static int
1945
fuse_vnop_read(struct vop_read_args *ap)
1946
{
1947
struct vnode *vp = ap->a_vp;
1948
struct uio *uio = ap->a_uio;
1949
int ioflag = ap->a_ioflag;
1950
struct ucred *cred = ap->a_cred;
1951
pid_t pid = curthread->td_proc->p_pid;
1952
struct fuse_filehandle *fufh;
1953
int err;
1954
bool closefufh = false, directio;
1955
1956
MPASS(vp->v_type == VREG || vp->v_type == VDIR);
1957
1958
if (fuse_isdeadfs(vp)) {
1959
return (EXTERROR(ENXIO, "This FUSE session is about "
1960
"to be closed"));
1961
}
1962
1963
if (VTOFUD(vp)->flag & FN_DIRECTIO) {
1964
ioflag |= IO_DIRECT;
1965
}
1966
1967
err = fuse_filehandle_getrw(vp, FREAD, &fufh, cred, pid);
1968
if (err == EBADF && vnode_mount(vp)->mnt_flag & MNT_EXPORTED) {
1969
/*
1970
* nfsd will do I/O without first doing VOP_OPEN. We
1971
* must implicitly open the file here
1972
*/
1973
err = fuse_filehandle_open(vp, FREAD, &fufh, curthread, cred);
1974
closefufh = true;
1975
}
1976
if (err) {
1977
SDT_PROBE3(fusefs, , vnops, filehandles_closed, vp, uio, cred);
1978
return err;
1979
}
1980
1981
/*
1982
* Ideally, when the daemon asks for direct io at open time, the
1983
* standard file flag should be set according to this, so that would
1984
* just change the default mode, which later on could be changed via
1985
* fcntl(2).
1986
* But this doesn't work, the O_DIRECT flag gets cleared at some point
1987
* (don't know where). So to make any use of the Fuse direct_io option,
1988
* we hardwire it into the file's private data (similarly to Linux,
1989
* btw.).
1990
*/
1991
directio = (ioflag & IO_DIRECT) || !fsess_opt_datacache(vnode_mount(vp));
1992
1993
fuse_vnode_update(vp, FN_ATIMECHANGE);
1994
if (directio) {
1995
SDT_PROBE2(fusefs, , vnops, trace, 1, "direct read of vnode");
1996
err = fuse_read_directbackend(vp, uio, cred, fufh);
1997
} else {
1998
SDT_PROBE2(fusefs, , vnops, trace, 1, "buffered read of vnode");
1999
err = fuse_read_biobackend(vp, uio, ioflag, cred, fufh, pid);
2000
}
2001
2002
if (closefufh)
2003
fuse_filehandle_close(vp, fufh, curthread, cred);
2004
2005
return (err);
2006
}
2007
2008
/*
2009
struct vnop_readdir_args {
2010
struct vnode *a_vp;
2011
struct uio *a_uio;
2012
struct ucred *a_cred;
2013
int *a_eofflag;
2014
int *a_ncookies;
2015
uint64_t **a_cookies;
2016
};
2017
*/
2018
static int
2019
fuse_vnop_readdir(struct vop_readdir_args *ap)
2020
{
2021
struct vnode *vp = ap->a_vp;
2022
struct uio *uio = ap->a_uio;
2023
struct ucred *cred = ap->a_cred;
2024
struct fuse_filehandle *fufh = NULL;
2025
struct mount *mp = vnode_mount(vp);
2026
struct fuse_iov cookediov;
2027
int err = 0;
2028
uint64_t *cookies;
2029
ssize_t tresid;
2030
int ncookies;
2031
bool closefufh = false;
2032
pid_t pid = curthread->td_proc->p_pid;
2033
2034
if (ap->a_eofflag)
2035
*ap->a_eofflag = 0;
2036
if (fuse_isdeadfs(vp)) {
2037
return (EXTERROR(ENXIO, "This FUSE session is about "
2038
"to be closed"));
2039
}
2040
if (uio_resid(uio) < sizeof(struct dirent))
2041
return (EXTERROR(EINVAL, "Buffer is too small"));
2042
2043
tresid = uio->uio_resid;
2044
err = fuse_filehandle_get_dir(vp, &fufh, cred, pid);
2045
if (err == EBADF && mp->mnt_flag & MNT_EXPORTED) {
2046
KASSERT(!fsess_is_impl(mp, FUSE_OPENDIR),
2047
("FUSE file systems that implement "
2048
"FUSE_OPENDIR should not be exported"));
2049
/*
2050
* nfsd will do VOP_READDIR without first doing VOP_OPEN. We
2051
* must implicitly open the directory here.
2052
*/
2053
err = fuse_filehandle_open(vp, FREAD, &fufh, curthread, cred);
2054
closefufh = true;
2055
}
2056
if (err)
2057
return (err);
2058
if (ap->a_ncookies != NULL) {
2059
ncookies = uio->uio_resid /
2060
(offsetof(struct dirent, d_name) + 4) + 1;
2061
cookies = malloc(ncookies * sizeof(*cookies), M_TEMP, M_WAITOK);
2062
*ap->a_ncookies = ncookies;
2063
*ap->a_cookies = cookies;
2064
} else {
2065
ncookies = 0;
2066
cookies = NULL;
2067
}
2068
#define DIRCOOKEDSIZE FUSE_DIRENT_ALIGN(FUSE_NAME_OFFSET + MAXNAMLEN + 1)
2069
fiov_init(&cookediov, DIRCOOKEDSIZE);
2070
2071
err = fuse_internal_readdir(vp, uio, fufh, &cookediov,
2072
&ncookies, cookies);
2073
2074
fiov_teardown(&cookediov);
2075
if (closefufh)
2076
fuse_filehandle_close(vp, fufh, curthread, cred);
2077
2078
if (ap->a_ncookies != NULL) {
2079
if (err == 0) {
2080
*ap->a_ncookies -= ncookies;
2081
} else {
2082
free(*ap->a_cookies, M_TEMP);
2083
*ap->a_ncookies = 0;
2084
*ap->a_cookies = NULL;
2085
}
2086
}
2087
if (err == 0 && tresid == uio->uio_resid)
2088
*ap->a_eofflag = 1;
2089
2090
return err;
2091
}
2092
2093
/*
2094
struct vnop_readlink_args {
2095
struct vnode *a_vp;
2096
struct uio *a_uio;
2097
struct ucred *a_cred;
2098
};
2099
*/
2100
static int
2101
fuse_vnop_readlink(struct vop_readlink_args *ap)
2102
{
2103
struct vnode *vp = ap->a_vp;
2104
struct uio *uio = ap->a_uio;
2105
struct ucred *cred = ap->a_cred;
2106
2107
struct fuse_dispatcher fdi;
2108
int err;
2109
2110
if (fuse_isdeadfs(vp)) {
2111
return (EXTERROR(ENXIO, "This FUSE session is about "
2112
"to be closed"));
2113
}
2114
if (!vnode_islnk(vp)) {
2115
return EINVAL;
2116
}
2117
fdisp_init(&fdi, 0);
2118
err = fdisp_simple_putget_vp(&fdi, FUSE_READLINK, vp, curthread, cred);
2119
if (err) {
2120
goto out;
2121
}
2122
if (strnlen(fdi.answ, fdi.iosize) + 1 < fdi.iosize) {
2123
static const char exterr[] = "Server returned an embedded NUL "
2124
"from FUSE_READLINK.";
2125
struct fuse_data *data = fuse_get_mpdata(vnode_mount(vp));
2126
fuse_warn(data, FSESS_WARN_READLINK_EMBEDDED_NUL, exterr);
2127
err = EXTERROR(EIO, exterr);
2128
goto out;
2129
}
2130
if (((char *)fdi.answ)[0] == '/' &&
2131
fuse_get_mpdata(vnode_mount(vp))->dataflags & FSESS_PUSH_SYMLINKS_IN) {
2132
char *mpth = vnode_mount(vp)->mnt_stat.f_mntonname;
2133
2134
err = uiomove(mpth, strlen(mpth), uio);
2135
}
2136
if (!err) {
2137
err = uiomove(fdi.answ, fdi.iosize, uio);
2138
}
2139
out:
2140
fdisp_destroy(&fdi);
2141
return err;
2142
}
2143
2144
/*
2145
struct vnop_reclaim_args {
2146
struct vnode *a_vp;
2147
};
2148
*/
2149
static int
2150
fuse_vnop_reclaim(struct vop_reclaim_args *ap)
2151
{
2152
struct vnode *vp = ap->a_vp;
2153
struct thread *td = curthread;
2154
struct fuse_vnode_data *fvdat = VTOFUD(vp);
2155
struct fuse_filehandle *fufh, *fufh_tmp;
2156
2157
if (!fvdat) {
2158
panic("FUSE: no vnode data during recycling");
2159
}
2160
LIST_FOREACH_SAFE(fufh, &fvdat->handles, next, fufh_tmp) {
2161
printf("FUSE: vnode being reclaimed with open fufh "
2162
"(type=%#x)", fufh->fufh_type);
2163
fuse_filehandle_close(vp, fufh, td, NULL);
2164
}
2165
2166
if (VTOI(vp) == 1) {
2167
/*
2168
* Don't send FUSE_FORGET for the root inode, because
2169
* we never send FUSE_LOOKUP for it (see
2170
* fuse_vfsop_root) and we don't want the server to see
2171
* mismatched lookup counts.
2172
*/
2173
struct fuse_data *data;
2174
struct vnode *vroot;
2175
2176
data = fuse_get_mpdata(vnode_mount(vp));
2177
FUSE_LOCK();
2178
vroot = data->vroot;
2179
data->vroot = NULL;
2180
FUSE_UNLOCK();
2181
if (vroot)
2182
vrele(vroot);
2183
} else if (!fuse_isdeadfs(vp) && fvdat->nlookup > 0) {
2184
fuse_internal_forget_send(vnode_mount(vp), td, NULL, VTOI(vp),
2185
fvdat->nlookup);
2186
}
2187
cache_purge(vp);
2188
vfs_hash_remove(vp);
2189
fuse_vnode_destroy(vp);
2190
2191
return 0;
2192
}
2193
2194
/*
2195
struct vnop_remove_args {
2196
struct vnode *a_dvp;
2197
struct vnode *a_vp;
2198
struct componentname *a_cnp;
2199
};
2200
*/
2201
static int
2202
fuse_vnop_remove(struct vop_remove_args *ap)
2203
{
2204
struct vnode *dvp = ap->a_dvp;
2205
struct vnode *vp = ap->a_vp;
2206
struct componentname *cnp = ap->a_cnp;
2207
2208
int err;
2209
2210
if (fuse_isdeadfs(vp)) {
2211
return (EXTERROR(ENXIO, "This FUSE session is about "
2212
"to be closed"));
2213
}
2214
if (vnode_isdir(vp)) {
2215
return (EXTERROR(EPERM, "vnode is a directory"));
2216
}
2217
2218
err = fuse_internal_remove(dvp, vp, cnp, FUSE_UNLINK);
2219
2220
return err;
2221
}
2222
2223
/*
2224
struct vnop_rename_args {
2225
struct vnode *a_fdvp;
2226
struct vnode *a_fvp;
2227
struct componentname *a_fcnp;
2228
struct vnode *a_tdvp;
2229
struct vnode *a_tvp;
2230
struct componentname *a_tcnp;
2231
};
2232
*/
2233
static int
2234
fuse_vnop_rename(struct vop_rename_args *ap)
2235
{
2236
struct vnode *fdvp = ap->a_fdvp;
2237
struct vnode *fvp = ap->a_fvp;
2238
struct componentname *fcnp = ap->a_fcnp;
2239
struct vnode *tdvp = ap->a_tdvp;
2240
struct vnode *tvp = ap->a_tvp;
2241
struct componentname *tcnp = ap->a_tcnp;
2242
struct fuse_data *data;
2243
bool newparent = fdvp != tdvp;
2244
bool isdir = fvp->v_type == VDIR;
2245
int err = 0;
2246
2247
if (fuse_isdeadfs(fdvp)) {
2248
return (EXTERROR(ENXIO, "This FUSE session is about "
2249
"to be closed"));
2250
}
2251
if (fvp->v_mount != tdvp->v_mount ||
2252
(tvp && fvp->v_mount != tvp->v_mount)) {
2253
SDT_PROBE2(fusefs, , vnops, trace, 1, "cross-device rename");
2254
err = EXTERROR(EXDEV, "Cross-device rename");
2255
goto out;
2256
}
2257
cache_purge(fvp);
2258
2259
/*
2260
* FUSE library is expected to check if target directory is not
2261
* under the source directory in the file system tree.
2262
* Linux performs this check at VFS level.
2263
*/
2264
/*
2265
* If source is a directory, and it will get a new parent, user must
2266
* have write permission to it, so ".." can be modified.
2267
*/
2268
data = fuse_get_mpdata(vnode_mount(tdvp));
2269
if (data->dataflags & FSESS_DEFAULT_PERMISSIONS && isdir && newparent) {
2270
err = fuse_internal_access(fvp, VWRITE,
2271
curthread, tcnp->cn_cred);
2272
if (err)
2273
goto out;
2274
}
2275
sx_xlock(&data->rename_lock);
2276
err = fuse_internal_rename(fdvp, fcnp, tdvp, tcnp);
2277
if (err == 0) {
2278
if (tdvp != fdvp)
2279
fuse_vnode_setparent(fvp, tdvp);
2280
if (tvp != NULL)
2281
fuse_vnode_setparent(tvp, NULL);
2282
}
2283
sx_unlock(&data->rename_lock);
2284
2285
if (tvp != NULL && tvp != fvp) {
2286
cache_purge(tvp);
2287
}
2288
if (vnode_isdir(fvp)) {
2289
if (((tvp != NULL) && vnode_isdir(tvp)) || vnode_isdir(fvp)) {
2290
cache_purge(tdvp);
2291
}
2292
cache_purge(fdvp);
2293
}
2294
out:
2295
if (tdvp == tvp) {
2296
vrele(tdvp);
2297
} else {
2298
vput(tdvp);
2299
}
2300
if (tvp != NULL) {
2301
vput(tvp);
2302
}
2303
vrele(fdvp);
2304
vrele(fvp);
2305
2306
return err;
2307
}
2308
2309
/*
2310
struct vnop_rmdir_args {
2311
struct vnode *a_dvp;
2312
struct vnode *a_vp;
2313
struct componentname *a_cnp;
2314
} *ap;
2315
*/
2316
static int
2317
fuse_vnop_rmdir(struct vop_rmdir_args *ap)
2318
{
2319
struct vnode *dvp = ap->a_dvp;
2320
struct vnode *vp = ap->a_vp;
2321
2322
int err;
2323
2324
if (fuse_isdeadfs(vp)) {
2325
return (EXTERROR(ENXIO, "This FUSE session is about "
2326
"to be closed"));
2327
}
2328
if (VTOFUD(vp) == VTOFUD(dvp)) {
2329
return (EXTERROR(EINVAL, "Directory to be removed "
2330
"contains itself"));
2331
}
2332
err = fuse_internal_remove(dvp, vp, ap->a_cnp, FUSE_RMDIR);
2333
2334
return err;
2335
}
2336
2337
/*
2338
struct vnop_setattr_args {
2339
struct vnode *a_vp;
2340
struct vattr *a_vap;
2341
struct ucred *a_cred;
2342
struct thread *a_td;
2343
};
2344
*/
2345
static int
2346
fuse_vnop_setattr(struct vop_setattr_args *ap)
2347
{
2348
struct vnode *vp = ap->a_vp;
2349
struct vattr *vap = ap->a_vap;
2350
struct ucred *cred = ap->a_cred;
2351
struct thread *td = curthread;
2352
struct mount *mp;
2353
struct fuse_data *data;
2354
struct vattr old_va;
2355
int dataflags;
2356
int err = 0, err2;
2357
accmode_t accmode = 0;
2358
bool checkperm;
2359
bool drop_suid = false;
2360
2361
mp = vnode_mount(vp);
2362
data = fuse_get_mpdata(mp);
2363
dataflags = data->dataflags;
2364
checkperm = dataflags & FSESS_DEFAULT_PERMISSIONS;
2365
2366
if (fuse_isdeadfs(vp)) {
2367
return (EXTERROR(ENXIO, "This FUSE session is about "
2368
"to be closed"));
2369
}
2370
2371
if (vap->va_uid != (uid_t)VNOVAL) {
2372
if (checkperm) {
2373
/* Only root may change a file's owner */
2374
err = priv_check_cred(cred, PRIV_VFS_CHOWN);
2375
if (err) {
2376
/* As a special case, allow the null chown */
2377
err2 = fuse_internal_getattr(vp, &old_va, cred,
2378
td);
2379
if (err2)
2380
return (err2);
2381
if (vap->va_uid != old_va.va_uid)
2382
return err;
2383
drop_suid = true;
2384
}
2385
}
2386
accmode |= VADMIN;
2387
}
2388
if (vap->va_gid != (gid_t)VNOVAL) {
2389
if (checkperm && priv_check_cred(cred, PRIV_VFS_CHOWN))
2390
drop_suid = true;
2391
if (checkperm && !groupmember(vap->va_gid, cred)) {
2392
/*
2393
* Non-root users may only chgrp to one of their own
2394
* groups
2395
*/
2396
err = priv_check_cred(cred, PRIV_VFS_CHOWN);
2397
if (err) {
2398
/* As a special case, allow the null chgrp */
2399
err2 = fuse_internal_getattr(vp, &old_va, cred,
2400
td);
2401
if (err2)
2402
return (err2);
2403
if (vap->va_gid != old_va.va_gid)
2404
return err;
2405
}
2406
}
2407
accmode |= VADMIN;
2408
}
2409
if (vap->va_size != VNOVAL) {
2410
switch (vp->v_type) {
2411
case VDIR:
2412
return (EISDIR);
2413
case VLNK:
2414
case VREG:
2415
if (vfs_isrdonly(mp))
2416
return (EROFS);
2417
err = vn_rlimit_trunc(vap->va_size, td);
2418
if (err)
2419
return (err);
2420
break;
2421
default:
2422
/*
2423
* According to POSIX, the result is unspecified
2424
* for file types other than regular files,
2425
* directories and shared memory objects. We
2426
* don't support shared memory objects in the file
2427
* system, and have dubious support for truncating
2428
* symlinks. Just ignore the request in other cases.
2429
*/
2430
return (0);
2431
}
2432
/* Don't set accmode. Permission to trunc is checked upstack */
2433
}
2434
if (vap->va_atime.tv_sec != VNOVAL || vap->va_mtime.tv_sec != VNOVAL) {
2435
if (vap->va_vaflags & VA_UTIMES_NULL)
2436
accmode |= VWRITE;
2437
else
2438
accmode |= VADMIN;
2439
}
2440
if (drop_suid) {
2441
if (vap->va_mode != (mode_t)VNOVAL)
2442
vap->va_mode &= ~(S_ISUID | S_ISGID);
2443
else {
2444
err = fuse_internal_getattr(vp, &old_va, cred, td);
2445
if (err)
2446
return (err);
2447
vap->va_mode = old_va.va_mode & ~(S_ISUID | S_ISGID);
2448
}
2449
}
2450
if (vap->va_mode != (mode_t)VNOVAL) {
2451
/* Only root may set the sticky bit on non-directories */
2452
if (checkperm && vp->v_type != VDIR && (vap->va_mode & S_ISTXT)
2453
&& priv_check_cred(cred, PRIV_VFS_STICKYFILE))
2454
return EFTYPE;
2455
if (checkperm && (vap->va_mode & S_ISGID)) {
2456
err = fuse_internal_getattr(vp, &old_va, cred, td);
2457
if (err)
2458
return (err);
2459
if (!groupmember(old_va.va_gid, cred)) {
2460
err = priv_check_cred(cred, PRIV_VFS_SETGID);
2461
if (err)
2462
return (err);
2463
}
2464
}
2465
accmode |= VADMIN;
2466
}
2467
2468
if (vfs_isrdonly(mp))
2469
return EROFS;
2470
2471
if (checkperm) {
2472
err = fuse_internal_access(vp, accmode, td, cred);
2473
} else {
2474
err = 0;
2475
}
2476
if (err)
2477
return err;
2478
else
2479
return fuse_internal_setattr(vp, vap, td, cred);
2480
}
2481
2482
/*
2483
struct vnop_strategy_args {
2484
struct vnode *a_vp;
2485
struct buf *a_bp;
2486
};
2487
*/
2488
static int
2489
fuse_vnop_strategy(struct vop_strategy_args *ap)
2490
{
2491
struct vnode *vp = ap->a_vp;
2492
struct buf *bp = ap->a_bp;
2493
2494
if (!vp || fuse_isdeadfs(vp)) {
2495
bp->b_ioflags |= BIO_ERROR;
2496
bp->b_error = ENXIO;
2497
bufdone(bp);
2498
return 0;
2499
}
2500
2501
/*
2502
* VOP_STRATEGY always returns zero and signals error via bp->b_ioflags.
2503
* fuse_io_strategy sets bp's error fields
2504
*/
2505
(void)fuse_io_strategy(vp, bp);
2506
2507
return 0;
2508
}
2509
2510
/*
2511
struct vnop_symlink_args {
2512
struct vnode *a_dvp;
2513
struct vnode **a_vpp;
2514
struct componentname *a_cnp;
2515
struct vattr *a_vap;
2516
char *a_target;
2517
};
2518
*/
2519
static int
2520
fuse_vnop_symlink(struct vop_symlink_args *ap)
2521
{
2522
struct vnode *dvp = ap->a_dvp;
2523
struct vnode **vpp = ap->a_vpp;
2524
struct componentname *cnp = ap->a_cnp;
2525
const char *target = ap->a_target;
2526
2527
struct fuse_dispatcher fdi;
2528
2529
int err;
2530
size_t len;
2531
2532
if (fuse_isdeadfs(dvp)) {
2533
return (EXTERROR(ENXIO, "This FUSE session is about "
2534
"to be closed"));
2535
}
2536
/*
2537
* Unlike the other creator type calls, here we have to create a message
2538
* where the name of the new entry comes first, and the data describing
2539
* the entry comes second.
2540
* Hence we can't rely on our handy fuse_internal_newentry() routine,
2541
* but put together the message manually and just call the core part.
2542
*/
2543
2544
len = strlen(target) + 1;
2545
fdisp_init(&fdi, len + cnp->cn_namelen + 1);
2546
fdisp_make_vp(&fdi, FUSE_SYMLINK, dvp, curthread, NULL);
2547
2548
memcpy(fdi.indata, cnp->cn_nameptr, cnp->cn_namelen);
2549
((char *)fdi.indata)[cnp->cn_namelen] = '\0';
2550
memcpy((char *)fdi.indata + cnp->cn_namelen + 1, target, len);
2551
2552
err = fuse_internal_newentry_core(dvp, vpp, cnp, VLNK, &fdi);
2553
fdisp_destroy(&fdi);
2554
return err;
2555
}
2556
2557
/*
2558
struct vnop_write_args {
2559
struct vnode *a_vp;
2560
struct uio *a_uio;
2561
int a_ioflag;
2562
struct ucred *a_cred;
2563
};
2564
*/
2565
static int
2566
fuse_vnop_write(struct vop_write_args *ap)
2567
{
2568
struct vnode *vp = ap->a_vp;
2569
struct uio *uio = ap->a_uio;
2570
int ioflag = ap->a_ioflag;
2571
struct ucred *cred = ap->a_cred;
2572
pid_t pid = curthread->td_proc->p_pid;
2573
struct fuse_filehandle *fufh;
2574
int err;
2575
bool closefufh = false, directio;
2576
2577
MPASS(vp->v_type == VREG || vp->v_type == VDIR);
2578
2579
if (fuse_isdeadfs(vp)) {
2580
return (EXTERROR(ENXIO, "This FUSE session is about "
2581
"to be closed"));
2582
}
2583
2584
if (VTOFUD(vp)->flag & FN_DIRECTIO) {
2585
ioflag |= IO_DIRECT;
2586
}
2587
2588
err = fuse_filehandle_getrw(vp, FWRITE, &fufh, cred, pid);
2589
if (err == EBADF && vnode_mount(vp)->mnt_flag & MNT_EXPORTED) {
2590
/*
2591
* nfsd will do I/O without first doing VOP_OPEN. We
2592
* must implicitly open the file here
2593
*/
2594
err = fuse_filehandle_open(vp, FWRITE, &fufh, curthread, cred);
2595
closefufh = true;
2596
}
2597
if (err) {
2598
SDT_PROBE3(fusefs, , vnops, filehandles_closed, vp, uio, cred);
2599
return err;
2600
}
2601
2602
/*
2603
* Ideally, when the daemon asks for direct io at open time, the
2604
* standard file flag should be set according to this, so that would
2605
* just change the default mode, which later on could be changed via
2606
* fcntl(2).
2607
* But this doesn't work, the O_DIRECT flag gets cleared at some point
2608
* (don't know where). So to make any use of the Fuse direct_io option,
2609
* we hardwire it into the file's private data (similarly to Linux,
2610
* btw.).
2611
*/
2612
directio = (ioflag & IO_DIRECT) || !fsess_opt_datacache(vnode_mount(vp));
2613
2614
fuse_vnode_update(vp, FN_MTIMECHANGE | FN_CTIMECHANGE);
2615
if (directio) {
2616
off_t start, end, filesize;
2617
bool pages = (ioflag & IO_VMIO) != 0;
2618
2619
SDT_PROBE2(fusefs, , vnops, trace, 1, "direct write of vnode");
2620
2621
err = fuse_vnode_size(vp, &filesize, cred, curthread);
2622
if (err)
2623
goto out;
2624
2625
start = uio->uio_offset;
2626
end = start + uio->uio_resid;
2627
if (!pages) {
2628
err = fuse_inval_buf_range(vp, filesize, start,
2629
end);
2630
if (err)
2631
goto out;
2632
}
2633
err = fuse_write_directbackend(vp, uio, cred, fufh,
2634
filesize, ioflag, pages);
2635
} else {
2636
SDT_PROBE2(fusefs, , vnops, trace, 1,
2637
"buffered write of vnode");
2638
if (!fsess_opt_writeback(vnode_mount(vp)))
2639
ioflag |= IO_SYNC;
2640
err = fuse_write_biobackend(vp, uio, cred, fufh, ioflag, pid);
2641
}
2642
fuse_internal_clear_suid_on_write(vp, cred, uio->uio_td);
2643
2644
out:
2645
if (closefufh)
2646
fuse_filehandle_close(vp, fufh, curthread, cred);
2647
2648
return (err);
2649
}
2650
2651
static daddr_t
2652
fuse_gbp_getblkno(struct vnode *vp, vm_ooffset_t off)
2653
{
2654
const int biosize = fuse_iosize(vp);
2655
2656
return (off / biosize);
2657
}
2658
2659
static int
2660
fuse_gbp_getblksz(struct vnode *vp, daddr_t lbn, long *blksz)
2661
{
2662
off_t filesize;
2663
int err;
2664
const int biosize = fuse_iosize(vp);
2665
2666
err = fuse_vnode_size(vp, &filesize, NULL, NULL);
2667
if (err) {
2668
/* This will turn into a SIGBUS */
2669
return (EIO);
2670
} else if ((off_t)lbn * biosize >= filesize) {
2671
*blksz = 0;
2672
} else if ((off_t)(lbn + 1) * biosize > filesize) {
2673
*blksz = filesize - (off_t)lbn *biosize;
2674
} else {
2675
*blksz = biosize;
2676
}
2677
return (0);
2678
}
2679
2680
/*
2681
struct vnop_getpages_args {
2682
struct vnode *a_vp;
2683
vm_page_t *a_m;
2684
int a_count;
2685
int a_reqpage;
2686
};
2687
*/
2688
static int
2689
fuse_vnop_getpages(struct vop_getpages_args *ap)
2690
{
2691
struct vnode *vp = ap->a_vp;
2692
2693
if (!fsess_opt_mmap(vnode_mount(vp))) {
2694
SDT_PROBE2(fusefs, , vnops, trace, 1,
2695
"called on non-cacheable vnode??\n");
2696
return (VM_PAGER_ERROR);
2697
}
2698
2699
return (vfs_bio_getpages(vp, ap->a_m, ap->a_count, ap->a_rbehind,
2700
ap->a_rahead, fuse_gbp_getblkno, fuse_gbp_getblksz));
2701
}
2702
2703
static const char extattr_namespace_separator = '.';
2704
2705
/*
2706
struct vop_getextattr_args {
2707
struct vop_generic_args a_gen;
2708
struct vnode *a_vp;
2709
int a_attrnamespace;
2710
const char *a_name;
2711
struct uio *a_uio;
2712
size_t *a_size;
2713
struct ucred *a_cred;
2714
struct thread *a_td;
2715
};
2716
*/
2717
static int
2718
fuse_vnop_getextattr(struct vop_getextattr_args *ap)
2719
{
2720
struct vnode *vp = ap->a_vp;
2721
struct uio *uio = ap->a_uio;
2722
struct fuse_dispatcher fdi;
2723
struct fuse_getxattr_in *get_xattr_in;
2724
struct fuse_getxattr_out *get_xattr_out;
2725
struct mount *mp = vnode_mount(vp);
2726
struct thread *td = ap->a_td;
2727
struct ucred *cred = ap->a_cred;
2728
char *prefix;
2729
char *attr_str;
2730
size_t len;
2731
int err;
2732
2733
if (fuse_isdeadfs(vp))
2734
return (EXTERROR(ENXIO, "This FUSE session is about "
2735
"to be closed"));
2736
2737
if (fsess_not_impl(mp, FUSE_GETXATTR))
2738
return (EXTERROR(EOPNOTSUPP, "This server does not implement "
2739
"extended attributes"));
2740
2741
err = fuse_extattr_check_cred(vp, ap->a_attrnamespace, cred, td, VREAD);
2742
if (err)
2743
return err;
2744
2745
/* Default to looking for user attributes. */
2746
if (ap->a_attrnamespace == EXTATTR_NAMESPACE_SYSTEM)
2747
prefix = EXTATTR_NAMESPACE_SYSTEM_STRING;
2748
else
2749
prefix = EXTATTR_NAMESPACE_USER_STRING;
2750
2751
len = strlen(prefix) + sizeof(extattr_namespace_separator) +
2752
strlen(ap->a_name) + 1;
2753
2754
fdisp_init(&fdi, len + sizeof(*get_xattr_in));
2755
fdisp_make_vp(&fdi, FUSE_GETXATTR, vp, td, cred);
2756
2757
get_xattr_in = fdi.indata;
2758
/*
2759
* Check to see whether we're querying the available size or
2760
* issuing the actual request. If we pass in 0, we get back struct
2761
* fuse_getxattr_out. If we pass in a non-zero size, we get back
2762
* that much data, without the struct fuse_getxattr_out header.
2763
*/
2764
if (uio == NULL)
2765
get_xattr_in->size = 0;
2766
else
2767
get_xattr_in->size = uio->uio_resid;
2768
2769
attr_str = (char *)fdi.indata + sizeof(*get_xattr_in);
2770
snprintf(attr_str, len, "%s%c%s", prefix, extattr_namespace_separator,
2771
ap->a_name);
2772
2773
err = fdisp_wait_answ(&fdi);
2774
if (err != 0) {
2775
if (err == ENOSYS) {
2776
fsess_set_notimpl(mp, FUSE_GETXATTR);
2777
err = (EXTERROR(EOPNOTSUPP, "This server does not "
2778
"implement extended attributes"));
2779
}
2780
goto out;
2781
}
2782
2783
get_xattr_out = fdi.answ;
2784
2785
if (ap->a_size != NULL)
2786
*ap->a_size = get_xattr_out->size;
2787
2788
if (uio != NULL)
2789
err = uiomove(fdi.answ, fdi.iosize, uio);
2790
2791
out:
2792
fdisp_destroy(&fdi);
2793
return (err);
2794
}
2795
2796
/*
2797
struct vop_setextattr_args {
2798
struct vop_generic_args a_gen;
2799
struct vnode *a_vp;
2800
int a_attrnamespace;
2801
const char *a_name;
2802
struct uio *a_uio;
2803
struct ucred *a_cred;
2804
struct thread *a_td;
2805
};
2806
*/
2807
static int
2808
fuse_vnop_setextattr(struct vop_setextattr_args *ap)
2809
{
2810
struct vnode *vp = ap->a_vp;
2811
struct uio *uio = ap->a_uio;
2812
struct fuse_dispatcher fdi;
2813
struct fuse_setxattr_in *set_xattr_in;
2814
struct mount *mp = vnode_mount(vp);
2815
struct thread *td = ap->a_td;
2816
struct ucred *cred = ap->a_cred;
2817
size_t struct_size = FUSE_COMPAT_SETXATTR_IN_SIZE;
2818
char *prefix;
2819
size_t len;
2820
char *attr_str;
2821
int err;
2822
2823
if (fuse_isdeadfs(vp))
2824
return (EXTERROR(ENXIO, "This FUSE session is about "
2825
"to be closed"));
2826
2827
if (fsess_not_impl(mp, FUSE_SETXATTR))
2828
return (EXTERROR(EOPNOTSUPP, "This server does not implement "
2829
"setting extended attributes"));
2830
2831
if (vfs_isrdonly(mp))
2832
return EROFS;
2833
2834
/* Deleting xattrs must use VOP_DELETEEXTATTR instead */
2835
if (ap->a_uio == NULL) {
2836
/*
2837
* If we got here as fallback from VOP_DELETEEXTATTR, then
2838
* return EOPNOTSUPP.
2839
*/
2840
if (fsess_not_impl(mp, FUSE_REMOVEXATTR))
2841
return (EXTERROR(EOPNOTSUPP, "This server does not "
2842
"implement removing extended attributes"));
2843
else
2844
return (EXTERROR(EINVAL, "DELETEEXTATTR should be used "
2845
"to remove extattrs"));
2846
}
2847
2848
err = fuse_extattr_check_cred(vp, ap->a_attrnamespace, cred, td,
2849
VWRITE);
2850
if (err)
2851
return err;
2852
2853
/* Default to looking for user attributes. */
2854
if (ap->a_attrnamespace == EXTATTR_NAMESPACE_SYSTEM)
2855
prefix = EXTATTR_NAMESPACE_SYSTEM_STRING;
2856
else
2857
prefix = EXTATTR_NAMESPACE_USER_STRING;
2858
2859
len = strlen(prefix) + sizeof(extattr_namespace_separator) +
2860
strlen(ap->a_name) + 1;
2861
2862
/* older FUSE servers use a smaller fuse_setxattr_in struct*/
2863
if (fuse_get_mpdata(mp)->dataflags & FSESS_SETXATTR_EXT)
2864
struct_size = sizeof(*set_xattr_in);
2865
2866
fdisp_init(&fdi, len + struct_size + uio->uio_resid);
2867
fdisp_make_vp(&fdi, FUSE_SETXATTR, vp, td, cred);
2868
2869
set_xattr_in = fdi.indata;
2870
set_xattr_in->size = uio->uio_resid;
2871
2872
if (fuse_get_mpdata(mp)->dataflags & FSESS_SETXATTR_EXT) {
2873
set_xattr_in->setxattr_flags = 0;
2874
set_xattr_in->padding = 0;
2875
}
2876
2877
attr_str = (char *)fdi.indata + struct_size;
2878
snprintf(attr_str, len, "%s%c%s", prefix, extattr_namespace_separator,
2879
ap->a_name);
2880
2881
err = uiomove((char *)fdi.indata + struct_size + len,
2882
uio->uio_resid, uio);
2883
if (err != 0) {
2884
goto out;
2885
}
2886
2887
err = fdisp_wait_answ(&fdi);
2888
2889
if (err == ENOSYS) {
2890
fsess_set_notimpl(mp, FUSE_SETXATTR);
2891
err = EXTERROR(EOPNOTSUPP, "This server does not implement "
2892
"setting extended attributes");
2893
}
2894
if (err == ERESTART) {
2895
/* Can't restart after calling uiomove */
2896
err = EINTR;
2897
}
2898
2899
out:
2900
fdisp_destroy(&fdi);
2901
return (err);
2902
}
2903
2904
/*
2905
* The Linux / FUSE extended attribute list is simply a collection of
2906
* NUL-terminated strings. The FreeBSD extended attribute list is a single
2907
* byte length followed by a non-NUL terminated string. So, this allows
2908
* conversion of the Linux / FUSE format to the FreeBSD format in place.
2909
* Linux attribute names are reported with the namespace as a prefix (e.g.
2910
* "user.attribute_name"), but in FreeBSD they are reported without the
2911
* namespace prefix (e.g. "attribute_name"). So, we're going from:
2912
*
2913
* user.attr_name1\0user.attr_name2\0
2914
*
2915
* to:
2916
*
2917
* <num>attr_name1<num>attr_name2
2918
*
2919
* Where "<num>" is a single byte number of characters in the attribute name.
2920
*
2921
* Args:
2922
* prefix - exattr namespace prefix string
2923
* list, list_len - input list with namespace prefixes
2924
* bsd_list, bsd_list_len - output list compatible with bsd vfs
2925
*/
2926
static int
2927
fuse_xattrlist_convert(char *prefix, const char *list, int list_len,
2928
char *bsd_list, int *bsd_list_len)
2929
{
2930
int len, pos, dist_to_next, prefix_len;
2931
2932
pos = 0;
2933
*bsd_list_len = 0;
2934
prefix_len = strlen(prefix);
2935
2936
while (pos < list_len && list[pos] != '\0') {
2937
dist_to_next = strlen(&list[pos]) + 1;
2938
if (bcmp(&list[pos], prefix, prefix_len) == 0 &&
2939
list[pos + prefix_len] == extattr_namespace_separator) {
2940
len = dist_to_next -
2941
(prefix_len + sizeof(extattr_namespace_separator)) - 1;
2942
if (len >= EXTATTR_MAXNAMELEN)
2943
return (ENAMETOOLONG);
2944
2945
bsd_list[*bsd_list_len] = len;
2946
memcpy(&bsd_list[*bsd_list_len + 1],
2947
&list[pos + prefix_len +
2948
sizeof(extattr_namespace_separator)], len);
2949
2950
*bsd_list_len += len + 1;
2951
}
2952
2953
pos += dist_to_next;
2954
}
2955
2956
return (0);
2957
}
2958
2959
/*
2960
* List extended attributes
2961
*
2962
* The FUSE_LISTXATTR operation is based on Linux's listxattr(2) syscall, which
2963
* has a number of differences compared to its FreeBSD equivalent,
2964
* extattr_list_file:
2965
*
2966
* - FUSE_LISTXATTR returns all extended attributes across all namespaces,
2967
* whereas listxattr(2) only returns attributes for a single namespace
2968
* - FUSE_LISTXATTR prepends each attribute name with "namespace."
2969
* - If the provided buffer is not large enough to hold the result,
2970
* FUSE_LISTXATTR should return ERANGE, whereas listxattr is expected to
2971
* return as many results as will fit.
2972
*/
2973
/*
2974
struct vop_listextattr_args {
2975
struct vop_generic_args a_gen;
2976
struct vnode *a_vp;
2977
int a_attrnamespace;
2978
struct uio *a_uio;
2979
size_t *a_size;
2980
struct ucred *a_cred;
2981
struct thread *a_td;
2982
};
2983
*/
2984
static int
2985
fuse_vnop_listextattr(struct vop_listextattr_args *ap)
2986
{
2987
struct vnode *vp = ap->a_vp;
2988
struct uio *uio = ap->a_uio;
2989
struct fuse_dispatcher fdi;
2990
struct fuse_listxattr_in *list_xattr_in;
2991
struct fuse_listxattr_out *list_xattr_out;
2992
struct mount *mp = vnode_mount(vp);
2993
struct thread *td = ap->a_td;
2994
struct ucred *cred = ap->a_cred;
2995
char *prefix;
2996
char *bsd_list = NULL;
2997
char *linux_list;
2998
int bsd_list_len;
2999
int linux_list_len;
3000
int err;
3001
3002
if (fuse_isdeadfs(vp))
3003
return (EXTERROR(ENXIO, "This FUSE session is about "
3004
"to be closed"));
3005
3006
if (fsess_not_impl(mp, FUSE_LISTXATTR))
3007
return (EXTERROR(EOPNOTSUPP, "This server does not implement "
3008
"extended attributes"));
3009
3010
err = fuse_extattr_check_cred(vp, ap->a_attrnamespace, cred, td, VREAD);
3011
if (err)
3012
return err;
3013
3014
/*
3015
* Add space for a NUL and the period separator if enabled.
3016
* Default to looking for user attributes.
3017
*/
3018
if (ap->a_attrnamespace == EXTATTR_NAMESPACE_SYSTEM)
3019
prefix = EXTATTR_NAMESPACE_SYSTEM_STRING;
3020
else
3021
prefix = EXTATTR_NAMESPACE_USER_STRING;
3022
3023
fdisp_init(&fdi, sizeof(*list_xattr_in));
3024
fdisp_make_vp(&fdi, FUSE_LISTXATTR, vp, td, cred);
3025
3026
/*
3027
* Retrieve Linux / FUSE compatible list size.
3028
*/
3029
list_xattr_in = fdi.indata;
3030
list_xattr_in->size = 0;
3031
3032
err = fdisp_wait_answ(&fdi);
3033
if (err != 0) {
3034
if (err == ENOSYS) {
3035
fsess_set_notimpl(mp, FUSE_LISTXATTR);
3036
err = EXTERROR(EOPNOTSUPP, "This server does not "
3037
"implement extended attributes");
3038
}
3039
goto out;
3040
}
3041
3042
list_xattr_out = fdi.answ;
3043
linux_list_len = list_xattr_out->size;
3044
if (linux_list_len == 0) {
3045
if (ap->a_size != NULL)
3046
*ap->a_size = linux_list_len;
3047
goto out;
3048
}
3049
3050
/*
3051
* Retrieve Linux / FUSE compatible list values.
3052
*/
3053
fdisp_refresh_vp(&fdi, FUSE_LISTXATTR, vp, td, cred);
3054
list_xattr_in = fdi.indata;
3055
list_xattr_in->size = linux_list_len;
3056
3057
err = fdisp_wait_answ(&fdi);
3058
if (err == ERANGE) {
3059
/*
3060
* Race detected. The attribute list must've grown since the
3061
* first FUSE_LISTXATTR call. Start over. Go all the way back
3062
* to userland so we can process signals, if necessary, before
3063
* restarting.
3064
*/
3065
err = ERESTART;
3066
goto out;
3067
} else if (err != 0)
3068
goto out;
3069
3070
linux_list = fdi.answ;
3071
/* FUSE doesn't allow the server to return more data than requested */
3072
if (fdi.iosize > linux_list_len) {
3073
struct fuse_data *data = fuse_get_mpdata(mp);
3074
3075
fuse_warn(data, FSESS_WARN_LSEXTATTR_LONG,
3076
"server returned "
3077
"more extended attribute data than requested; "
3078
"should've returned ERANGE instead.");
3079
} else {
3080
/* But returning less data is fine */
3081
linux_list_len = fdi.iosize;
3082
}
3083
3084
/*
3085
* Retrieve the BSD compatible list values.
3086
* The Linux / FUSE attribute list format isn't the same
3087
* as FreeBSD's format. So we need to transform it into
3088
* FreeBSD's format before giving it to the user.
3089
*/
3090
bsd_list = malloc(linux_list_len, M_TEMP, M_WAITOK);
3091
err = fuse_xattrlist_convert(prefix, linux_list, linux_list_len,
3092
bsd_list, &bsd_list_len);
3093
if (err != 0)
3094
goto out;
3095
3096
if (ap->a_size != NULL)
3097
*ap->a_size = bsd_list_len;
3098
3099
if (uio != NULL)
3100
err = uiomove(bsd_list, bsd_list_len, uio);
3101
3102
out:
3103
free(bsd_list, M_TEMP);
3104
fdisp_destroy(&fdi);
3105
return (err);
3106
}
3107
3108
/*
3109
struct vop_deallocate_args {
3110
struct vop_generic_args a_gen;
3111
struct vnode *a_vp;
3112
off_t *a_offset;
3113
off_t *a_len;
3114
int a_flags;
3115
int a_ioflag;
3116
struct ucred *a_cred;
3117
};
3118
*/
3119
static int
3120
fuse_vnop_deallocate(struct vop_deallocate_args *ap)
3121
{
3122
struct vnode *vp = ap->a_vp;
3123
struct mount *mp = vnode_mount(vp);
3124
struct fuse_filehandle *fufh;
3125
struct fuse_dispatcher fdi;
3126
struct fuse_fallocate_in *ffi;
3127
struct ucred *cred = ap->a_cred;
3128
pid_t pid = curthread->td_proc->p_pid;
3129
off_t *len = ap->a_len;
3130
off_t *offset = ap->a_offset;
3131
int ioflag = ap->a_ioflag;
3132
off_t filesize;
3133
int err;
3134
bool closefufh = false;
3135
3136
if (fuse_isdeadfs(vp))
3137
return (EXTERROR(ENXIO, "This FUSE session is about "
3138
"to be closed"));
3139
3140
if (vfs_isrdonly(mp))
3141
return (EROFS);
3142
3143
if (fsess_not_impl(mp, FUSE_FALLOCATE))
3144
goto fallback;
3145
3146
err = fuse_filehandle_getrw(vp, FWRITE, &fufh, cred, pid);
3147
if (err == EBADF && vnode_mount(vp)->mnt_flag & MNT_EXPORTED) {
3148
/*
3149
* nfsd will do I/O without first doing VOP_OPEN. We
3150
* must implicitly open the file here
3151
*/
3152
err = fuse_filehandle_open(vp, FWRITE, &fufh, curthread, cred);
3153
closefufh = true;
3154
}
3155
if (err)
3156
return (err);
3157
3158
fuse_vnode_update(vp, FN_MTIMECHANGE | FN_CTIMECHANGE);
3159
3160
err = fuse_vnode_size(vp, &filesize, cred, curthread);
3161
if (err)
3162
goto out;
3163
fuse_inval_buf_range(vp, filesize, *offset, *offset + *len);
3164
3165
fdisp_init(&fdi, sizeof(*ffi));
3166
fdisp_make_vp(&fdi, FUSE_FALLOCATE, vp, curthread, cred);
3167
ffi = fdi.indata;
3168
ffi->fh = fufh->fh_id;
3169
ffi->offset = *offset;
3170
ffi->length = *len;
3171
/*
3172
* FreeBSD's fspacectl is equivalent to Linux's fallocate with
3173
* mode == FALLOC_FL_PUNCH_HOLE | FALLOC_FL_KEEP_SIZE
3174
*/
3175
ffi->mode = FUSE_FALLOC_FL_PUNCH_HOLE | FUSE_FALLOC_FL_KEEP_SIZE;
3176
err = fdisp_wait_answ(&fdi);
3177
3178
if (err == ENOSYS) {
3179
fdisp_destroy(&fdi);
3180
fsess_set_notimpl(mp, FUSE_FALLOCATE);
3181
goto fallback;
3182
} else if (err == EOPNOTSUPP) {
3183
/*
3184
* The file system server does not support FUSE_FALLOCATE with
3185
* the supplied mode for this particular file.
3186
*/
3187
fdisp_destroy(&fdi);
3188
goto fallback;
3189
} else if (!err) {
3190
/*
3191
* Clip the returned offset to EoF. Do it here rather than
3192
* before FUSE_FALLOCATE just in case the kernel's cached file
3193
* size is out of date. Unfortunately, FUSE does not return
3194
* any information about filesize from that operation.
3195
*/
3196
*offset = MIN(*offset + *len, filesize);
3197
*len = 0;
3198
fuse_vnode_undirty_cached_timestamps(vp, false);
3199
fuse_internal_clear_suid_on_write(vp, cred, curthread);
3200
3201
if (ioflag & IO_SYNC)
3202
err = fuse_internal_fsync(vp, curthread, MNT_WAIT,
3203
false);
3204
}
3205
3206
fdisp_destroy(&fdi);
3207
out:
3208
if (closefufh)
3209
fuse_filehandle_close(vp, fufh, curthread, cred);
3210
3211
return (err);
3212
3213
fallback:
3214
if (closefufh)
3215
fuse_filehandle_close(vp, fufh, curthread, cred);
3216
3217
return (vop_stddeallocate(ap));
3218
}
3219
3220
/*
3221
struct vop_deleteextattr_args {
3222
struct vop_generic_args a_gen;
3223
struct vnode *a_vp;
3224
int a_attrnamespace;
3225
const char *a_name;
3226
struct ucred *a_cred;
3227
struct thread *a_td;
3228
};
3229
*/
3230
static int
3231
fuse_vnop_deleteextattr(struct vop_deleteextattr_args *ap)
3232
{
3233
struct vnode *vp = ap->a_vp;
3234
struct fuse_dispatcher fdi;
3235
struct mount *mp = vnode_mount(vp);
3236
struct thread *td = ap->a_td;
3237
struct ucred *cred = ap->a_cred;
3238
char *prefix;
3239
size_t len;
3240
char *attr_str;
3241
int err;
3242
3243
if (fuse_isdeadfs(vp))
3244
return (EXTERROR(ENXIO, "This FUSE session is about "
3245
"to be closed"));
3246
3247
if (fsess_not_impl(mp, FUSE_REMOVEXATTR))
3248
return (EXTERROR(EOPNOTSUPP, "This server does not implement "
3249
"removing extended attributes"));
3250
3251
if (vfs_isrdonly(mp))
3252
return EROFS;
3253
3254
err = fuse_extattr_check_cred(vp, ap->a_attrnamespace, cred, td,
3255
VWRITE);
3256
if (err)
3257
return err;
3258
3259
/* Default to looking for user attributes. */
3260
if (ap->a_attrnamespace == EXTATTR_NAMESPACE_SYSTEM)
3261
prefix = EXTATTR_NAMESPACE_SYSTEM_STRING;
3262
else
3263
prefix = EXTATTR_NAMESPACE_USER_STRING;
3264
3265
len = strlen(prefix) + sizeof(extattr_namespace_separator) +
3266
strlen(ap->a_name) + 1;
3267
3268
fdisp_init(&fdi, len);
3269
fdisp_make_vp(&fdi, FUSE_REMOVEXATTR, vp, td, cred);
3270
3271
attr_str = fdi.indata;
3272
snprintf(attr_str, len, "%s%c%s", prefix, extattr_namespace_separator,
3273
ap->a_name);
3274
3275
err = fdisp_wait_answ(&fdi);
3276
if (err == ENOSYS) {
3277
fsess_set_notimpl(mp, FUSE_REMOVEXATTR);
3278
err = EXTERROR(EOPNOTSUPP, "This server does not implement "
3279
"removing extended attributes");
3280
}
3281
3282
fdisp_destroy(&fdi);
3283
return (err);
3284
}
3285
3286
/*
3287
struct vnop_print_args {
3288
struct vnode *a_vp;
3289
};
3290
*/
3291
static int
3292
fuse_vnop_print(struct vop_print_args *ap)
3293
{
3294
struct fuse_vnode_data *fvdat = VTOFUD(ap->a_vp);
3295
3296
printf("nodeid: %ju, parent nodeid: %ju, nlookup: %ju, flag: %#x\n",
3297
(uintmax_t)VTOILLU(ap->a_vp), (uintmax_t)fvdat->parent_nid,
3298
(uintmax_t)fvdat->nlookup,
3299
fvdat->flag);
3300
3301
return 0;
3302
}
3303
3304
/*
3305
* Get an NFS filehandle for a FUSE file.
3306
*
3307
* This will only work for FUSE file systems that guarantee the uniqueness of
3308
* nodeid:generation, which most don't.
3309
*/
3310
/*
3311
vop_vptofh {
3312
IN struct vnode *a_vp;
3313
IN struct fid *a_fhp;
3314
};
3315
*/
3316
static int
3317
fuse_vnop_vptofh(struct vop_vptofh_args *ap)
3318
{
3319
struct vnode *vp = ap->a_vp;
3320
struct fuse_vnode_data *fvdat = VTOFUD(vp);
3321
struct fuse_fid *fhp = (struct fuse_fid *)(ap->a_fhp);
3322
_Static_assert(sizeof(struct fuse_fid) <= sizeof(struct fid),
3323
"FUSE fid type is too big");
3324
struct mount *mp = vnode_mount(vp);
3325
struct fuse_data *data = fuse_get_mpdata(mp);
3326
struct vattr va;
3327
int err;
3328
3329
if (!(data->dataflags & FSESS_EXPORT_SUPPORT)) {
3330
/* NFS requires lookups for "." and ".." */
3331
SDT_PROBE2(fusefs, , vnops, trace, 1,
3332
"VOP_VPTOFH without FUSE_EXPORT_SUPPORT");
3333
return (EXTERROR(EOPNOTSUPP, "This server is "
3334
"missing FUSE_EXPORT_SUPPORT"));
3335
}
3336
if ((mp->mnt_flag & MNT_EXPORTED) &&
3337
fsess_is_impl(mp, FUSE_OPENDIR))
3338
{
3339
/*
3340
* NFS is stateless, so nfsd must reopen a directory on every
3341
* call to VOP_READDIR, passing in the d_off field from the
3342
* final dirent of the previous invocation. But if the server
3343
* implements FUSE_OPENDIR, the FUSE protocol does not
3344
* guarantee that d_off will be valid after a directory is
3345
* closed and reopened. So prohibit exporting FUSE file
3346
* systems that implement FUSE_OPENDIR.
3347
*
3348
* But userspace NFS servers don't have this problem.
3349
*/
3350
SDT_PROBE2(fusefs, , vnops, trace, 1,
3351
"VOP_VPTOFH with FUSE_OPENDIR");
3352
return (EXTERROR(EOPNOTSUPP, "This server implements "
3353
"FUSE_OPENDIR so is not compatible with getfh"));
3354
}
3355
3356
err = fuse_internal_getattr(vp, &va, curthread->td_ucred, curthread);
3357
if (err)
3358
return err;
3359
3360
/*ip = VTOI(ap->a_vp);*/
3361
/*ufhp = (struct ufid *)ap->a_fhp;*/
3362
fhp->len = sizeof(struct fuse_fid);
3363
fhp->nid = fvdat->nid;
3364
if (fvdat->generation <= UINT32_MAX)
3365
fhp->gen = fvdat->generation;
3366
else
3367
return (EXTERROR(EOVERFLOW, "inode generation "
3368
"number overflow"));
3369
return (0);
3370
}
3371
3372