1 /*- 2 * SPDX-License-Identifier: BSD-3-Clause 3 * 4 * Copyright (c) 2007-2009 Google Inc. and Amit Singh 5 * All rights reserved. 6 * 7 * Redistribution and use in source and binary forms, with or without 8 * modification, are permitted provided that the following conditions are 9 * met: 10 * 11 * * Redistributions of source code must retain the above copyright 12 * notice, this list of conditions and the following disclaimer. 13 * * Redistributions in binary form must reproduce the above 14 * copyright notice, this list of conditions and the following disclaimer 15 * in the documentation and/or other materials provided with the 16 * distribution. 17 * * Neither the name of Google Inc. nor the names of its 18 * contributors may be used to endorse or promote products derived from 19 * this software without specific prior written permission. 20 * 21 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS 22 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT 23 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR 24 * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT 25 * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, 26 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT 27 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, 28 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY 29 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 30 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE 31 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 32 * 33 * Copyright (C) 2005 Csaba Henk. 34 * All rights reserved. 35 * 36 * Copyright (c) 2019 The FreeBSD Foundation 37 * 38 * Portions of this software were developed by BFF Storage Systems, LLC under 39 * sponsorship from the FreeBSD Foundation. 40 * 41 * Redistribution and use in source and binary forms, with or without 42 * modification, are permitted provided that the following conditions 43 * are met: 44 * 1. Redistributions of source code must retain the above copyright 45 * notice, this list of conditions and the following disclaimer. 46 * 2. Redistributions in binary form must reproduce the above copyright 47 * notice, this list of conditions and the following disclaimer in the 48 * documentation and/or other materials provided with the distribution. 49 * 50 * THIS SOFTWARE IS PROVIDED BY AUTHOR AND CONTRIBUTORS ``AS IS'' AND 51 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 52 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 53 * ARE DISCLAIMED. IN NO EVENT SHALL AUTHOR OR CONTRIBUTORS BE LIABLE 54 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 55 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 56 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 57 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 58 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 59 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 60 * SUCH DAMAGE. 61 */ 62 63 #include <sys/cdefs.h> 64 #include <sys/types.h> 65 #include <sys/systm.h> 66 #include <sys/counter.h> 67 #include <sys/module.h> 68 #include <sys/errno.h> 69 #include <sys/param.h> 70 #include <sys/kernel.h> 71 #include <sys/conf.h> 72 #include <sys/uio.h> 73 #include <sys/malloc.h> 74 #include <sys/queue.h> 75 #include <sys/lock.h> 76 #include <sys/sx.h> 77 #include <sys/mutex.h> 78 #include <sys/proc.h> 79 #include <sys/vnode.h> 80 #include <sys/namei.h> 81 #include <sys/mount.h> 82 #include <sys/sysctl.h> 83 #include <sys/fcntl.h> 84 #include <sys/priv.h> 85 #include <sys/buf.h> 86 #include <security/mac/mac_framework.h> 87 #include <vm/vm.h> 88 #include <vm/vm_extern.h> 89 90 #include "fuse.h" 91 #include "fuse_node.h" 92 #include "fuse_internal.h" 93 #include "fuse_io.h" 94 #include "fuse_ipc.h" 95 96 SDT_PROVIDER_DECLARE(fusefs); 97 /* 98 * Fuse trace probe: 99 * arg0: verbosity. Higher numbers give more verbose messages 100 * arg1: Textual message 101 */ 102 SDT_PROBE_DEFINE2(fusefs, , node, trace, "int", "char*"); 103 104 MALLOC_DEFINE(M_FUSEVN, "fuse_vnode", "fuse vnode private data"); 105 106 static int sysctl_fuse_cache_mode(SYSCTL_HANDLER_ARGS); 107 108 static counter_u64_t fuse_node_count; 109 110 SYSCTL_COUNTER_U64(_vfs_fusefs_stats, OID_AUTO, node_count, CTLFLAG_RD, 111 &fuse_node_count, "Count of FUSE vnodes"); 112 113 int fuse_data_cache_mode = FUSE_CACHE_WT; 114 115 /* 116 * OBSOLETE 117 * This sysctl is no longer needed as of fuse protocol 7.23. Now, individual 118 * servers can select the cache behavior they need for each mountpoint: 119 * - writethrough: the default 120 * - writeback: set FUSE_WRITEBACK_CACHE in fuse_init_out.flags 121 * - uncached: set FOPEN_DIRECT_IO for every file 122 * The sysctl is retained primarily due to the enduring popularity of libfuse2, 123 * which is frozen at protocol version 7.19. As of 4-April-2024, 90% of 124 * FreeBSD ports that use libfuse still bind to libfuse2. 125 */ 126 SYSCTL_PROC(_vfs_fusefs, OID_AUTO, data_cache_mode, 127 CTLTYPE_INT | CTLFLAG_MPSAFE | CTLFLAG_RW, 128 &fuse_data_cache_mode, 0, sysctl_fuse_cache_mode, "I", 129 "Zero: disable caching of FUSE file data; One: write-through caching " 130 "(default); Two: write-back caching (generally unsafe)"); 131 132 static int 133 sysctl_fuse_cache_mode(SYSCTL_HANDLER_ARGS) 134 { 135 int val, error; 136 137 val = *(int *)arg1; 138 error = sysctl_handle_int(oidp, &val, 0, req); 139 if (error || !req->newptr) 140 return (error); 141 142 switch (val) { 143 case FUSE_CACHE_UC: 144 case FUSE_CACHE_WT: 145 case FUSE_CACHE_WB: 146 *(int *)arg1 = val; 147 break; 148 default: 149 return (EDOM); 150 } 151 return (0); 152 } 153 154 static void 155 fuse_vnode_init(struct vnode *vp, struct fuse_vnode_data *fvdat, 156 uint64_t nodeid, __enum_uint8(vtype) vtyp) 157 { 158 fvdat->nid = nodeid; 159 LIST_INIT(&fvdat->handles); 160 161 vattr_null(&fvdat->cached_attrs); 162 fvdat->cached_attrs.va_birthtime.tv_sec = -1; 163 fvdat->cached_attrs.va_birthtime.tv_nsec = 0; 164 fvdat->cached_attrs.va_fsid = VNOVAL; 165 fvdat->cached_attrs.va_gen = 0; 166 fvdat->cached_attrs.va_rdev = NODEV; 167 168 if (nodeid == FUSE_ROOT_ID) { 169 vp->v_vflag |= VV_ROOT; 170 } 171 vp->v_type = vtyp; 172 vp->v_data = fvdat; 173 cluster_init_vn(&fvdat->clusterw); 174 timespecclear(&fvdat->last_local_modify); 175 176 counter_u64_add(fuse_node_count, 1); 177 } 178 179 void 180 fuse_vnode_destroy(struct vnode *vp) 181 { 182 struct fuse_vnode_data *fvdat = vp->v_data; 183 184 vp->v_data = NULL; 185 KASSERT(LIST_EMPTY(&fvdat->handles), 186 ("Destroying fuse vnode with open files!")); 187 free(fvdat, M_FUSEVN); 188 189 counter_u64_add(fuse_node_count, -1); 190 } 191 192 int 193 fuse_vnode_cmp(struct vnode *vp, void *nidp) 194 { 195 return (VTOI(vp) != *((uint64_t *)nidp)); 196 } 197 198 SDT_PROBE_DEFINE3(fusefs, , node, stale_vnode, "struct vnode*", "uint8_t", 199 "uint64_t"); 200 static int 201 fuse_vnode_alloc(struct mount *mp, 202 struct thread *td, 203 uint64_t nodeid, 204 __enum_uint8(vtype) vtyp, 205 struct vnode **vpp) 206 { 207 struct fuse_data *data; 208 struct fuse_vnode_data *fvdat; 209 struct vnode *vp2; 210 int err = 0; 211 212 data = fuse_get_mpdata(mp); 213 if (vtyp == VNON) { 214 return EINVAL; 215 } 216 *vpp = NULL; 217 err = vfs_hash_get(mp, fuse_vnode_hash(nodeid), LK_EXCLUSIVE, td, vpp, 218 fuse_vnode_cmp, &nodeid); 219 if (err) 220 return (err); 221 222 if (*vpp) { 223 if ((*vpp)->v_type == vtyp) { 224 /* Reuse a vnode that hasn't yet been reclaimed */ 225 MPASS((*vpp)->v_data != NULL); 226 MPASS(VTOFUD(*vpp)->nid == nodeid); 227 SDT_PROBE2(fusefs, , node, trace, 1, 228 "vnode taken from hash"); 229 return (0); 230 } else { 231 /* 232 * The inode changed types! If we get here, we can't 233 * tell whether the inode's entry cache had expired 234 * yet. So this could be the result of a buggy server, 235 * but more likely the server just reused an inode 236 * number following an entry cache expiration. 237 */ 238 SDT_PROBE3(fusefs, , node, stale_vnode, *vpp, vtyp, 239 nodeid); 240 fuse_internal_vnode_disappear(*vpp); 241 vgone(*vpp); 242 lockmgr((*vpp)->v_vnlock, LK_RELEASE, NULL); 243 } 244 } 245 fvdat = malloc(sizeof(*fvdat), M_FUSEVN, M_WAITOK | M_ZERO); 246 switch (vtyp) { 247 case VFIFO: 248 err = getnewvnode("fuse", mp, &fuse_fifoops, vpp); 249 break; 250 default: 251 err = getnewvnode("fuse", mp, &fuse_vnops, vpp); 252 break; 253 } 254 if (err) { 255 free(fvdat, M_FUSEVN); 256 return (err); 257 } 258 lockmgr((*vpp)->v_vnlock, LK_EXCLUSIVE, NULL); 259 fuse_vnode_init(*vpp, fvdat, nodeid, vtyp); 260 err = insmntque(*vpp, mp); 261 ASSERT_VOP_ELOCKED(*vpp, "fuse_vnode_alloc"); 262 if (err) { 263 lockmgr((*vpp)->v_vnlock, LK_RELEASE, NULL); 264 free(fvdat, M_FUSEVN); 265 *vpp = NULL; 266 return (err); 267 } 268 /* Disallow async reads for fifos because UFS does. I don't know why */ 269 if (data->dataflags & FSESS_ASYNC_READ && vtyp != VFIFO) 270 VN_LOCK_ASHARE(*vpp); 271 272 vn_set_state(*vpp, VSTATE_CONSTRUCTED); 273 err = vfs_hash_insert(*vpp, fuse_vnode_hash(nodeid), LK_EXCLUSIVE, 274 td, &vp2, fuse_vnode_cmp, &nodeid); 275 if (err) { 276 lockmgr((*vpp)->v_vnlock, LK_RELEASE, NULL); 277 free(fvdat, M_FUSEVN); 278 *vpp = NULL; 279 return (err); 280 } 281 if (vp2 != NULL) { 282 *vpp = vp2; 283 return (0); 284 } 285 286 ASSERT_VOP_ELOCKED(*vpp, "fuse_vnode_alloc"); 287 288 return (0); 289 } 290 291 int 292 fuse_vnode_get(struct mount *mp, 293 struct fuse_entry_out *feo, 294 uint64_t nodeid, 295 struct vnode *dvp, 296 struct vnode **vpp, 297 struct componentname *cnp, 298 __enum_uint8(vtype) vtyp) 299 { 300 struct thread *td = curthread; 301 /* 302 * feo should only be NULL for the root directory, which (when libfuse 303 * is used) always has generation 0 304 */ 305 uint64_t generation = feo ? feo->generation : 0; 306 int err = 0; 307 308 if (dvp != NULL && VTOFUD(dvp)->nid == nodeid) { 309 fuse_warn(fuse_get_mpdata(mp), FSESS_WARN_ILLEGAL_INODE, 310 "Assigned same inode to both parent and child."); 311 return EIO; 312 } 313 314 err = fuse_vnode_alloc(mp, td, nodeid, vtyp, vpp); 315 if (err) { 316 return err; 317 } 318 if (dvp != NULL) { 319 MPASS(cnp && (cnp->cn_flags & ISDOTDOT) == 0); 320 MPASS(cnp && 321 !(cnp->cn_namelen == 1 && cnp->cn_nameptr[0] == '.')); 322 fuse_vnode_setparent(*vpp, dvp); 323 } 324 if (dvp != NULL && cnp != NULL && (cnp->cn_flags & MAKEENTRY) != 0 && 325 feo != NULL && 326 (feo->entry_valid != 0 || feo->entry_valid_nsec != 0)) { 327 struct timespec timeout; 328 329 ASSERT_VOP_LOCKED(*vpp, "fuse_vnode_get"); 330 ASSERT_VOP_LOCKED(dvp, "fuse_vnode_get"); 331 332 fuse_validity_2_timespec(feo, &timeout); 333 cache_enter_time(dvp, *vpp, cnp, &timeout, NULL); 334 } 335 336 VTOFUD(*vpp)->generation = generation; 337 /* 338 * In userland, libfuse uses cached lookups for dot and dotdot entries, 339 * thus it does not really bump the nlookup counter for forget. 340 * Follow the same semantic and avoid the bump in order to keep 341 * nlookup counters consistent. 342 */ 343 if (cnp == NULL || ((cnp->cn_flags & ISDOTDOT) == 0 && 344 (cnp->cn_namelen != 1 || cnp->cn_nameptr[0] != '.'))) 345 VTOFUD(*vpp)->nlookup++; 346 347 return 0; 348 } 349 350 /* 351 * Called for every fusefs vnode open to initialize the vnode (not 352 * fuse_filehandle) for use 353 */ 354 void 355 fuse_vnode_open(struct vnode *vp, int32_t fuse_open_flags, struct thread *td) 356 { 357 if (vnode_vtype(vp) == VREG) 358 vnode_create_vobject(vp, 0, td); 359 } 360 361 int 362 fuse_vnode_savesize(struct vnode *vp, struct ucred *cred, pid_t pid) 363 { 364 struct fuse_vnode_data *fvdat = VTOFUD(vp); 365 struct thread *td = curthread; 366 struct fuse_filehandle *fufh = NULL; 367 struct fuse_dispatcher fdi; 368 struct fuse_setattr_in *fsai; 369 int err = 0; 370 371 ASSERT_VOP_ELOCKED(vp, "fuse_io_extend"); 372 373 if (fuse_isdeadfs(vp)) { 374 return EBADF; 375 } 376 if (vnode_vtype(vp) == VDIR) { 377 return EISDIR; 378 } 379 if (vfs_isrdonly(vnode_mount(vp))) { 380 return EROFS; 381 } 382 if (cred == NULL) { 383 cred = td->td_ucred; 384 } 385 fdisp_init(&fdi, sizeof(*fsai)); 386 fdisp_make_vp(&fdi, FUSE_SETATTR, vp, td, cred); 387 fsai = fdi.indata; 388 fsai->valid = 0; 389 390 /* Truncate to a new value. */ 391 MPASS((fvdat->flag & FN_SIZECHANGE) != 0); 392 fsai->size = fvdat->cached_attrs.va_size; 393 fsai->valid |= FATTR_SIZE; 394 395 fuse_filehandle_getrw(vp, FWRITE, &fufh, cred, pid); 396 if (fufh) { 397 fsai->fh = fufh->fh_id; 398 fsai->valid |= FATTR_FH; 399 } 400 err = fdisp_wait_answ(&fdi); 401 fdisp_destroy(&fdi); 402 if (err == 0) { 403 getnanouptime(&fvdat->last_local_modify); 404 fvdat->flag &= ~FN_SIZECHANGE; 405 } 406 407 return err; 408 } 409 410 /* 411 * Adjust the vnode's size to a new value. 412 * 413 * If the new value came from the server, such as from a FUSE_GETATTR 414 * operation, set `from_server` true. But if it came from a local operation, 415 * such as write(2) or truncate(2), set `from_server` false. 416 */ 417 int 418 fuse_vnode_setsize(struct vnode *vp, off_t newsize, bool from_server) 419 { 420 struct fuse_vnode_data *fvdat = VTOFUD(vp); 421 struct vattr *attrs; 422 off_t oldsize; 423 size_t iosize; 424 struct buf *bp = NULL; 425 int err = 0; 426 427 ASSERT_VOP_ELOCKED(vp, "fuse_vnode_setsize"); 428 429 iosize = fuse_iosize(vp); 430 oldsize = fvdat->cached_attrs.va_size; 431 fvdat->cached_attrs.va_size = newsize; 432 if ((attrs = VTOVA(vp)) != NULL) 433 attrs->va_size = newsize; 434 435 if (newsize < oldsize) { 436 daddr_t lbn; 437 438 err = vtruncbuf(vp, newsize, fuse_iosize(vp)); 439 if (err) 440 goto out; 441 if (newsize % iosize == 0) 442 goto out; 443 /* 444 * Zero the contents of the last partial block. 445 * Sure seems like vtruncbuf should do this for us. 446 */ 447 448 lbn = newsize / iosize; 449 bp = getblk(vp, lbn, iosize, PCATCH, 0, 0); 450 if (!bp) { 451 err = EINTR; 452 goto out; 453 } 454 if (!(bp->b_flags & B_CACHE)) 455 goto out; /* Nothing to do */ 456 MPASS(bp->b_flags & B_VMIO); 457 vfs_bio_clrbuf(bp); 458 bp->b_dirtyend = MIN(bp->b_dirtyend, newsize - lbn * iosize); 459 } else if (from_server && newsize > oldsize && oldsize != VNOVAL) { 460 /* 461 * The FUSE server changed the file size behind our back. We 462 * should invalidate the entire cache. 463 */ 464 daddr_t end_lbn; 465 466 end_lbn = howmany(newsize, iosize); 467 v_inval_buf_range(vp, 0, end_lbn, iosize); 468 } 469 out: 470 if (bp) 471 brelse(bp); 472 vnode_pager_setsize(vp, newsize); 473 return err; 474 } 475 476 /* Get the current, possibly dirty, size of the file */ 477 int 478 fuse_vnode_size(struct vnode *vp, off_t *filesize, struct ucred *cred, 479 struct thread *td) 480 { 481 struct fuse_vnode_data *fvdat = VTOFUD(vp); 482 int error = 0; 483 484 if (!(fvdat->flag & FN_SIZECHANGE) && 485 (!fuse_vnode_attr_cache_valid(vp) || 486 fvdat->cached_attrs.va_size == VNOVAL)) 487 error = fuse_internal_do_getattr(vp, NULL, cred, td); 488 489 if (!error) 490 *filesize = fvdat->cached_attrs.va_size; 491 492 return error; 493 } 494 495 void 496 fuse_vnode_undirty_cached_timestamps(struct vnode *vp, bool atime) 497 { 498 struct fuse_vnode_data *fvdat = VTOFUD(vp); 499 500 fvdat->flag &= ~(FN_MTIMECHANGE | FN_CTIMECHANGE); 501 if (atime) 502 fvdat->flag &= ~FN_ATIMECHANGE; 503 } 504 505 /* Update a fuse file's cached timestamps */ 506 void 507 fuse_vnode_update(struct vnode *vp, int flags) 508 { 509 struct fuse_vnode_data *fvdat = VTOFUD(vp); 510 struct mount *mp = vnode_mount(vp); 511 struct fuse_data *data = fuse_get_mpdata(mp); 512 struct timespec ts; 513 514 vfs_timestamp(&ts); 515 516 if (data->time_gran > 1) 517 ts.tv_nsec = rounddown(ts.tv_nsec, data->time_gran); 518 519 if (mp->mnt_flag & MNT_NOATIME) 520 flags &= ~FN_ATIMECHANGE; 521 522 if (flags & FN_ATIMECHANGE) 523 fvdat->cached_attrs.va_atime = ts; 524 if (flags & FN_MTIMECHANGE) 525 fvdat->cached_attrs.va_mtime = ts; 526 if (flags & FN_CTIMECHANGE) 527 fvdat->cached_attrs.va_ctime = ts; 528 529 fvdat->flag |= flags; 530 } 531 532 void 533 fuse_node_init(void) 534 { 535 fuse_node_count = counter_u64_alloc(M_WAITOK); 536 } 537 538 void 539 fuse_node_destroy(void) 540 { 541 counter_u64_free(fuse_node_count); 542 } 543