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