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 * Redistribution and use in source and binary forms, with or without 37 * modification, are permitted provided that the following conditions 38 * are met: 39 * 1. Redistributions of source code must retain the above copyright 40 * notice, this list of conditions and the following disclaimer. 41 * 2. Redistributions in binary form must reproduce the above copyright 42 * notice, this list of conditions and the following disclaimer in the 43 * documentation and/or other materials provided with the distribution. 44 * 45 * THIS SOFTWARE IS PROVIDED BY AUTHOR AND CONTRIBUTORS ``AS IS'' AND 46 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 47 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 48 * ARE DISCLAIMED. IN NO EVENT SHALL AUTHOR OR CONTRIBUTORS BE LIABLE 49 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 50 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 51 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 52 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 53 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 54 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 55 * SUCH DAMAGE. 56 */ 57 58 #include <sys/cdefs.h> 59 __FBSDID("$FreeBSD$"); 60 61 #include <sys/types.h> 62 #include <sys/module.h> 63 #include <sys/systm.h> 64 #include <sys/errno.h> 65 #include <sys/param.h> 66 #include <sys/kernel.h> 67 #include <sys/conf.h> 68 #include <sys/uio.h> 69 #include <sys/malloc.h> 70 #include <sys/queue.h> 71 #include <sys/lock.h> 72 #include <sys/mutex.h> 73 #include <sys/sx.h> 74 #include <sys/proc.h> 75 #include <sys/mount.h> 76 #include <sys/vnode.h> 77 #include <sys/namei.h> 78 #include <sys/stat.h> 79 #include <sys/unistd.h> 80 #include <sys/filedesc.h> 81 #include <sys/file.h> 82 #include <sys/fcntl.h> 83 #include <sys/dirent.h> 84 #include <sys/bio.h> 85 #include <sys/buf.h> 86 #include <sys/sysctl.h> 87 #include <sys/priv.h> 88 89 #include "fuse.h" 90 #include "fuse_file.h" 91 #include "fuse_internal.h" 92 #include "fuse_ipc.h" 93 #include "fuse_node.h" 94 #include "fuse_file.h" 95 #include "fuse_param.h" 96 97 #define FUSE_DEBUG_MODULE INTERNAL 98 #include "fuse_debug.h" 99 100 #ifdef ZERO_PAD_INCOMPLETE_BUFS 101 static int isbzero(void *buf, size_t len); 102 103 #endif 104 105 /* access */ 106 107 int 108 fuse_internal_access(struct vnode *vp, 109 mode_t mode, 110 struct fuse_access_param *facp, 111 struct thread *td, 112 struct ucred *cred) 113 { 114 int err = 0; 115 uint32_t mask = 0; 116 int dataflags; 117 int vtype; 118 struct mount *mp; 119 struct fuse_dispatcher fdi; 120 struct fuse_access_in *fai; 121 struct fuse_data *data; 122 123 /* NOT YET DONE */ 124 /* 125 * If this vnop gives you trouble, just return 0 here for a lazy 126 * kludge. 127 */ 128 /* return 0;*/ 129 130 fuse_trace_printf_func(); 131 132 mp = vnode_mount(vp); 133 vtype = vnode_vtype(vp); 134 135 data = fuse_get_mpdata(mp); 136 dataflags = data->dataflags; 137 138 if ((mode & VWRITE) && vfs_isrdonly(mp)) { 139 return EACCES; 140 } 141 /* Unless explicitly permitted, deny everyone except the fs owner. */ 142 if (vnode_isvroot(vp) && !(facp->facc_flags & FACCESS_NOCHECKSPY)) { 143 if (!(dataflags & FSESS_DAEMON_CAN_SPY)) { 144 int denied = fuse_match_cred(data->daemoncred, 145 cred); 146 147 if (denied) { 148 return EPERM; 149 } 150 } 151 facp->facc_flags |= FACCESS_NOCHECKSPY; 152 } 153 if (!(facp->facc_flags & FACCESS_DO_ACCESS)) { 154 return 0; 155 } 156 if (((vtype == VREG) && (mode & VEXEC))) { 157 #ifdef NEED_MOUNT_ARGUMENT_FOR_THIS 158 /* Let the kernel handle this through open / close heuristics.*/ 159 return ENOTSUP; 160 #else 161 /* Let the kernel handle this. */ 162 return 0; 163 #endif 164 } 165 if (!fsess_isimpl(mp, FUSE_ACCESS)) { 166 /* Let the kernel handle this. */ 167 return 0; 168 } 169 if (dataflags & FSESS_DEFAULT_PERMISSIONS) { 170 /* Let the kernel handle this. */ 171 return 0; 172 } 173 if ((mode & VADMIN) != 0) { 174 err = priv_check_cred(cred, PRIV_VFS_ADMIN); 175 if (err) { 176 return err; 177 } 178 } 179 if ((mode & (VWRITE | VAPPEND | VADMIN)) != 0) { 180 mask |= W_OK; 181 } 182 if ((mode & VREAD) != 0) { 183 mask |= R_OK; 184 } 185 if ((mode & VEXEC) != 0) { 186 mask |= X_OK; 187 } 188 bzero(&fdi, sizeof(fdi)); 189 190 fdisp_init(&fdi, sizeof(*fai)); 191 fdisp_make_vp(&fdi, FUSE_ACCESS, vp, td, cred); 192 193 fai = fdi.indata; 194 fai->mask = F_OK; 195 fai->mask |= mask; 196 197 err = fdisp_wait_answ(&fdi); 198 fdisp_destroy(&fdi); 199 200 if (err == ENOSYS) { 201 fsess_set_notimpl(mp, FUSE_ACCESS); 202 err = 0; 203 } 204 return err; 205 } 206 207 /* fsync */ 208 209 int 210 fuse_internal_fsync_callback(struct fuse_ticket *tick, struct uio *uio) 211 { 212 fuse_trace_printf_func(); 213 214 if (tick->tk_aw_ohead.error == ENOSYS) { 215 fsess_set_notimpl(tick->tk_data->mp, fticket_opcode(tick)); 216 } 217 return 0; 218 } 219 220 int 221 fuse_internal_fsync(struct vnode *vp, 222 struct thread *td, 223 struct ucred *cred, 224 struct fuse_filehandle *fufh) 225 { 226 int op = FUSE_FSYNC; 227 struct fuse_fsync_in *ffsi; 228 struct fuse_dispatcher fdi; 229 230 fuse_trace_printf_func(); 231 232 if (vnode_isdir(vp)) { 233 op = FUSE_FSYNCDIR; 234 } 235 fdisp_init(&fdi, sizeof(*ffsi)); 236 fdisp_make_vp(&fdi, op, vp, td, cred); 237 ffsi = fdi.indata; 238 ffsi->fh = fufh->fh_id; 239 240 ffsi->fsync_flags = 1; /* datasync */ 241 242 fuse_insert_callback(fdi.tick, fuse_internal_fsync_callback); 243 fuse_insert_message(fdi.tick); 244 245 fdisp_destroy(&fdi); 246 247 return 0; 248 249 } 250 251 /* readdir */ 252 253 int 254 fuse_internal_readdir(struct vnode *vp, 255 struct uio *uio, 256 struct fuse_filehandle *fufh, 257 struct fuse_iov *cookediov) 258 { 259 int err = 0; 260 struct fuse_dispatcher fdi; 261 struct fuse_read_in *fri; 262 263 if (uio_resid(uio) == 0) { 264 return 0; 265 } 266 fdisp_init(&fdi, 0); 267 268 /* 269 * Note that we DO NOT have a UIO_SYSSPACE here (so no need for p2p 270 * I/O). 271 */ 272 273 while (uio_resid(uio) > 0) { 274 275 fdi.iosize = sizeof(*fri); 276 fdisp_make_vp(&fdi, FUSE_READDIR, vp, NULL, NULL); 277 278 fri = fdi.indata; 279 fri->fh = fufh->fh_id; 280 fri->offset = uio_offset(uio); 281 fri->size = min(uio_resid(uio), FUSE_DEFAULT_IOSIZE); 282 /* mp->max_read */ 283 284 if ((err = fdisp_wait_answ(&fdi))) { 285 break; 286 } 287 if ((err = fuse_internal_readdir_processdata(uio, fri->size, fdi.answ, 288 fdi.iosize, cookediov))) { 289 break; 290 } 291 } 292 293 fdisp_destroy(&fdi); 294 return ((err == -1) ? 0 : err); 295 } 296 297 int 298 fuse_internal_readdir_processdata(struct uio *uio, 299 size_t reqsize, 300 void *buf, 301 size_t bufsize, 302 void *param) 303 { 304 int err = 0; 305 int cou = 0; 306 int bytesavail; 307 size_t freclen; 308 309 struct dirent *de; 310 struct fuse_dirent *fudge; 311 struct fuse_iov *cookediov = param; 312 313 if (bufsize < FUSE_NAME_OFFSET) { 314 return -1; 315 } 316 for (;;) { 317 318 if (bufsize < FUSE_NAME_OFFSET) { 319 err = -1; 320 break; 321 } 322 fudge = (struct fuse_dirent *)buf; 323 freclen = FUSE_DIRENT_SIZE(fudge); 324 325 cou++; 326 327 if (bufsize < freclen) { 328 err = ((cou == 1) ? -1 : 0); 329 break; 330 } 331 #ifdef ZERO_PAD_INCOMPLETE_BUFS 332 if (isbzero(buf, FUSE_NAME_OFFSET)) { 333 err = -1; 334 break; 335 } 336 #endif 337 338 if (!fudge->namelen || fudge->namelen > MAXNAMLEN) { 339 err = EINVAL; 340 break; 341 } 342 bytesavail = GENERIC_DIRSIZ((struct pseudo_dirent *) 343 &fudge->namelen); 344 345 if (bytesavail > uio_resid(uio)) { 346 err = -1; 347 break; 348 } 349 fiov_refresh(cookediov); 350 fiov_adjust(cookediov, bytesavail); 351 352 de = (struct dirent *)cookediov->base; 353 de->d_fileno = fudge->ino; 354 de->d_reclen = bytesavail; 355 de->d_type = fudge->type; 356 de->d_namlen = fudge->namelen; 357 memcpy((char *)cookediov->base + sizeof(struct dirent) - 358 MAXNAMLEN - 1, 359 (char *)buf + FUSE_NAME_OFFSET, fudge->namelen); 360 dirent_terminate(de); 361 362 err = uiomove(cookediov->base, cookediov->len, uio); 363 if (err) { 364 break; 365 } 366 buf = (char *)buf + freclen; 367 bufsize -= freclen; 368 uio_setoffset(uio, fudge->off); 369 } 370 371 return err; 372 } 373 374 /* remove */ 375 376 int 377 fuse_internal_remove(struct vnode *dvp, 378 struct vnode *vp, 379 struct componentname *cnp, 380 enum fuse_opcode op) 381 { 382 struct fuse_dispatcher fdi; 383 struct fuse_vnode_data *fvdat; 384 int err; 385 386 err = 0; 387 fvdat = VTOFUD(vp); 388 389 debug_printf("dvp=%p, cnp=%p, op=%d\n", vp, cnp, op); 390 391 fdisp_init(&fdi, cnp->cn_namelen + 1); 392 fdisp_make_vp(&fdi, op, dvp, cnp->cn_thread, cnp->cn_cred); 393 394 memcpy(fdi.indata, cnp->cn_nameptr, cnp->cn_namelen); 395 ((char *)fdi.indata)[cnp->cn_namelen] = '\0'; 396 397 err = fdisp_wait_answ(&fdi); 398 fdisp_destroy(&fdi); 399 return err; 400 } 401 402 /* rename */ 403 404 int 405 fuse_internal_rename(struct vnode *fdvp, 406 struct componentname *fcnp, 407 struct vnode *tdvp, 408 struct componentname *tcnp) 409 { 410 struct fuse_dispatcher fdi; 411 struct fuse_rename_in *fri; 412 int err = 0; 413 414 fdisp_init(&fdi, sizeof(*fri) + fcnp->cn_namelen + tcnp->cn_namelen + 2); 415 fdisp_make_vp(&fdi, FUSE_RENAME, fdvp, tcnp->cn_thread, tcnp->cn_cred); 416 417 fri = fdi.indata; 418 fri->newdir = VTOI(tdvp); 419 memcpy((char *)fdi.indata + sizeof(*fri), fcnp->cn_nameptr, 420 fcnp->cn_namelen); 421 ((char *)fdi.indata)[sizeof(*fri) + fcnp->cn_namelen] = '\0'; 422 memcpy((char *)fdi.indata + sizeof(*fri) + fcnp->cn_namelen + 1, 423 tcnp->cn_nameptr, tcnp->cn_namelen); 424 ((char *)fdi.indata)[sizeof(*fri) + fcnp->cn_namelen + 425 tcnp->cn_namelen + 1] = '\0'; 426 427 err = fdisp_wait_answ(&fdi); 428 fdisp_destroy(&fdi); 429 return err; 430 } 431 432 /* strategy */ 433 434 /* entity creation */ 435 436 void 437 fuse_internal_newentry_makerequest(struct mount *mp, 438 uint64_t dnid, 439 struct componentname *cnp, 440 enum fuse_opcode op, 441 void *buf, 442 size_t bufsize, 443 struct fuse_dispatcher *fdip) 444 { 445 debug_printf("fdip=%p\n", fdip); 446 447 fdip->iosize = bufsize + cnp->cn_namelen + 1; 448 449 fdisp_make(fdip, op, mp, dnid, cnp->cn_thread, cnp->cn_cred); 450 memcpy(fdip->indata, buf, bufsize); 451 memcpy((char *)fdip->indata + bufsize, cnp->cn_nameptr, cnp->cn_namelen); 452 ((char *)fdip->indata)[bufsize + cnp->cn_namelen] = '\0'; 453 } 454 455 int 456 fuse_internal_newentry_core(struct vnode *dvp, 457 struct vnode **vpp, 458 struct componentname *cnp, 459 enum vtype vtyp, 460 struct fuse_dispatcher *fdip) 461 { 462 int err = 0; 463 struct fuse_entry_out *feo; 464 struct mount *mp = vnode_mount(dvp); 465 466 if ((err = fdisp_wait_answ(fdip))) { 467 return err; 468 } 469 feo = fdip->answ; 470 471 if ((err = fuse_internal_checkentry(feo, vtyp))) { 472 return err; 473 } 474 err = fuse_vnode_get(mp, feo, feo->nodeid, dvp, vpp, cnp, vtyp); 475 if (err) { 476 fuse_internal_forget_send(mp, cnp->cn_thread, cnp->cn_cred, 477 feo->nodeid, 1); 478 return err; 479 } 480 cache_attrs(*vpp, feo, NULL); 481 482 return err; 483 } 484 485 int 486 fuse_internal_newentry(struct vnode *dvp, 487 struct vnode **vpp, 488 struct componentname *cnp, 489 enum fuse_opcode op, 490 void *buf, 491 size_t bufsize, 492 enum vtype vtype) 493 { 494 int err; 495 struct fuse_dispatcher fdi; 496 struct mount *mp = vnode_mount(dvp); 497 498 fdisp_init(&fdi, 0); 499 fuse_internal_newentry_makerequest(mp, VTOI(dvp), cnp, op, buf, 500 bufsize, &fdi); 501 err = fuse_internal_newentry_core(dvp, vpp, cnp, vtype, &fdi); 502 fdisp_destroy(&fdi); 503 504 return err; 505 } 506 507 /* entity destruction */ 508 509 int 510 fuse_internal_forget_callback(struct fuse_ticket *ftick, struct uio *uio) 511 { 512 fuse_internal_forget_send(ftick->tk_data->mp, curthread, NULL, 513 ((struct fuse_in_header *)ftick->tk_ms_fiov.base)->nodeid, 1); 514 515 return 0; 516 } 517 518 void 519 fuse_internal_forget_send(struct mount *mp, 520 struct thread *td, 521 struct ucred *cred, 522 uint64_t nodeid, 523 uint64_t nlookup) 524 { 525 526 struct fuse_dispatcher fdi; 527 struct fuse_forget_in *ffi; 528 529 debug_printf("mp=%p, nodeid=%ju, nlookup=%ju\n", 530 mp, (uintmax_t)nodeid, (uintmax_t)nlookup); 531 532 /* 533 * KASSERT(nlookup > 0, ("zero-times forget for vp #%llu", 534 * (long long unsigned) nodeid)); 535 */ 536 537 fdisp_init(&fdi, sizeof(*ffi)); 538 fdisp_make(&fdi, FUSE_FORGET, mp, nodeid, td, cred); 539 540 ffi = fdi.indata; 541 ffi->nlookup = nlookup; 542 543 fuse_insert_message(fdi.tick); 544 fdisp_destroy(&fdi); 545 } 546 547 void 548 fuse_internal_vnode_disappear(struct vnode *vp) 549 { 550 struct fuse_vnode_data *fvdat = VTOFUD(vp); 551 552 ASSERT_VOP_ELOCKED(vp, "fuse_internal_vnode_disappear"); 553 fvdat->flag |= FN_REVOKED; 554 fvdat->valid_attr_cache = false; 555 cache_purge(vp); 556 } 557 558 /* fuse start/stop */ 559 560 int 561 fuse_internal_init_callback(struct fuse_ticket *tick, struct uio *uio) 562 { 563 int err = 0; 564 struct fuse_data *data = tick->tk_data; 565 struct fuse_init_out *fiio; 566 567 if ((err = tick->tk_aw_ohead.error)) { 568 goto out; 569 } 570 if ((err = fticket_pull(tick, uio))) { 571 goto out; 572 } 573 fiio = fticket_resp(tick)->base; 574 575 /* XXX: Do we want to check anything further besides this? */ 576 if (fiio->major < 7) { 577 debug_printf("userpace version too low\n"); 578 err = EPROTONOSUPPORT; 579 goto out; 580 } 581 data->fuse_libabi_major = fiio->major; 582 data->fuse_libabi_minor = fiio->minor; 583 584 if (fuse_libabi_geq(data, 7, 5)) { 585 if (fticket_resp(tick)->len == sizeof(struct fuse_init_out)) { 586 data->max_write = fiio->max_write; 587 } else { 588 err = EINVAL; 589 } 590 } else { 591 /* Old fix values */ 592 data->max_write = 4096; 593 } 594 595 out: 596 if (err) { 597 fdata_set_dead(data); 598 } 599 FUSE_LOCK(); 600 data->dataflags |= FSESS_INITED; 601 wakeup(&data->ticketer); 602 FUSE_UNLOCK(); 603 604 return 0; 605 } 606 607 void 608 fuse_internal_send_init(struct fuse_data *data, struct thread *td) 609 { 610 struct fuse_init_in *fiii; 611 struct fuse_dispatcher fdi; 612 613 fdisp_init(&fdi, sizeof(*fiii)); 614 fdisp_make(&fdi, FUSE_INIT, data->mp, 0, td, NULL); 615 fiii = fdi.indata; 616 fiii->major = FUSE_KERNEL_VERSION; 617 fiii->minor = FUSE_KERNEL_MINOR_VERSION; 618 fiii->max_readahead = FUSE_DEFAULT_IOSIZE * 16; 619 fiii->flags = 0; 620 621 fuse_insert_callback(fdi.tick, fuse_internal_init_callback); 622 fuse_insert_message(fdi.tick); 623 fdisp_destroy(&fdi); 624 } 625 626 #ifdef ZERO_PAD_INCOMPLETE_BUFS 627 static int 628 isbzero(void *buf, size_t len) 629 { 630 int i; 631 632 for (i = 0; i < len; i++) { 633 if (((char *)buf)[i]) 634 return (0); 635 } 636 637 return (1); 638 } 639 640 #endif 641