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