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 + 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 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 599 error = insmntque1(vp, mp, tmpfs_insmntque_dtr, NULL); 600 if (error) 601 vp = NULL; 602 603 unlock: 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 617 out: 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 634 /* 635 * Destroys the association between the vnode vp and the node it 636 * references. 637 */ 638 void 639 tmpfs_free_vp(struct vnode *vp) 640 { 641 struct tmpfs_node *node; 642 643 node = VP_TO_TMPFS_NODE(vp); 644 645 mtx_assert(TMPFS_NODE_MTX(node), MA_OWNED); 646 node->tn_vnode = NULL; 647 if ((node->tn_vpstate & TMPFS_VNODE_WRECLAIM) != 0) 648 wakeup(&node->tn_vnode); 649 node->tn_vpstate &= ~TMPFS_VNODE_WRECLAIM; 650 vp->v_data = NULL; 651 } 652 653 /* --------------------------------------------------------------------- */ 654 655 /* 656 * Allocates a new file of type 'type' and adds it to the parent directory 657 * 'dvp'; this addition is done using the component name given in 'cnp'. 658 * The ownership of the new file is automatically assigned based on the 659 * credentials of the caller (through 'cnp'), the group is set based on 660 * the parent directory and the mode is determined from the 'vap' argument. 661 * If successful, *vpp holds a vnode to the newly created file and zero 662 * is returned. Otherwise *vpp is NULL and the function returns an 663 * appropriate error code. 664 */ 665 int 666 tmpfs_alloc_file(struct vnode *dvp, struct vnode **vpp, struct vattr *vap, 667 struct componentname *cnp, char *target) 668 { 669 int error; 670 struct tmpfs_dirent *de; 671 struct tmpfs_mount *tmp; 672 struct tmpfs_node *dnode; 673 struct tmpfs_node *node; 674 struct tmpfs_node *parent; 675 676 MPASS(VOP_ISLOCKED(dvp)); 677 MPASS(cnp->cn_flags & HASBUF); 678 679 tmp = VFS_TO_TMPFS(dvp->v_mount); 680 dnode = VP_TO_TMPFS_DIR(dvp); 681 *vpp = NULL; 682 683 /* If the entry we are creating is a directory, we cannot overflow 684 * the number of links of its parent, because it will get a new 685 * link. */ 686 if (vap->va_type == VDIR) { 687 /* Ensure that we do not overflow the maximum number of links 688 * imposed by the system. */ 689 MPASS(dnode->tn_links <= LINK_MAX); 690 if (dnode->tn_links == LINK_MAX) { 691 error = EMLINK; 692 goto out; 693 } 694 695 parent = dnode; 696 MPASS(parent != NULL); 697 } else 698 parent = NULL; 699 700 /* Allocate a node that represents the new file. */ 701 error = tmpfs_alloc_node(tmp, vap->va_type, cnp->cn_cred->cr_uid, 702 dnode->tn_gid, vap->va_mode, parent, target, vap->va_rdev, &node); 703 if (error != 0) 704 goto out; 705 706 /* Allocate a directory entry that points to the new file. */ 707 error = tmpfs_alloc_dirent(tmp, node, cnp->cn_nameptr, cnp->cn_namelen, 708 &de); 709 if (error != 0) { 710 tmpfs_free_node(tmp, node); 711 goto out; 712 } 713 714 /* Allocate a vnode for the new file. */ 715 error = tmpfs_alloc_vp(dvp->v_mount, node, LK_EXCLUSIVE, vpp); 716 if (error != 0) { 717 tmpfs_free_dirent(tmp, de); 718 tmpfs_free_node(tmp, node); 719 goto out; 720 } 721 722 /* Now that all required items are allocated, we can proceed to 723 * insert the new node into the directory, an operation that 724 * cannot fail. */ 725 if (cnp->cn_flags & ISWHITEOUT) 726 tmpfs_dir_whiteout_remove(dvp, cnp); 727 tmpfs_dir_attach(dvp, de); 728 729 out: 730 731 return error; 732 } 733 734 /* --------------------------------------------------------------------- */ 735 736 static struct tmpfs_dirent * 737 tmpfs_dir_first(struct tmpfs_node *dnode, struct tmpfs_dir_cursor *dc) 738 { 739 struct tmpfs_dirent *de; 740 741 de = RB_MIN(tmpfs_dir, &dnode->tn_dir.tn_dirhead); 742 dc->tdc_tree = de; 743 if (de != NULL && tmpfs_dirent_duphead(de)) 744 de = LIST_FIRST(&de->ud.td_duphead); 745 dc->tdc_current = de; 746 747 return (dc->tdc_current); 748 } 749 750 static struct tmpfs_dirent * 751 tmpfs_dir_next(struct tmpfs_node *dnode, struct tmpfs_dir_cursor *dc) 752 { 753 struct tmpfs_dirent *de; 754 755 MPASS(dc->tdc_tree != NULL); 756 if (tmpfs_dirent_dup(dc->tdc_current)) { 757 dc->tdc_current = LIST_NEXT(dc->tdc_current, uh.td_dup.entries); 758 if (dc->tdc_current != NULL) 759 return (dc->tdc_current); 760 } 761 dc->tdc_tree = dc->tdc_current = RB_NEXT(tmpfs_dir, 762 &dnode->tn_dir.tn_dirhead, dc->tdc_tree); 763 if ((de = dc->tdc_current) != NULL && tmpfs_dirent_duphead(de)) { 764 dc->tdc_current = LIST_FIRST(&de->ud.td_duphead); 765 MPASS(dc->tdc_current != NULL); 766 } 767 768 return (dc->tdc_current); 769 } 770 771 /* Lookup directory entry in RB-Tree. Function may return duphead entry. */ 772 static struct tmpfs_dirent * 773 tmpfs_dir_xlookup_hash(struct tmpfs_node *dnode, uint32_t hash) 774 { 775 struct tmpfs_dirent *de, dekey; 776 777 dekey.td_hash = hash; 778 de = RB_FIND(tmpfs_dir, &dnode->tn_dir.tn_dirhead, &dekey); 779 return (de); 780 } 781 782 /* Lookup directory entry by cookie, initialize directory cursor accordingly. */ 783 static struct tmpfs_dirent * 784 tmpfs_dir_lookup_cookie(struct tmpfs_node *node, off_t cookie, 785 struct tmpfs_dir_cursor *dc) 786 { 787 struct tmpfs_dir *dirhead = &node->tn_dir.tn_dirhead; 788 struct tmpfs_dirent *de, dekey; 789 790 MPASS(cookie >= TMPFS_DIRCOOKIE_MIN); 791 792 if (cookie == node->tn_dir.tn_readdir_lastn && 793 (de = node->tn_dir.tn_readdir_lastp) != NULL) { 794 /* Protect against possible race, tn_readdir_last[pn] 795 * may be updated with only shared vnode lock held. */ 796 if (cookie == tmpfs_dirent_cookie(de)) 797 goto out; 798 } 799 800 if ((cookie & TMPFS_DIRCOOKIE_DUP) != 0) { 801 LIST_FOREACH(de, &node->tn_dir.tn_dupindex, 802 uh.td_dup.index_entries) { 803 MPASS(tmpfs_dirent_dup(de)); 804 if (de->td_cookie == cookie) 805 goto out; 806 /* dupindex list is sorted. */ 807 if (de->td_cookie < cookie) { 808 de = NULL; 809 goto out; 810 } 811 } 812 MPASS(de == NULL); 813 goto out; 814 } 815 816 MPASS((cookie & TMPFS_DIRCOOKIE_MASK) == cookie); 817 dekey.td_hash = cookie; 818 /* Recover if direntry for cookie was removed */ 819 de = RB_NFIND(tmpfs_dir, dirhead, &dekey); 820 dc->tdc_tree = de; 821 dc->tdc_current = de; 822 if (de != NULL && tmpfs_dirent_duphead(de)) { 823 dc->tdc_current = LIST_FIRST(&de->ud.td_duphead); 824 MPASS(dc->tdc_current != NULL); 825 } 826 return (dc->tdc_current); 827 828 out: 829 dc->tdc_tree = de; 830 dc->tdc_current = de; 831 if (de != NULL && tmpfs_dirent_dup(de)) 832 dc->tdc_tree = tmpfs_dir_xlookup_hash(node, 833 de->td_hash); 834 return (dc->tdc_current); 835 } 836 837 /* 838 * Looks for a directory entry in the directory represented by node. 839 * 'cnp' describes the name of the entry to look for. Note that the . 840 * and .. components are not allowed as they do not physically exist 841 * within directories. 842 * 843 * Returns a pointer to the entry when found, otherwise NULL. 844 */ 845 struct tmpfs_dirent * 846 tmpfs_dir_lookup(struct tmpfs_node *node, struct tmpfs_node *f, 847 struct componentname *cnp) 848 { 849 struct tmpfs_dir_duphead *duphead; 850 struct tmpfs_dirent *de; 851 uint32_t hash; 852 853 MPASS(IMPLIES(cnp->cn_namelen == 1, cnp->cn_nameptr[0] != '.')); 854 MPASS(IMPLIES(cnp->cn_namelen == 2, !(cnp->cn_nameptr[0] == '.' && 855 cnp->cn_nameptr[1] == '.'))); 856 TMPFS_VALIDATE_DIR(node); 857 858 hash = tmpfs_dirent_hash(cnp->cn_nameptr, cnp->cn_namelen); 859 de = tmpfs_dir_xlookup_hash(node, hash); 860 if (de != NULL && tmpfs_dirent_duphead(de)) { 861 duphead = &de->ud.td_duphead; 862 LIST_FOREACH(de, duphead, uh.td_dup.entries) { 863 if (TMPFS_DIRENT_MATCHES(de, cnp->cn_nameptr, 864 cnp->cn_namelen)) 865 break; 866 } 867 } else if (de != NULL) { 868 if (!TMPFS_DIRENT_MATCHES(de, cnp->cn_nameptr, 869 cnp->cn_namelen)) 870 de = NULL; 871 } 872 if (de != NULL && f != NULL && de->td_node != f) 873 de = NULL; 874 875 return (de); 876 } 877 878 /* 879 * Attach duplicate-cookie directory entry nde to dnode and insert to dupindex 880 * list, allocate new cookie value. 881 */ 882 static void 883 tmpfs_dir_attach_dup(struct tmpfs_node *dnode, 884 struct tmpfs_dir_duphead *duphead, struct tmpfs_dirent *nde) 885 { 886 struct tmpfs_dir_duphead *dupindex; 887 struct tmpfs_dirent *de, *pde; 888 889 dupindex = &dnode->tn_dir.tn_dupindex; 890 de = LIST_FIRST(dupindex); 891 if (de == NULL || de->td_cookie < TMPFS_DIRCOOKIE_DUP_MAX) { 892 if (de == NULL) 893 nde->td_cookie = TMPFS_DIRCOOKIE_DUP_MIN; 894 else 895 nde->td_cookie = de->td_cookie + 1; 896 MPASS(tmpfs_dirent_dup(nde)); 897 LIST_INSERT_HEAD(dupindex, nde, uh.td_dup.index_entries); 898 LIST_INSERT_HEAD(duphead, nde, uh.td_dup.entries); 899 return; 900 } 901 902 /* 903 * Cookie numbers are near exhaustion. Scan dupindex list for unused 904 * numbers. dupindex list is sorted in descending order. Keep it so 905 * after inserting nde. 906 */ 907 while (1) { 908 pde = de; 909 de = LIST_NEXT(de, uh.td_dup.index_entries); 910 if (de == NULL && pde->td_cookie != TMPFS_DIRCOOKIE_DUP_MIN) { 911 /* 912 * Last element of the index doesn't have minimal cookie 913 * value, use it. 914 */ 915 nde->td_cookie = TMPFS_DIRCOOKIE_DUP_MIN; 916 LIST_INSERT_AFTER(pde, nde, uh.td_dup.index_entries); 917 LIST_INSERT_HEAD(duphead, nde, uh.td_dup.entries); 918 return; 919 } else if (de == NULL) { 920 /* 921 * We are so lucky have 2^30 hash duplicates in single 922 * directory :) Return largest possible cookie value. 923 * It should be fine except possible issues with 924 * VOP_READDIR restart. 925 */ 926 nde->td_cookie = TMPFS_DIRCOOKIE_DUP_MAX; 927 LIST_INSERT_HEAD(dupindex, nde, 928 uh.td_dup.index_entries); 929 LIST_INSERT_HEAD(duphead, nde, uh.td_dup.entries); 930 return; 931 } 932 if (de->td_cookie + 1 == pde->td_cookie || 933 de->td_cookie >= TMPFS_DIRCOOKIE_DUP_MAX) 934 continue; /* No hole or invalid cookie. */ 935 nde->td_cookie = de->td_cookie + 1; 936 MPASS(tmpfs_dirent_dup(nde)); 937 MPASS(pde->td_cookie > nde->td_cookie); 938 MPASS(nde->td_cookie > de->td_cookie); 939 LIST_INSERT_BEFORE(de, nde, uh.td_dup.index_entries); 940 LIST_INSERT_HEAD(duphead, nde, uh.td_dup.entries); 941 return; 942 }; 943 } 944 945 /* 946 * Attaches the directory entry de to the directory represented by vp. 947 * Note that this does not change the link count of the node pointed by 948 * the directory entry, as this is done by tmpfs_alloc_dirent. 949 */ 950 void 951 tmpfs_dir_attach(struct vnode *vp, struct tmpfs_dirent *de) 952 { 953 struct tmpfs_node *dnode; 954 struct tmpfs_dirent *xde, *nde; 955 956 ASSERT_VOP_ELOCKED(vp, __func__); 957 MPASS(de->td_namelen > 0); 958 MPASS(de->td_hash >= TMPFS_DIRCOOKIE_MIN); 959 MPASS(de->td_cookie == de->td_hash); 960 961 dnode = VP_TO_TMPFS_DIR(vp); 962 dnode->tn_dir.tn_readdir_lastn = 0; 963 dnode->tn_dir.tn_readdir_lastp = NULL; 964 965 MPASS(!tmpfs_dirent_dup(de)); 966 xde = RB_INSERT(tmpfs_dir, &dnode->tn_dir.tn_dirhead, de); 967 if (xde != NULL && tmpfs_dirent_duphead(xde)) 968 tmpfs_dir_attach_dup(dnode, &xde->ud.td_duphead, de); 969 else if (xde != NULL) { 970 /* 971 * Allocate new duphead. Swap xde with duphead to avoid 972 * adding/removing elements with the same hash. 973 */ 974 MPASS(!tmpfs_dirent_dup(xde)); 975 tmpfs_alloc_dirent(VFS_TO_TMPFS(vp->v_mount), NULL, NULL, 0, 976 &nde); 977 /* *nde = *xde; XXX gcc 4.2.1 may generate invalid code. */ 978 memcpy(nde, xde, sizeof(*xde)); 979 xde->td_cookie |= TMPFS_DIRCOOKIE_DUPHEAD; 980 LIST_INIT(&xde->ud.td_duphead); 981 xde->td_namelen = 0; 982 xde->td_node = NULL; 983 tmpfs_dir_attach_dup(dnode, &xde->ud.td_duphead, nde); 984 tmpfs_dir_attach_dup(dnode, &xde->ud.td_duphead, de); 985 } 986 dnode->tn_size += sizeof(struct tmpfs_dirent); 987 dnode->tn_status |= TMPFS_NODE_ACCESSED | TMPFS_NODE_CHANGED | \ 988 TMPFS_NODE_MODIFIED; 989 } 990 991 /* --------------------------------------------------------------------- */ 992 993 /* 994 * Detaches the directory entry de from the directory represented by vp. 995 * Note that this does not change the link count of the node pointed by 996 * the directory entry, as this is done by tmpfs_free_dirent. 997 */ 998 void 999 tmpfs_dir_detach(struct vnode *vp, struct tmpfs_dirent *de) 1000 { 1001 struct tmpfs_mount *tmp; 1002 struct tmpfs_dir *head; 1003 struct tmpfs_node *dnode; 1004 struct tmpfs_dirent *xde; 1005 1006 ASSERT_VOP_ELOCKED(vp, __func__); 1007 1008 dnode = VP_TO_TMPFS_DIR(vp); 1009 head = &dnode->tn_dir.tn_dirhead; 1010 dnode->tn_dir.tn_readdir_lastn = 0; 1011 dnode->tn_dir.tn_readdir_lastp = NULL; 1012 1013 if (tmpfs_dirent_dup(de)) { 1014 /* Remove duphead if de was last entry. */ 1015 if (LIST_NEXT(de, uh.td_dup.entries) == NULL) { 1016 xde = tmpfs_dir_xlookup_hash(dnode, de->td_hash); 1017 MPASS(tmpfs_dirent_duphead(xde)); 1018 } else 1019 xde = NULL; 1020 LIST_REMOVE(de, uh.td_dup.entries); 1021 LIST_REMOVE(de, uh.td_dup.index_entries); 1022 if (xde != NULL) { 1023 if (LIST_EMPTY(&xde->ud.td_duphead)) { 1024 RB_REMOVE(tmpfs_dir, head, xde); 1025 tmp = VFS_TO_TMPFS(vp->v_mount); 1026 MPASS(xde->td_node == NULL); 1027 tmpfs_free_dirent(tmp, xde); 1028 } 1029 } 1030 } else 1031 RB_REMOVE(tmpfs_dir, head, de); 1032 1033 dnode->tn_size -= sizeof(struct tmpfs_dirent); 1034 dnode->tn_status |= TMPFS_NODE_ACCESSED | TMPFS_NODE_CHANGED | \ 1035 TMPFS_NODE_MODIFIED; 1036 } 1037 1038 void 1039 tmpfs_dir_destroy(struct tmpfs_mount *tmp, struct tmpfs_node *dnode) 1040 { 1041 struct tmpfs_dirent *de, *dde, *nde; 1042 1043 RB_FOREACH_SAFE(de, tmpfs_dir, &dnode->tn_dir.tn_dirhead, nde) { 1044 RB_REMOVE(tmpfs_dir, &dnode->tn_dir.tn_dirhead, de); 1045 /* Node may already be destroyed. */ 1046 de->td_node = NULL; 1047 if (tmpfs_dirent_duphead(de)) { 1048 while ((dde = LIST_FIRST(&de->ud.td_duphead)) != NULL) { 1049 LIST_REMOVE(dde, uh.td_dup.entries); 1050 dde->td_node = NULL; 1051 tmpfs_free_dirent(tmp, dde); 1052 } 1053 } 1054 tmpfs_free_dirent(tmp, de); 1055 } 1056 } 1057 1058 /* --------------------------------------------------------------------- */ 1059 1060 /* 1061 * Helper function for tmpfs_readdir. Creates a '.' entry for the given 1062 * directory and returns it in the uio space. The function returns 0 1063 * on success, -1 if there was not enough space in the uio structure to 1064 * hold the directory entry or an appropriate error code if another 1065 * error happens. 1066 */ 1067 static int 1068 tmpfs_dir_getdotdent(struct tmpfs_node *node, struct uio *uio) 1069 { 1070 int error; 1071 struct dirent dent; 1072 1073 TMPFS_VALIDATE_DIR(node); 1074 MPASS(uio->uio_offset == TMPFS_DIRCOOKIE_DOT); 1075 1076 dent.d_fileno = node->tn_id; 1077 dent.d_type = DT_DIR; 1078 dent.d_namlen = 1; 1079 dent.d_name[0] = '.'; 1080 dent.d_name[1] = '\0'; 1081 dent.d_reclen = GENERIC_DIRSIZ(&dent); 1082 1083 if (dent.d_reclen > uio->uio_resid) 1084 error = EJUSTRETURN; 1085 else 1086 error = uiomove(&dent, dent.d_reclen, uio); 1087 1088 node->tn_status |= TMPFS_NODE_ACCESSED; 1089 1090 return error; 1091 } 1092 1093 /* --------------------------------------------------------------------- */ 1094 1095 /* 1096 * Helper function for tmpfs_readdir. Creates a '..' entry for the given 1097 * directory and returns it in the uio space. The function returns 0 1098 * on success, -1 if there was not enough space in the uio structure to 1099 * hold the directory entry or an appropriate error code if another 1100 * error happens. 1101 */ 1102 static int 1103 tmpfs_dir_getdotdotdent(struct tmpfs_node *node, struct uio *uio) 1104 { 1105 int error; 1106 struct dirent dent; 1107 1108 TMPFS_VALIDATE_DIR(node); 1109 MPASS(uio->uio_offset == TMPFS_DIRCOOKIE_DOTDOT); 1110 1111 /* 1112 * Return ENOENT if the current node is already removed. 1113 */ 1114 TMPFS_ASSERT_LOCKED(node); 1115 if (node->tn_dir.tn_parent == NULL) { 1116 return (ENOENT); 1117 } 1118 1119 TMPFS_NODE_LOCK(node->tn_dir.tn_parent); 1120 dent.d_fileno = node->tn_dir.tn_parent->tn_id; 1121 TMPFS_NODE_UNLOCK(node->tn_dir.tn_parent); 1122 1123 dent.d_type = DT_DIR; 1124 dent.d_namlen = 2; 1125 dent.d_name[0] = '.'; 1126 dent.d_name[1] = '.'; 1127 dent.d_name[2] = '\0'; 1128 dent.d_reclen = GENERIC_DIRSIZ(&dent); 1129 1130 if (dent.d_reclen > uio->uio_resid) 1131 error = EJUSTRETURN; 1132 else 1133 error = uiomove(&dent, dent.d_reclen, uio); 1134 1135 node->tn_status |= TMPFS_NODE_ACCESSED; 1136 1137 return error; 1138 } 1139 1140 /* --------------------------------------------------------------------- */ 1141 1142 /* 1143 * Helper function for tmpfs_readdir. Returns as much directory entries 1144 * as can fit in the uio space. The read starts at uio->uio_offset. 1145 * The function returns 0 on success, -1 if there was not enough space 1146 * in the uio structure to hold the directory entry or an appropriate 1147 * error code if another error happens. 1148 */ 1149 int 1150 tmpfs_dir_getdents(struct tmpfs_node *node, struct uio *uio, int cnt, 1151 u_long *cookies, int *ncookies) 1152 { 1153 struct tmpfs_dir_cursor dc; 1154 struct tmpfs_dirent *de; 1155 off_t off; 1156 int error; 1157 1158 TMPFS_VALIDATE_DIR(node); 1159 1160 off = 0; 1161 1162 /* 1163 * Lookup the node from the current offset. The starting offset of 1164 * 0 will lookup both '.' and '..', and then the first real entry, 1165 * or EOF if there are none. Then find all entries for the dir that 1166 * fit into the buffer. Once no more entries are found (de == NULL), 1167 * the offset is set to TMPFS_DIRCOOKIE_EOF, which will cause the next 1168 * call to return 0. 1169 */ 1170 switch (uio->uio_offset) { 1171 case TMPFS_DIRCOOKIE_DOT: 1172 error = tmpfs_dir_getdotdent(node, uio); 1173 if (error != 0) 1174 return (error); 1175 uio->uio_offset = TMPFS_DIRCOOKIE_DOTDOT; 1176 if (cnt != 0) 1177 cookies[(*ncookies)++] = off = uio->uio_offset; 1178 case TMPFS_DIRCOOKIE_DOTDOT: 1179 error = tmpfs_dir_getdotdotdent(node, uio); 1180 if (error != 0) 1181 return (error); 1182 de = tmpfs_dir_first(node, &dc); 1183 uio->uio_offset = tmpfs_dirent_cookie(de); 1184 if (cnt != 0) 1185 cookies[(*ncookies)++] = off = uio->uio_offset; 1186 /* EOF. */ 1187 if (de == NULL) 1188 return (0); 1189 break; 1190 case TMPFS_DIRCOOKIE_EOF: 1191 return (0); 1192 default: 1193 de = tmpfs_dir_lookup_cookie(node, uio->uio_offset, &dc); 1194 if (de == NULL) 1195 return (EINVAL); 1196 if (cnt != 0) 1197 off = tmpfs_dirent_cookie(de); 1198 } 1199 1200 /* Read as much entries as possible; i.e., until we reach the end of 1201 * the directory or we exhaust uio space. */ 1202 do { 1203 struct dirent d; 1204 1205 /* Create a dirent structure representing the current 1206 * tmpfs_node and fill it. */ 1207 if (de->td_node == NULL) { 1208 d.d_fileno = 1; 1209 d.d_type = DT_WHT; 1210 } else { 1211 d.d_fileno = de->td_node->tn_id; 1212 switch (de->td_node->tn_type) { 1213 case VBLK: 1214 d.d_type = DT_BLK; 1215 break; 1216 1217 case VCHR: 1218 d.d_type = DT_CHR; 1219 break; 1220 1221 case VDIR: 1222 d.d_type = DT_DIR; 1223 break; 1224 1225 case VFIFO: 1226 d.d_type = DT_FIFO; 1227 break; 1228 1229 case VLNK: 1230 d.d_type = DT_LNK; 1231 break; 1232 1233 case VREG: 1234 d.d_type = DT_REG; 1235 break; 1236 1237 case VSOCK: 1238 d.d_type = DT_SOCK; 1239 break; 1240 1241 default: 1242 panic("tmpfs_dir_getdents: type %p %d", 1243 de->td_node, (int)de->td_node->tn_type); 1244 } 1245 } 1246 d.d_namlen = de->td_namelen; 1247 MPASS(de->td_namelen < sizeof(d.d_name)); 1248 (void)memcpy(d.d_name, de->ud.td_name, de->td_namelen); 1249 d.d_name[de->td_namelen] = '\0'; 1250 d.d_reclen = GENERIC_DIRSIZ(&d); 1251 1252 /* Stop reading if the directory entry we are treating is 1253 * bigger than the amount of data that can be returned. */ 1254 if (d.d_reclen > uio->uio_resid) { 1255 error = EJUSTRETURN; 1256 break; 1257 } 1258 1259 /* Copy the new dirent structure into the output buffer and 1260 * advance pointers. */ 1261 error = uiomove(&d, d.d_reclen, uio); 1262 if (error == 0) { 1263 de = tmpfs_dir_next(node, &dc); 1264 if (cnt != 0) { 1265 off = tmpfs_dirent_cookie(de); 1266 MPASS(*ncookies < cnt); 1267 cookies[(*ncookies)++] = off; 1268 } 1269 } 1270 } while (error == 0 && uio->uio_resid > 0 && de != NULL); 1271 1272 /* Update the offset and cache. */ 1273 if (cnt == 0) 1274 off = tmpfs_dirent_cookie(de); 1275 1276 uio->uio_offset = off; 1277 node->tn_dir.tn_readdir_lastn = off; 1278 node->tn_dir.tn_readdir_lastp = de; 1279 1280 node->tn_status |= TMPFS_NODE_ACCESSED; 1281 return error; 1282 } 1283 1284 int 1285 tmpfs_dir_whiteout_add(struct vnode *dvp, struct componentname *cnp) 1286 { 1287 struct tmpfs_dirent *de; 1288 int error; 1289 1290 error = tmpfs_alloc_dirent(VFS_TO_TMPFS(dvp->v_mount), NULL, 1291 cnp->cn_nameptr, cnp->cn_namelen, &de); 1292 if (error != 0) 1293 return (error); 1294 tmpfs_dir_attach(dvp, de); 1295 return (0); 1296 } 1297 1298 void 1299 tmpfs_dir_whiteout_remove(struct vnode *dvp, struct componentname *cnp) 1300 { 1301 struct tmpfs_dirent *de; 1302 1303 de = tmpfs_dir_lookup(VP_TO_TMPFS_DIR(dvp), NULL, cnp); 1304 MPASS(de != NULL && de->td_node == NULL); 1305 tmpfs_dir_detach(dvp, de); 1306 tmpfs_free_dirent(VFS_TO_TMPFS(dvp->v_mount), de); 1307 } 1308 1309 /* --------------------------------------------------------------------- */ 1310 1311 /* 1312 * Resizes the aobj associated with the regular file pointed to by 'vp' to the 1313 * size 'newsize'. 'vp' must point to a vnode that represents a regular file. 1314 * 'newsize' must be positive. 1315 * 1316 * Returns zero on success or an appropriate error code on failure. 1317 */ 1318 int 1319 tmpfs_reg_resize(struct vnode *vp, off_t newsize, boolean_t ignerr) 1320 { 1321 struct tmpfs_mount *tmp; 1322 struct tmpfs_node *node; 1323 vm_object_t uobj; 1324 vm_page_t m, ma[1]; 1325 vm_pindex_t idx, newpages, oldpages; 1326 off_t oldsize; 1327 int base, rv; 1328 1329 MPASS(vp->v_type == VREG); 1330 MPASS(newsize >= 0); 1331 1332 node = VP_TO_TMPFS_NODE(vp); 1333 uobj = node->tn_reg.tn_aobj; 1334 tmp = VFS_TO_TMPFS(vp->v_mount); 1335 1336 /* 1337 * Convert the old and new sizes to the number of pages needed to 1338 * store them. It may happen that we do not need to do anything 1339 * because the last allocated page can accommodate the change on 1340 * its own. 1341 */ 1342 oldsize = node->tn_size; 1343 oldpages = OFF_TO_IDX(oldsize + PAGE_MASK); 1344 MPASS(oldpages == uobj->size); 1345 newpages = OFF_TO_IDX(newsize + PAGE_MASK); 1346 if (newpages > oldpages && 1347 tmpfs_pages_check_avail(tmp, newpages - oldpages) == 0) 1348 return (ENOSPC); 1349 1350 VM_OBJECT_WLOCK(uobj); 1351 if (newsize < oldsize) { 1352 /* 1353 * Zero the truncated part of the last page. 1354 */ 1355 base = newsize & PAGE_MASK; 1356 if (base != 0) { 1357 idx = OFF_TO_IDX(newsize); 1358 retry: 1359 m = vm_page_lookup(uobj, idx); 1360 if (m != NULL) { 1361 if (vm_page_sleep_if_busy(m, "tmfssz")) 1362 goto retry; 1363 MPASS(m->valid == VM_PAGE_BITS_ALL); 1364 } else if (vm_pager_has_page(uobj, idx, NULL, NULL)) { 1365 m = vm_page_alloc(uobj, idx, VM_ALLOC_NORMAL); 1366 if (m == NULL) { 1367 VM_OBJECT_WUNLOCK(uobj); 1368 VM_WAIT; 1369 VM_OBJECT_WLOCK(uobj); 1370 goto retry; 1371 } else if (m->valid != VM_PAGE_BITS_ALL) { 1372 ma[0] = m; 1373 rv = vm_pager_get_pages(uobj, ma, 1, 0); 1374 m = vm_page_lookup(uobj, idx); 1375 } else 1376 /* A cached page was reactivated. */ 1377 rv = VM_PAGER_OK; 1378 vm_page_lock(m); 1379 if (rv == VM_PAGER_OK) { 1380 vm_page_deactivate(m); 1381 vm_page_unlock(m); 1382 vm_page_xunbusy(m); 1383 } else { 1384 vm_page_free(m); 1385 vm_page_unlock(m); 1386 if (ignerr) 1387 m = NULL; 1388 else { 1389 VM_OBJECT_WUNLOCK(uobj); 1390 return (EIO); 1391 } 1392 } 1393 } 1394 if (m != NULL) { 1395 pmap_zero_page_area(m, base, PAGE_SIZE - base); 1396 vm_page_dirty(m); 1397 vm_pager_page_unswapped(m); 1398 } 1399 } 1400 1401 /* 1402 * Release any swap space and free any whole pages. 1403 */ 1404 if (newpages < oldpages) { 1405 swap_pager_freespace(uobj, newpages, oldpages - 1406 newpages); 1407 vm_object_page_remove(uobj, newpages, 0, 0); 1408 } 1409 } 1410 uobj->size = newpages; 1411 VM_OBJECT_WUNLOCK(uobj); 1412 1413 TMPFS_LOCK(tmp); 1414 tmp->tm_pages_used += (newpages - oldpages); 1415 TMPFS_UNLOCK(tmp); 1416 1417 node->tn_size = newsize; 1418 return (0); 1419 } 1420 1421 /* --------------------------------------------------------------------- */ 1422 1423 /* 1424 * Change flags of the given vnode. 1425 * Caller should execute tmpfs_update on vp after a successful execution. 1426 * The vnode must be locked on entry and remain locked on exit. 1427 */ 1428 int 1429 tmpfs_chflags(struct vnode *vp, u_long flags, struct ucred *cred, 1430 struct thread *p) 1431 { 1432 int error; 1433 struct tmpfs_node *node; 1434 1435 MPASS(VOP_ISLOCKED(vp)); 1436 1437 node = VP_TO_TMPFS_NODE(vp); 1438 1439 if ((flags & ~(SF_APPEND | SF_ARCHIVED | SF_IMMUTABLE | SF_NOUNLINK | 1440 UF_APPEND | UF_ARCHIVE | UF_HIDDEN | UF_IMMUTABLE | UF_NODUMP | 1441 UF_NOUNLINK | UF_OFFLINE | UF_OPAQUE | UF_READONLY | UF_REPARSE | 1442 UF_SPARSE | UF_SYSTEM)) != 0) 1443 return (EOPNOTSUPP); 1444 1445 /* Disallow this operation if the file system is mounted read-only. */ 1446 if (vp->v_mount->mnt_flag & MNT_RDONLY) 1447 return EROFS; 1448 1449 /* 1450 * Callers may only modify the file flags on objects they 1451 * have VADMIN rights for. 1452 */ 1453 if ((error = VOP_ACCESS(vp, VADMIN, cred, p))) 1454 return (error); 1455 /* 1456 * Unprivileged processes are not permitted to unset system 1457 * flags, or modify flags if any system flags are set. 1458 */ 1459 if (!priv_check_cred(cred, PRIV_VFS_SYSFLAGS, 0)) { 1460 if (node->tn_flags & 1461 (SF_NOUNLINK | SF_IMMUTABLE | SF_APPEND)) { 1462 error = securelevel_gt(cred, 0); 1463 if (error) 1464 return (error); 1465 } 1466 } else { 1467 if (node->tn_flags & 1468 (SF_NOUNLINK | SF_IMMUTABLE | SF_APPEND) || 1469 ((flags ^ node->tn_flags) & SF_SETTABLE)) 1470 return (EPERM); 1471 } 1472 node->tn_flags = flags; 1473 node->tn_status |= TMPFS_NODE_CHANGED; 1474 1475 MPASS(VOP_ISLOCKED(vp)); 1476 1477 return 0; 1478 } 1479 1480 /* --------------------------------------------------------------------- */ 1481 1482 /* 1483 * Change access mode on the given vnode. 1484 * Caller should execute tmpfs_update on vp after a successful execution. 1485 * The vnode must be locked on entry and remain locked on exit. 1486 */ 1487 int 1488 tmpfs_chmod(struct vnode *vp, mode_t mode, struct ucred *cred, struct thread *p) 1489 { 1490 int error; 1491 struct tmpfs_node *node; 1492 1493 MPASS(VOP_ISLOCKED(vp)); 1494 1495 node = VP_TO_TMPFS_NODE(vp); 1496 1497 /* Disallow this operation if the file system is mounted read-only. */ 1498 if (vp->v_mount->mnt_flag & MNT_RDONLY) 1499 return EROFS; 1500 1501 /* Immutable or append-only files cannot be modified, either. */ 1502 if (node->tn_flags & (IMMUTABLE | APPEND)) 1503 return EPERM; 1504 1505 /* 1506 * To modify the permissions on a file, must possess VADMIN 1507 * for that file. 1508 */ 1509 if ((error = VOP_ACCESS(vp, VADMIN, cred, p))) 1510 return (error); 1511 1512 /* 1513 * Privileged processes may set the sticky bit on non-directories, 1514 * as well as set the setgid bit on a file with a group that the 1515 * process is not a member of. 1516 */ 1517 if (vp->v_type != VDIR && (mode & S_ISTXT)) { 1518 if (priv_check_cred(cred, PRIV_VFS_STICKYFILE, 0)) 1519 return (EFTYPE); 1520 } 1521 if (!groupmember(node->tn_gid, cred) && (mode & S_ISGID)) { 1522 error = priv_check_cred(cred, PRIV_VFS_SETGID, 0); 1523 if (error) 1524 return (error); 1525 } 1526 1527 1528 node->tn_mode &= ~ALLPERMS; 1529 node->tn_mode |= mode & ALLPERMS; 1530 1531 node->tn_status |= TMPFS_NODE_CHANGED; 1532 1533 MPASS(VOP_ISLOCKED(vp)); 1534 1535 return 0; 1536 } 1537 1538 /* --------------------------------------------------------------------- */ 1539 1540 /* 1541 * Change ownership of the given vnode. At least one of uid or gid must 1542 * be different than VNOVAL. If one is set to that value, the attribute 1543 * is unchanged. 1544 * Caller should execute tmpfs_update on vp after a successful execution. 1545 * The vnode must be locked on entry and remain locked on exit. 1546 */ 1547 int 1548 tmpfs_chown(struct vnode *vp, uid_t uid, gid_t gid, struct ucred *cred, 1549 struct thread *p) 1550 { 1551 int error; 1552 struct tmpfs_node *node; 1553 uid_t ouid; 1554 gid_t ogid; 1555 1556 MPASS(VOP_ISLOCKED(vp)); 1557 1558 node = VP_TO_TMPFS_NODE(vp); 1559 1560 /* Assign default values if they are unknown. */ 1561 MPASS(uid != VNOVAL || gid != VNOVAL); 1562 if (uid == VNOVAL) 1563 uid = node->tn_uid; 1564 if (gid == VNOVAL) 1565 gid = node->tn_gid; 1566 MPASS(uid != VNOVAL && gid != VNOVAL); 1567 1568 /* Disallow this operation if the file system is mounted read-only. */ 1569 if (vp->v_mount->mnt_flag & MNT_RDONLY) 1570 return EROFS; 1571 1572 /* Immutable or append-only files cannot be modified, either. */ 1573 if (node->tn_flags & (IMMUTABLE | APPEND)) 1574 return EPERM; 1575 1576 /* 1577 * To modify the ownership of a file, must possess VADMIN for that 1578 * file. 1579 */ 1580 if ((error = VOP_ACCESS(vp, VADMIN, cred, p))) 1581 return (error); 1582 1583 /* 1584 * To change the owner of a file, or change the group of a file to a 1585 * group of which we are not a member, the caller must have 1586 * privilege. 1587 */ 1588 if ((uid != node->tn_uid || 1589 (gid != node->tn_gid && !groupmember(gid, cred))) && 1590 (error = priv_check_cred(cred, PRIV_VFS_CHOWN, 0))) 1591 return (error); 1592 1593 ogid = node->tn_gid; 1594 ouid = node->tn_uid; 1595 1596 node->tn_uid = uid; 1597 node->tn_gid = gid; 1598 1599 node->tn_status |= TMPFS_NODE_CHANGED; 1600 1601 if ((node->tn_mode & (S_ISUID | S_ISGID)) && (ouid != uid || ogid != gid)) { 1602 if (priv_check_cred(cred, PRIV_VFS_RETAINSUGID, 0)) 1603 node->tn_mode &= ~(S_ISUID | S_ISGID); 1604 } 1605 1606 MPASS(VOP_ISLOCKED(vp)); 1607 1608 return 0; 1609 } 1610 1611 /* --------------------------------------------------------------------- */ 1612 1613 /* 1614 * Change size of the given vnode. 1615 * Caller should execute tmpfs_update on vp after a successful execution. 1616 * The vnode must be locked on entry and remain locked on exit. 1617 */ 1618 int 1619 tmpfs_chsize(struct vnode *vp, u_quad_t size, struct ucred *cred, 1620 struct thread *p) 1621 { 1622 int error; 1623 struct tmpfs_node *node; 1624 1625 MPASS(VOP_ISLOCKED(vp)); 1626 1627 node = VP_TO_TMPFS_NODE(vp); 1628 1629 /* Decide whether this is a valid operation based on the file type. */ 1630 error = 0; 1631 switch (vp->v_type) { 1632 case VDIR: 1633 return EISDIR; 1634 1635 case VREG: 1636 if (vp->v_mount->mnt_flag & MNT_RDONLY) 1637 return EROFS; 1638 break; 1639 1640 case VBLK: 1641 /* FALLTHROUGH */ 1642 case VCHR: 1643 /* FALLTHROUGH */ 1644 case VFIFO: 1645 /* Allow modifications of special files even if in the file 1646 * system is mounted read-only (we are not modifying the 1647 * files themselves, but the objects they represent). */ 1648 return 0; 1649 1650 default: 1651 /* Anything else is unsupported. */ 1652 return EOPNOTSUPP; 1653 } 1654 1655 /* Immutable or append-only files cannot be modified, either. */ 1656 if (node->tn_flags & (IMMUTABLE | APPEND)) 1657 return EPERM; 1658 1659 error = tmpfs_truncate(vp, size); 1660 /* tmpfs_truncate will raise the NOTE_EXTEND and NOTE_ATTRIB kevents 1661 * for us, as will update tn_status; no need to do that here. */ 1662 1663 MPASS(VOP_ISLOCKED(vp)); 1664 1665 return error; 1666 } 1667 1668 /* --------------------------------------------------------------------- */ 1669 1670 /* 1671 * Change access and modification times of the given vnode. 1672 * Caller should execute tmpfs_update on vp after a successful execution. 1673 * The vnode must be locked on entry and remain locked on exit. 1674 */ 1675 int 1676 tmpfs_chtimes(struct vnode *vp, struct timespec *atime, struct timespec *mtime, 1677 struct timespec *birthtime, int vaflags, struct ucred *cred, struct thread *l) 1678 { 1679 int error; 1680 struct tmpfs_node *node; 1681 1682 MPASS(VOP_ISLOCKED(vp)); 1683 1684 node = VP_TO_TMPFS_NODE(vp); 1685 1686 /* Disallow this operation if the file system is mounted read-only. */ 1687 if (vp->v_mount->mnt_flag & MNT_RDONLY) 1688 return EROFS; 1689 1690 /* Immutable or append-only files cannot be modified, either. */ 1691 if (node->tn_flags & (IMMUTABLE | APPEND)) 1692 return EPERM; 1693 1694 /* Determine if the user have proper privilege to update time. */ 1695 if (vaflags & VA_UTIMES_NULL) { 1696 error = VOP_ACCESS(vp, VADMIN, cred, l); 1697 if (error) 1698 error = VOP_ACCESS(vp, VWRITE, cred, l); 1699 } else 1700 error = VOP_ACCESS(vp, VADMIN, cred, l); 1701 if (error) 1702 return (error); 1703 1704 if (atime->tv_sec != VNOVAL && atime->tv_nsec != VNOVAL) 1705 node->tn_status |= TMPFS_NODE_ACCESSED; 1706 1707 if (mtime->tv_sec != VNOVAL && mtime->tv_nsec != VNOVAL) 1708 node->tn_status |= TMPFS_NODE_MODIFIED; 1709 1710 if (birthtime->tv_nsec != VNOVAL && birthtime->tv_nsec != VNOVAL) 1711 node->tn_status |= TMPFS_NODE_MODIFIED; 1712 1713 tmpfs_itimes(vp, atime, mtime); 1714 1715 if (birthtime->tv_nsec != VNOVAL && birthtime->tv_nsec != VNOVAL) 1716 node->tn_birthtime = *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