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