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