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