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