1 /* $NetBSD: tmpfs_subr.c,v 1.35 2007/07/09 21:10:50 ad Exp $ */ 2 3 /*- 4 * SPDX-License-Identifier: BSD-2-Clause-NetBSD 5 * 6 * Copyright (c) 2005 The NetBSD Foundation, Inc. 7 * All rights reserved. 8 * 9 * This code is derived from software contributed to The NetBSD Foundation 10 * by Julio M. Merino Vidal, developed as part of Google's Summer of Code 11 * 2005 program. 12 * 13 * Redistribution and use in source and binary forms, with or without 14 * modification, are permitted provided that the following conditions 15 * are met: 16 * 1. Redistributions of source code must retain the above copyright 17 * notice, this list of conditions and the following disclaimer. 18 * 2. Redistributions in binary form must reproduce the above copyright 19 * notice, this list of conditions and the following disclaimer in the 20 * documentation and/or other materials provided with the distribution. 21 * 22 * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS 23 * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED 24 * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR 25 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS 26 * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR 27 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF 28 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS 29 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN 30 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) 31 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE 32 * POSSIBILITY OF SUCH DAMAGE. 33 */ 34 35 /* 36 * Efficient memory file system supporting functions. 37 */ 38 #include <sys/cdefs.h> 39 __FBSDID("$FreeBSD$"); 40 41 #include <sys/param.h> 42 #include <sys/systm.h> 43 #include <sys/dirent.h> 44 #include <sys/fnv_hash.h> 45 #include <sys/lock.h> 46 #include <sys/limits.h> 47 #include <sys/mount.h> 48 #include <sys/namei.h> 49 #include <sys/priv.h> 50 #include <sys/proc.h> 51 #include <sys/random.h> 52 #include <sys/refcount.h> 53 #include <sys/rwlock.h> 54 #include <sys/smr.h> 55 #include <sys/stat.h> 56 #include <sys/sysctl.h> 57 #include <sys/vnode.h> 58 #include <sys/vmmeter.h> 59 60 #include <vm/vm.h> 61 #include <vm/vm_param.h> 62 #include <vm/vm_object.h> 63 #include <vm/vm_page.h> 64 #include <vm/vm_pageout.h> 65 #include <vm/vm_pager.h> 66 #include <vm/vm_extern.h> 67 #include <vm/swap_pager.h> 68 69 #include <fs/tmpfs/tmpfs.h> 70 #include <fs/tmpfs/tmpfs_fifoops.h> 71 #include <fs/tmpfs/tmpfs_vnops.h> 72 73 SYSCTL_NODE(_vfs, OID_AUTO, tmpfs, CTLFLAG_RW | CTLFLAG_MPSAFE, 0, 74 "tmpfs file system"); 75 76 static long tmpfs_pages_reserved = TMPFS_PAGES_MINRESERVED; 77 78 MALLOC_DEFINE(M_TMPFSDIR, "tmpfs dir", "tmpfs dirent structure"); 79 static uma_zone_t tmpfs_node_pool; 80 VFS_SMR_DECLARE; 81 82 static int 83 tmpfs_node_ctor(void *mem, int size, void *arg, int flags) 84 { 85 struct tmpfs_node *node; 86 87 node = mem; 88 node->tn_gen++; 89 node->tn_size = 0; 90 node->tn_status = 0; 91 node->tn_accessed = false; 92 node->tn_flags = 0; 93 node->tn_links = 0; 94 node->tn_vnode = NULL; 95 node->tn_vpstate = 0; 96 return (0); 97 } 98 99 static void 100 tmpfs_node_dtor(void *mem, int size, void *arg) 101 { 102 struct tmpfs_node *node; 103 104 node = mem; 105 node->tn_type = VNON; 106 } 107 108 static int 109 tmpfs_node_init(void *mem, int size, int flags) 110 { 111 struct tmpfs_node *node; 112 113 node = mem; 114 node->tn_id = 0; 115 mtx_init(&node->tn_interlock, "tmpfsni", NULL, MTX_DEF); 116 node->tn_gen = arc4random(); 117 return (0); 118 } 119 120 static void 121 tmpfs_node_fini(void *mem, int size) 122 { 123 struct tmpfs_node *node; 124 125 node = mem; 126 mtx_destroy(&node->tn_interlock); 127 } 128 129 void 130 tmpfs_subr_init(void) 131 { 132 tmpfs_node_pool = uma_zcreate("TMPFS node", 133 sizeof(struct tmpfs_node), tmpfs_node_ctor, tmpfs_node_dtor, 134 tmpfs_node_init, tmpfs_node_fini, UMA_ALIGN_PTR, 0); 135 VFS_SMR_ZONE_SET(tmpfs_node_pool); 136 } 137 138 void 139 tmpfs_subr_uninit(void) 140 { 141 uma_zdestroy(tmpfs_node_pool); 142 } 143 144 static int 145 sysctl_mem_reserved(SYSCTL_HANDLER_ARGS) 146 { 147 int error; 148 long pages, bytes; 149 150 pages = *(long *)arg1; 151 bytes = pages * PAGE_SIZE; 152 153 error = sysctl_handle_long(oidp, &bytes, 0, req); 154 if (error || !req->newptr) 155 return (error); 156 157 pages = bytes / PAGE_SIZE; 158 if (pages < TMPFS_PAGES_MINRESERVED) 159 return (EINVAL); 160 161 *(long *)arg1 = pages; 162 return (0); 163 } 164 165 SYSCTL_PROC(_vfs_tmpfs, OID_AUTO, memory_reserved, 166 CTLTYPE_LONG|CTLFLAG_MPSAFE|CTLFLAG_RW, &tmpfs_pages_reserved, 0, 167 sysctl_mem_reserved, "L", 168 "Amount of available memory and swap below which tmpfs growth stops"); 169 170 static __inline int tmpfs_dirtree_cmp(struct tmpfs_dirent *a, 171 struct tmpfs_dirent *b); 172 RB_PROTOTYPE_STATIC(tmpfs_dir, tmpfs_dirent, uh.td_entries, tmpfs_dirtree_cmp); 173 174 size_t 175 tmpfs_mem_avail(void) 176 { 177 size_t avail; 178 long reserved; 179 180 avail = swap_pager_avail + vm_free_count(); 181 reserved = atomic_load_long(&tmpfs_pages_reserved); 182 if (__predict_false(avail < reserved)) 183 return (0); 184 return (avail - reserved); 185 } 186 187 size_t 188 tmpfs_pages_used(struct tmpfs_mount *tmp) 189 { 190 const size_t node_size = sizeof(struct tmpfs_node) + 191 sizeof(struct tmpfs_dirent); 192 size_t meta_pages; 193 194 meta_pages = howmany((uintmax_t)tmp->tm_nodes_inuse * node_size, 195 PAGE_SIZE); 196 return (meta_pages + tmp->tm_pages_used); 197 } 198 199 static size_t 200 tmpfs_pages_check_avail(struct tmpfs_mount *tmp, size_t req_pages) 201 { 202 if (tmpfs_mem_avail() < req_pages) 203 return (0); 204 205 if (tmp->tm_pages_max != ULONG_MAX && 206 tmp->tm_pages_max < req_pages + tmpfs_pages_used(tmp)) 207 return (0); 208 209 return (1); 210 } 211 212 void 213 tmpfs_ref_node(struct tmpfs_node *node) 214 { 215 #ifdef INVARIANTS 216 u_int old; 217 218 old = 219 #endif 220 refcount_acquire(&node->tn_refcount); 221 #ifdef INVARIANTS 222 KASSERT(old > 0, ("node %p zero refcount", node)); 223 #endif 224 } 225 226 /* 227 * Allocates a new node of type 'type' inside the 'tmp' mount point, with 228 * its owner set to 'uid', its group to 'gid' and its mode set to 'mode', 229 * using the credentials of the process 'p'. 230 * 231 * If the node type is set to 'VDIR', then the parent parameter must point 232 * to the parent directory of the node being created. It may only be NULL 233 * while allocating the root node. 234 * 235 * If the node type is set to 'VBLK' or 'VCHR', then the rdev parameter 236 * specifies the device the node represents. 237 * 238 * If the node type is set to 'VLNK', then the parameter target specifies 239 * the file name of the target file for the symbolic link that is being 240 * created. 241 * 242 * Note that new nodes are retrieved from the available list if it has 243 * items or, if it is empty, from the node pool as long as there is enough 244 * space to create them. 245 * 246 * Returns zero on success or an appropriate error code on failure. 247 */ 248 int 249 tmpfs_alloc_node(struct mount *mp, struct tmpfs_mount *tmp, enum vtype type, 250 uid_t uid, gid_t gid, mode_t mode, struct tmpfs_node *parent, 251 const char *target, dev_t rdev, struct tmpfs_node **node) 252 { 253 struct tmpfs_node *nnode; 254 vm_object_t obj; 255 char *symlink; 256 char symlink_smr; 257 258 /* If the root directory of the 'tmp' file system is not yet 259 * allocated, this must be the request to do it. */ 260 MPASS(IMPLIES(tmp->tm_root == NULL, parent == NULL && type == VDIR)); 261 262 MPASS(IFF(type == VLNK, target != NULL)); 263 MPASS(IFF(type == VBLK || type == VCHR, rdev != VNOVAL)); 264 265 if (tmp->tm_nodes_inuse >= tmp->tm_nodes_max) 266 return (ENOSPC); 267 if (tmpfs_pages_check_avail(tmp, 1) == 0) 268 return (ENOSPC); 269 270 if ((mp->mnt_kern_flag & MNTK_UNMOUNT) != 0) { 271 /* 272 * When a new tmpfs node is created for fully 273 * constructed mount point, there must be a parent 274 * node, which vnode is locked exclusively. As 275 * consequence, if the unmount is executing in 276 * parallel, vflush() cannot reclaim the parent vnode. 277 * Due to this, the check for MNTK_UNMOUNT flag is not 278 * racy: if we did not see MNTK_UNMOUNT flag, then tmp 279 * cannot be destroyed until node construction is 280 * finished and the parent vnode unlocked. 281 * 282 * Tmpfs does not need to instantiate new nodes during 283 * unmount. 284 */ 285 return (EBUSY); 286 } 287 if ((mp->mnt_kern_flag & MNT_RDONLY) != 0) 288 return (EROFS); 289 290 nnode = uma_zalloc_smr(tmpfs_node_pool, M_WAITOK); 291 292 /* Generic initialization. */ 293 nnode->tn_type = type; 294 vfs_timestamp(&nnode->tn_atime); 295 nnode->tn_birthtime = nnode->tn_ctime = nnode->tn_mtime = 296 nnode->tn_atime; 297 nnode->tn_uid = uid; 298 nnode->tn_gid = gid; 299 nnode->tn_mode = mode; 300 nnode->tn_id = alloc_unr64(&tmp->tm_ino_unr); 301 nnode->tn_refcount = 1; 302 303 /* Type-specific initialization. */ 304 switch (nnode->tn_type) { 305 case VBLK: 306 case VCHR: 307 nnode->tn_rdev = rdev; 308 break; 309 310 case VDIR: 311 RB_INIT(&nnode->tn_dir.tn_dirhead); 312 LIST_INIT(&nnode->tn_dir.tn_dupindex); 313 MPASS(parent != nnode); 314 MPASS(IMPLIES(parent == NULL, tmp->tm_root == NULL)); 315 nnode->tn_dir.tn_parent = (parent == NULL) ? nnode : parent; 316 nnode->tn_dir.tn_readdir_lastn = 0; 317 nnode->tn_dir.tn_readdir_lastp = NULL; 318 nnode->tn_links++; 319 TMPFS_NODE_LOCK(nnode->tn_dir.tn_parent); 320 nnode->tn_dir.tn_parent->tn_links++; 321 TMPFS_NODE_UNLOCK(nnode->tn_dir.tn_parent); 322 break; 323 324 case VFIFO: 325 /* FALLTHROUGH */ 326 case VSOCK: 327 break; 328 329 case VLNK: 330 MPASS(strlen(target) < MAXPATHLEN); 331 nnode->tn_size = strlen(target); 332 333 symlink = NULL; 334 if (!tmp->tm_nonc) { 335 symlink = cache_symlink_alloc(nnode->tn_size + 1, M_WAITOK); 336 symlink_smr = true; 337 } 338 if (symlink == NULL) { 339 symlink = malloc(nnode->tn_size + 1, M_TMPFSNAME, M_WAITOK); 340 symlink_smr = false; 341 } 342 memcpy(symlink, target, nnode->tn_size + 1); 343 344 /* 345 * Allow safe symlink resolving for lockless lookup. 346 * tmpfs_fplookup_symlink references this comment. 347 * 348 * 1. nnode is not yet visible to the world 349 * 2. both tn_link_target and tn_link_smr get populated 350 * 3. release fence publishes their content 351 * 4. tn_link_target content is immutable until node destruction, 352 * where the pointer gets set to NULL 353 * 5. tn_link_smr is never changed once set 354 * 355 * As a result it is sufficient to issue load consume on the node 356 * pointer to also get the above content in a stable manner. 357 * Worst case tn_link_smr flag may be set to true despite being stale, 358 * while the target buffer is already cleared out. 359 * 360 * TODO: Since there is no load consume primitive provided 361 * right now, the load is performed with an acquire fence. 362 */ 363 atomic_store_ptr((uintptr_t *)&nnode->tn_link_target, 364 (uintptr_t)symlink); 365 atomic_store_char((char *)&nnode->tn_link_smr, symlink_smr); 366 atomic_thread_fence_rel(); 367 break; 368 369 case VREG: 370 obj = nnode->tn_reg.tn_aobj = 371 vm_pager_allocate(OBJT_SWAP, NULL, 0, VM_PROT_DEFAULT, 0, 372 NULL /* XXXKIB - tmpfs needs swap reservation */); 373 VM_OBJECT_WLOCK(obj); 374 /* OBJ_TMPFS is set together with the setting of vp->v_object */ 375 vm_object_set_flag(obj, OBJ_TMPFS_NODE); 376 VM_OBJECT_WUNLOCK(obj); 377 nnode->tn_reg.tn_tmp = tmp; 378 break; 379 380 default: 381 panic("tmpfs_alloc_node: type %p %d", nnode, 382 (int)nnode->tn_type); 383 } 384 385 TMPFS_LOCK(tmp); 386 LIST_INSERT_HEAD(&tmp->tm_nodes_used, nnode, tn_entries); 387 nnode->tn_attached = true; 388 tmp->tm_nodes_inuse++; 389 tmp->tm_refcount++; 390 TMPFS_UNLOCK(tmp); 391 392 *node = nnode; 393 return (0); 394 } 395 396 /* 397 * Destroys the node pointed to by node from the file system 'tmp'. 398 * If the node references a directory, no entries are allowed. 399 */ 400 void 401 tmpfs_free_node(struct tmpfs_mount *tmp, struct tmpfs_node *node) 402 { 403 if (refcount_release_if_not_last(&node->tn_refcount)) 404 return; 405 406 TMPFS_LOCK(tmp); 407 TMPFS_NODE_LOCK(node); 408 if (!tmpfs_free_node_locked(tmp, node, false)) { 409 TMPFS_NODE_UNLOCK(node); 410 TMPFS_UNLOCK(tmp); 411 } 412 } 413 414 bool 415 tmpfs_free_node_locked(struct tmpfs_mount *tmp, struct tmpfs_node *node, 416 bool detach) 417 { 418 vm_object_t uobj; 419 char *symlink; 420 bool last; 421 422 TMPFS_MP_ASSERT_LOCKED(tmp); 423 TMPFS_NODE_ASSERT_LOCKED(node); 424 425 last = refcount_release(&node->tn_refcount); 426 if (node->tn_attached && (detach || last)) { 427 MPASS(tmp->tm_nodes_inuse > 0); 428 tmp->tm_nodes_inuse--; 429 LIST_REMOVE(node, tn_entries); 430 node->tn_attached = false; 431 } 432 if (!last) 433 return (false); 434 435 #ifdef INVARIANTS 436 MPASS(node->tn_vnode == NULL); 437 MPASS((node->tn_vpstate & TMPFS_VNODE_ALLOCATING) == 0); 438 #endif 439 TMPFS_NODE_UNLOCK(node); 440 TMPFS_UNLOCK(tmp); 441 442 switch (node->tn_type) { 443 case VBLK: 444 /* FALLTHROUGH */ 445 case VCHR: 446 /* FALLTHROUGH */ 447 case VDIR: 448 /* FALLTHROUGH */ 449 case VFIFO: 450 /* FALLTHROUGH */ 451 case VSOCK: 452 break; 453 454 case VLNK: 455 symlink = node->tn_link_target; 456 atomic_store_ptr((uintptr_t *)&node->tn_link_target, 457 (uintptr_t)NULL); 458 if (atomic_load_char(&node->tn_link_smr)) { 459 cache_symlink_free(symlink, node->tn_size + 1); 460 } else { 461 free(symlink, M_TMPFSNAME); 462 } 463 break; 464 465 case VREG: 466 uobj = node->tn_reg.tn_aobj; 467 if (uobj != NULL) { 468 if (uobj->size != 0) 469 atomic_subtract_long(&tmp->tm_pages_used, uobj->size); 470 KASSERT((uobj->flags & OBJ_TMPFS) == 0, 471 ("leaked OBJ_TMPFS node %p vm_obj %p", node, uobj)); 472 vm_object_deallocate(uobj); 473 } 474 break; 475 476 default: 477 panic("tmpfs_free_node: type %p %d", node, (int)node->tn_type); 478 } 479 480 uma_zfree_smr(tmpfs_node_pool, node); 481 TMPFS_LOCK(tmp); 482 tmpfs_free_tmp(tmp); 483 return (true); 484 } 485 486 static __inline uint32_t 487 tmpfs_dirent_hash(const char *name, u_int len) 488 { 489 uint32_t hash; 490 491 hash = fnv_32_buf(name, len, FNV1_32_INIT + len) & TMPFS_DIRCOOKIE_MASK; 492 #ifdef TMPFS_DEBUG_DIRCOOKIE_DUP 493 hash &= 0xf; 494 #endif 495 if (hash < TMPFS_DIRCOOKIE_MIN) 496 hash += TMPFS_DIRCOOKIE_MIN; 497 498 return (hash); 499 } 500 501 static __inline off_t 502 tmpfs_dirent_cookie(struct tmpfs_dirent *de) 503 { 504 if (de == NULL) 505 return (TMPFS_DIRCOOKIE_EOF); 506 507 MPASS(de->td_cookie >= TMPFS_DIRCOOKIE_MIN); 508 509 return (de->td_cookie); 510 } 511 512 static __inline boolean_t 513 tmpfs_dirent_dup(struct tmpfs_dirent *de) 514 { 515 return ((de->td_cookie & TMPFS_DIRCOOKIE_DUP) != 0); 516 } 517 518 static __inline boolean_t 519 tmpfs_dirent_duphead(struct tmpfs_dirent *de) 520 { 521 return ((de->td_cookie & TMPFS_DIRCOOKIE_DUPHEAD) != 0); 522 } 523 524 void 525 tmpfs_dirent_init(struct tmpfs_dirent *de, const char *name, u_int namelen) 526 { 527 de->td_hash = de->td_cookie = tmpfs_dirent_hash(name, namelen); 528 memcpy(de->ud.td_name, name, namelen); 529 de->td_namelen = namelen; 530 } 531 532 /* 533 * Allocates a new directory entry for the node node with a name of name. 534 * The new directory entry is returned in *de. 535 * 536 * The link count of node is increased by one to reflect the new object 537 * referencing it. 538 * 539 * Returns zero on success or an appropriate error code on failure. 540 */ 541 int 542 tmpfs_alloc_dirent(struct tmpfs_mount *tmp, struct tmpfs_node *node, 543 const char *name, u_int len, struct tmpfs_dirent **de) 544 { 545 struct tmpfs_dirent *nde; 546 547 nde = malloc(sizeof(*nde), M_TMPFSDIR, M_WAITOK); 548 nde->td_node = node; 549 if (name != NULL) { 550 nde->ud.td_name = malloc(len, M_TMPFSNAME, M_WAITOK); 551 tmpfs_dirent_init(nde, name, len); 552 } else 553 nde->td_namelen = 0; 554 if (node != NULL) 555 node->tn_links++; 556 557 *de = nde; 558 559 return 0; 560 } 561 562 /* 563 * Frees a directory entry. It is the caller's responsibility to destroy 564 * the node referenced by it if needed. 565 * 566 * The link count of node is decreased by one to reflect the removal of an 567 * object that referenced it. This only happens if 'node_exists' is true; 568 * otherwise the function will not access the node referred to by the 569 * directory entry, as it may already have been released from the outside. 570 */ 571 void 572 tmpfs_free_dirent(struct tmpfs_mount *tmp, struct tmpfs_dirent *de) 573 { 574 struct tmpfs_node *node; 575 576 node = de->td_node; 577 if (node != NULL) { 578 MPASS(node->tn_links > 0); 579 node->tn_links--; 580 } 581 if (!tmpfs_dirent_duphead(de) && de->ud.td_name != NULL) 582 free(de->ud.td_name, M_TMPFSNAME); 583 free(de, M_TMPFSDIR); 584 } 585 586 void 587 tmpfs_destroy_vobject(struct vnode *vp, vm_object_t obj) 588 { 589 590 ASSERT_VOP_ELOCKED(vp, "tmpfs_destroy_vobject"); 591 if (vp->v_type != VREG || obj == NULL) 592 return; 593 594 VM_OBJECT_WLOCK(obj); 595 VI_LOCK(vp); 596 vm_object_clear_flag(obj, OBJ_TMPFS); 597 obj->un_pager.swp.swp_tmpfs = NULL; 598 if (vp->v_writecount < 0) 599 vp->v_writecount = 0; 600 VI_UNLOCK(vp); 601 VM_OBJECT_WUNLOCK(obj); 602 } 603 604 /* 605 * Need to clear v_object for insmntque failure. 606 */ 607 static void 608 tmpfs_insmntque_dtr(struct vnode *vp, void *dtr_arg) 609 { 610 611 tmpfs_destroy_vobject(vp, vp->v_object); 612 vp->v_object = NULL; 613 vp->v_data = NULL; 614 vp->v_op = &dead_vnodeops; 615 vgone(vp); 616 vput(vp); 617 } 618 619 /* 620 * Allocates a new vnode for the node node or returns a new reference to 621 * an existing one if the node had already a vnode referencing it. The 622 * resulting locked vnode is returned in *vpp. 623 * 624 * Returns zero on success or an appropriate error code on failure. 625 */ 626 int 627 tmpfs_alloc_vp(struct mount *mp, struct tmpfs_node *node, int lkflag, 628 struct vnode **vpp) 629 { 630 struct vnode *vp; 631 enum vgetstate vs; 632 struct tmpfs_mount *tm; 633 vm_object_t object; 634 int error; 635 636 error = 0; 637 tm = VFS_TO_TMPFS(mp); 638 TMPFS_NODE_LOCK(node); 639 tmpfs_ref_node(node); 640 loop: 641 TMPFS_NODE_ASSERT_LOCKED(node); 642 if ((vp = node->tn_vnode) != NULL) { 643 MPASS((node->tn_vpstate & TMPFS_VNODE_DOOMED) == 0); 644 if ((node->tn_type == VDIR && node->tn_dir.tn_parent == NULL) || 645 (VN_IS_DOOMED(vp) && 646 (lkflag & LK_NOWAIT) != 0)) { 647 TMPFS_NODE_UNLOCK(node); 648 error = ENOENT; 649 vp = NULL; 650 goto out; 651 } 652 if (VN_IS_DOOMED(vp)) { 653 node->tn_vpstate |= TMPFS_VNODE_WRECLAIM; 654 while ((node->tn_vpstate & TMPFS_VNODE_WRECLAIM) != 0) { 655 msleep(&node->tn_vnode, TMPFS_NODE_MTX(node), 656 0, "tmpfsE", 0); 657 } 658 goto loop; 659 } 660 vs = vget_prep(vp); 661 TMPFS_NODE_UNLOCK(node); 662 error = vget_finish(vp, lkflag, vs); 663 if (error == ENOENT) { 664 TMPFS_NODE_LOCK(node); 665 goto loop; 666 } 667 if (error != 0) { 668 vp = NULL; 669 goto out; 670 } 671 672 /* 673 * Make sure the vnode is still there after 674 * getting the interlock to avoid racing a free. 675 */ 676 if (node->tn_vnode == NULL || node->tn_vnode != vp) { 677 vput(vp); 678 TMPFS_NODE_LOCK(node); 679 goto loop; 680 } 681 682 goto out; 683 } 684 685 if ((node->tn_vpstate & TMPFS_VNODE_DOOMED) || 686 (node->tn_type == VDIR && node->tn_dir.tn_parent == NULL)) { 687 TMPFS_NODE_UNLOCK(node); 688 error = ENOENT; 689 vp = NULL; 690 goto out; 691 } 692 693 /* 694 * otherwise lock the vp list while we call getnewvnode 695 * since that can block. 696 */ 697 if (node->tn_vpstate & TMPFS_VNODE_ALLOCATING) { 698 node->tn_vpstate |= TMPFS_VNODE_WANT; 699 error = msleep((caddr_t) &node->tn_vpstate, 700 TMPFS_NODE_MTX(node), 0, "tmpfs_alloc_vp", 0); 701 if (error != 0) 702 goto out; 703 goto loop; 704 } else 705 node->tn_vpstate |= TMPFS_VNODE_ALLOCATING; 706 707 TMPFS_NODE_UNLOCK(node); 708 709 /* Get a new vnode and associate it with our node. */ 710 error = getnewvnode("tmpfs", mp, VFS_TO_TMPFS(mp)->tm_nonc ? 711 &tmpfs_vnodeop_nonc_entries : &tmpfs_vnodeop_entries, &vp); 712 if (error != 0) 713 goto unlock; 714 MPASS(vp != NULL); 715 716 /* lkflag is ignored, the lock is exclusive */ 717 (void) vn_lock(vp, LK_EXCLUSIVE | LK_RETRY); 718 719 vp->v_data = node; 720 vp->v_type = node->tn_type; 721 722 /* Type-specific initialization. */ 723 switch (node->tn_type) { 724 case VBLK: 725 /* FALLTHROUGH */ 726 case VCHR: 727 /* FALLTHROUGH */ 728 case VLNK: 729 /* FALLTHROUGH */ 730 case VSOCK: 731 break; 732 case VFIFO: 733 vp->v_op = &tmpfs_fifoop_entries; 734 break; 735 case VREG: 736 object = node->tn_reg.tn_aobj; 737 VM_OBJECT_WLOCK(object); 738 VI_LOCK(vp); 739 KASSERT(vp->v_object == NULL, ("Not NULL v_object in tmpfs")); 740 vp->v_object = object; 741 object->un_pager.swp.swp_tmpfs = vp; 742 vm_object_set_flag(object, OBJ_TMPFS); 743 vn_irflag_set_locked(vp, VIRF_PGREAD); 744 VI_UNLOCK(vp); 745 VM_OBJECT_WUNLOCK(object); 746 break; 747 case VDIR: 748 MPASS(node->tn_dir.tn_parent != NULL); 749 if (node->tn_dir.tn_parent == node) 750 vp->v_vflag |= VV_ROOT; 751 break; 752 753 default: 754 panic("tmpfs_alloc_vp: type %p %d", node, (int)node->tn_type); 755 } 756 if (vp->v_type != VFIFO) 757 VN_LOCK_ASHARE(vp); 758 759 error = insmntque1(vp, mp, tmpfs_insmntque_dtr, NULL); 760 if (error != 0) 761 vp = NULL; 762 763 unlock: 764 TMPFS_NODE_LOCK(node); 765 766 MPASS(node->tn_vpstate & TMPFS_VNODE_ALLOCATING); 767 node->tn_vpstate &= ~TMPFS_VNODE_ALLOCATING; 768 node->tn_vnode = vp; 769 770 if (node->tn_vpstate & TMPFS_VNODE_WANT) { 771 node->tn_vpstate &= ~TMPFS_VNODE_WANT; 772 TMPFS_NODE_UNLOCK(node); 773 wakeup((caddr_t) &node->tn_vpstate); 774 } else 775 TMPFS_NODE_UNLOCK(node); 776 777 out: 778 if (error == 0) { 779 *vpp = vp; 780 781 #ifdef INVARIANTS 782 MPASS(*vpp != NULL && VOP_ISLOCKED(*vpp)); 783 TMPFS_NODE_LOCK(node); 784 MPASS(*vpp == node->tn_vnode); 785 TMPFS_NODE_UNLOCK(node); 786 #endif 787 } 788 tmpfs_free_node(tm, node); 789 790 return (error); 791 } 792 793 /* 794 * Destroys the association between the vnode vp and the node it 795 * references. 796 */ 797 void 798 tmpfs_free_vp(struct vnode *vp) 799 { 800 struct tmpfs_node *node; 801 802 node = VP_TO_TMPFS_NODE(vp); 803 804 TMPFS_NODE_ASSERT_LOCKED(node); 805 node->tn_vnode = NULL; 806 if ((node->tn_vpstate & TMPFS_VNODE_WRECLAIM) != 0) 807 wakeup(&node->tn_vnode); 808 node->tn_vpstate &= ~TMPFS_VNODE_WRECLAIM; 809 vp->v_data = NULL; 810 } 811 812 /* 813 * Allocates a new file of type 'type' and adds it to the parent directory 814 * 'dvp'; this addition is done using the component name given in 'cnp'. 815 * The ownership of the new file is automatically assigned based on the 816 * credentials of the caller (through 'cnp'), the group is set based on 817 * the parent directory and the mode is determined from the 'vap' argument. 818 * If successful, *vpp holds a vnode to the newly created file and zero 819 * is returned. Otherwise *vpp is NULL and the function returns an 820 * appropriate error code. 821 */ 822 int 823 tmpfs_alloc_file(struct vnode *dvp, struct vnode **vpp, struct vattr *vap, 824 struct componentname *cnp, const char *target) 825 { 826 int error; 827 struct tmpfs_dirent *de; 828 struct tmpfs_mount *tmp; 829 struct tmpfs_node *dnode; 830 struct tmpfs_node *node; 831 struct tmpfs_node *parent; 832 833 ASSERT_VOP_ELOCKED(dvp, "tmpfs_alloc_file"); 834 MPASS(cnp->cn_flags & HASBUF); 835 836 tmp = VFS_TO_TMPFS(dvp->v_mount); 837 dnode = VP_TO_TMPFS_DIR(dvp); 838 *vpp = NULL; 839 840 /* If the entry we are creating is a directory, we cannot overflow 841 * the number of links of its parent, because it will get a new 842 * link. */ 843 if (vap->va_type == VDIR) { 844 /* Ensure that we do not overflow the maximum number of links 845 * imposed by the system. */ 846 MPASS(dnode->tn_links <= TMPFS_LINK_MAX); 847 if (dnode->tn_links == TMPFS_LINK_MAX) { 848 return (EMLINK); 849 } 850 851 parent = dnode; 852 MPASS(parent != NULL); 853 } else 854 parent = NULL; 855 856 /* Allocate a node that represents the new file. */ 857 error = tmpfs_alloc_node(dvp->v_mount, tmp, vap->va_type, 858 cnp->cn_cred->cr_uid, dnode->tn_gid, vap->va_mode, parent, 859 target, vap->va_rdev, &node); 860 if (error != 0) 861 return (error); 862 863 /* Allocate a directory entry that points to the new file. */ 864 error = tmpfs_alloc_dirent(tmp, node, cnp->cn_nameptr, cnp->cn_namelen, 865 &de); 866 if (error != 0) { 867 tmpfs_free_node(tmp, node); 868 return (error); 869 } 870 871 /* Allocate a vnode for the new file. */ 872 error = tmpfs_alloc_vp(dvp->v_mount, node, LK_EXCLUSIVE, vpp); 873 if (error != 0) { 874 tmpfs_free_dirent(tmp, de); 875 tmpfs_free_node(tmp, node); 876 return (error); 877 } 878 879 /* Now that all required items are allocated, we can proceed to 880 * insert the new node into the directory, an operation that 881 * cannot fail. */ 882 if (cnp->cn_flags & ISWHITEOUT) 883 tmpfs_dir_whiteout_remove(dvp, cnp); 884 tmpfs_dir_attach(dvp, de); 885 return (0); 886 } 887 888 struct tmpfs_dirent * 889 tmpfs_dir_first(struct tmpfs_node *dnode, struct tmpfs_dir_cursor *dc) 890 { 891 struct tmpfs_dirent *de; 892 893 de = RB_MIN(tmpfs_dir, &dnode->tn_dir.tn_dirhead); 894 dc->tdc_tree = de; 895 if (de != NULL && tmpfs_dirent_duphead(de)) 896 de = LIST_FIRST(&de->ud.td_duphead); 897 dc->tdc_current = de; 898 899 return (dc->tdc_current); 900 } 901 902 struct tmpfs_dirent * 903 tmpfs_dir_next(struct tmpfs_node *dnode, struct tmpfs_dir_cursor *dc) 904 { 905 struct tmpfs_dirent *de; 906 907 MPASS(dc->tdc_tree != NULL); 908 if (tmpfs_dirent_dup(dc->tdc_current)) { 909 dc->tdc_current = LIST_NEXT(dc->tdc_current, uh.td_dup.entries); 910 if (dc->tdc_current != NULL) 911 return (dc->tdc_current); 912 } 913 dc->tdc_tree = dc->tdc_current = RB_NEXT(tmpfs_dir, 914 &dnode->tn_dir.tn_dirhead, dc->tdc_tree); 915 if ((de = dc->tdc_current) != NULL && tmpfs_dirent_duphead(de)) { 916 dc->tdc_current = LIST_FIRST(&de->ud.td_duphead); 917 MPASS(dc->tdc_current != NULL); 918 } 919 920 return (dc->tdc_current); 921 } 922 923 /* Lookup directory entry in RB-Tree. Function may return duphead entry. */ 924 static struct tmpfs_dirent * 925 tmpfs_dir_xlookup_hash(struct tmpfs_node *dnode, uint32_t hash) 926 { 927 struct tmpfs_dirent *de, dekey; 928 929 dekey.td_hash = hash; 930 de = RB_FIND(tmpfs_dir, &dnode->tn_dir.tn_dirhead, &dekey); 931 return (de); 932 } 933 934 /* Lookup directory entry by cookie, initialize directory cursor accordingly. */ 935 static struct tmpfs_dirent * 936 tmpfs_dir_lookup_cookie(struct tmpfs_node *node, off_t cookie, 937 struct tmpfs_dir_cursor *dc) 938 { 939 struct tmpfs_dir *dirhead = &node->tn_dir.tn_dirhead; 940 struct tmpfs_dirent *de, dekey; 941 942 MPASS(cookie >= TMPFS_DIRCOOKIE_MIN); 943 944 if (cookie == node->tn_dir.tn_readdir_lastn && 945 (de = node->tn_dir.tn_readdir_lastp) != NULL) { 946 /* Protect against possible race, tn_readdir_last[pn] 947 * may be updated with only shared vnode lock held. */ 948 if (cookie == tmpfs_dirent_cookie(de)) 949 goto out; 950 } 951 952 if ((cookie & TMPFS_DIRCOOKIE_DUP) != 0) { 953 LIST_FOREACH(de, &node->tn_dir.tn_dupindex, 954 uh.td_dup.index_entries) { 955 MPASS(tmpfs_dirent_dup(de)); 956 if (de->td_cookie == cookie) 957 goto out; 958 /* dupindex list is sorted. */ 959 if (de->td_cookie < cookie) { 960 de = NULL; 961 goto out; 962 } 963 } 964 MPASS(de == NULL); 965 goto out; 966 } 967 968 if ((cookie & TMPFS_DIRCOOKIE_MASK) != cookie) { 969 de = NULL; 970 } else { 971 dekey.td_hash = cookie; 972 /* Recover if direntry for cookie was removed */ 973 de = RB_NFIND(tmpfs_dir, dirhead, &dekey); 974 } 975 dc->tdc_tree = de; 976 dc->tdc_current = de; 977 if (de != NULL && tmpfs_dirent_duphead(de)) { 978 dc->tdc_current = LIST_FIRST(&de->ud.td_duphead); 979 MPASS(dc->tdc_current != NULL); 980 } 981 return (dc->tdc_current); 982 983 out: 984 dc->tdc_tree = de; 985 dc->tdc_current = de; 986 if (de != NULL && tmpfs_dirent_dup(de)) 987 dc->tdc_tree = tmpfs_dir_xlookup_hash(node, 988 de->td_hash); 989 return (dc->tdc_current); 990 } 991 992 /* 993 * Looks for a directory entry in the directory represented by node. 994 * 'cnp' describes the name of the entry to look for. Note that the . 995 * and .. components are not allowed as they do not physically exist 996 * within directories. 997 * 998 * Returns a pointer to the entry when found, otherwise NULL. 999 */ 1000 struct tmpfs_dirent * 1001 tmpfs_dir_lookup(struct tmpfs_node *node, struct tmpfs_node *f, 1002 struct componentname *cnp) 1003 { 1004 struct tmpfs_dir_duphead *duphead; 1005 struct tmpfs_dirent *de; 1006 uint32_t hash; 1007 1008 MPASS(IMPLIES(cnp->cn_namelen == 1, cnp->cn_nameptr[0] != '.')); 1009 MPASS(IMPLIES(cnp->cn_namelen == 2, !(cnp->cn_nameptr[0] == '.' && 1010 cnp->cn_nameptr[1] == '.'))); 1011 TMPFS_VALIDATE_DIR(node); 1012 1013 hash = tmpfs_dirent_hash(cnp->cn_nameptr, cnp->cn_namelen); 1014 de = tmpfs_dir_xlookup_hash(node, hash); 1015 if (de != NULL && tmpfs_dirent_duphead(de)) { 1016 duphead = &de->ud.td_duphead; 1017 LIST_FOREACH(de, duphead, uh.td_dup.entries) { 1018 if (TMPFS_DIRENT_MATCHES(de, cnp->cn_nameptr, 1019 cnp->cn_namelen)) 1020 break; 1021 } 1022 } else if (de != NULL) { 1023 if (!TMPFS_DIRENT_MATCHES(de, cnp->cn_nameptr, 1024 cnp->cn_namelen)) 1025 de = NULL; 1026 } 1027 if (de != NULL && f != NULL && de->td_node != f) 1028 de = NULL; 1029 1030 return (de); 1031 } 1032 1033 /* 1034 * Attach duplicate-cookie directory entry nde to dnode and insert to dupindex 1035 * list, allocate new cookie value. 1036 */ 1037 static void 1038 tmpfs_dir_attach_dup(struct tmpfs_node *dnode, 1039 struct tmpfs_dir_duphead *duphead, struct tmpfs_dirent *nde) 1040 { 1041 struct tmpfs_dir_duphead *dupindex; 1042 struct tmpfs_dirent *de, *pde; 1043 1044 dupindex = &dnode->tn_dir.tn_dupindex; 1045 de = LIST_FIRST(dupindex); 1046 if (de == NULL || de->td_cookie < TMPFS_DIRCOOKIE_DUP_MAX) { 1047 if (de == NULL) 1048 nde->td_cookie = TMPFS_DIRCOOKIE_DUP_MIN; 1049 else 1050 nde->td_cookie = de->td_cookie + 1; 1051 MPASS(tmpfs_dirent_dup(nde)); 1052 LIST_INSERT_HEAD(dupindex, nde, uh.td_dup.index_entries); 1053 LIST_INSERT_HEAD(duphead, nde, uh.td_dup.entries); 1054 return; 1055 } 1056 1057 /* 1058 * Cookie numbers are near exhaustion. Scan dupindex list for unused 1059 * numbers. dupindex list is sorted in descending order. Keep it so 1060 * after inserting nde. 1061 */ 1062 while (1) { 1063 pde = de; 1064 de = LIST_NEXT(de, uh.td_dup.index_entries); 1065 if (de == NULL && pde->td_cookie != TMPFS_DIRCOOKIE_DUP_MIN) { 1066 /* 1067 * Last element of the index doesn't have minimal cookie 1068 * value, use it. 1069 */ 1070 nde->td_cookie = TMPFS_DIRCOOKIE_DUP_MIN; 1071 LIST_INSERT_AFTER(pde, nde, uh.td_dup.index_entries); 1072 LIST_INSERT_HEAD(duphead, nde, uh.td_dup.entries); 1073 return; 1074 } else if (de == NULL) { 1075 /* 1076 * We are so lucky have 2^30 hash duplicates in single 1077 * directory :) Return largest possible cookie value. 1078 * It should be fine except possible issues with 1079 * VOP_READDIR restart. 1080 */ 1081 nde->td_cookie = TMPFS_DIRCOOKIE_DUP_MAX; 1082 LIST_INSERT_HEAD(dupindex, nde, 1083 uh.td_dup.index_entries); 1084 LIST_INSERT_HEAD(duphead, nde, uh.td_dup.entries); 1085 return; 1086 } 1087 if (de->td_cookie + 1 == pde->td_cookie || 1088 de->td_cookie >= TMPFS_DIRCOOKIE_DUP_MAX) 1089 continue; /* No hole or invalid cookie. */ 1090 nde->td_cookie = de->td_cookie + 1; 1091 MPASS(tmpfs_dirent_dup(nde)); 1092 MPASS(pde->td_cookie > nde->td_cookie); 1093 MPASS(nde->td_cookie > de->td_cookie); 1094 LIST_INSERT_BEFORE(de, nde, uh.td_dup.index_entries); 1095 LIST_INSERT_HEAD(duphead, nde, uh.td_dup.entries); 1096 return; 1097 } 1098 } 1099 1100 /* 1101 * Attaches the directory entry de to the directory represented by vp. 1102 * Note that this does not change the link count of the node pointed by 1103 * the directory entry, as this is done by tmpfs_alloc_dirent. 1104 */ 1105 void 1106 tmpfs_dir_attach(struct vnode *vp, struct tmpfs_dirent *de) 1107 { 1108 struct tmpfs_node *dnode; 1109 struct tmpfs_dirent *xde, *nde; 1110 1111 ASSERT_VOP_ELOCKED(vp, __func__); 1112 MPASS(de->td_namelen > 0); 1113 MPASS(de->td_hash >= TMPFS_DIRCOOKIE_MIN); 1114 MPASS(de->td_cookie == de->td_hash); 1115 1116 dnode = VP_TO_TMPFS_DIR(vp); 1117 dnode->tn_dir.tn_readdir_lastn = 0; 1118 dnode->tn_dir.tn_readdir_lastp = NULL; 1119 1120 MPASS(!tmpfs_dirent_dup(de)); 1121 xde = RB_INSERT(tmpfs_dir, &dnode->tn_dir.tn_dirhead, de); 1122 if (xde != NULL && tmpfs_dirent_duphead(xde)) 1123 tmpfs_dir_attach_dup(dnode, &xde->ud.td_duphead, de); 1124 else if (xde != NULL) { 1125 /* 1126 * Allocate new duphead. Swap xde with duphead to avoid 1127 * adding/removing elements with the same hash. 1128 */ 1129 MPASS(!tmpfs_dirent_dup(xde)); 1130 tmpfs_alloc_dirent(VFS_TO_TMPFS(vp->v_mount), NULL, NULL, 0, 1131 &nde); 1132 /* *nde = *xde; XXX gcc 4.2.1 may generate invalid code. */ 1133 memcpy(nde, xde, sizeof(*xde)); 1134 xde->td_cookie |= TMPFS_DIRCOOKIE_DUPHEAD; 1135 LIST_INIT(&xde->ud.td_duphead); 1136 xde->td_namelen = 0; 1137 xde->td_node = NULL; 1138 tmpfs_dir_attach_dup(dnode, &xde->ud.td_duphead, nde); 1139 tmpfs_dir_attach_dup(dnode, &xde->ud.td_duphead, de); 1140 } 1141 dnode->tn_size += sizeof(struct tmpfs_dirent); 1142 dnode->tn_status |= TMPFS_NODE_CHANGED | TMPFS_NODE_MODIFIED; 1143 dnode->tn_accessed = true; 1144 tmpfs_update(vp); 1145 } 1146 1147 /* 1148 * Detaches the directory entry de from the directory represented by vp. 1149 * Note that this does not change the link count of the node pointed by 1150 * the directory entry, as this is done by tmpfs_free_dirent. 1151 */ 1152 void 1153 tmpfs_dir_detach(struct vnode *vp, struct tmpfs_dirent *de) 1154 { 1155 struct tmpfs_mount *tmp; 1156 struct tmpfs_dir *head; 1157 struct tmpfs_node *dnode; 1158 struct tmpfs_dirent *xde; 1159 1160 ASSERT_VOP_ELOCKED(vp, __func__); 1161 1162 dnode = VP_TO_TMPFS_DIR(vp); 1163 head = &dnode->tn_dir.tn_dirhead; 1164 dnode->tn_dir.tn_readdir_lastn = 0; 1165 dnode->tn_dir.tn_readdir_lastp = NULL; 1166 1167 if (tmpfs_dirent_dup(de)) { 1168 /* Remove duphead if de was last entry. */ 1169 if (LIST_NEXT(de, uh.td_dup.entries) == NULL) { 1170 xde = tmpfs_dir_xlookup_hash(dnode, de->td_hash); 1171 MPASS(tmpfs_dirent_duphead(xde)); 1172 } else 1173 xde = NULL; 1174 LIST_REMOVE(de, uh.td_dup.entries); 1175 LIST_REMOVE(de, uh.td_dup.index_entries); 1176 if (xde != NULL) { 1177 if (LIST_EMPTY(&xde->ud.td_duphead)) { 1178 RB_REMOVE(tmpfs_dir, head, xde); 1179 tmp = VFS_TO_TMPFS(vp->v_mount); 1180 MPASS(xde->td_node == NULL); 1181 tmpfs_free_dirent(tmp, xde); 1182 } 1183 } 1184 de->td_cookie = de->td_hash; 1185 } else 1186 RB_REMOVE(tmpfs_dir, head, de); 1187 1188 dnode->tn_size -= sizeof(struct tmpfs_dirent); 1189 dnode->tn_status |= TMPFS_NODE_CHANGED | TMPFS_NODE_MODIFIED; 1190 dnode->tn_accessed = true; 1191 tmpfs_update(vp); 1192 } 1193 1194 void 1195 tmpfs_dir_destroy(struct tmpfs_mount *tmp, struct tmpfs_node *dnode) 1196 { 1197 struct tmpfs_dirent *de, *dde, *nde; 1198 1199 RB_FOREACH_SAFE(de, tmpfs_dir, &dnode->tn_dir.tn_dirhead, nde) { 1200 RB_REMOVE(tmpfs_dir, &dnode->tn_dir.tn_dirhead, de); 1201 /* Node may already be destroyed. */ 1202 de->td_node = NULL; 1203 if (tmpfs_dirent_duphead(de)) { 1204 while ((dde = LIST_FIRST(&de->ud.td_duphead)) != NULL) { 1205 LIST_REMOVE(dde, uh.td_dup.entries); 1206 dde->td_node = NULL; 1207 tmpfs_free_dirent(tmp, dde); 1208 } 1209 } 1210 tmpfs_free_dirent(tmp, de); 1211 } 1212 } 1213 1214 /* 1215 * Helper function for tmpfs_readdir. Creates a '.' entry for the given 1216 * directory and returns it in the uio space. The function returns 0 1217 * on success, -1 if there was not enough space in the uio structure to 1218 * hold the directory entry or an appropriate error code if another 1219 * error happens. 1220 */ 1221 static int 1222 tmpfs_dir_getdotdent(struct tmpfs_mount *tm, struct tmpfs_node *node, 1223 struct uio *uio) 1224 { 1225 int error; 1226 struct dirent dent; 1227 1228 TMPFS_VALIDATE_DIR(node); 1229 MPASS(uio->uio_offset == TMPFS_DIRCOOKIE_DOT); 1230 1231 dent.d_fileno = node->tn_id; 1232 dent.d_off = TMPFS_DIRCOOKIE_DOTDOT; 1233 dent.d_type = DT_DIR; 1234 dent.d_namlen = 1; 1235 dent.d_name[0] = '.'; 1236 dent.d_reclen = GENERIC_DIRSIZ(&dent); 1237 dirent_terminate(&dent); 1238 1239 if (dent.d_reclen > uio->uio_resid) 1240 error = EJUSTRETURN; 1241 else 1242 error = uiomove(&dent, dent.d_reclen, uio); 1243 1244 tmpfs_set_accessed(tm, node); 1245 1246 return (error); 1247 } 1248 1249 /* 1250 * Helper function for tmpfs_readdir. Creates a '..' entry for the given 1251 * directory and returns it in the uio space. The function returns 0 1252 * on success, -1 if there was not enough space in the uio structure to 1253 * hold the directory entry or an appropriate error code if another 1254 * error happens. 1255 */ 1256 static int 1257 tmpfs_dir_getdotdotdent(struct tmpfs_mount *tm, struct tmpfs_node *node, 1258 struct uio *uio, off_t next) 1259 { 1260 struct tmpfs_node *parent; 1261 struct dirent dent; 1262 int error; 1263 1264 TMPFS_VALIDATE_DIR(node); 1265 MPASS(uio->uio_offset == TMPFS_DIRCOOKIE_DOTDOT); 1266 1267 /* 1268 * Return ENOENT if the current node is already removed. 1269 */ 1270 TMPFS_ASSERT_LOCKED(node); 1271 parent = node->tn_dir.tn_parent; 1272 if (parent == NULL) 1273 return (ENOENT); 1274 1275 TMPFS_NODE_LOCK(parent); 1276 dent.d_fileno = parent->tn_id; 1277 TMPFS_NODE_UNLOCK(parent); 1278 1279 dent.d_off = next; 1280 dent.d_type = DT_DIR; 1281 dent.d_namlen = 2; 1282 dent.d_name[0] = '.'; 1283 dent.d_name[1] = '.'; 1284 dent.d_reclen = GENERIC_DIRSIZ(&dent); 1285 dirent_terminate(&dent); 1286 1287 if (dent.d_reclen > uio->uio_resid) 1288 error = EJUSTRETURN; 1289 else 1290 error = uiomove(&dent, dent.d_reclen, uio); 1291 1292 tmpfs_set_accessed(tm, node); 1293 1294 return (error); 1295 } 1296 1297 /* 1298 * Helper function for tmpfs_readdir. Returns as much directory entries 1299 * as can fit in the uio space. The read starts at uio->uio_offset. 1300 * The function returns 0 on success, -1 if there was not enough space 1301 * in the uio structure to hold the directory entry or an appropriate 1302 * error code if another error happens. 1303 */ 1304 int 1305 tmpfs_dir_getdents(struct tmpfs_mount *tm, struct tmpfs_node *node, 1306 struct uio *uio, int maxcookies, u_long *cookies, int *ncookies) 1307 { 1308 struct tmpfs_dir_cursor dc; 1309 struct tmpfs_dirent *de, *nde; 1310 off_t off; 1311 int error; 1312 1313 TMPFS_VALIDATE_DIR(node); 1314 1315 off = 0; 1316 1317 /* 1318 * Lookup the node from the current offset. The starting offset of 1319 * 0 will lookup both '.' and '..', and then the first real entry, 1320 * or EOF if there are none. Then find all entries for the dir that 1321 * fit into the buffer. Once no more entries are found (de == NULL), 1322 * the offset is set to TMPFS_DIRCOOKIE_EOF, which will cause the next 1323 * call to return 0. 1324 */ 1325 switch (uio->uio_offset) { 1326 case TMPFS_DIRCOOKIE_DOT: 1327 error = tmpfs_dir_getdotdent(tm, node, uio); 1328 if (error != 0) 1329 return (error); 1330 uio->uio_offset = off = TMPFS_DIRCOOKIE_DOTDOT; 1331 if (cookies != NULL) 1332 cookies[(*ncookies)++] = off; 1333 /* FALLTHROUGH */ 1334 case TMPFS_DIRCOOKIE_DOTDOT: 1335 de = tmpfs_dir_first(node, &dc); 1336 off = tmpfs_dirent_cookie(de); 1337 error = tmpfs_dir_getdotdotdent(tm, node, uio, off); 1338 if (error != 0) 1339 return (error); 1340 uio->uio_offset = off; 1341 if (cookies != NULL) 1342 cookies[(*ncookies)++] = off; 1343 /* EOF. */ 1344 if (de == NULL) 1345 return (0); 1346 break; 1347 case TMPFS_DIRCOOKIE_EOF: 1348 return (0); 1349 default: 1350 de = tmpfs_dir_lookup_cookie(node, uio->uio_offset, &dc); 1351 if (de == NULL) 1352 return (EINVAL); 1353 if (cookies != NULL) 1354 off = tmpfs_dirent_cookie(de); 1355 } 1356 1357 /* 1358 * Read as much entries as possible; i.e., until we reach the end of the 1359 * directory or we exhaust uio space. 1360 */ 1361 do { 1362 struct dirent d; 1363 1364 /* 1365 * Create a dirent structure representing the current tmpfs_node 1366 * and fill it. 1367 */ 1368 if (de->td_node == NULL) { 1369 d.d_fileno = 1; 1370 d.d_type = DT_WHT; 1371 } else { 1372 d.d_fileno = de->td_node->tn_id; 1373 switch (de->td_node->tn_type) { 1374 case VBLK: 1375 d.d_type = DT_BLK; 1376 break; 1377 1378 case VCHR: 1379 d.d_type = DT_CHR; 1380 break; 1381 1382 case VDIR: 1383 d.d_type = DT_DIR; 1384 break; 1385 1386 case VFIFO: 1387 d.d_type = DT_FIFO; 1388 break; 1389 1390 case VLNK: 1391 d.d_type = DT_LNK; 1392 break; 1393 1394 case VREG: 1395 d.d_type = DT_REG; 1396 break; 1397 1398 case VSOCK: 1399 d.d_type = DT_SOCK; 1400 break; 1401 1402 default: 1403 panic("tmpfs_dir_getdents: type %p %d", 1404 de->td_node, (int)de->td_node->tn_type); 1405 } 1406 } 1407 d.d_namlen = de->td_namelen; 1408 MPASS(de->td_namelen < sizeof(d.d_name)); 1409 (void)memcpy(d.d_name, de->ud.td_name, de->td_namelen); 1410 d.d_reclen = GENERIC_DIRSIZ(&d); 1411 1412 /* 1413 * Stop reading if the directory entry we are treating is bigger 1414 * than the amount of data that can be returned. 1415 */ 1416 if (d.d_reclen > uio->uio_resid) { 1417 error = EJUSTRETURN; 1418 break; 1419 } 1420 1421 nde = tmpfs_dir_next(node, &dc); 1422 d.d_off = tmpfs_dirent_cookie(nde); 1423 dirent_terminate(&d); 1424 1425 /* 1426 * Copy the new dirent structure into the output buffer and 1427 * advance pointers. 1428 */ 1429 error = uiomove(&d, d.d_reclen, uio); 1430 if (error == 0) { 1431 de = nde; 1432 if (cookies != NULL) { 1433 off = tmpfs_dirent_cookie(de); 1434 MPASS(*ncookies < maxcookies); 1435 cookies[(*ncookies)++] = off; 1436 } 1437 } 1438 } while (error == 0 && uio->uio_resid > 0 && de != NULL); 1439 1440 /* Skip setting off when using cookies as it is already done above. */ 1441 if (cookies == NULL) 1442 off = tmpfs_dirent_cookie(de); 1443 1444 /* Update the offset and cache. */ 1445 uio->uio_offset = off; 1446 node->tn_dir.tn_readdir_lastn = off; 1447 node->tn_dir.tn_readdir_lastp = de; 1448 1449 tmpfs_set_accessed(tm, node); 1450 return (error); 1451 } 1452 1453 int 1454 tmpfs_dir_whiteout_add(struct vnode *dvp, struct componentname *cnp) 1455 { 1456 struct tmpfs_dirent *de; 1457 int error; 1458 1459 error = tmpfs_alloc_dirent(VFS_TO_TMPFS(dvp->v_mount), NULL, 1460 cnp->cn_nameptr, cnp->cn_namelen, &de); 1461 if (error != 0) 1462 return (error); 1463 tmpfs_dir_attach(dvp, de); 1464 return (0); 1465 } 1466 1467 void 1468 tmpfs_dir_whiteout_remove(struct vnode *dvp, struct componentname *cnp) 1469 { 1470 struct tmpfs_dirent *de; 1471 1472 de = tmpfs_dir_lookup(VP_TO_TMPFS_DIR(dvp), NULL, cnp); 1473 MPASS(de != NULL && de->td_node == NULL); 1474 tmpfs_dir_detach(dvp, de); 1475 tmpfs_free_dirent(VFS_TO_TMPFS(dvp->v_mount), de); 1476 } 1477 1478 /* 1479 * Resizes the aobj associated with the regular file pointed to by 'vp' to the 1480 * size 'newsize'. 'vp' must point to a vnode that represents a regular file. 1481 * 'newsize' must be positive. 1482 * 1483 * Returns zero on success or an appropriate error code on failure. 1484 */ 1485 int 1486 tmpfs_reg_resize(struct vnode *vp, off_t newsize, boolean_t ignerr) 1487 { 1488 struct tmpfs_mount *tmp; 1489 struct tmpfs_node *node; 1490 vm_object_t uobj; 1491 vm_page_t m; 1492 vm_pindex_t idx, newpages, oldpages; 1493 off_t oldsize; 1494 int base, rv; 1495 1496 MPASS(vp->v_type == VREG); 1497 MPASS(newsize >= 0); 1498 1499 node = VP_TO_TMPFS_NODE(vp); 1500 uobj = node->tn_reg.tn_aobj; 1501 tmp = VFS_TO_TMPFS(vp->v_mount); 1502 1503 /* 1504 * Convert the old and new sizes to the number of pages needed to 1505 * store them. It may happen that we do not need to do anything 1506 * because the last allocated page can accommodate the change on 1507 * its own. 1508 */ 1509 oldsize = node->tn_size; 1510 oldpages = OFF_TO_IDX(oldsize + PAGE_MASK); 1511 MPASS(oldpages == uobj->size); 1512 newpages = OFF_TO_IDX(newsize + PAGE_MASK); 1513 1514 if (__predict_true(newpages == oldpages && newsize >= oldsize)) { 1515 node->tn_size = newsize; 1516 return (0); 1517 } 1518 1519 if (newpages > oldpages && 1520 tmpfs_pages_check_avail(tmp, newpages - oldpages) == 0) 1521 return (ENOSPC); 1522 1523 VM_OBJECT_WLOCK(uobj); 1524 if (newsize < oldsize) { 1525 /* 1526 * Zero the truncated part of the last page. 1527 */ 1528 base = newsize & PAGE_MASK; 1529 if (base != 0) { 1530 idx = OFF_TO_IDX(newsize); 1531 retry: 1532 m = vm_page_grab(uobj, idx, VM_ALLOC_NOCREAT); 1533 if (m != NULL) { 1534 MPASS(vm_page_all_valid(m)); 1535 } else if (vm_pager_has_page(uobj, idx, NULL, NULL)) { 1536 m = vm_page_alloc(uobj, idx, VM_ALLOC_NORMAL | 1537 VM_ALLOC_WAITFAIL); 1538 if (m == NULL) 1539 goto retry; 1540 vm_object_pip_add(uobj, 1); 1541 VM_OBJECT_WUNLOCK(uobj); 1542 rv = vm_pager_get_pages(uobj, &m, 1, NULL, 1543 NULL); 1544 VM_OBJECT_WLOCK(uobj); 1545 vm_object_pip_wakeup(uobj); 1546 if (rv == VM_PAGER_OK) { 1547 /* 1548 * Since the page was not resident, 1549 * and therefore not recently 1550 * accessed, immediately enqueue it 1551 * for asynchronous laundering. The 1552 * current operation is not regarded 1553 * as an access. 1554 */ 1555 vm_page_launder(m); 1556 } else { 1557 vm_page_free(m); 1558 if (ignerr) 1559 m = NULL; 1560 else { 1561 VM_OBJECT_WUNLOCK(uobj); 1562 return (EIO); 1563 } 1564 } 1565 } 1566 if (m != NULL) { 1567 pmap_zero_page_area(m, base, PAGE_SIZE - base); 1568 vm_page_set_dirty(m); 1569 vm_page_xunbusy(m); 1570 } 1571 } 1572 1573 /* 1574 * Release any swap space and free any whole pages. 1575 */ 1576 if (newpages < oldpages) 1577 vm_object_page_remove(uobj, newpages, 0, 0); 1578 } 1579 uobj->size = newpages; 1580 VM_OBJECT_WUNLOCK(uobj); 1581 1582 atomic_add_long(&tmp->tm_pages_used, newpages - oldpages); 1583 1584 node->tn_size = newsize; 1585 return (0); 1586 } 1587 1588 void 1589 tmpfs_check_mtime(struct vnode *vp) 1590 { 1591 struct tmpfs_node *node; 1592 struct vm_object *obj; 1593 1594 ASSERT_VOP_ELOCKED(vp, "check_mtime"); 1595 if (vp->v_type != VREG) 1596 return; 1597 obj = vp->v_object; 1598 KASSERT((obj->flags & (OBJ_TMPFS_NODE | OBJ_TMPFS)) == 1599 (OBJ_TMPFS_NODE | OBJ_TMPFS), ("non-tmpfs obj")); 1600 /* unlocked read */ 1601 if (obj->generation != obj->cleangeneration) { 1602 VM_OBJECT_WLOCK(obj); 1603 if (obj->generation != obj->cleangeneration) { 1604 obj->cleangeneration = obj->generation; 1605 node = VP_TO_TMPFS_NODE(vp); 1606 node->tn_status |= TMPFS_NODE_MODIFIED | 1607 TMPFS_NODE_CHANGED; 1608 } 1609 VM_OBJECT_WUNLOCK(obj); 1610 } 1611 } 1612 1613 /* 1614 * Change flags of the given vnode. 1615 * Caller should execute tmpfs_update on vp after a successful execution. 1616 * The vnode must be locked on entry and remain locked on exit. 1617 */ 1618 int 1619 tmpfs_chflags(struct vnode *vp, u_long flags, struct ucred *cred, 1620 struct thread *p) 1621 { 1622 int error; 1623 struct tmpfs_node *node; 1624 1625 ASSERT_VOP_ELOCKED(vp, "chflags"); 1626 1627 node = VP_TO_TMPFS_NODE(vp); 1628 1629 if ((flags & ~(SF_APPEND | SF_ARCHIVED | SF_IMMUTABLE | SF_NOUNLINK | 1630 UF_APPEND | UF_ARCHIVE | UF_HIDDEN | UF_IMMUTABLE | UF_NODUMP | 1631 UF_NOUNLINK | UF_OFFLINE | UF_OPAQUE | UF_READONLY | UF_REPARSE | 1632 UF_SPARSE | UF_SYSTEM)) != 0) 1633 return (EOPNOTSUPP); 1634 1635 /* Disallow this operation if the file system is mounted read-only. */ 1636 if (vp->v_mount->mnt_flag & MNT_RDONLY) 1637 return (EROFS); 1638 1639 /* 1640 * Callers may only modify the file flags on objects they 1641 * have VADMIN rights for. 1642 */ 1643 if ((error = VOP_ACCESS(vp, VADMIN, cred, p))) 1644 return (error); 1645 /* 1646 * Unprivileged processes are not permitted to unset system 1647 * flags, or modify flags if any system flags are set. 1648 */ 1649 if (!priv_check_cred(cred, PRIV_VFS_SYSFLAGS)) { 1650 if (node->tn_flags & 1651 (SF_NOUNLINK | SF_IMMUTABLE | SF_APPEND)) { 1652 error = securelevel_gt(cred, 0); 1653 if (error) 1654 return (error); 1655 } 1656 } else { 1657 if (node->tn_flags & 1658 (SF_NOUNLINK | SF_IMMUTABLE | SF_APPEND) || 1659 ((flags ^ node->tn_flags) & SF_SETTABLE)) 1660 return (EPERM); 1661 } 1662 node->tn_flags = flags; 1663 node->tn_status |= TMPFS_NODE_CHANGED; 1664 1665 ASSERT_VOP_ELOCKED(vp, "chflags2"); 1666 1667 return (0); 1668 } 1669 1670 /* 1671 * Change access mode on the given vnode. 1672 * Caller should execute tmpfs_update on vp after a successful execution. 1673 * The vnode must be locked on entry and remain locked on exit. 1674 */ 1675 int 1676 tmpfs_chmod(struct vnode *vp, mode_t mode, struct ucred *cred, struct thread *p) 1677 { 1678 int error; 1679 struct tmpfs_node *node; 1680 mode_t newmode; 1681 1682 ASSERT_VOP_ELOCKED(vp, "chmod"); 1683 ASSERT_VOP_IN_SEQC(vp); 1684 1685 node = VP_TO_TMPFS_NODE(vp); 1686 1687 /* Disallow this operation if the file system is mounted read-only. */ 1688 if (vp->v_mount->mnt_flag & MNT_RDONLY) 1689 return EROFS; 1690 1691 /* Immutable or append-only files cannot be modified, either. */ 1692 if (node->tn_flags & (IMMUTABLE | APPEND)) 1693 return EPERM; 1694 1695 /* 1696 * To modify the permissions on a file, must possess VADMIN 1697 * for that file. 1698 */ 1699 if ((error = VOP_ACCESS(vp, VADMIN, cred, p))) 1700 return (error); 1701 1702 /* 1703 * Privileged processes may set the sticky bit on non-directories, 1704 * as well as set the setgid bit on a file with a group that the 1705 * process is not a member of. 1706 */ 1707 if (vp->v_type != VDIR && (mode & S_ISTXT)) { 1708 if (priv_check_cred(cred, PRIV_VFS_STICKYFILE)) 1709 return (EFTYPE); 1710 } 1711 if (!groupmember(node->tn_gid, cred) && (mode & S_ISGID)) { 1712 error = priv_check_cred(cred, PRIV_VFS_SETGID); 1713 if (error) 1714 return (error); 1715 } 1716 1717 newmode = node->tn_mode & ~ALLPERMS; 1718 newmode |= mode & ALLPERMS; 1719 atomic_store_short(&node->tn_mode, newmode); 1720 1721 node->tn_status |= TMPFS_NODE_CHANGED; 1722 1723 ASSERT_VOP_ELOCKED(vp, "chmod2"); 1724 1725 return (0); 1726 } 1727 1728 /* 1729 * Change ownership of the given vnode. At least one of uid or gid must 1730 * be different than VNOVAL. If one is set to that value, the attribute 1731 * is unchanged. 1732 * Caller should execute tmpfs_update on vp after a successful execution. 1733 * The vnode must be locked on entry and remain locked on exit. 1734 */ 1735 int 1736 tmpfs_chown(struct vnode *vp, uid_t uid, gid_t gid, struct ucred *cred, 1737 struct thread *p) 1738 { 1739 int error; 1740 struct tmpfs_node *node; 1741 uid_t ouid; 1742 gid_t ogid; 1743 mode_t newmode; 1744 1745 ASSERT_VOP_ELOCKED(vp, "chown"); 1746 ASSERT_VOP_IN_SEQC(vp); 1747 1748 node = VP_TO_TMPFS_NODE(vp); 1749 1750 /* Assign default values if they are unknown. */ 1751 MPASS(uid != VNOVAL || gid != VNOVAL); 1752 if (uid == VNOVAL) 1753 uid = node->tn_uid; 1754 if (gid == VNOVAL) 1755 gid = node->tn_gid; 1756 MPASS(uid != VNOVAL && gid != VNOVAL); 1757 1758 /* Disallow this operation if the file system is mounted read-only. */ 1759 if (vp->v_mount->mnt_flag & MNT_RDONLY) 1760 return (EROFS); 1761 1762 /* Immutable or append-only files cannot be modified, either. */ 1763 if (node->tn_flags & (IMMUTABLE | APPEND)) 1764 return (EPERM); 1765 1766 /* 1767 * To modify the ownership of a file, must possess VADMIN for that 1768 * file. 1769 */ 1770 if ((error = VOP_ACCESS(vp, VADMIN, cred, p))) 1771 return (error); 1772 1773 /* 1774 * To change the owner of a file, or change the group of a file to a 1775 * group of which we are not a member, the caller must have 1776 * privilege. 1777 */ 1778 if ((uid != node->tn_uid || 1779 (gid != node->tn_gid && !groupmember(gid, cred))) && 1780 (error = priv_check_cred(cred, PRIV_VFS_CHOWN))) 1781 return (error); 1782 1783 ogid = node->tn_gid; 1784 ouid = node->tn_uid; 1785 1786 node->tn_uid = uid; 1787 node->tn_gid = gid; 1788 1789 node->tn_status |= TMPFS_NODE_CHANGED; 1790 1791 if ((node->tn_mode & (S_ISUID | S_ISGID)) && (ouid != uid || ogid != gid)) { 1792 if (priv_check_cred(cred, PRIV_VFS_RETAINSUGID)) { 1793 newmode = node->tn_mode & ~(S_ISUID | S_ISGID); 1794 atomic_store_short(&node->tn_mode, newmode); 1795 } 1796 } 1797 1798 ASSERT_VOP_ELOCKED(vp, "chown2"); 1799 1800 return (0); 1801 } 1802 1803 /* 1804 * Change size of the given vnode. 1805 * Caller should execute tmpfs_update on vp after a successful execution. 1806 * The vnode must be locked on entry and remain locked on exit. 1807 */ 1808 int 1809 tmpfs_chsize(struct vnode *vp, u_quad_t size, struct ucred *cred, 1810 struct thread *p) 1811 { 1812 int error; 1813 struct tmpfs_node *node; 1814 1815 ASSERT_VOP_ELOCKED(vp, "chsize"); 1816 1817 node = VP_TO_TMPFS_NODE(vp); 1818 1819 /* Decide whether this is a valid operation based on the file type. */ 1820 error = 0; 1821 switch (vp->v_type) { 1822 case VDIR: 1823 return (EISDIR); 1824 1825 case VREG: 1826 if (vp->v_mount->mnt_flag & MNT_RDONLY) 1827 return (EROFS); 1828 break; 1829 1830 case VBLK: 1831 /* FALLTHROUGH */ 1832 case VCHR: 1833 /* FALLTHROUGH */ 1834 case VFIFO: 1835 /* 1836 * Allow modifications of special files even if in the file 1837 * system is mounted read-only (we are not modifying the 1838 * files themselves, but the objects they represent). 1839 */ 1840 return (0); 1841 1842 default: 1843 /* Anything else is unsupported. */ 1844 return (EOPNOTSUPP); 1845 } 1846 1847 /* Immutable or append-only files cannot be modified, either. */ 1848 if (node->tn_flags & (IMMUTABLE | APPEND)) 1849 return (EPERM); 1850 1851 error = tmpfs_truncate(vp, size); 1852 /* 1853 * tmpfs_truncate will raise the NOTE_EXTEND and NOTE_ATTRIB kevents 1854 * for us, as will update tn_status; no need to do that here. 1855 */ 1856 1857 ASSERT_VOP_ELOCKED(vp, "chsize2"); 1858 1859 return (error); 1860 } 1861 1862 /* 1863 * Change access and modification times of the given vnode. 1864 * Caller should execute tmpfs_update on vp after a successful execution. 1865 * The vnode must be locked on entry and remain locked on exit. 1866 */ 1867 int 1868 tmpfs_chtimes(struct vnode *vp, struct vattr *vap, 1869 struct ucred *cred, struct thread *l) 1870 { 1871 int error; 1872 struct tmpfs_node *node; 1873 1874 ASSERT_VOP_ELOCKED(vp, "chtimes"); 1875 1876 node = VP_TO_TMPFS_NODE(vp); 1877 1878 /* Disallow this operation if the file system is mounted read-only. */ 1879 if (vp->v_mount->mnt_flag & MNT_RDONLY) 1880 return (EROFS); 1881 1882 /* Immutable or append-only files cannot be modified, either. */ 1883 if (node->tn_flags & (IMMUTABLE | APPEND)) 1884 return (EPERM); 1885 1886 error = vn_utimes_perm(vp, vap, cred, l); 1887 if (error != 0) 1888 return (error); 1889 1890 if (vap->va_atime.tv_sec != VNOVAL) 1891 node->tn_accessed = true; 1892 1893 if (vap->va_mtime.tv_sec != VNOVAL) 1894 node->tn_status |= TMPFS_NODE_MODIFIED; 1895 1896 if (vap->va_birthtime.tv_sec != VNOVAL) 1897 node->tn_status |= TMPFS_NODE_MODIFIED; 1898 1899 tmpfs_itimes(vp, &vap->va_atime, &vap->va_mtime); 1900 1901 if (vap->va_birthtime.tv_sec != VNOVAL) 1902 node->tn_birthtime = vap->va_birthtime; 1903 ASSERT_VOP_ELOCKED(vp, "chtimes2"); 1904 1905 return (0); 1906 } 1907 1908 void 1909 tmpfs_set_status(struct tmpfs_mount *tm, struct tmpfs_node *node, int status) 1910 { 1911 1912 if ((node->tn_status & status) == status || tm->tm_ronly) 1913 return; 1914 TMPFS_NODE_LOCK(node); 1915 node->tn_status |= status; 1916 TMPFS_NODE_UNLOCK(node); 1917 } 1918 1919 void 1920 tmpfs_set_accessed(struct tmpfs_mount *tm, struct tmpfs_node *node) 1921 { 1922 if (node->tn_accessed || tm->tm_ronly) 1923 return; 1924 atomic_store_8(&node->tn_accessed, true); 1925 } 1926 1927 /* Sync timestamps */ 1928 void 1929 tmpfs_itimes(struct vnode *vp, const struct timespec *acc, 1930 const struct timespec *mod) 1931 { 1932 struct tmpfs_node *node; 1933 struct timespec now; 1934 1935 ASSERT_VOP_LOCKED(vp, "tmpfs_itimes"); 1936 node = VP_TO_TMPFS_NODE(vp); 1937 1938 if (!node->tn_accessed && 1939 (node->tn_status & (TMPFS_NODE_MODIFIED | TMPFS_NODE_CHANGED)) == 0) 1940 return; 1941 1942 vfs_timestamp(&now); 1943 TMPFS_NODE_LOCK(node); 1944 if (node->tn_accessed) { 1945 if (acc == NULL) 1946 acc = &now; 1947 node->tn_atime = *acc; 1948 } 1949 if (node->tn_status & TMPFS_NODE_MODIFIED) { 1950 if (mod == NULL) 1951 mod = &now; 1952 node->tn_mtime = *mod; 1953 } 1954 if (node->tn_status & TMPFS_NODE_CHANGED) 1955 node->tn_ctime = now; 1956 node->tn_status &= ~(TMPFS_NODE_MODIFIED | TMPFS_NODE_CHANGED); 1957 node->tn_accessed = false; 1958 TMPFS_NODE_UNLOCK(node); 1959 1960 /* XXX: FIX? The entropy here is desirable, but the harvesting may be expensive */ 1961 random_harvest_queue(node, sizeof(*node), RANDOM_FS_ATIME); 1962 } 1963 1964 int 1965 tmpfs_truncate(struct vnode *vp, off_t length) 1966 { 1967 int error; 1968 struct tmpfs_node *node; 1969 1970 node = VP_TO_TMPFS_NODE(vp); 1971 1972 if (length < 0) { 1973 error = EINVAL; 1974 goto out; 1975 } 1976 1977 if (node->tn_size == length) { 1978 error = 0; 1979 goto out; 1980 } 1981 1982 if (length > VFS_TO_TMPFS(vp->v_mount)->tm_maxfilesize) 1983 return (EFBIG); 1984 1985 error = tmpfs_reg_resize(vp, length, FALSE); 1986 if (error == 0) 1987 node->tn_status |= TMPFS_NODE_CHANGED | TMPFS_NODE_MODIFIED; 1988 1989 out: 1990 tmpfs_update(vp); 1991 1992 return (error); 1993 } 1994 1995 static __inline int 1996 tmpfs_dirtree_cmp(struct tmpfs_dirent *a, struct tmpfs_dirent *b) 1997 { 1998 if (a->td_hash > b->td_hash) 1999 return (1); 2000 else if (a->td_hash < b->td_hash) 2001 return (-1); 2002 return (0); 2003 } 2004 2005 RB_GENERATE_STATIC(tmpfs_dir, tmpfs_dirent, uh.td_entries, tmpfs_dirtree_cmp); 2006