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