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