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