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