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