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/counter.h> 63 #include <sys/module.h> 64 #include <sys/systm.h> 65 #include <sys/errno.h> 66 #include <sys/param.h> 67 #include <sys/kernel.h> 68 #include <sys/conf.h> 69 #include <sys/uio.h> 70 #include <sys/malloc.h> 71 #include <sys/queue.h> 72 #include <sys/lock.h> 73 #include <sys/sx.h> 74 #include <sys/mutex.h> 75 #include <sys/proc.h> 76 #include <sys/vnode.h> 77 #include <sys/namei.h> 78 #include <sys/mount.h> 79 #include <sys/sysctl.h> 80 #include <sys/fcntl.h> 81 #include <sys/priv.h> 82 #include <sys/buf.h> 83 #include <security/mac/mac_framework.h> 84 #include <vm/vm.h> 85 #include <vm/vm_extern.h> 86 87 #include "fuse.h" 88 #include "fuse_node.h" 89 #include "fuse_internal.h" 90 #include "fuse_io.h" 91 #include "fuse_ipc.h" 92 93 SDT_PROVIDER_DECLARE(fusefs); 94 /* 95 * Fuse trace probe: 96 * arg0: verbosity. Higher numbers give more verbose messages 97 * arg1: Textual message 98 */ 99 SDT_PROBE_DEFINE2(fusefs, , node, trace, "int", "char*"); 100 101 MALLOC_DEFINE(M_FUSEVN, "fuse_vnode", "fuse vnode private data"); 102 103 static int sysctl_fuse_cache_mode(SYSCTL_HANDLER_ARGS); 104 105 static counter_u64_t fuse_node_count; 106 107 SYSCTL_COUNTER_U64(_vfs_fusefs_stats, OID_AUTO, node_count, CTLFLAG_RD, 108 &fuse_node_count, "Count of FUSE vnodes"); 109 110 int fuse_data_cache_mode = FUSE_CACHE_WT; 111 112 /* 113 * DEPRECATED 114 * This sysctl is no longer needed as of fuse protocol 7.23. Individual 115 * servers can select the cache behavior they need for each mountpoint: 116 * - writethrough: the default 117 * - writeback: set FUSE_WRITEBACK_CACHE in fuse_init_out.flags 118 * - uncached: set FOPEN_DIRECT_IO for every file 119 * The sysctl is retained primarily for use by jails supporting older FUSE 120 * protocols. It may be removed entirely once FreeBSD 11.3 and 12.0 are EOL. 121 */ 122 SYSCTL_PROC(_vfs_fusefs, OID_AUTO, data_cache_mode, CTLTYPE_INT|CTLFLAG_RW, 123 &fuse_data_cache_mode, 0, sysctl_fuse_cache_mode, "I", 124 "Zero: disable caching of FUSE file data; One: write-through caching " 125 "(default); Two: write-back caching (generally unsafe)"); 126 127 static int 128 sysctl_fuse_cache_mode(SYSCTL_HANDLER_ARGS) 129 { 130 int val, error; 131 132 val = *(int *)arg1; 133 error = sysctl_handle_int(oidp, &val, 0, req); 134 if (error || !req->newptr) 135 return (error); 136 137 switch (val) { 138 case FUSE_CACHE_UC: 139 case FUSE_CACHE_WT: 140 case FUSE_CACHE_WB: 141 *(int *)arg1 = val; 142 break; 143 default: 144 return (EDOM); 145 } 146 return (0); 147 } 148 149 static void 150 fuse_vnode_init(struct vnode *vp, struct fuse_vnode_data *fvdat, 151 uint64_t nodeid, enum vtype vtyp) 152 { 153 fvdat->nid = nodeid; 154 LIST_INIT(&fvdat->handles); 155 vattr_null(&fvdat->cached_attrs); 156 if (nodeid == FUSE_ROOT_ID) { 157 vp->v_vflag |= VV_ROOT; 158 } 159 vp->v_type = vtyp; 160 vp->v_data = fvdat; 161 162 counter_u64_add(fuse_node_count, 1); 163 } 164 165 void 166 fuse_vnode_destroy(struct vnode *vp) 167 { 168 struct fuse_vnode_data *fvdat = vp->v_data; 169 170 vp->v_data = NULL; 171 KASSERT(LIST_EMPTY(&fvdat->handles), 172 ("Destroying fuse vnode with open files!")); 173 free(fvdat, M_FUSEVN); 174 175 counter_u64_add(fuse_node_count, -1); 176 } 177 178 int 179 fuse_vnode_cmp(struct vnode *vp, void *nidp) 180 { 181 return (VTOI(vp) != *((uint64_t *)nidp)); 182 } 183 184 SDT_PROBE_DEFINE3(fusefs, , node, stale_vnode, "struct vnode*", "enum vtype", 185 "uint64_t"); 186 static int 187 fuse_vnode_alloc(struct mount *mp, 188 struct thread *td, 189 uint64_t nodeid, 190 enum vtype vtyp, 191 struct vnode **vpp) 192 { 193 struct fuse_data *data; 194 struct fuse_vnode_data *fvdat; 195 struct vnode *vp2; 196 int err = 0; 197 198 data = fuse_get_mpdata(mp); 199 if (vtyp == VNON) { 200 return EINVAL; 201 } 202 *vpp = NULL; 203 err = vfs_hash_get(mp, fuse_vnode_hash(nodeid), LK_EXCLUSIVE, td, vpp, 204 fuse_vnode_cmp, &nodeid); 205 if (err) 206 return (err); 207 208 if (*vpp) { 209 if ((*vpp)->v_type != vtyp) { 210 /* 211 * STALE vnode! This probably indicates a buggy 212 * server, but it could also be the result of a race 213 * between FUSE_LOOKUP and another client's 214 * FUSE_UNLINK/FUSE_CREATE 215 */ 216 SDT_PROBE3(fusefs, , node, stale_vnode, *vpp, vtyp, 217 nodeid); 218 fuse_internal_vnode_disappear(*vpp); 219 lockmgr((*vpp)->v_vnlock, LK_RELEASE, NULL); 220 *vpp = NULL; 221 return (EAGAIN); 222 } 223 MPASS((*vpp)->v_data != NULL); 224 MPASS(VTOFUD(*vpp)->nid == nodeid); 225 SDT_PROBE2(fusefs, , node, trace, 1, "vnode taken from hash"); 226 return (0); 227 } 228 fvdat = malloc(sizeof(*fvdat), M_FUSEVN, M_WAITOK | M_ZERO); 229 switch (vtyp) { 230 case VFIFO: 231 err = getnewvnode("fuse", mp, &fuse_fifoops, vpp); 232 break; 233 default: 234 err = getnewvnode("fuse", mp, &fuse_vnops, vpp); 235 break; 236 } 237 if (err) { 238 free(fvdat, M_FUSEVN); 239 return (err); 240 } 241 lockmgr((*vpp)->v_vnlock, LK_EXCLUSIVE, NULL); 242 fuse_vnode_init(*vpp, fvdat, nodeid, vtyp); 243 err = insmntque(*vpp, mp); 244 ASSERT_VOP_ELOCKED(*vpp, "fuse_vnode_alloc"); 245 if (err) { 246 lockmgr((*vpp)->v_vnlock, LK_RELEASE, NULL); 247 free(fvdat, M_FUSEVN); 248 *vpp = NULL; 249 return (err); 250 } 251 /* Disallow async reads for fifos because UFS does. I don't know why */ 252 if (data->dataflags & FSESS_ASYNC_READ && vtyp != VFIFO) 253 VN_LOCK_ASHARE(*vpp); 254 255 err = vfs_hash_insert(*vpp, fuse_vnode_hash(nodeid), LK_EXCLUSIVE, 256 td, &vp2, fuse_vnode_cmp, &nodeid); 257 if (err) { 258 lockmgr((*vpp)->v_vnlock, LK_RELEASE, NULL); 259 free(fvdat, M_FUSEVN); 260 *vpp = NULL; 261 return (err); 262 } 263 if (vp2 != NULL) { 264 *vpp = vp2; 265 return (0); 266 } 267 268 ASSERT_VOP_ELOCKED(*vpp, "fuse_vnode_alloc"); 269 270 return (0); 271 } 272 273 int 274 fuse_vnode_get(struct mount *mp, 275 struct fuse_entry_out *feo, 276 uint64_t nodeid, 277 struct vnode *dvp, 278 struct vnode **vpp, 279 struct componentname *cnp, 280 enum vtype vtyp) 281 { 282 struct thread *td = (cnp != NULL ? cnp->cn_thread : curthread); 283 /* 284 * feo should only be NULL for the root directory, which (when libfuse 285 * is used) always has generation 0 286 */ 287 uint64_t generation = feo ? feo->generation : 0; 288 int err = 0; 289 290 err = fuse_vnode_alloc(mp, td, nodeid, vtyp, vpp); 291 if (err) { 292 return err; 293 } 294 if (dvp != NULL) { 295 MPASS(cnp && (cnp->cn_flags & ISDOTDOT) == 0); 296 MPASS(cnp && 297 !(cnp->cn_namelen == 1 && cnp->cn_nameptr[0] == '.')); 298 fuse_vnode_setparent(*vpp, dvp); 299 } 300 if (dvp != NULL && cnp != NULL && (cnp->cn_flags & MAKEENTRY) != 0 && 301 feo != NULL && 302 (feo->entry_valid != 0 || feo->entry_valid_nsec != 0)) { 303 struct timespec timeout; 304 305 ASSERT_VOP_LOCKED(*vpp, "fuse_vnode_get"); 306 ASSERT_VOP_LOCKED(dvp, "fuse_vnode_get"); 307 308 fuse_validity_2_timespec(feo, &timeout); 309 cache_enter_time(dvp, *vpp, cnp, &timeout, NULL); 310 } 311 312 VTOFUD(*vpp)->generation = generation; 313 /* 314 * In userland, libfuse uses cached lookups for dot and dotdot entries, 315 * thus it does not really bump the nlookup counter for forget. 316 * Follow the same semantic and avoid the bump in order to keep 317 * nlookup counters consistent. 318 */ 319 if (cnp == NULL || ((cnp->cn_flags & ISDOTDOT) == 0 && 320 (cnp->cn_namelen != 1 || cnp->cn_nameptr[0] != '.'))) 321 VTOFUD(*vpp)->nlookup++; 322 323 return 0; 324 } 325 326 /* 327 * Called for every fusefs vnode open to initialize the vnode (not 328 * fuse_filehandle) for use 329 */ 330 void 331 fuse_vnode_open(struct vnode *vp, int32_t fuse_open_flags, struct thread *td) 332 { 333 if (vnode_vtype(vp) == VREG) 334 vnode_create_vobject(vp, 0, td); 335 } 336 337 int 338 fuse_vnode_savesize(struct vnode *vp, struct ucred *cred, pid_t pid) 339 { 340 struct fuse_vnode_data *fvdat = VTOFUD(vp); 341 struct thread *td = curthread; 342 struct fuse_filehandle *fufh = NULL; 343 struct fuse_dispatcher fdi; 344 struct fuse_setattr_in *fsai; 345 int err = 0; 346 347 ASSERT_VOP_ELOCKED(vp, "fuse_io_extend"); 348 349 if (fuse_isdeadfs(vp)) { 350 return EBADF; 351 } 352 if (vnode_vtype(vp) == VDIR) { 353 return EISDIR; 354 } 355 if (vfs_isrdonly(vnode_mount(vp))) { 356 return EROFS; 357 } 358 if (cred == NULL) { 359 cred = td->td_ucred; 360 } 361 fdisp_init(&fdi, sizeof(*fsai)); 362 fdisp_make_vp(&fdi, FUSE_SETATTR, vp, td, cred); 363 fsai = fdi.indata; 364 fsai->valid = 0; 365 366 /* Truncate to a new value. */ 367 MPASS((fvdat->flag & FN_SIZECHANGE) != 0); 368 fsai->size = fvdat->cached_attrs.va_size; 369 fsai->valid |= FATTR_SIZE; 370 371 fuse_filehandle_getrw(vp, FWRITE, &fufh, cred, pid); 372 if (fufh) { 373 fsai->fh = fufh->fh_id; 374 fsai->valid |= FATTR_FH; 375 } 376 err = fdisp_wait_answ(&fdi); 377 fdisp_destroy(&fdi); 378 if (err == 0) 379 fvdat->flag &= ~FN_SIZECHANGE; 380 381 return err; 382 } 383 384 /* 385 * Adjust the vnode's size to a new value, such as that provided by 386 * FUSE_GETATTR. 387 */ 388 int 389 fuse_vnode_setsize(struct vnode *vp, off_t newsize) 390 { 391 struct fuse_vnode_data *fvdat = VTOFUD(vp); 392 struct vattr *attrs; 393 off_t oldsize; 394 size_t iosize; 395 struct buf *bp = NULL; 396 int err = 0; 397 398 ASSERT_VOP_ELOCKED(vp, "fuse_vnode_setsize"); 399 400 iosize = fuse_iosize(vp); 401 oldsize = fvdat->cached_attrs.va_size; 402 fvdat->cached_attrs.va_size = newsize; 403 if ((attrs = VTOVA(vp)) != NULL) 404 attrs->va_size = newsize; 405 406 if (newsize < oldsize) { 407 daddr_t lbn; 408 409 err = vtruncbuf(vp, newsize, fuse_iosize(vp)); 410 if (err) 411 goto out; 412 if (newsize % iosize == 0) 413 goto out; 414 /* 415 * Zero the contents of the last partial block. 416 * Sure seems like vtruncbuf should do this for us. 417 */ 418 419 lbn = newsize / iosize; 420 bp = getblk(vp, lbn, iosize, PCATCH, 0, 0); 421 if (!bp) { 422 err = EINTR; 423 goto out; 424 } 425 if (!(bp->b_flags & B_CACHE)) 426 goto out; /* Nothing to do */ 427 MPASS(bp->b_flags & B_VMIO); 428 vfs_bio_clrbuf(bp); 429 bp->b_dirtyend = MIN(bp->b_dirtyend, newsize - lbn * iosize); 430 } 431 out: 432 if (bp) 433 brelse(bp); 434 vnode_pager_setsize(vp, newsize); 435 return err; 436 } 437 438 /* Get the current, possibly dirty, size of the file */ 439 int 440 fuse_vnode_size(struct vnode *vp, off_t *filesize, struct ucred *cred, 441 struct thread *td) 442 { 443 struct fuse_vnode_data *fvdat = VTOFUD(vp); 444 int error = 0; 445 446 if (!(fvdat->flag & FN_SIZECHANGE) && 447 (VTOVA(vp) == NULL || fvdat->cached_attrs.va_size == VNOVAL)) 448 error = fuse_internal_do_getattr(vp, NULL, cred, td); 449 450 if (!error) 451 *filesize = fvdat->cached_attrs.va_size; 452 453 return error; 454 } 455 456 void 457 fuse_vnode_undirty_cached_timestamps(struct vnode *vp) 458 { 459 struct fuse_vnode_data *fvdat = VTOFUD(vp); 460 461 fvdat->flag &= ~(FN_MTIMECHANGE | FN_CTIMECHANGE); 462 } 463 464 /* Update a fuse file's cached timestamps */ 465 void 466 fuse_vnode_update(struct vnode *vp, int flags) 467 { 468 struct fuse_vnode_data *fvdat = VTOFUD(vp); 469 struct fuse_data *data = fuse_get_mpdata(vnode_mount(vp)); 470 struct timespec ts; 471 472 vfs_timestamp(&ts); 473 474 if (data->time_gran > 1) 475 ts.tv_nsec = rounddown(ts.tv_nsec, data->time_gran); 476 477 if (flags & FN_MTIMECHANGE) 478 fvdat->cached_attrs.va_mtime = ts; 479 if (flags & FN_CTIMECHANGE) 480 fvdat->cached_attrs.va_ctime = ts; 481 482 fvdat->flag |= flags; 483 } 484 485 void 486 fuse_node_init(void) 487 { 488 fuse_node_count = counter_u64_alloc(M_WAITOK); 489 counter_u64_zero(fuse_node_count); 490 } 491 492 void 493 fuse_node_destroy(void) 494 { 495 counter_u64_free(fuse_node_count); 496 } 497