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