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