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_gid != outcred->cr_gid)
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 int err = 0;
1223
1224 err = fuse_internal_getattr(vp, vap, cred, td);
1225 if (err == ENOTCONN && vnode_isvroot(vp)) {
1226 /*
1227 * We want to seem a legitimate fs even if the daemon is dead,
1228 * so that, eg., we can still do path based unmounting after
1229 * the daemon dies.
1230 */
1231 err = 0;
1232 bzero(vap, sizeof(*vap));
1233 vap->va_type = vnode_vtype(vp);
1234 }
1235 return err;
1236 }
1237
1238 /*
1239 struct vnop_inactive_args {
1240 struct vnode *a_vp;
1241 };
1242 */
1243 static int
fuse_vnop_inactive(struct vop_inactive_args * ap)1244 fuse_vnop_inactive(struct vop_inactive_args *ap)
1245 {
1246 struct vnode *vp = ap->a_vp;
1247 struct thread *td = curthread;
1248
1249 struct fuse_vnode_data *fvdat = VTOFUD(vp);
1250 struct fuse_filehandle *fufh, *fufh_tmp;
1251
1252 int need_flush = 1;
1253
1254 LIST_FOREACH_SAFE(fufh, &fvdat->handles, next, fufh_tmp) {
1255 if (need_flush && vp->v_type == VREG) {
1256 if ((VTOFUD(vp)->flag & FN_SIZECHANGE) != 0) {
1257 fuse_vnode_savesize(vp, NULL, 0);
1258 }
1259 if ((fvdat->flag & FN_REVOKED) != 0)
1260 fuse_io_invalbuf(vp, td);
1261 else
1262 fuse_io_flushbuf(vp, MNT_WAIT, td);
1263 need_flush = 0;
1264 }
1265 fuse_filehandle_close(vp, fufh, td, NULL);
1266 }
1267
1268 if ((fvdat->flag & FN_REVOKED) != 0)
1269 vrecycle(vp);
1270
1271 return 0;
1272 }
1273
1274 /*
1275 struct vnop_ioctl_args {
1276 struct vnode *a_vp;
1277 u_long a_command;
1278 caddr_t a_data;
1279 int a_fflag;
1280 struct ucred *a_cred;
1281 struct thread *a_td;
1282 };
1283 */
1284 static int
fuse_vnop_ioctl(struct vop_ioctl_args * ap)1285 fuse_vnop_ioctl(struct vop_ioctl_args *ap)
1286 {
1287 struct vnode *vp = ap->a_vp;
1288 struct mount *mp = vnode_mount(vp);
1289 struct ucred *cred = ap->a_cred;
1290 off_t *offp;
1291 pid_t pid = ap->a_td->td_proc->p_pid;
1292 int err;
1293
1294 switch (ap->a_command) {
1295 case FIOSEEKDATA:
1296 case FIOSEEKHOLE:
1297 /* Call FUSE_LSEEK, if we can, or fall back to vop_stdioctl */
1298 if (fsess_maybe_impl(mp, FUSE_LSEEK)) {
1299 int whence;
1300
1301 offp = ap->a_data;
1302 if (ap->a_command == FIOSEEKDATA)
1303 whence = SEEK_DATA;
1304 else
1305 whence = SEEK_HOLE;
1306
1307 vn_lock(vp, LK_SHARED | LK_RETRY);
1308 err = fuse_vnop_do_lseek(vp, ap->a_td, cred, pid, offp,
1309 whence);
1310 VOP_UNLOCK(vp);
1311 }
1312 if (fsess_not_impl(mp, FUSE_LSEEK))
1313 err = vop_stdioctl(ap);
1314 break;
1315 default:
1316 /* TODO: implement FUSE_IOCTL */
1317 err = ENOTTY;
1318 break;
1319 }
1320 return (err);
1321 }
1322
1323
1324 /*
1325 struct vnop_link_args {
1326 struct vnode *a_tdvp;
1327 struct vnode *a_vp;
1328 struct componentname *a_cnp;
1329 };
1330 */
1331 static int
fuse_vnop_link(struct vop_link_args * ap)1332 fuse_vnop_link(struct vop_link_args *ap)
1333 {
1334 struct vnode *vp = ap->a_vp;
1335 struct vnode *tdvp = ap->a_tdvp;
1336 struct componentname *cnp = ap->a_cnp;
1337
1338 struct vattr *vap = VTOVA(vp);
1339
1340 struct fuse_dispatcher fdi;
1341 struct fuse_entry_out *feo;
1342 struct fuse_link_in fli;
1343
1344 int err;
1345
1346 if (fuse_isdeadfs(vp)) {
1347 return (EXTERROR(ENXIO, "This FUSE session is about "
1348 "to be closed"));
1349 }
1350 if (vnode_mount(tdvp) != vnode_mount(vp)) {
1351 return (EXDEV);
1352 }
1353
1354 /*
1355 * This is a seatbelt check to protect naive userspace filesystems from
1356 * themselves and the limitations of the FUSE IPC protocol. If a
1357 * filesystem does not allow attribute caching, assume it is capable of
1358 * validating that nlink does not overflow.
1359 */
1360 if (vap != NULL && vap->va_nlink >= FUSE_LINK_MAX)
1361 return (EMLINK);
1362 fli.oldnodeid = VTOI(vp);
1363
1364 fdisp_init(&fdi, 0);
1365 fuse_internal_newentry_makerequest(vnode_mount(tdvp), VTOI(tdvp), cnp,
1366 FUSE_LINK, &fli, sizeof(fli), &fdi);
1367 if ((err = fdisp_wait_answ(&fdi))) {
1368 goto out;
1369 }
1370 feo = fdi.answ;
1371
1372 if (fli.oldnodeid != feo->nodeid) {
1373 static const char exterr[] = "Server assigned wrong inode "
1374 "for a hard link.";
1375 struct fuse_data *data = fuse_get_mpdata(vnode_mount(vp));
1376 fuse_warn(data, FSESS_WARN_ILLEGAL_INODE, exterr);
1377 fuse_vnode_clear_attr_cache(vp);
1378 fuse_vnode_clear_attr_cache(tdvp);
1379 err = EXTERROR(EIO, exterr);
1380 goto out;
1381 }
1382
1383 err = fuse_internal_checkentry(feo, vnode_vtype(vp));
1384 if (!err) {
1385 /*
1386 * Purge the parent's attribute cache because the daemon
1387 * should've updated its mtime and ctime
1388 */
1389 fuse_vnode_clear_attr_cache(tdvp);
1390 fuse_internal_cache_attrs(vp, &feo->attr, feo->attr_valid,
1391 feo->attr_valid_nsec, NULL, true);
1392 }
1393 out:
1394 fdisp_destroy(&fdi);
1395 return err;
1396 }
1397
1398 struct fuse_lookup_alloc_arg {
1399 struct fuse_entry_out *feo;
1400 struct componentname *cnp;
1401 uint64_t nid;
1402 __enum_uint8(vtype) vtyp;
1403 };
1404
1405 /* Callback for vn_get_ino */
1406 static int
fuse_lookup_alloc(struct mount * mp,void * arg,int lkflags,struct vnode ** vpp)1407 fuse_lookup_alloc(struct mount *mp, void *arg, int lkflags, struct vnode **vpp)
1408 {
1409 struct fuse_lookup_alloc_arg *flaa = arg;
1410
1411 return fuse_vnode_get(mp, flaa->feo, flaa->nid, NULL, vpp, flaa->cnp,
1412 flaa->vtyp);
1413 }
1414
1415 SDT_PROBE_DEFINE3(fusefs, , vnops, cache_lookup,
1416 "int", "struct timespec*", "struct timespec*");
1417 /*
1418 struct vnop_lookup_args {
1419 struct vnodeop_desc *a_desc;
1420 struct vnode *a_dvp;
1421 struct vnode **a_vpp;
1422 struct componentname *a_cnp;
1423 };
1424 */
1425 int
fuse_vnop_lookup(struct vop_lookup_args * ap)1426 fuse_vnop_lookup(struct vop_lookup_args *ap)
1427 {
1428 struct vnode *dvp = ap->a_dvp;
1429 struct vnode **vpp = ap->a_vpp;
1430 struct componentname *cnp = ap->a_cnp;
1431 struct thread *td = curthread;
1432 struct ucred *cred = cnp->cn_cred;
1433 struct timespec now;
1434
1435 int nameiop = cnp->cn_nameiop;
1436 bool isdotdot = cnp->cn_flags & ISDOTDOT;
1437 bool islastcn = cnp->cn_flags & ISLASTCN;
1438 struct mount *mp = vnode_mount(dvp);
1439 struct fuse_data *data = fuse_get_mpdata(mp);
1440 int default_permissions = data->dataflags & FSESS_DEFAULT_PERMISSIONS;
1441 bool is_dot;
1442
1443 int err = 0;
1444 int lookup_err = 0;
1445 struct vnode *vp = NULL;
1446
1447 struct fuse_dispatcher fdi;
1448 bool did_lookup = false;
1449 struct fuse_entry_out *feo = NULL;
1450 __enum_uint8(vtype) vtyp; /* vnode type of target */
1451
1452 uint64_t nid;
1453
1454 if (fuse_isdeadfs(dvp)) {
1455 *vpp = NULL;
1456 return (EXTERROR(ENXIO, "This FUSE session is about "
1457 "to be closed"));
1458 }
1459 if (!vnode_isdir(dvp))
1460 return ENOTDIR;
1461
1462 if (islastcn && vfs_isrdonly(mp) && (nameiop != LOOKUP))
1463 return EROFS;
1464
1465 if ((cnp->cn_flags & NOEXECCHECK) != 0)
1466 cnp->cn_flags &= ~NOEXECCHECK;
1467 else if ((err = fuse_internal_access(dvp, VEXEC, td, cred)))
1468 return err;
1469
1470 is_dot = cnp->cn_namelen == 1 && *(cnp->cn_nameptr) == '.';
1471 if (isdotdot && !(data->dataflags & FSESS_EXPORT_SUPPORT)) {
1472 if (!(VTOFUD(dvp)->flag & FN_PARENT_NID)) {
1473 /*
1474 * Since the file system doesn't support ".." lookups,
1475 * we have no way to find this entry.
1476 */
1477 return (EXTERROR(ESTALE, "This server does not support "
1478 "'..' lookups"));
1479 }
1480 nid = VTOFUD(dvp)->parent_nid;
1481 if (nid == 0)
1482 return ENOENT;
1483 /* .. is obviously a directory */
1484 vtyp = VDIR;
1485 } else if (is_dot) {
1486 nid = VTOI(dvp);
1487 /* . is obviously a directory */
1488 vtyp = VDIR;
1489 } else {
1490 struct timespec timeout;
1491 int ncpticks; /* here to accommodate for API contract */
1492
1493 err = cache_lookup(dvp, vpp, cnp, &timeout, &ncpticks);
1494 getnanouptime(&now);
1495 SDT_PROBE3(fusefs, , vnops, cache_lookup, err, &timeout, &now);
1496 switch (err) {
1497 case -1: /* positive match */
1498 if (timespeccmp(&timeout, &now, >)) {
1499 counter_u64_add(fuse_lookup_cache_hits, 1);
1500 } else {
1501 /* Cache timeout */
1502 counter_u64_add(fuse_lookup_cache_misses, 1);
1503 bintime_clear(
1504 &VTOFUD(*vpp)->entry_cache_timeout);
1505 cache_purge(*vpp);
1506 if (dvp != *vpp)
1507 vput(*vpp);
1508 else
1509 vrele(*vpp);
1510 *vpp = NULL;
1511 break;
1512 }
1513 return 0;
1514
1515 case 0: /* no match in cache */
1516 counter_u64_add(fuse_lookup_cache_misses, 1);
1517 break;
1518
1519 case ENOENT: /* negative match */
1520 if (timespeccmp(&timeout, &now, <=)) {
1521 /* Cache timeout */
1522 cache_purge_negative(dvp);
1523 break;
1524 }
1525 /* fall through */
1526 default:
1527 return err;
1528 }
1529
1530 fdisp_init(&fdi, cnp->cn_namelen + 1);
1531 fdisp_make(&fdi, FUSE_LOOKUP, mp, VTOI(dvp), td, cred);
1532
1533 memcpy(fdi.indata, cnp->cn_nameptr, cnp->cn_namelen);
1534 ((char *)fdi.indata)[cnp->cn_namelen] = '\0';
1535 lookup_err = fdisp_wait_answ(&fdi);
1536 did_lookup = true;
1537
1538 if (!lookup_err) {
1539 /* lookup call succeeded */
1540 feo = (struct fuse_entry_out *)fdi.answ;
1541 nid = feo->nodeid;
1542 if (nid == 0) {
1543 /* zero nodeid means ENOENT and cache it */
1544 struct timespec timeout;
1545
1546 fdi.answ_stat = ENOENT;
1547 lookup_err = ENOENT;
1548 if (cnp->cn_flags & MAKEENTRY) {
1549 fuse_validity_2_timespec(feo, &timeout);
1550 /* Use the same entry_time for .. as for
1551 * the file itself. That doesn't honor
1552 * exactly what the fuse server tells
1553 * us, but to do otherwise would require
1554 * another cache lookup at this point.
1555 */
1556 struct timespec *dtsp = NULL;
1557 cache_enter_time(dvp, *vpp, cnp,
1558 &timeout, dtsp);
1559 }
1560 }
1561 vtyp = IFTOVT(feo->attr.mode);
1562 }
1563 if (lookup_err && (!fdi.answ_stat || lookup_err != ENOENT)) {
1564 fdisp_destroy(&fdi);
1565 return lookup_err;
1566 }
1567 }
1568 /* lookup_err, if non-zero, must be ENOENT at this point */
1569
1570 if (lookup_err) {
1571 /* Entry not found */
1572 if ((nameiop == CREATE || nameiop == RENAME) && islastcn) {
1573 if (default_permissions)
1574 err = fuse_internal_access(dvp, VWRITE, td,
1575 cred);
1576 else
1577 err = 0;
1578 if (!err) {
1579 err = EJUSTRETURN;
1580 }
1581 } else {
1582 err = ENOENT;
1583 }
1584 } else {
1585 /* Entry was found */
1586 if (isdotdot) {
1587 struct fuse_lookup_alloc_arg flaa;
1588
1589 flaa.nid = nid;
1590 flaa.feo = feo;
1591 flaa.cnp = cnp;
1592 flaa.vtyp = vtyp;
1593 err = vn_vget_ino_gen(dvp, fuse_lookup_alloc, &flaa, 0,
1594 &vp);
1595 *vpp = vp;
1596 } else if (nid == VTOI(dvp)) {
1597 if (is_dot) {
1598 vref(dvp);
1599 *vpp = dvp;
1600 } else {
1601 static const char exterr[] = "Server assigned "
1602 "same inode to both parent and child.";
1603 fuse_warn(fuse_get_mpdata(mp),
1604 FSESS_WARN_ILLEGAL_INODE, exterr);
1605 err = EXTERROR(EIO, exterr);
1606 }
1607
1608 } else {
1609 struct fuse_vnode_data *fvdat;
1610
1611 err = fuse_vnode_get(vnode_mount(dvp), feo, nid, dvp,
1612 &vp, cnp, vtyp);
1613 if (err)
1614 goto out;
1615 *vpp = vp;
1616 fvdat = VTOFUD(vp);
1617
1618 MPASS(feo != NULL);
1619 if (timespeccmp(&now, &fvdat->last_local_modify, >)) {
1620 /*
1621 * Attributes from the server are definitely
1622 * newer than the last attributes we sent to
1623 * the server, so cache them.
1624 */
1625 fuse_internal_cache_attrs(*vpp, &feo->attr,
1626 feo->attr_valid, feo->attr_valid_nsec,
1627 NULL, true);
1628 }
1629 fuse_validity_2_bintime(feo->entry_valid,
1630 feo->entry_valid_nsec,
1631 &fvdat->entry_cache_timeout);
1632
1633 if ((nameiop == DELETE || nameiop == RENAME) &&
1634 islastcn && default_permissions)
1635 {
1636 struct vattr dvattr;
1637
1638 err = fuse_internal_access(dvp, VWRITE, td,
1639 cred);
1640 if (err != 0)
1641 goto out;
1642 /*
1643 * if the parent's sticky bit is set, check
1644 * whether we're allowed to remove the file.
1645 * Need to figure out the vnode locking to make
1646 * this work.
1647 */
1648 fuse_internal_getattr(dvp, &dvattr, cred, td);
1649 if ((dvattr.va_mode & S_ISTXT) &&
1650 fuse_internal_access(dvp, VADMIN, td,
1651 cred) &&
1652 fuse_internal_access(*vpp, VADMIN, td,
1653 cred)) {
1654 err = EPERM;
1655 goto out;
1656 }
1657 }
1658 }
1659 }
1660 out:
1661 if (err) {
1662 if (vp != NULL && dvp != vp)
1663 vput(vp);
1664 else if (vp != NULL)
1665 vrele(vp);
1666 *vpp = NULL;
1667 }
1668 if (did_lookup)
1669 fdisp_destroy(&fdi);
1670
1671 return err;
1672 }
1673
1674 /*
1675 struct vnop_mkdir_args {
1676 struct vnode *a_dvp;
1677 struct vnode **a_vpp;
1678 struct componentname *a_cnp;
1679 struct vattr *a_vap;
1680 };
1681 */
1682 static int
fuse_vnop_mkdir(struct vop_mkdir_args * ap)1683 fuse_vnop_mkdir(struct vop_mkdir_args *ap)
1684 {
1685 struct vnode *dvp = ap->a_dvp;
1686 struct vnode **vpp = ap->a_vpp;
1687 struct componentname *cnp = ap->a_cnp;
1688 struct vattr *vap = ap->a_vap;
1689
1690 struct fuse_mkdir_in fmdi;
1691
1692 if (fuse_isdeadfs(dvp)) {
1693 return (EXTERROR(ENXIO, "This FUSE session is about "
1694 "to be closed"));
1695 }
1696 fmdi.mode = MAKEIMODE(vap->va_type, vap->va_mode);
1697 fmdi.umask = curthread->td_proc->p_pd->pd_cmask;
1698
1699 return (fuse_internal_newentry(dvp, vpp, cnp, FUSE_MKDIR, &fmdi,
1700 sizeof(fmdi), VDIR));
1701 }
1702
1703 /*
1704 struct vnop_mknod_args {
1705 struct vnode *a_dvp;
1706 struct vnode **a_vpp;
1707 struct componentname *a_cnp;
1708 struct vattr *a_vap;
1709 };
1710 */
1711 static int
fuse_vnop_mknod(struct vop_mknod_args * ap)1712 fuse_vnop_mknod(struct vop_mknod_args *ap)
1713 {
1714
1715 struct vnode *dvp = ap->a_dvp;
1716 struct vnode **vpp = ap->a_vpp;
1717 struct componentname *cnp = ap->a_cnp;
1718 struct vattr *vap = ap->a_vap;
1719
1720 if (fuse_isdeadfs(dvp))
1721 return (EXTERROR(ENXIO, "This FUSE session is about "
1722 "to be closed"));
1723
1724 return fuse_internal_mknod(dvp, vpp, cnp, vap);
1725 }
1726
1727 /*
1728 struct vop_open_args {
1729 struct vnode *a_vp;
1730 int a_mode;
1731 struct ucred *a_cred;
1732 struct thread *a_td;
1733 int a_fdidx; / struct file *a_fp;
1734 };
1735 */
1736 static int
fuse_vnop_open(struct vop_open_args * ap)1737 fuse_vnop_open(struct vop_open_args *ap)
1738 {
1739 struct vnode *vp = ap->a_vp;
1740 int a_mode = ap->a_mode;
1741 struct thread *td = ap->a_td;
1742 struct ucred *cred = ap->a_cred;
1743 pid_t pid = td->td_proc->p_pid;
1744
1745 if (fuse_isdeadfs(vp))
1746 return (EXTERROR(ENXIO, "This FUSE session is about "
1747 "to be closed"));
1748 if (vp->v_type == VCHR || vp->v_type == VBLK || vp->v_type == VFIFO)
1749 return (EXTERROR(EOPNOTSUPP, "Unsupported vnode type",
1750 vp->v_type));
1751 if ((a_mode & (FREAD | FWRITE | FEXEC)) == 0)
1752 return (EXTERROR(EINVAL, "Illegal mode", a_mode));
1753
1754 if (fuse_filehandle_validrw(vp, a_mode, cred, pid)) {
1755 fuse_vnode_open(vp, 0, td);
1756 return 0;
1757 }
1758
1759 return fuse_filehandle_open(vp, a_mode, NULL, td, cred);
1760 }
1761
1762 static int
fuse_vnop_pathconf(struct vop_pathconf_args * ap)1763 fuse_vnop_pathconf(struct vop_pathconf_args *ap)
1764 {
1765 struct vnode *vp = ap->a_vp;
1766 struct mount *mp;
1767 struct fuse_filehandle *fufh;
1768 int err;
1769 bool closefufh = false;
1770
1771 switch (ap->a_name) {
1772 case _PC_FILESIZEBITS:
1773 *ap->a_retval = 64;
1774 return (0);
1775 case _PC_NAME_MAX:
1776 *ap->a_retval = NAME_MAX;
1777 return (0);
1778 case _PC_LINK_MAX:
1779 *ap->a_retval = MIN(LONG_MAX, FUSE_LINK_MAX);
1780 return (0);
1781 case _PC_SYMLINK_MAX:
1782 *ap->a_retval = MAXPATHLEN;
1783 return (0);
1784 case _PC_NO_TRUNC:
1785 *ap->a_retval = 1;
1786 return (0);
1787 case _PC_MIN_HOLE_SIZE:
1788 /*
1789 * The FUSE protocol provides no mechanism for a server to
1790 * report _PC_MIN_HOLE_SIZE. It's a protocol bug. Instead,
1791 * return EINVAL if the server does not support FUSE_LSEEK, or
1792 * 1 if it does.
1793 */
1794 mp = vnode_mount(vp);
1795 if (!fsess_is_impl(mp, FUSE_LSEEK) &&
1796 !fsess_not_impl(mp, FUSE_LSEEK)) {
1797 off_t offset = 0;
1798
1799 /*
1800 * Issue a FUSE_LSEEK to find out if it's supported.
1801 * Use SEEK_DATA instead of SEEK_HOLE, because the
1802 * latter generally requires sequential scans of file
1803 * metadata, which can be slow.
1804 */
1805 err = fuse_vnop_do_lseek(vp, curthread,
1806 curthread->td_ucred, curthread->td_proc->p_pid,
1807 &offset, SEEK_DATA);
1808 if (err == EBADF) {
1809 /*
1810 * pathconf() doesn't necessarily open the
1811 * file. So we may need to do it here.
1812 */
1813 err = fuse_filehandle_open(vp, FREAD, &fufh,
1814 curthread, curthread->td_ucred);
1815 if (err == 0) {
1816 closefufh = true;
1817 err = fuse_vnop_do_lseek(vp, curthread,
1818 curthread->td_ucred,
1819 curthread->td_proc->p_pid, &offset,
1820 SEEK_DATA);
1821 }
1822 if (closefufh)
1823 fuse_filehandle_close(vp, fufh,
1824 curthread, curthread->td_ucred);
1825 }
1826
1827 }
1828
1829 if (fsess_is_impl(mp, FUSE_LSEEK)) {
1830 *ap->a_retval = 1;
1831 return (0);
1832 } else if (fsess_not_impl(mp, FUSE_LSEEK)) {
1833 /* FUSE_LSEEK is not implemented */
1834 return (EXTERROR(EINVAL, "This server does not "
1835 "implement FUSE_LSEEK"));
1836 } else {
1837 return (err);
1838 }
1839 default:
1840 return (vop_stdpathconf(ap));
1841 }
1842 }
1843
1844 SDT_PROBE_DEFINE3(fusefs, , vnops, filehandles_closed, "struct vnode*",
1845 "struct uio*", "struct ucred*");
1846 /*
1847 struct vnop_read_args {
1848 struct vnode *a_vp;
1849 struct uio *a_uio;
1850 int a_ioflag;
1851 struct ucred *a_cred;
1852 };
1853 */
1854 static int
fuse_vnop_read(struct vop_read_args * ap)1855 fuse_vnop_read(struct vop_read_args *ap)
1856 {
1857 struct vnode *vp = ap->a_vp;
1858 struct uio *uio = ap->a_uio;
1859 int ioflag = ap->a_ioflag;
1860 struct ucred *cred = ap->a_cred;
1861 pid_t pid = curthread->td_proc->p_pid;
1862 struct fuse_filehandle *fufh;
1863 int err;
1864 bool closefufh = false, directio;
1865
1866 MPASS(vp->v_type == VREG || vp->v_type == VDIR);
1867
1868 if (fuse_isdeadfs(vp)) {
1869 return (EXTERROR(ENXIO, "This FUSE session is about "
1870 "to be closed"));
1871 }
1872
1873 if (VTOFUD(vp)->flag & FN_DIRECTIO) {
1874 ioflag |= IO_DIRECT;
1875 }
1876
1877 err = fuse_filehandle_getrw(vp, FREAD, &fufh, cred, pid);
1878 if (err == EBADF && vnode_mount(vp)->mnt_flag & MNT_EXPORTED) {
1879 /*
1880 * nfsd will do I/O without first doing VOP_OPEN. We
1881 * must implicitly open the file here
1882 */
1883 err = fuse_filehandle_open(vp, FREAD, &fufh, curthread, cred);
1884 closefufh = true;
1885 }
1886 if (err) {
1887 SDT_PROBE3(fusefs, , vnops, filehandles_closed, vp, uio, cred);
1888 return err;
1889 }
1890
1891 /*
1892 * Ideally, when the daemon asks for direct io at open time, the
1893 * standard file flag should be set according to this, so that would
1894 * just change the default mode, which later on could be changed via
1895 * fcntl(2).
1896 * But this doesn't work, the O_DIRECT flag gets cleared at some point
1897 * (don't know where). So to make any use of the Fuse direct_io option,
1898 * we hardwire it into the file's private data (similarly to Linux,
1899 * btw.).
1900 */
1901 directio = (ioflag & IO_DIRECT) || !fsess_opt_datacache(vnode_mount(vp));
1902
1903 fuse_vnode_update(vp, FN_ATIMECHANGE);
1904 if (directio) {
1905 SDT_PROBE2(fusefs, , vnops, trace, 1, "direct read of vnode");
1906 err = fuse_read_directbackend(vp, uio, cred, fufh);
1907 } else {
1908 SDT_PROBE2(fusefs, , vnops, trace, 1, "buffered read of vnode");
1909 err = fuse_read_biobackend(vp, uio, ioflag, cred, fufh, pid);
1910 }
1911
1912 if (closefufh)
1913 fuse_filehandle_close(vp, fufh, curthread, cred);
1914
1915 return (err);
1916 }
1917
1918 /*
1919 struct vnop_readdir_args {
1920 struct vnode *a_vp;
1921 struct uio *a_uio;
1922 struct ucred *a_cred;
1923 int *a_eofflag;
1924 int *a_ncookies;
1925 uint64_t **a_cookies;
1926 };
1927 */
1928 static int
fuse_vnop_readdir(struct vop_readdir_args * ap)1929 fuse_vnop_readdir(struct vop_readdir_args *ap)
1930 {
1931 struct vnode *vp = ap->a_vp;
1932 struct uio *uio = ap->a_uio;
1933 struct ucred *cred = ap->a_cred;
1934 struct fuse_filehandle *fufh = NULL;
1935 struct mount *mp = vnode_mount(vp);
1936 struct fuse_iov cookediov;
1937 int err = 0;
1938 uint64_t *cookies;
1939 ssize_t tresid;
1940 int ncookies;
1941 bool closefufh = false;
1942 pid_t pid = curthread->td_proc->p_pid;
1943
1944 if (ap->a_eofflag)
1945 *ap->a_eofflag = 0;
1946 if (fuse_isdeadfs(vp)) {
1947 return (EXTERROR(ENXIO, "This FUSE session is about "
1948 "to be closed"));
1949 }
1950 if (uio_resid(uio) < sizeof(struct dirent))
1951 return (EXTERROR(EINVAL, "Buffer is too small"));
1952
1953 tresid = uio->uio_resid;
1954 err = fuse_filehandle_get_dir(vp, &fufh, cred, pid);
1955 if (err == EBADF && mp->mnt_flag & MNT_EXPORTED) {
1956 KASSERT(!fsess_is_impl(mp, FUSE_OPENDIR),
1957 ("FUSE file systems that implement "
1958 "FUSE_OPENDIR should not be exported"));
1959 /*
1960 * nfsd will do VOP_READDIR without first doing VOP_OPEN. We
1961 * must implicitly open the directory here.
1962 */
1963 err = fuse_filehandle_open(vp, FREAD, &fufh, curthread, cred);
1964 closefufh = true;
1965 }
1966 if (err)
1967 return (err);
1968 if (ap->a_ncookies != NULL) {
1969 ncookies = uio->uio_resid /
1970 (offsetof(struct dirent, d_name) + 4) + 1;
1971 cookies = malloc(ncookies * sizeof(*cookies), M_TEMP, M_WAITOK);
1972 *ap->a_ncookies = ncookies;
1973 *ap->a_cookies = cookies;
1974 } else {
1975 ncookies = 0;
1976 cookies = NULL;
1977 }
1978 #define DIRCOOKEDSIZE FUSE_DIRENT_ALIGN(FUSE_NAME_OFFSET + MAXNAMLEN + 1)
1979 fiov_init(&cookediov, DIRCOOKEDSIZE);
1980
1981 err = fuse_internal_readdir(vp, uio, fufh, &cookediov,
1982 &ncookies, cookies);
1983
1984 fiov_teardown(&cookediov);
1985 if (closefufh)
1986 fuse_filehandle_close(vp, fufh, curthread, cred);
1987
1988 if (ap->a_ncookies != NULL) {
1989 if (err == 0) {
1990 *ap->a_ncookies -= ncookies;
1991 } else {
1992 free(*ap->a_cookies, M_TEMP);
1993 *ap->a_ncookies = 0;
1994 *ap->a_cookies = NULL;
1995 }
1996 }
1997 if (err == 0 && tresid == uio->uio_resid)
1998 *ap->a_eofflag = 1;
1999
2000 return err;
2001 }
2002
2003 /*
2004 struct vnop_readlink_args {
2005 struct vnode *a_vp;
2006 struct uio *a_uio;
2007 struct ucred *a_cred;
2008 };
2009 */
2010 static int
fuse_vnop_readlink(struct vop_readlink_args * ap)2011 fuse_vnop_readlink(struct vop_readlink_args *ap)
2012 {
2013 struct vnode *vp = ap->a_vp;
2014 struct uio *uio = ap->a_uio;
2015 struct ucred *cred = ap->a_cred;
2016
2017 struct fuse_dispatcher fdi;
2018 int err;
2019
2020 if (fuse_isdeadfs(vp)) {
2021 return (EXTERROR(ENXIO, "This FUSE session is about "
2022 "to be closed"));
2023 }
2024 if (!vnode_islnk(vp)) {
2025 return EINVAL;
2026 }
2027 fdisp_init(&fdi, 0);
2028 err = fdisp_simple_putget_vp(&fdi, FUSE_READLINK, vp, curthread, cred);
2029 if (err) {
2030 goto out;
2031 }
2032 if (strnlen(fdi.answ, fdi.iosize) + 1 < fdi.iosize) {
2033 static const char exterr[] = "Server returned an embedded NUL "
2034 "from FUSE_READLINK.";
2035 struct fuse_data *data = fuse_get_mpdata(vnode_mount(vp));
2036 fuse_warn(data, FSESS_WARN_READLINK_EMBEDDED_NUL, exterr);
2037 err = EXTERROR(EIO, exterr);
2038 goto out;
2039 }
2040 if (((char *)fdi.answ)[0] == '/' &&
2041 fuse_get_mpdata(vnode_mount(vp))->dataflags & FSESS_PUSH_SYMLINKS_IN) {
2042 char *mpth = vnode_mount(vp)->mnt_stat.f_mntonname;
2043
2044 err = uiomove(mpth, strlen(mpth), uio);
2045 }
2046 if (!err) {
2047 err = uiomove(fdi.answ, fdi.iosize, uio);
2048 }
2049 out:
2050 fdisp_destroy(&fdi);
2051 return err;
2052 }
2053
2054 /*
2055 struct vnop_reclaim_args {
2056 struct vnode *a_vp;
2057 };
2058 */
2059 static int
fuse_vnop_reclaim(struct vop_reclaim_args * ap)2060 fuse_vnop_reclaim(struct vop_reclaim_args *ap)
2061 {
2062 struct vnode *vp = ap->a_vp;
2063 struct thread *td = curthread;
2064 struct fuse_vnode_data *fvdat = VTOFUD(vp);
2065 struct fuse_filehandle *fufh, *fufh_tmp;
2066
2067 if (!fvdat) {
2068 panic("FUSE: no vnode data during recycling");
2069 }
2070 LIST_FOREACH_SAFE(fufh, &fvdat->handles, next, fufh_tmp) {
2071 printf("FUSE: vnode being reclaimed with open fufh "
2072 "(type=%#x)", fufh->fufh_type);
2073 fuse_filehandle_close(vp, fufh, td, NULL);
2074 }
2075
2076 if (VTOI(vp) == 1) {
2077 /*
2078 * Don't send FUSE_FORGET for the root inode, because
2079 * we never send FUSE_LOOKUP for it (see
2080 * fuse_vfsop_root) and we don't want the server to see
2081 * mismatched lookup counts.
2082 */
2083 struct fuse_data *data;
2084 struct vnode *vroot;
2085
2086 data = fuse_get_mpdata(vnode_mount(vp));
2087 FUSE_LOCK();
2088 vroot = data->vroot;
2089 data->vroot = NULL;
2090 FUSE_UNLOCK();
2091 if (vroot)
2092 vrele(vroot);
2093 } else if (!fuse_isdeadfs(vp) && fvdat->nlookup > 0) {
2094 fuse_internal_forget_send(vnode_mount(vp), td, NULL, VTOI(vp),
2095 fvdat->nlookup);
2096 }
2097 cache_purge(vp);
2098 vfs_hash_remove(vp);
2099 fuse_vnode_destroy(vp);
2100
2101 return 0;
2102 }
2103
2104 /*
2105 struct vnop_remove_args {
2106 struct vnode *a_dvp;
2107 struct vnode *a_vp;
2108 struct componentname *a_cnp;
2109 };
2110 */
2111 static int
fuse_vnop_remove(struct vop_remove_args * ap)2112 fuse_vnop_remove(struct vop_remove_args *ap)
2113 {
2114 struct vnode *dvp = ap->a_dvp;
2115 struct vnode *vp = ap->a_vp;
2116 struct componentname *cnp = ap->a_cnp;
2117
2118 int err;
2119
2120 if (fuse_isdeadfs(vp)) {
2121 return (EXTERROR(ENXIO, "This FUSE session is about "
2122 "to be closed"));
2123 }
2124 if (vnode_isdir(vp)) {
2125 return (EXTERROR(EPERM, "vnode is a directory"));
2126 }
2127
2128 err = fuse_internal_remove(dvp, vp, cnp, FUSE_UNLINK);
2129
2130 return err;
2131 }
2132
2133 /*
2134 struct vnop_rename_args {
2135 struct vnode *a_fdvp;
2136 struct vnode *a_fvp;
2137 struct componentname *a_fcnp;
2138 struct vnode *a_tdvp;
2139 struct vnode *a_tvp;
2140 struct componentname *a_tcnp;
2141 };
2142 */
2143 static int
fuse_vnop_rename(struct vop_rename_args * ap)2144 fuse_vnop_rename(struct vop_rename_args *ap)
2145 {
2146 struct vnode *fdvp = ap->a_fdvp;
2147 struct vnode *fvp = ap->a_fvp;
2148 struct componentname *fcnp = ap->a_fcnp;
2149 struct vnode *tdvp = ap->a_tdvp;
2150 struct vnode *tvp = ap->a_tvp;
2151 struct componentname *tcnp = ap->a_tcnp;
2152 struct fuse_data *data;
2153 bool newparent = fdvp != tdvp;
2154 bool isdir = fvp->v_type == VDIR;
2155 int err = 0;
2156
2157 if (fuse_isdeadfs(fdvp)) {
2158 return (EXTERROR(ENXIO, "This FUSE session is about "
2159 "to be closed"));
2160 }
2161 if (fvp->v_mount != tdvp->v_mount ||
2162 (tvp && fvp->v_mount != tvp->v_mount)) {
2163 SDT_PROBE2(fusefs, , vnops, trace, 1, "cross-device rename");
2164 err = EXTERROR(EXDEV, "Cross-device rename");
2165 goto out;
2166 }
2167 cache_purge(fvp);
2168
2169 /*
2170 * FUSE library is expected to check if target directory is not
2171 * under the source directory in the file system tree.
2172 * Linux performs this check at VFS level.
2173 */
2174 /*
2175 * If source is a directory, and it will get a new parent, user must
2176 * have write permission to it, so ".." can be modified.
2177 */
2178 data = fuse_get_mpdata(vnode_mount(tdvp));
2179 if (data->dataflags & FSESS_DEFAULT_PERMISSIONS && isdir && newparent) {
2180 err = fuse_internal_access(fvp, VWRITE,
2181 curthread, tcnp->cn_cred);
2182 if (err)
2183 goto out;
2184 }
2185 sx_xlock(&data->rename_lock);
2186 err = fuse_internal_rename(fdvp, fcnp, tdvp, tcnp);
2187 if (err == 0) {
2188 if (tdvp != fdvp)
2189 fuse_vnode_setparent(fvp, tdvp);
2190 if (tvp != NULL)
2191 fuse_vnode_setparent(tvp, NULL);
2192 }
2193 sx_unlock(&data->rename_lock);
2194
2195 if (tvp != NULL && tvp != fvp) {
2196 cache_purge(tvp);
2197 }
2198 if (vnode_isdir(fvp)) {
2199 if (((tvp != NULL) && vnode_isdir(tvp)) || vnode_isdir(fvp)) {
2200 cache_purge(tdvp);
2201 }
2202 cache_purge(fdvp);
2203 }
2204 out:
2205 if (tdvp == tvp) {
2206 vrele(tdvp);
2207 } else {
2208 vput(tdvp);
2209 }
2210 if (tvp != NULL) {
2211 vput(tvp);
2212 }
2213 vrele(fdvp);
2214 vrele(fvp);
2215
2216 return err;
2217 }
2218
2219 /*
2220 struct vnop_rmdir_args {
2221 struct vnode *a_dvp;
2222 struct vnode *a_vp;
2223 struct componentname *a_cnp;
2224 } *ap;
2225 */
2226 static int
fuse_vnop_rmdir(struct vop_rmdir_args * ap)2227 fuse_vnop_rmdir(struct vop_rmdir_args *ap)
2228 {
2229 struct vnode *dvp = ap->a_dvp;
2230 struct vnode *vp = ap->a_vp;
2231
2232 int err;
2233
2234 if (fuse_isdeadfs(vp)) {
2235 return (EXTERROR(ENXIO, "This FUSE session is about "
2236 "to be closed"));
2237 }
2238 if (VTOFUD(vp) == VTOFUD(dvp)) {
2239 return (EXTERROR(EINVAL, "Directory to be removed "
2240 "contains itself"));
2241 }
2242 err = fuse_internal_remove(dvp, vp, ap->a_cnp, FUSE_RMDIR);
2243
2244 return err;
2245 }
2246
2247 /*
2248 struct vnop_setattr_args {
2249 struct vnode *a_vp;
2250 struct vattr *a_vap;
2251 struct ucred *a_cred;
2252 struct thread *a_td;
2253 };
2254 */
2255 static int
fuse_vnop_setattr(struct vop_setattr_args * ap)2256 fuse_vnop_setattr(struct vop_setattr_args *ap)
2257 {
2258 struct vnode *vp = ap->a_vp;
2259 struct vattr *vap = ap->a_vap;
2260 struct ucred *cred = ap->a_cred;
2261 struct thread *td = curthread;
2262 struct mount *mp;
2263 struct fuse_data *data;
2264 struct vattr old_va;
2265 int dataflags;
2266 int err = 0, err2;
2267 accmode_t accmode = 0;
2268 bool checkperm;
2269 bool drop_suid = false;
2270
2271 mp = vnode_mount(vp);
2272 data = fuse_get_mpdata(mp);
2273 dataflags = data->dataflags;
2274 checkperm = dataflags & FSESS_DEFAULT_PERMISSIONS;
2275
2276 if (fuse_isdeadfs(vp)) {
2277 return (EXTERROR(ENXIO, "This FUSE session is about "
2278 "to be closed"));
2279 }
2280
2281 if (vap->va_uid != (uid_t)VNOVAL) {
2282 if (checkperm) {
2283 /* Only root may change a file's owner */
2284 err = priv_check_cred(cred, PRIV_VFS_CHOWN);
2285 if (err) {
2286 /* As a special case, allow the null chown */
2287 err2 = fuse_internal_getattr(vp, &old_va, cred,
2288 td);
2289 if (err2)
2290 return (err2);
2291 if (vap->va_uid != old_va.va_uid)
2292 return err;
2293 drop_suid = true;
2294 }
2295 }
2296 accmode |= VADMIN;
2297 }
2298 if (vap->va_gid != (gid_t)VNOVAL) {
2299 if (checkperm && priv_check_cred(cred, PRIV_VFS_CHOWN))
2300 drop_suid = true;
2301 if (checkperm && !groupmember(vap->va_gid, cred)) {
2302 /*
2303 * Non-root users may only chgrp to one of their own
2304 * groups
2305 */
2306 err = priv_check_cred(cred, PRIV_VFS_CHOWN);
2307 if (err) {
2308 /* As a special case, allow the null chgrp */
2309 err2 = fuse_internal_getattr(vp, &old_va, cred,
2310 td);
2311 if (err2)
2312 return (err2);
2313 if (vap->va_gid != old_va.va_gid)
2314 return err;
2315 }
2316 }
2317 accmode |= VADMIN;
2318 }
2319 if (vap->va_size != VNOVAL) {
2320 switch (vp->v_type) {
2321 case VDIR:
2322 return (EISDIR);
2323 case VLNK:
2324 case VREG:
2325 if (vfs_isrdonly(mp))
2326 return (EROFS);
2327 err = vn_rlimit_trunc(vap->va_size, td);
2328 if (err)
2329 return (err);
2330 break;
2331 default:
2332 /*
2333 * According to POSIX, the result is unspecified
2334 * for file types other than regular files,
2335 * directories and shared memory objects. We
2336 * don't support shared memory objects in the file
2337 * system, and have dubious support for truncating
2338 * symlinks. Just ignore the request in other cases.
2339 */
2340 return (0);
2341 }
2342 /* Don't set accmode. Permission to trunc is checked upstack */
2343 }
2344 if (vap->va_atime.tv_sec != VNOVAL || vap->va_mtime.tv_sec != VNOVAL) {
2345 if (vap->va_vaflags & VA_UTIMES_NULL)
2346 accmode |= VWRITE;
2347 else
2348 accmode |= VADMIN;
2349 }
2350 if (drop_suid) {
2351 if (vap->va_mode != (mode_t)VNOVAL)
2352 vap->va_mode &= ~(S_ISUID | S_ISGID);
2353 else {
2354 err = fuse_internal_getattr(vp, &old_va, cred, td);
2355 if (err)
2356 return (err);
2357 vap->va_mode = old_va.va_mode & ~(S_ISUID | S_ISGID);
2358 }
2359 }
2360 if (vap->va_mode != (mode_t)VNOVAL) {
2361 /* Only root may set the sticky bit on non-directories */
2362 if (checkperm && vp->v_type != VDIR && (vap->va_mode & S_ISTXT)
2363 && priv_check_cred(cred, PRIV_VFS_STICKYFILE))
2364 return EFTYPE;
2365 if (checkperm && (vap->va_mode & S_ISGID)) {
2366 err = fuse_internal_getattr(vp, &old_va, cred, td);
2367 if (err)
2368 return (err);
2369 if (!groupmember(old_va.va_gid, cred)) {
2370 err = priv_check_cred(cred, PRIV_VFS_SETGID);
2371 if (err)
2372 return (err);
2373 }
2374 }
2375 accmode |= VADMIN;
2376 }
2377
2378 if (vfs_isrdonly(mp))
2379 return EROFS;
2380
2381 if (checkperm) {
2382 err = fuse_internal_access(vp, accmode, td, cred);
2383 } else {
2384 err = 0;
2385 }
2386 if (err)
2387 return err;
2388 else
2389 return fuse_internal_setattr(vp, vap, td, cred);
2390 }
2391
2392 /*
2393 struct vnop_strategy_args {
2394 struct vnode *a_vp;
2395 struct buf *a_bp;
2396 };
2397 */
2398 static int
fuse_vnop_strategy(struct vop_strategy_args * ap)2399 fuse_vnop_strategy(struct vop_strategy_args *ap)
2400 {
2401 struct vnode *vp = ap->a_vp;
2402 struct buf *bp = ap->a_bp;
2403
2404 if (!vp || fuse_isdeadfs(vp)) {
2405 bp->b_ioflags |= BIO_ERROR;
2406 bp->b_error = ENXIO;
2407 bufdone(bp);
2408 return 0;
2409 }
2410
2411 /*
2412 * VOP_STRATEGY always returns zero and signals error via bp->b_ioflags.
2413 * fuse_io_strategy sets bp's error fields
2414 */
2415 (void)fuse_io_strategy(vp, bp);
2416
2417 return 0;
2418 }
2419
2420 /*
2421 struct vnop_symlink_args {
2422 struct vnode *a_dvp;
2423 struct vnode **a_vpp;
2424 struct componentname *a_cnp;
2425 struct vattr *a_vap;
2426 char *a_target;
2427 };
2428 */
2429 static int
fuse_vnop_symlink(struct vop_symlink_args * ap)2430 fuse_vnop_symlink(struct vop_symlink_args *ap)
2431 {
2432 struct vnode *dvp = ap->a_dvp;
2433 struct vnode **vpp = ap->a_vpp;
2434 struct componentname *cnp = ap->a_cnp;
2435 const char *target = ap->a_target;
2436
2437 struct fuse_dispatcher fdi;
2438
2439 int err;
2440 size_t len;
2441
2442 if (fuse_isdeadfs(dvp)) {
2443 return (EXTERROR(ENXIO, "This FUSE session is about "
2444 "to be closed"));
2445 }
2446 /*
2447 * Unlike the other creator type calls, here we have to create a message
2448 * where the name of the new entry comes first, and the data describing
2449 * the entry comes second.
2450 * Hence we can't rely on our handy fuse_internal_newentry() routine,
2451 * but put together the message manually and just call the core part.
2452 */
2453
2454 len = strlen(target) + 1;
2455 fdisp_init(&fdi, len + cnp->cn_namelen + 1);
2456 fdisp_make_vp(&fdi, FUSE_SYMLINK, dvp, curthread, NULL);
2457
2458 memcpy(fdi.indata, cnp->cn_nameptr, cnp->cn_namelen);
2459 ((char *)fdi.indata)[cnp->cn_namelen] = '\0';
2460 memcpy((char *)fdi.indata + cnp->cn_namelen + 1, target, len);
2461
2462 err = fuse_internal_newentry_core(dvp, vpp, cnp, VLNK, &fdi);
2463 fdisp_destroy(&fdi);
2464 return err;
2465 }
2466
2467 /*
2468 struct vnop_write_args {
2469 struct vnode *a_vp;
2470 struct uio *a_uio;
2471 int a_ioflag;
2472 struct ucred *a_cred;
2473 };
2474 */
2475 static int
fuse_vnop_write(struct vop_write_args * ap)2476 fuse_vnop_write(struct vop_write_args *ap)
2477 {
2478 struct vnode *vp = ap->a_vp;
2479 struct uio *uio = ap->a_uio;
2480 int ioflag = ap->a_ioflag;
2481 struct ucred *cred = ap->a_cred;
2482 pid_t pid = curthread->td_proc->p_pid;
2483 struct fuse_filehandle *fufh;
2484 int err;
2485 bool closefufh = false, directio;
2486
2487 MPASS(vp->v_type == VREG || vp->v_type == VDIR);
2488
2489 if (fuse_isdeadfs(vp)) {
2490 return (EXTERROR(ENXIO, "This FUSE session is about "
2491 "to be closed"));
2492 }
2493
2494 if (VTOFUD(vp)->flag & FN_DIRECTIO) {
2495 ioflag |= IO_DIRECT;
2496 }
2497
2498 err = fuse_filehandle_getrw(vp, FWRITE, &fufh, cred, pid);
2499 if (err == EBADF && vnode_mount(vp)->mnt_flag & MNT_EXPORTED) {
2500 /*
2501 * nfsd will do I/O without first doing VOP_OPEN. We
2502 * must implicitly open the file here
2503 */
2504 err = fuse_filehandle_open(vp, FWRITE, &fufh, curthread, cred);
2505 closefufh = true;
2506 }
2507 if (err) {
2508 SDT_PROBE3(fusefs, , vnops, filehandles_closed, vp, uio, cred);
2509 return err;
2510 }
2511
2512 /*
2513 * Ideally, when the daemon asks for direct io at open time, the
2514 * standard file flag should be set according to this, so that would
2515 * just change the default mode, which later on could be changed via
2516 * fcntl(2).
2517 * But this doesn't work, the O_DIRECT flag gets cleared at some point
2518 * (don't know where). So to make any use of the Fuse direct_io option,
2519 * we hardwire it into the file's private data (similarly to Linux,
2520 * btw.).
2521 */
2522 directio = (ioflag & IO_DIRECT) || !fsess_opt_datacache(vnode_mount(vp));
2523
2524 fuse_vnode_update(vp, FN_MTIMECHANGE | FN_CTIMECHANGE);
2525 if (directio) {
2526 off_t start, end, filesize;
2527 bool pages = (ioflag & IO_VMIO) != 0;
2528
2529 SDT_PROBE2(fusefs, , vnops, trace, 1, "direct write of vnode");
2530
2531 err = fuse_vnode_size(vp, &filesize, cred, curthread);
2532 if (err)
2533 goto out;
2534
2535 start = uio->uio_offset;
2536 end = start + uio->uio_resid;
2537 if (!pages) {
2538 err = fuse_inval_buf_range(vp, filesize, start,
2539 end);
2540 if (err)
2541 goto out;
2542 }
2543 err = fuse_write_directbackend(vp, uio, cred, fufh,
2544 filesize, ioflag, pages);
2545 } else {
2546 SDT_PROBE2(fusefs, , vnops, trace, 1,
2547 "buffered write of vnode");
2548 if (!fsess_opt_writeback(vnode_mount(vp)))
2549 ioflag |= IO_SYNC;
2550 err = fuse_write_biobackend(vp, uio, cred, fufh, ioflag, pid);
2551 }
2552 fuse_internal_clear_suid_on_write(vp, cred, uio->uio_td);
2553
2554 out:
2555 if (closefufh)
2556 fuse_filehandle_close(vp, fufh, curthread, cred);
2557
2558 return (err);
2559 }
2560
2561 static daddr_t
fuse_gbp_getblkno(struct vnode * vp,vm_ooffset_t off)2562 fuse_gbp_getblkno(struct vnode *vp, vm_ooffset_t off)
2563 {
2564 const int biosize = fuse_iosize(vp);
2565
2566 return (off / biosize);
2567 }
2568
2569 static int
fuse_gbp_getblksz(struct vnode * vp,daddr_t lbn,long * blksz)2570 fuse_gbp_getblksz(struct vnode *vp, daddr_t lbn, long *blksz)
2571 {
2572 off_t filesize;
2573 int err;
2574 const int biosize = fuse_iosize(vp);
2575
2576 err = fuse_vnode_size(vp, &filesize, NULL, NULL);
2577 if (err) {
2578 /* This will turn into a SIGBUS */
2579 return (EIO);
2580 } else if ((off_t)lbn * biosize >= filesize) {
2581 *blksz = 0;
2582 } else if ((off_t)(lbn + 1) * biosize > filesize) {
2583 *blksz = filesize - (off_t)lbn *biosize;
2584 } else {
2585 *blksz = biosize;
2586 }
2587 return (0);
2588 }
2589
2590 /*
2591 struct vnop_getpages_args {
2592 struct vnode *a_vp;
2593 vm_page_t *a_m;
2594 int a_count;
2595 int a_reqpage;
2596 };
2597 */
2598 static int
fuse_vnop_getpages(struct vop_getpages_args * ap)2599 fuse_vnop_getpages(struct vop_getpages_args *ap)
2600 {
2601 struct vnode *vp = ap->a_vp;
2602
2603 if (!fsess_opt_mmap(vnode_mount(vp))) {
2604 SDT_PROBE2(fusefs, , vnops, trace, 1,
2605 "called on non-cacheable vnode??\n");
2606 return (VM_PAGER_ERROR);
2607 }
2608
2609 return (vfs_bio_getpages(vp, ap->a_m, ap->a_count, ap->a_rbehind,
2610 ap->a_rahead, fuse_gbp_getblkno, fuse_gbp_getblksz));
2611 }
2612
2613 static const char extattr_namespace_separator = '.';
2614
2615 /*
2616 struct vop_getextattr_args {
2617 struct vop_generic_args a_gen;
2618 struct vnode *a_vp;
2619 int a_attrnamespace;
2620 const char *a_name;
2621 struct uio *a_uio;
2622 size_t *a_size;
2623 struct ucred *a_cred;
2624 struct thread *a_td;
2625 };
2626 */
2627 static int
fuse_vnop_getextattr(struct vop_getextattr_args * ap)2628 fuse_vnop_getextattr(struct vop_getextattr_args *ap)
2629 {
2630 struct vnode *vp = ap->a_vp;
2631 struct uio *uio = ap->a_uio;
2632 struct fuse_dispatcher fdi;
2633 struct fuse_getxattr_in *get_xattr_in;
2634 struct fuse_getxattr_out *get_xattr_out;
2635 struct mount *mp = vnode_mount(vp);
2636 struct thread *td = ap->a_td;
2637 struct ucred *cred = ap->a_cred;
2638 char *prefix;
2639 char *attr_str;
2640 size_t len;
2641 int err;
2642
2643 if (fuse_isdeadfs(vp))
2644 return (EXTERROR(ENXIO, "This FUSE session is about "
2645 "to be closed"));
2646
2647 if (fsess_not_impl(mp, FUSE_GETXATTR))
2648 return (EXTERROR(EOPNOTSUPP, "This server does not implement "
2649 "extended attributes"));
2650
2651 err = fuse_extattr_check_cred(vp, ap->a_attrnamespace, cred, td, VREAD);
2652 if (err)
2653 return err;
2654
2655 /* Default to looking for user attributes. */
2656 if (ap->a_attrnamespace == EXTATTR_NAMESPACE_SYSTEM)
2657 prefix = EXTATTR_NAMESPACE_SYSTEM_STRING;
2658 else
2659 prefix = EXTATTR_NAMESPACE_USER_STRING;
2660
2661 len = strlen(prefix) + sizeof(extattr_namespace_separator) +
2662 strlen(ap->a_name) + 1;
2663
2664 fdisp_init(&fdi, len + sizeof(*get_xattr_in));
2665 fdisp_make_vp(&fdi, FUSE_GETXATTR, vp, td, cred);
2666
2667 get_xattr_in = fdi.indata;
2668 /*
2669 * Check to see whether we're querying the available size or
2670 * issuing the actual request. If we pass in 0, we get back struct
2671 * fuse_getxattr_out. If we pass in a non-zero size, we get back
2672 * that much data, without the struct fuse_getxattr_out header.
2673 */
2674 if (uio == NULL)
2675 get_xattr_in->size = 0;
2676 else
2677 get_xattr_in->size = uio->uio_resid;
2678
2679 attr_str = (char *)fdi.indata + sizeof(*get_xattr_in);
2680 snprintf(attr_str, len, "%s%c%s", prefix, extattr_namespace_separator,
2681 ap->a_name);
2682
2683 err = fdisp_wait_answ(&fdi);
2684 if (err != 0) {
2685 if (err == ENOSYS) {
2686 fsess_set_notimpl(mp, FUSE_GETXATTR);
2687 err = (EXTERROR(EOPNOTSUPP, "This server does not "
2688 "implement extended attributes"));
2689 }
2690 goto out;
2691 }
2692
2693 get_xattr_out = fdi.answ;
2694
2695 if (ap->a_size != NULL)
2696 *ap->a_size = get_xattr_out->size;
2697
2698 if (uio != NULL)
2699 err = uiomove(fdi.answ, fdi.iosize, uio);
2700
2701 out:
2702 fdisp_destroy(&fdi);
2703 return (err);
2704 }
2705
2706 /*
2707 struct vop_setextattr_args {
2708 struct vop_generic_args a_gen;
2709 struct vnode *a_vp;
2710 int a_attrnamespace;
2711 const char *a_name;
2712 struct uio *a_uio;
2713 struct ucred *a_cred;
2714 struct thread *a_td;
2715 };
2716 */
2717 static int
fuse_vnop_setextattr(struct vop_setextattr_args * ap)2718 fuse_vnop_setextattr(struct vop_setextattr_args *ap)
2719 {
2720 struct vnode *vp = ap->a_vp;
2721 struct uio *uio = ap->a_uio;
2722 struct fuse_dispatcher fdi;
2723 struct fuse_setxattr_in *set_xattr_in;
2724 struct mount *mp = vnode_mount(vp);
2725 struct thread *td = ap->a_td;
2726 struct ucred *cred = ap->a_cred;
2727 size_t struct_size = FUSE_COMPAT_SETXATTR_IN_SIZE;
2728 char *prefix;
2729 size_t len;
2730 char *attr_str;
2731 int err;
2732
2733 if (fuse_isdeadfs(vp))
2734 return (EXTERROR(ENXIO, "This FUSE session is about "
2735 "to be closed"));
2736
2737 if (fsess_not_impl(mp, FUSE_SETXATTR))
2738 return (EXTERROR(EOPNOTSUPP, "This server does not implement "
2739 "setting extended attributes"));
2740
2741 if (vfs_isrdonly(mp))
2742 return EROFS;
2743
2744 /* Deleting xattrs must use VOP_DELETEEXTATTR instead */
2745 if (ap->a_uio == NULL) {
2746 /*
2747 * If we got here as fallback from VOP_DELETEEXTATTR, then
2748 * return EOPNOTSUPP.
2749 */
2750 if (fsess_not_impl(mp, FUSE_REMOVEXATTR))
2751 return (EXTERROR(EOPNOTSUPP, "This server does not "
2752 "implement removing extended attributess"));
2753 else
2754 return (EXTERROR(EINVAL, "DELETEEXTATTR should be used "
2755 "to remove extattrs"));
2756 }
2757
2758 err = fuse_extattr_check_cred(vp, ap->a_attrnamespace, cred, td,
2759 VWRITE);
2760 if (err)
2761 return err;
2762
2763 /* Default to looking for user attributes. */
2764 if (ap->a_attrnamespace == EXTATTR_NAMESPACE_SYSTEM)
2765 prefix = EXTATTR_NAMESPACE_SYSTEM_STRING;
2766 else
2767 prefix = EXTATTR_NAMESPACE_USER_STRING;
2768
2769 len = strlen(prefix) + sizeof(extattr_namespace_separator) +
2770 strlen(ap->a_name) + 1;
2771
2772 /* older FUSE servers use a smaller fuse_setxattr_in struct*/
2773 if (fuse_libabi_geq(fuse_get_mpdata(mp), 7, 33))
2774 struct_size = sizeof(*set_xattr_in);
2775
2776 fdisp_init(&fdi, len + struct_size + uio->uio_resid);
2777 fdisp_make_vp(&fdi, FUSE_SETXATTR, vp, td, cred);
2778
2779 set_xattr_in = fdi.indata;
2780 set_xattr_in->size = uio->uio_resid;
2781
2782 if (fuse_libabi_geq(fuse_get_mpdata(mp), 7, 33)) {
2783 set_xattr_in->setxattr_flags = 0;
2784 set_xattr_in->padding = 0;
2785 }
2786
2787 attr_str = (char *)fdi.indata + struct_size;
2788 snprintf(attr_str, len, "%s%c%s", prefix, extattr_namespace_separator,
2789 ap->a_name);
2790
2791 err = uiomove((char *)fdi.indata + struct_size + len,
2792 uio->uio_resid, uio);
2793 if (err != 0) {
2794 goto out;
2795 }
2796
2797 err = fdisp_wait_answ(&fdi);
2798
2799 if (err == ENOSYS) {
2800 fsess_set_notimpl(mp, FUSE_SETXATTR);
2801 err = EXTERROR(EOPNOTSUPP, "This server does not implement "
2802 "setting extended attributes");
2803 }
2804 if (err == ERESTART) {
2805 /* Can't restart after calling uiomove */
2806 err = EINTR;
2807 }
2808
2809 out:
2810 fdisp_destroy(&fdi);
2811 return (err);
2812 }
2813
2814 /*
2815 * The Linux / FUSE extended attribute list is simply a collection of
2816 * NUL-terminated strings. The FreeBSD extended attribute list is a single
2817 * byte length followed by a non-NUL terminated string. So, this allows
2818 * conversion of the Linux / FUSE format to the FreeBSD format in place.
2819 * Linux attribute names are reported with the namespace as a prefix (e.g.
2820 * "user.attribute_name"), but in FreeBSD they are reported without the
2821 * namespace prefix (e.g. "attribute_name"). So, we're going from:
2822 *
2823 * user.attr_name1\0user.attr_name2\0
2824 *
2825 * to:
2826 *
2827 * <num>attr_name1<num>attr_name2
2828 *
2829 * Where "<num>" is a single byte number of characters in the attribute name.
2830 *
2831 * Args:
2832 * prefix - exattr namespace prefix string
2833 * list, list_len - input list with namespace prefixes
2834 * bsd_list, bsd_list_len - output list compatible with bsd vfs
2835 */
2836 static int
fuse_xattrlist_convert(char * prefix,const char * list,int list_len,char * bsd_list,int * bsd_list_len)2837 fuse_xattrlist_convert(char *prefix, const char *list, int list_len,
2838 char *bsd_list, int *bsd_list_len)
2839 {
2840 int len, pos, dist_to_next, prefix_len;
2841
2842 pos = 0;
2843 *bsd_list_len = 0;
2844 prefix_len = strlen(prefix);
2845
2846 while (pos < list_len && list[pos] != '\0') {
2847 dist_to_next = strlen(&list[pos]) + 1;
2848 if (bcmp(&list[pos], prefix, prefix_len) == 0 &&
2849 list[pos + prefix_len] == extattr_namespace_separator) {
2850 len = dist_to_next -
2851 (prefix_len + sizeof(extattr_namespace_separator)) - 1;
2852 if (len >= EXTATTR_MAXNAMELEN)
2853 return (ENAMETOOLONG);
2854
2855 bsd_list[*bsd_list_len] = len;
2856 memcpy(&bsd_list[*bsd_list_len + 1],
2857 &list[pos + prefix_len +
2858 sizeof(extattr_namespace_separator)], len);
2859
2860 *bsd_list_len += len + 1;
2861 }
2862
2863 pos += dist_to_next;
2864 }
2865
2866 return (0);
2867 }
2868
2869 /*
2870 * List extended attributes
2871 *
2872 * The FUSE_LISTXATTR operation is based on Linux's listxattr(2) syscall, which
2873 * has a number of differences compared to its FreeBSD equivalent,
2874 * extattr_list_file:
2875 *
2876 * - FUSE_LISTXATTR returns all extended attributes across all namespaces,
2877 * whereas listxattr(2) only returns attributes for a single namespace
2878 * - FUSE_LISTXATTR prepends each attribute name with "namespace."
2879 * - If the provided buffer is not large enough to hold the result,
2880 * FUSE_LISTXATTR should return ERANGE, whereas listxattr is expected to
2881 * return as many results as will fit.
2882 */
2883 /*
2884 struct vop_listextattr_args {
2885 struct vop_generic_args a_gen;
2886 struct vnode *a_vp;
2887 int a_attrnamespace;
2888 struct uio *a_uio;
2889 size_t *a_size;
2890 struct ucred *a_cred;
2891 struct thread *a_td;
2892 };
2893 */
2894 static int
fuse_vnop_listextattr(struct vop_listextattr_args * ap)2895 fuse_vnop_listextattr(struct vop_listextattr_args *ap)
2896 {
2897 struct vnode *vp = ap->a_vp;
2898 struct uio *uio = ap->a_uio;
2899 struct fuse_dispatcher fdi;
2900 struct fuse_listxattr_in *list_xattr_in;
2901 struct fuse_listxattr_out *list_xattr_out;
2902 struct mount *mp = vnode_mount(vp);
2903 struct thread *td = ap->a_td;
2904 struct ucred *cred = ap->a_cred;
2905 char *prefix;
2906 char *bsd_list = NULL;
2907 char *linux_list;
2908 int bsd_list_len;
2909 int linux_list_len;
2910 int err;
2911
2912 if (fuse_isdeadfs(vp))
2913 return (EXTERROR(ENXIO, "This FUSE session is about "
2914 "to be closed"));
2915
2916 if (fsess_not_impl(mp, FUSE_LISTXATTR))
2917 return (EXTERROR(EOPNOTSUPP, "This server does not implement "
2918 "extended attributes"));
2919
2920 err = fuse_extattr_check_cred(vp, ap->a_attrnamespace, cred, td, VREAD);
2921 if (err)
2922 return err;
2923
2924 /*
2925 * Add space for a NUL and the period separator if enabled.
2926 * Default to looking for user attributes.
2927 */
2928 if (ap->a_attrnamespace == EXTATTR_NAMESPACE_SYSTEM)
2929 prefix = EXTATTR_NAMESPACE_SYSTEM_STRING;
2930 else
2931 prefix = EXTATTR_NAMESPACE_USER_STRING;
2932
2933 fdisp_init(&fdi, sizeof(*list_xattr_in));
2934 fdisp_make_vp(&fdi, FUSE_LISTXATTR, vp, td, cred);
2935
2936 /*
2937 * Retrieve Linux / FUSE compatible list size.
2938 */
2939 list_xattr_in = fdi.indata;
2940 list_xattr_in->size = 0;
2941
2942 err = fdisp_wait_answ(&fdi);
2943 if (err != 0) {
2944 if (err == ENOSYS) {
2945 fsess_set_notimpl(mp, FUSE_LISTXATTR);
2946 err = EXTERROR(EOPNOTSUPP, "This server does not "
2947 "implement extended attributes");
2948 }
2949 goto out;
2950 }
2951
2952 list_xattr_out = fdi.answ;
2953 linux_list_len = list_xattr_out->size;
2954 if (linux_list_len == 0) {
2955 if (ap->a_size != NULL)
2956 *ap->a_size = linux_list_len;
2957 goto out;
2958 }
2959
2960 /*
2961 * Retrieve Linux / FUSE compatible list values.
2962 */
2963 fdisp_refresh_vp(&fdi, FUSE_LISTXATTR, vp, td, cred);
2964 list_xattr_in = fdi.indata;
2965 list_xattr_in->size = linux_list_len;
2966
2967 err = fdisp_wait_answ(&fdi);
2968 if (err == ERANGE) {
2969 /*
2970 * Race detected. The attribute list must've grown since the
2971 * first FUSE_LISTXATTR call. Start over. Go all the way back
2972 * to userland so we can process signals, if necessary, before
2973 * restarting.
2974 */
2975 err = ERESTART;
2976 goto out;
2977 } else if (err != 0)
2978 goto out;
2979
2980 linux_list = fdi.answ;
2981 /* FUSE doesn't allow the server to return more data than requested */
2982 if (fdi.iosize > linux_list_len) {
2983 struct fuse_data *data = fuse_get_mpdata(mp);
2984
2985 fuse_warn(data, FSESS_WARN_LSEXTATTR_LONG,
2986 "server returned "
2987 "more extended attribute data than requested; "
2988 "should've returned ERANGE instead.");
2989 } else {
2990 /* But returning less data is fine */
2991 linux_list_len = fdi.iosize;
2992 }
2993
2994 /*
2995 * Retrieve the BSD compatible list values.
2996 * The Linux / FUSE attribute list format isn't the same
2997 * as FreeBSD's format. So we need to transform it into
2998 * FreeBSD's format before giving it to the user.
2999 */
3000 bsd_list = malloc(linux_list_len, M_TEMP, M_WAITOK);
3001 err = fuse_xattrlist_convert(prefix, linux_list, linux_list_len,
3002 bsd_list, &bsd_list_len);
3003 if (err != 0)
3004 goto out;
3005
3006 if (ap->a_size != NULL)
3007 *ap->a_size = bsd_list_len;
3008
3009 if (uio != NULL)
3010 err = uiomove(bsd_list, bsd_list_len, uio);
3011
3012 out:
3013 free(bsd_list, M_TEMP);
3014 fdisp_destroy(&fdi);
3015 return (err);
3016 }
3017
3018 /*
3019 struct vop_deallocate_args {
3020 struct vop_generic_args a_gen;
3021 struct vnode *a_vp;
3022 off_t *a_offset;
3023 off_t *a_len;
3024 int a_flags;
3025 int a_ioflag;
3026 struct ucred *a_cred;
3027 };
3028 */
3029 static int
fuse_vnop_deallocate(struct vop_deallocate_args * ap)3030 fuse_vnop_deallocate(struct vop_deallocate_args *ap)
3031 {
3032 struct vnode *vp = ap->a_vp;
3033 struct mount *mp = vnode_mount(vp);
3034 struct fuse_filehandle *fufh;
3035 struct fuse_dispatcher fdi;
3036 struct fuse_fallocate_in *ffi;
3037 struct ucred *cred = ap->a_cred;
3038 pid_t pid = curthread->td_proc->p_pid;
3039 off_t *len = ap->a_len;
3040 off_t *offset = ap->a_offset;
3041 int ioflag = ap->a_ioflag;
3042 off_t filesize;
3043 int err;
3044 bool closefufh = false;
3045
3046 if (fuse_isdeadfs(vp))
3047 return (EXTERROR(ENXIO, "This FUSE session is about "
3048 "to be closed"));
3049
3050 if (vfs_isrdonly(mp))
3051 return (EROFS);
3052
3053 if (fsess_not_impl(mp, FUSE_FALLOCATE))
3054 goto fallback;
3055
3056 err = fuse_filehandle_getrw(vp, FWRITE, &fufh, cred, pid);
3057 if (err == EBADF && vnode_mount(vp)->mnt_flag & MNT_EXPORTED) {
3058 /*
3059 * nfsd will do I/O without first doing VOP_OPEN. We
3060 * must implicitly open the file here
3061 */
3062 err = fuse_filehandle_open(vp, FWRITE, &fufh, curthread, cred);
3063 closefufh = true;
3064 }
3065 if (err)
3066 return (err);
3067
3068 fuse_vnode_update(vp, FN_MTIMECHANGE | FN_CTIMECHANGE);
3069
3070 err = fuse_vnode_size(vp, &filesize, cred, curthread);
3071 if (err)
3072 goto out;
3073 fuse_inval_buf_range(vp, filesize, *offset, *offset + *len);
3074
3075 fdisp_init(&fdi, sizeof(*ffi));
3076 fdisp_make_vp(&fdi, FUSE_FALLOCATE, vp, curthread, cred);
3077 ffi = fdi.indata;
3078 ffi->fh = fufh->fh_id;
3079 ffi->offset = *offset;
3080 ffi->length = *len;
3081 /*
3082 * FreeBSD's fspacectl is equivalent to Linux's fallocate with
3083 * mode == FALLOC_FL_PUNCH_HOLE | FALLOC_FL_KEEP_SIZE
3084 */
3085 ffi->mode = FUSE_FALLOC_FL_PUNCH_HOLE | FUSE_FALLOC_FL_KEEP_SIZE;
3086 err = fdisp_wait_answ(&fdi);
3087
3088 if (err == ENOSYS) {
3089 fdisp_destroy(&fdi);
3090 fsess_set_notimpl(mp, FUSE_FALLOCATE);
3091 goto fallback;
3092 } else if (err == EOPNOTSUPP) {
3093 /*
3094 * The file system server does not support FUSE_FALLOCATE with
3095 * the supplied mode for this particular file.
3096 */
3097 fdisp_destroy(&fdi);
3098 goto fallback;
3099 } else if (!err) {
3100 /*
3101 * Clip the returned offset to EoF. Do it here rather than
3102 * before FUSE_FALLOCATE just in case the kernel's cached file
3103 * size is out of date. Unfortunately, FUSE does not return
3104 * any information about filesize from that operation.
3105 */
3106 *offset = MIN(*offset + *len, filesize);
3107 *len = 0;
3108 fuse_vnode_undirty_cached_timestamps(vp, false);
3109 fuse_internal_clear_suid_on_write(vp, cred, curthread);
3110
3111 if (ioflag & IO_SYNC)
3112 err = fuse_internal_fsync(vp, curthread, MNT_WAIT,
3113 false);
3114 }
3115
3116 fdisp_destroy(&fdi);
3117 out:
3118 if (closefufh)
3119 fuse_filehandle_close(vp, fufh, curthread, cred);
3120
3121 return (err);
3122
3123 fallback:
3124 if (closefufh)
3125 fuse_filehandle_close(vp, fufh, curthread, cred);
3126
3127 return (vop_stddeallocate(ap));
3128 }
3129
3130 /*
3131 struct vop_deleteextattr_args {
3132 struct vop_generic_args a_gen;
3133 struct vnode *a_vp;
3134 int a_attrnamespace;
3135 const char *a_name;
3136 struct ucred *a_cred;
3137 struct thread *a_td;
3138 };
3139 */
3140 static int
fuse_vnop_deleteextattr(struct vop_deleteextattr_args * ap)3141 fuse_vnop_deleteextattr(struct vop_deleteextattr_args *ap)
3142 {
3143 struct vnode *vp = ap->a_vp;
3144 struct fuse_dispatcher fdi;
3145 struct mount *mp = vnode_mount(vp);
3146 struct thread *td = ap->a_td;
3147 struct ucred *cred = ap->a_cred;
3148 char *prefix;
3149 size_t len;
3150 char *attr_str;
3151 int err;
3152
3153 if (fuse_isdeadfs(vp))
3154 return (EXTERROR(ENXIO, "This FUSE session is about "
3155 "to be closed"));
3156
3157 if (fsess_not_impl(mp, FUSE_REMOVEXATTR))
3158 return (EXTERROR(EOPNOTSUPP, "This server does not implement "
3159 "removing extended attributes"));
3160
3161 if (vfs_isrdonly(mp))
3162 return EROFS;
3163
3164 err = fuse_extattr_check_cred(vp, ap->a_attrnamespace, cred, td,
3165 VWRITE);
3166 if (err)
3167 return err;
3168
3169 /* Default to looking for user attributes. */
3170 if (ap->a_attrnamespace == EXTATTR_NAMESPACE_SYSTEM)
3171 prefix = EXTATTR_NAMESPACE_SYSTEM_STRING;
3172 else
3173 prefix = EXTATTR_NAMESPACE_USER_STRING;
3174
3175 len = strlen(prefix) + sizeof(extattr_namespace_separator) +
3176 strlen(ap->a_name) + 1;
3177
3178 fdisp_init(&fdi, len);
3179 fdisp_make_vp(&fdi, FUSE_REMOVEXATTR, vp, td, cred);
3180
3181 attr_str = fdi.indata;
3182 snprintf(attr_str, len, "%s%c%s", prefix, extattr_namespace_separator,
3183 ap->a_name);
3184
3185 err = fdisp_wait_answ(&fdi);
3186 if (err == ENOSYS) {
3187 fsess_set_notimpl(mp, FUSE_REMOVEXATTR);
3188 err = EXTERROR(EOPNOTSUPP, "This server does not implement "
3189 "removing extended attributes");
3190 }
3191
3192 fdisp_destroy(&fdi);
3193 return (err);
3194 }
3195
3196 /*
3197 struct vnop_print_args {
3198 struct vnode *a_vp;
3199 };
3200 */
3201 static int
fuse_vnop_print(struct vop_print_args * ap)3202 fuse_vnop_print(struct vop_print_args *ap)
3203 {
3204 struct fuse_vnode_data *fvdat = VTOFUD(ap->a_vp);
3205
3206 printf("nodeid: %ju, parent nodeid: %ju, nlookup: %ju, flag: %#x\n",
3207 (uintmax_t)VTOILLU(ap->a_vp), (uintmax_t)fvdat->parent_nid,
3208 (uintmax_t)fvdat->nlookup,
3209 fvdat->flag);
3210
3211 return 0;
3212 }
3213
3214 /*
3215 * Get an NFS filehandle for a FUSE file.
3216 *
3217 * This will only work for FUSE file systems that guarantee the uniqueness of
3218 * nodeid:generation, which most don't.
3219 */
3220 /*
3221 vop_vptofh {
3222 IN struct vnode *a_vp;
3223 IN struct fid *a_fhp;
3224 };
3225 */
3226 static int
fuse_vnop_vptofh(struct vop_vptofh_args * ap)3227 fuse_vnop_vptofh(struct vop_vptofh_args *ap)
3228 {
3229 struct vnode *vp = ap->a_vp;
3230 struct fuse_vnode_data *fvdat = VTOFUD(vp);
3231 struct fuse_fid *fhp = (struct fuse_fid *)(ap->a_fhp);
3232 _Static_assert(sizeof(struct fuse_fid) <= sizeof(struct fid),
3233 "FUSE fid type is too big");
3234 struct mount *mp = vnode_mount(vp);
3235 struct fuse_data *data = fuse_get_mpdata(mp);
3236 struct vattr va;
3237 int err;
3238
3239 if (!(data->dataflags & FSESS_EXPORT_SUPPORT)) {
3240 /* NFS requires lookups for "." and ".." */
3241 SDT_PROBE2(fusefs, , vnops, trace, 1,
3242 "VOP_VPTOFH without FUSE_EXPORT_SUPPORT");
3243 return (EXTERROR(EOPNOTSUPP, "This server is "
3244 "missing FUSE_EXPORT_SUPPORT"));
3245 }
3246 if ((mp->mnt_flag & MNT_EXPORTED) &&
3247 fsess_is_impl(mp, FUSE_OPENDIR))
3248 {
3249 /*
3250 * NFS is stateless, so nfsd must reopen a directory on every
3251 * call to VOP_READDIR, passing in the d_off field from the
3252 * final dirent of the previous invocation. But if the server
3253 * implements FUSE_OPENDIR, the FUSE protocol does not
3254 * guarantee that d_off will be valid after a directory is
3255 * closed and reopened. So prohibit exporting FUSE file
3256 * systems that implement FUSE_OPENDIR.
3257 *
3258 * But userspace NFS servers don't have this problem.
3259 */
3260 SDT_PROBE2(fusefs, , vnops, trace, 1,
3261 "VOP_VPTOFH with FUSE_OPENDIR");
3262 return (EXTERROR(EOPNOTSUPP, "This server implements "
3263 "FUSE_OPENDIR so is not compatible with getfh"));
3264 }
3265
3266 err = fuse_internal_getattr(vp, &va, curthread->td_ucred, curthread);
3267 if (err)
3268 return err;
3269
3270 /*ip = VTOI(ap->a_vp);*/
3271 /*ufhp = (struct ufid *)ap->a_fhp;*/
3272 fhp->len = sizeof(struct fuse_fid);
3273 fhp->nid = fvdat->nid;
3274 if (fvdat->generation <= UINT32_MAX)
3275 fhp->gen = fvdat->generation;
3276 else
3277 return (EXTERROR(EOVERFLOW, "inode generation "
3278 "number overflow"));
3279 return (0);
3280 }
3281