1 /* $NetBSD: tmpfs_subr.c,v 1.21 2006/06/07 22:33:39 kardel 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 * 3. All advertising materials mentioning features or use of this software 20 * must display the following acknowledgement: 21 * This product includes software developed by the NetBSD 22 * Foundation, Inc. and its contributors. 23 * 4. Neither the name of The NetBSD Foundation nor the names of its 24 * contributors may be used to endorse or promote products derived 25 * from this software without specific prior written permission. 26 * 27 * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS 28 * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED 29 * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR 30 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS 31 * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR 32 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF 33 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS 34 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN 35 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) 36 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE 37 * POSSIBILITY OF SUCH DAMAGE. 38 */ 39 40 /* 41 * Efficient memory file system supporting functions. 42 */ 43 #include <sys/cdefs.h> 44 __FBSDID("$FreeBSD$"); 45 46 #include <sys/param.h> 47 #include <sys/namei.h> 48 #include <sys/priv.h> 49 #include <sys/proc.h> 50 #include <sys/stat.h> 51 #include <sys/systm.h> 52 #include <sys/vnode.h> 53 #include <sys/vmmeter.h> 54 55 #include <vm/vm.h> 56 #include <vm/vm_object.h> 57 #include <vm/vm_page.h> 58 #include <vm/vm_pager.h> 59 #include <vm/vm_extern.h> 60 61 #include <fs/tmpfs/tmpfs.h> 62 #include <fs/tmpfs/tmpfs_fifoops.h> 63 #include <fs/tmpfs/tmpfs_vnops.h> 64 65 /* --------------------------------------------------------------------- */ 66 67 /* 68 * Allocates a new node of type 'type' inside the 'tmp' mount point, with 69 * its owner set to 'uid', its group to 'gid' and its mode set to 'mode', 70 * using the credentials of the process 'p'. 71 * 72 * If the node type is set to 'VDIR', then the parent parameter must point 73 * to the parent directory of the node being created. It may only be NULL 74 * while allocating the root node. 75 * 76 * If the node type is set to 'VBLK' or 'VCHR', then the rdev parameter 77 * specifies the device the node represents. 78 * 79 * If the node type is set to 'VLNK', then the parameter target specifies 80 * the file name of the target file for the symbolic link that is being 81 * created. 82 * 83 * Note that new nodes are retrieved from the available list if it has 84 * items or, if it is empty, from the node pool as long as there is enough 85 * space to create them. 86 * 87 * Returns zero on success or an appropriate error code on failure. 88 */ 89 int 90 tmpfs_alloc_node(struct tmpfs_mount *tmp, enum vtype type, 91 uid_t uid, gid_t gid, mode_t mode, struct tmpfs_node *parent, 92 char *target, dev_t rdev, struct thread *p, struct tmpfs_node **node) 93 { 94 struct tmpfs_node *nnode; 95 96 /* If the root directory of the 'tmp' file system is not yet 97 * allocated, this must be the request to do it. */ 98 MPASS(IMPLIES(tmp->tm_root == NULL, parent == NULL && type == VDIR)); 99 100 MPASS(IFF(type == VLNK, target != NULL)); 101 MPASS(IFF(type == VBLK || type == VCHR, rdev != VNOVAL)); 102 103 if (tmp->tm_nodes_inuse > tmp->tm_nodes_max) 104 return (ENOSPC); 105 106 nnode = (struct tmpfs_node *)uma_zalloc_arg( 107 tmp->tm_node_pool, tmp, M_WAITOK); 108 109 /* Generic initialization. */ 110 nnode->tn_type = type; 111 vfs_timestamp(&nnode->tn_atime); 112 nnode->tn_birthtime = nnode->tn_ctime = nnode->tn_mtime = 113 nnode->tn_atime; 114 nnode->tn_uid = uid; 115 nnode->tn_gid = gid; 116 nnode->tn_mode = mode; 117 nnode->tn_id = alloc_unr(tmp->tm_ino_unr); 118 119 /* Type-specific initialization. */ 120 switch (nnode->tn_type) { 121 case VBLK: 122 case VCHR: 123 nnode->tn_rdev = rdev; 124 break; 125 126 case VDIR: 127 TAILQ_INIT(&nnode->tn_dir.tn_dirhead); 128 nnode->tn_dir.tn_parent = (parent == NULL) ? nnode : parent; 129 nnode->tn_dir.tn_readdir_lastn = 0; 130 nnode->tn_dir.tn_readdir_lastp = NULL; 131 nnode->tn_links++; 132 nnode->tn_dir.tn_parent->tn_links++; 133 break; 134 135 case VFIFO: 136 /* FALLTHROUGH */ 137 case VSOCK: 138 break; 139 140 case VLNK: 141 MPASS(strlen(target) < MAXPATHLEN); 142 nnode->tn_size = strlen(target); 143 nnode->tn_link = malloc(nnode->tn_size, M_TMPFSNAME, 144 M_WAITOK); 145 memcpy(nnode->tn_link, target, nnode->tn_size); 146 break; 147 148 case VREG: 149 nnode->tn_reg.tn_aobj = 150 vm_pager_allocate(OBJT_SWAP, NULL, 0, VM_PROT_DEFAULT, 0); 151 nnode->tn_reg.tn_aobj_pages = 0; 152 break; 153 154 default: 155 MPASS(0); 156 } 157 158 TMPFS_LOCK(tmp); 159 LIST_INSERT_HEAD(&tmp->tm_nodes_used, nnode, tn_entries); 160 tmp->tm_nodes_inuse++; 161 TMPFS_UNLOCK(tmp); 162 163 *node = nnode; 164 return 0; 165 } 166 167 /* --------------------------------------------------------------------- */ 168 169 /* 170 * Destroys the node pointed to by node from the file system 'tmp'. 171 * If the node does not belong to the given mount point, the results are 172 * unpredicted. 173 * 174 * If the node references a directory; no entries are allowed because 175 * their removal could need a recursive algorithm, something forbidden in 176 * kernel space. Furthermore, there is not need to provide such 177 * functionality (recursive removal) because the only primitives offered 178 * to the user are the removal of empty directories and the deletion of 179 * individual files. 180 * 181 * Note that nodes are not really deleted; in fact, when a node has been 182 * allocated, it cannot be deleted during the whole life of the file 183 * system. Instead, they are moved to the available list and remain there 184 * until reused. 185 */ 186 void 187 tmpfs_free_node(struct tmpfs_mount *tmp, struct tmpfs_node *node) 188 { 189 size_t pages = 0; 190 191 TMPFS_LOCK(tmp); 192 LIST_REMOVE(node, tn_entries); 193 tmp->tm_nodes_inuse--; 194 TMPFS_UNLOCK(tmp); 195 196 switch (node->tn_type) { 197 case VNON: 198 /* Do not do anything. VNON is provided to let the 199 * allocation routine clean itself easily by avoiding 200 * duplicating code in it. */ 201 /* FALLTHROUGH */ 202 case VBLK: 203 /* FALLTHROUGH */ 204 case VCHR: 205 /* FALLTHROUGH */ 206 case VDIR: 207 /* FALLTHROUGH */ 208 case VFIFO: 209 /* FALLTHROUGH */ 210 case VSOCK: 211 break; 212 213 case VLNK: 214 free(node->tn_link, M_TMPFSNAME); 215 break; 216 217 case VREG: 218 if (node->tn_reg.tn_aobj != NULL) { 219 vm_object_deallocate(node->tn_reg.tn_aobj); 220 } 221 pages = node->tn_reg.tn_aobj_pages; 222 break; 223 224 default: 225 MPASS(0); 226 break; 227 } 228 229 free_unr(tmp->tm_ino_unr, node->tn_id); 230 uma_zfree(tmp->tm_node_pool, node); 231 232 TMPFS_LOCK(tmp); 233 tmp->tm_pages_used -= pages; 234 TMPFS_UNLOCK(tmp); 235 } 236 237 /* --------------------------------------------------------------------- */ 238 239 /* 240 * Allocates a new directory entry for the node node with a name of name. 241 * The new directory entry is returned in *de. 242 * 243 * The link count of node is increased by one to reflect the new object 244 * referencing it. 245 * 246 * Returns zero on success or an appropriate error code on failure. 247 */ 248 int 249 tmpfs_alloc_dirent(struct tmpfs_mount *tmp, struct tmpfs_node *node, 250 const char *name, uint16_t len, struct tmpfs_dirent **de) 251 { 252 struct tmpfs_dirent *nde; 253 254 nde = (struct tmpfs_dirent *)uma_zalloc( 255 tmp->tm_dirent_pool, M_WAITOK); 256 nde->td_name = malloc(len, M_TMPFSNAME, M_WAITOK); 257 nde->td_namelen = len; 258 memcpy(nde->td_name, name, len); 259 260 nde->td_node = node; 261 node->tn_links++; 262 263 *de = nde; 264 265 return 0; 266 } 267 268 /* --------------------------------------------------------------------- */ 269 270 /* 271 * Frees a directory entry. It is the caller's responsibility to destroy 272 * the node referenced by it if needed. 273 * 274 * The link count of node is decreased by one to reflect the removal of an 275 * object that referenced it. This only happens if 'node_exists' is true; 276 * otherwise the function will not access the node referred to by the 277 * directory entry, as it may already have been released from the outside. 278 */ 279 void 280 tmpfs_free_dirent(struct tmpfs_mount *tmp, struct tmpfs_dirent *de, 281 boolean_t node_exists) 282 { 283 if (node_exists) { 284 struct tmpfs_node *node; 285 286 node = de->td_node; 287 288 MPASS(node->tn_links > 0); 289 node->tn_links--; 290 } 291 292 free(de->td_name, M_TMPFSNAME); 293 uma_zfree(tmp->tm_dirent_pool, de); 294 } 295 296 /* --------------------------------------------------------------------- */ 297 298 /* 299 * Allocates a new vnode for the node node or returns a new reference to 300 * an existing one if the node had already a vnode referencing it. The 301 * resulting locked vnode is returned in *vpp. 302 * 303 * Returns zero on success or an appropriate error code on failure. 304 */ 305 int 306 tmpfs_alloc_vp(struct mount *mp, struct tmpfs_node *node, struct vnode **vpp, 307 struct thread *td) 308 { 309 int error; 310 struct vnode *vp; 311 312 vp = NULL; 313 314 loop: 315 if (node->tn_vnode != NULL) { 316 vp = node->tn_vnode; 317 error = vget(vp, LK_EXCLUSIVE | LK_RETRY, td); 318 if (error) 319 return error; 320 321 /* 322 * Make sure the vnode is still there after 323 * getting the interlock to avoid racing a free. 324 */ 325 if (node->tn_vnode == NULL || node->tn_vnode != vp) { 326 vput(vp); 327 goto loop; 328 } 329 330 goto out; 331 } 332 333 /* 334 * otherwise lock the vp list while we call getnewvnode 335 * since that can block. 336 */ 337 TMPFS_NODE_LOCK(node); 338 if (node->tn_vpstate & TMPFS_VNODE_ALLOCATING) { 339 node->tn_vpstate |= TMPFS_VNODE_WANT; 340 TMPFS_NODE_UNLOCK(node); 341 (void) tsleep((caddr_t) &node->tn_vpstate, 0, "tmpfs_vplock", 0); 342 goto loop; 343 } 344 345 node->tn_vpstate |= TMPFS_VNODE_ALLOCATING; 346 TMPFS_NODE_UNLOCK(node); 347 348 /* Get a new vnode and associate it with our node. */ 349 error = getnewvnode("tmpfs", mp, &tmpfs_vnodeop_entries, &vp); 350 if (error != 0) 351 goto unlock; 352 MPASS(vp != NULL); 353 354 error = vn_lock(vp, LK_EXCLUSIVE | LK_RETRY, td); 355 if (error != 0) { 356 vp->v_data = NULL; 357 vput(vp); 358 vp = NULL; 359 goto unlock; 360 } 361 362 vp->v_data = node; 363 vp->v_type = node->tn_type; 364 365 /* Type-specific initialization. */ 366 switch (node->tn_type) { 367 case VBLK: 368 /* FALLTHROUGH */ 369 case VCHR: 370 break; 371 372 case VDIR: 373 break; 374 375 case VFIFO: 376 vp->v_op = &tmpfs_fifoop_entries; 377 break; 378 379 case VLNK: 380 /* FALLTHROUGH */ 381 case VREG: 382 /* FALLTHROUGH */ 383 case VSOCK: 384 break; 385 386 default: 387 MPASS(0); 388 } 389 390 vnode_pager_setsize(vp, node->tn_size); 391 error = insmntque(vp, mp); 392 if (error) { 393 node->tn_vnode = NULL; 394 if (node->tn_vpstate & TMPFS_VNODE_WANT) { 395 node->tn_vpstate &= ~TMPFS_VNODE_WANT; 396 TMPFS_NODE_UNLOCK(node); 397 wakeup((caddr_t) &node->tn_vpstate); 398 } else 399 TMPFS_NODE_UNLOCK(node); 400 return error; 401 } 402 node->tn_vnode = vp; 403 404 unlock: 405 MPASS(node->tn_vpstate & TMPFS_VNODE_ALLOCATING); 406 TMPFS_NODE_LOCK(node); 407 node->tn_vpstate &= ~TMPFS_VNODE_ALLOCATING; 408 409 if (node->tn_vpstate & TMPFS_VNODE_WANT) { 410 node->tn_vpstate &= ~TMPFS_VNODE_WANT; 411 TMPFS_NODE_UNLOCK(node); 412 wakeup((caddr_t) &node->tn_vpstate); 413 } else 414 TMPFS_NODE_UNLOCK(node); 415 416 out: 417 *vpp = vp; 418 419 MPASS(IFF(error == 0, *vpp != NULL && VOP_ISLOCKED(*vpp, td))); 420 MPASS(*vpp == node->tn_vnode); 421 422 return error; 423 } 424 425 /* --------------------------------------------------------------------- */ 426 427 /* 428 * Destroys the association between the vnode vp and the node it 429 * references. 430 */ 431 void 432 tmpfs_free_vp(struct vnode *vp) 433 { 434 struct tmpfs_node *node; 435 436 node = VP_TO_TMPFS_NODE(vp); 437 438 node->tn_vnode = NULL; 439 vp->v_data = NULL; 440 } 441 442 /* --------------------------------------------------------------------- */ 443 444 /* 445 * Allocates a new file of type 'type' and adds it to the parent directory 446 * 'dvp'; this addition is done using the component name given in 'cnp'. 447 * The ownership of the new file is automatically assigned based on the 448 * credentials of the caller (through 'cnp'), the group is set based on 449 * the parent directory and the mode is determined from the 'vap' argument. 450 * If successful, *vpp holds a vnode to the newly created file and zero 451 * is returned. Otherwise *vpp is NULL and the function returns an 452 * appropriate error code. 453 */ 454 int 455 tmpfs_alloc_file(struct vnode *dvp, struct vnode **vpp, struct vattr *vap, 456 struct componentname *cnp, char *target) 457 { 458 int error; 459 struct tmpfs_dirent *de; 460 struct tmpfs_mount *tmp; 461 struct tmpfs_node *dnode; 462 struct tmpfs_node *node; 463 struct tmpfs_node *parent; 464 465 MPASS(VOP_ISLOCKED(dvp, cnp->cn_thread)); 466 MPASS(cnp->cn_flags & HASBUF); 467 468 tmp = VFS_TO_TMPFS(dvp->v_mount); 469 dnode = VP_TO_TMPFS_DIR(dvp); 470 *vpp = NULL; 471 472 /* If the entry we are creating is a directory, we cannot overflow 473 * the number of links of its parent, because it will get a new 474 * link. */ 475 if (vap->va_type == VDIR) { 476 /* Ensure that we do not overflow the maximum number of links 477 * imposed by the system. */ 478 MPASS(dnode->tn_links <= LINK_MAX); 479 if (dnode->tn_links == LINK_MAX) { 480 error = EMLINK; 481 goto out; 482 } 483 484 parent = dnode; 485 } else 486 parent = NULL; 487 488 /* Allocate a node that represents the new file. */ 489 error = tmpfs_alloc_node(tmp, vap->va_type, cnp->cn_cred->cr_uid, 490 dnode->tn_gid, vap->va_mode, parent, target, vap->va_rdev, 491 cnp->cn_thread, &node); 492 if (error != 0) 493 goto out; 494 495 /* Allocate a directory entry that points to the new file. */ 496 error = tmpfs_alloc_dirent(tmp, node, cnp->cn_nameptr, cnp->cn_namelen, 497 &de); 498 if (error != 0) { 499 tmpfs_free_node(tmp, node); 500 goto out; 501 } 502 503 /* Allocate a vnode for the new file. */ 504 error = tmpfs_alloc_vp(dvp->v_mount, node, vpp, cnp->cn_thread); 505 if (error != 0) { 506 tmpfs_free_dirent(tmp, de, TRUE); 507 tmpfs_free_node(tmp, node); 508 goto out; 509 } 510 511 /* Now that all required items are allocated, we can proceed to 512 * insert the new node into the directory, an operation that 513 * cannot fail. */ 514 tmpfs_dir_attach(dvp, de); 515 516 out: 517 518 return error; 519 } 520 521 /* --------------------------------------------------------------------- */ 522 523 /* 524 * Attaches the directory entry de to the directory represented by vp. 525 * Note that this does not change the link count of the node pointed by 526 * the directory entry, as this is done by tmpfs_alloc_dirent. 527 */ 528 void 529 tmpfs_dir_attach(struct vnode *vp, struct tmpfs_dirent *de) 530 { 531 struct tmpfs_node *dnode; 532 533 dnode = VP_TO_TMPFS_DIR(vp); 534 TAILQ_INSERT_TAIL(&dnode->tn_dir.tn_dirhead, de, td_entries); 535 dnode->tn_size += sizeof(struct tmpfs_dirent); 536 dnode->tn_status |= TMPFS_NODE_ACCESSED | TMPFS_NODE_CHANGED | \ 537 TMPFS_NODE_MODIFIED; 538 } 539 540 /* --------------------------------------------------------------------- */ 541 542 /* 543 * Detaches the directory entry de from the directory represented by vp. 544 * Note that this does not change the link count of the node pointed by 545 * the directory entry, as this is done by tmpfs_free_dirent. 546 */ 547 void 548 tmpfs_dir_detach(struct vnode *vp, struct tmpfs_dirent *de) 549 { 550 struct tmpfs_node *dnode; 551 552 dnode = VP_TO_TMPFS_DIR(vp); 553 554 if (dnode->tn_dir.tn_readdir_lastp == de) { 555 dnode->tn_dir.tn_readdir_lastn = 0; 556 dnode->tn_dir.tn_readdir_lastp = NULL; 557 } 558 559 TAILQ_REMOVE(&dnode->tn_dir.tn_dirhead, de, td_entries); 560 dnode->tn_size -= sizeof(struct tmpfs_dirent); 561 dnode->tn_status |= TMPFS_NODE_ACCESSED | TMPFS_NODE_CHANGED | \ 562 TMPFS_NODE_MODIFIED; 563 } 564 565 /* --------------------------------------------------------------------- */ 566 567 /* 568 * Looks for a directory entry in the directory represented by node. 569 * 'cnp' describes the name of the entry to look for. Note that the . 570 * and .. components are not allowed as they do not physically exist 571 * within directories. 572 * 573 * Returns a pointer to the entry when found, otherwise NULL. 574 */ 575 struct tmpfs_dirent * 576 tmpfs_dir_lookup(struct tmpfs_node *node, struct componentname *cnp) 577 { 578 boolean_t found; 579 struct tmpfs_dirent *de; 580 581 MPASS(IMPLIES(cnp->cn_namelen == 1, cnp->cn_nameptr[0] != '.')); 582 MPASS(IMPLIES(cnp->cn_namelen == 2, !(cnp->cn_nameptr[0] == '.' && 583 cnp->cn_nameptr[1] == '.'))); 584 TMPFS_VALIDATE_DIR(node); 585 586 found = 0; 587 TAILQ_FOREACH(de, &node->tn_dir.tn_dirhead, td_entries) { 588 MPASS(cnp->cn_namelen < 0xffff); 589 if (de->td_namelen == (uint16_t)cnp->cn_namelen && 590 memcmp(de->td_name, cnp->cn_nameptr, de->td_namelen) == 0) { 591 found = 1; 592 break; 593 } 594 } 595 node->tn_status |= TMPFS_NODE_ACCESSED; 596 597 return found ? de : NULL; 598 } 599 600 /* --------------------------------------------------------------------- */ 601 602 /* 603 * Helper function for tmpfs_readdir. Creates a '.' entry for the given 604 * directory and returns it in the uio space. The function returns 0 605 * on success, -1 if there was not enough space in the uio structure to 606 * hold the directory entry or an appropriate error code if another 607 * error happens. 608 */ 609 int 610 tmpfs_dir_getdotdent(struct tmpfs_node *node, struct uio *uio) 611 { 612 int error; 613 struct dirent dent; 614 615 TMPFS_VALIDATE_DIR(node); 616 MPASS(uio->uio_offset == TMPFS_DIRCOOKIE_DOT); 617 618 dent.d_fileno = node->tn_id; 619 dent.d_type = DT_DIR; 620 dent.d_namlen = 1; 621 dent.d_name[0] = '.'; 622 dent.d_name[1] = '\0'; 623 dent.d_reclen = GENERIC_DIRSIZ(&dent); 624 625 if (dent.d_reclen > uio->uio_resid) 626 error = -1; 627 else { 628 error = uiomove(&dent, dent.d_reclen, uio); 629 if (error == 0) 630 uio->uio_offset = TMPFS_DIRCOOKIE_DOTDOT; 631 } 632 633 node->tn_status |= TMPFS_NODE_ACCESSED; 634 635 return error; 636 } 637 638 /* --------------------------------------------------------------------- */ 639 640 /* 641 * Helper function for tmpfs_readdir. Creates a '..' entry for the given 642 * directory and returns it in the uio space. The function returns 0 643 * on success, -1 if there was not enough space in the uio structure to 644 * hold the directory entry or an appropriate error code if another 645 * error happens. 646 */ 647 int 648 tmpfs_dir_getdotdotdent(struct tmpfs_node *node, struct uio *uio) 649 { 650 int error; 651 struct dirent dent; 652 653 TMPFS_VALIDATE_DIR(node); 654 MPASS(uio->uio_offset == TMPFS_DIRCOOKIE_DOTDOT); 655 656 dent.d_fileno = node->tn_dir.tn_parent->tn_id; 657 dent.d_type = DT_DIR; 658 dent.d_namlen = 2; 659 dent.d_name[0] = '.'; 660 dent.d_name[1] = '.'; 661 dent.d_name[2] = '\0'; 662 dent.d_reclen = GENERIC_DIRSIZ(&dent); 663 664 if (dent.d_reclen > uio->uio_resid) 665 error = -1; 666 else { 667 error = uiomove(&dent, dent.d_reclen, uio); 668 if (error == 0) { 669 struct tmpfs_dirent *de; 670 671 de = TAILQ_FIRST(&node->tn_dir.tn_dirhead); 672 if (de == NULL) 673 uio->uio_offset = TMPFS_DIRCOOKIE_EOF; 674 else 675 uio->uio_offset = TMPFS_DIRCOOKIE(de); 676 } 677 } 678 679 node->tn_status |= TMPFS_NODE_ACCESSED; 680 681 return error; 682 } 683 684 /* --------------------------------------------------------------------- */ 685 686 /* 687 * Lookup a directory entry by its associated cookie. 688 */ 689 struct tmpfs_dirent * 690 tmpfs_dir_lookupbycookie(struct tmpfs_node *node, off_t cookie) 691 { 692 struct tmpfs_dirent *de; 693 694 if (cookie == node->tn_dir.tn_readdir_lastn && 695 node->tn_dir.tn_readdir_lastp != NULL) { 696 return node->tn_dir.tn_readdir_lastp; 697 } 698 699 TAILQ_FOREACH(de, &node->tn_dir.tn_dirhead, td_entries) { 700 if (TMPFS_DIRCOOKIE(de) == cookie) { 701 break; 702 } 703 } 704 705 return de; 706 } 707 708 /* --------------------------------------------------------------------- */ 709 710 /* 711 * Helper function for tmpfs_readdir. Returns as much directory entries 712 * as can fit in the uio space. The read starts at uio->uio_offset. 713 * The function returns 0 on success, -1 if there was not enough space 714 * in the uio structure to hold the directory entry or an appropriate 715 * error code if another error happens. 716 */ 717 int 718 tmpfs_dir_getdents(struct tmpfs_node *node, struct uio *uio, off_t *cntp) 719 { 720 int error; 721 off_t startcookie; 722 struct tmpfs_dirent *de; 723 724 TMPFS_VALIDATE_DIR(node); 725 726 /* Locate the first directory entry we have to return. We have cached 727 * the last readdir in the node, so use those values if appropriate. 728 * Otherwise do a linear scan to find the requested entry. */ 729 startcookie = uio->uio_offset; 730 MPASS(startcookie != TMPFS_DIRCOOKIE_DOT); 731 MPASS(startcookie != TMPFS_DIRCOOKIE_DOTDOT); 732 if (startcookie == TMPFS_DIRCOOKIE_EOF) { 733 return 0; 734 } else { 735 de = tmpfs_dir_lookupbycookie(node, startcookie); 736 } 737 if (de == NULL) { 738 return EINVAL; 739 } 740 741 /* Read as much entries as possible; i.e., until we reach the end of 742 * the directory or we exhaust uio space. */ 743 do { 744 struct dirent d; 745 746 /* Create a dirent structure representing the current 747 * tmpfs_node and fill it. */ 748 d.d_fileno = de->td_node->tn_id; 749 switch (de->td_node->tn_type) { 750 case VBLK: 751 d.d_type = DT_BLK; 752 break; 753 754 case VCHR: 755 d.d_type = DT_CHR; 756 break; 757 758 case VDIR: 759 d.d_type = DT_DIR; 760 break; 761 762 case VFIFO: 763 d.d_type = DT_FIFO; 764 break; 765 766 case VLNK: 767 d.d_type = DT_LNK; 768 break; 769 770 case VREG: 771 d.d_type = DT_REG; 772 break; 773 774 case VSOCK: 775 d.d_type = DT_SOCK; 776 break; 777 778 default: 779 MPASS(0); 780 } 781 d.d_namlen = de->td_namelen; 782 MPASS(de->td_namelen < sizeof(d.d_name)); 783 (void)memcpy(d.d_name, de->td_name, de->td_namelen); 784 d.d_name[de->td_namelen] = '\0'; 785 d.d_reclen = GENERIC_DIRSIZ(&d); 786 787 /* Stop reading if the directory entry we are treating is 788 * bigger than the amount of data that can be returned. */ 789 if (d.d_reclen > uio->uio_resid) { 790 error = -1; 791 break; 792 } 793 794 /* Copy the new dirent structure into the output buffer and 795 * advance pointers. */ 796 error = uiomove(&d, d.d_reclen, uio); 797 798 (*cntp)++; 799 de = TAILQ_NEXT(de, td_entries); 800 } while (error == 0 && uio->uio_resid > 0 && de != NULL); 801 802 /* Update the offset and cache. */ 803 if (de == NULL) { 804 uio->uio_offset = TMPFS_DIRCOOKIE_EOF; 805 node->tn_dir.tn_readdir_lastn = 0; 806 node->tn_dir.tn_readdir_lastp = NULL; 807 } else { 808 node->tn_dir.tn_readdir_lastn = uio->uio_offset = TMPFS_DIRCOOKIE(de); 809 node->tn_dir.tn_readdir_lastp = de; 810 } 811 812 node->tn_status |= TMPFS_NODE_ACCESSED; 813 return error; 814 } 815 816 /* --------------------------------------------------------------------- */ 817 818 /* 819 * Resizes the aobj associated to the regular file pointed to by vp to 820 * the size newsize. 'vp' must point to a vnode that represents a regular 821 * file. 'newsize' must be positive. 822 * 823 * Returns zero on success or an appropriate error code on failure. 824 */ 825 int 826 tmpfs_reg_resize(struct vnode *vp, off_t newsize) 827 { 828 int error; 829 size_t newpages, oldpages; 830 struct tmpfs_mount *tmp; 831 struct tmpfs_node *node; 832 off_t oldsize; 833 834 MPASS(vp->v_type == VREG); 835 MPASS(newsize >= 0); 836 837 node = VP_TO_TMPFS_NODE(vp); 838 tmp = VFS_TO_TMPFS(vp->v_mount); 839 840 /* Convert the old and new sizes to the number of pages needed to 841 * store them. It may happen that we do not need to do anything 842 * because the last allocated page can accommodate the change on 843 * its own. */ 844 oldsize = node->tn_size; 845 oldpages = round_page(oldsize) / PAGE_SIZE; 846 MPASS(oldpages == node->tn_reg.tn_aobj_pages); 847 newpages = round_page(newsize) / PAGE_SIZE; 848 849 if (newpages > oldpages && 850 newpages - oldpages > TMPFS_PAGES_AVAIL(tmp)) { 851 error = ENOSPC; 852 goto out; 853 } 854 855 node->tn_reg.tn_aobj_pages = newpages; 856 857 TMPFS_LOCK(tmp); 858 tmp->tm_pages_used += (newpages - oldpages); 859 TMPFS_UNLOCK(tmp); 860 861 node->tn_size = newsize; 862 vnode_pager_setsize(vp, newsize); 863 if (newsize < oldsize) { 864 size_t zerolen = round_page(newsize) - newsize; 865 vm_object_t uobj = node->tn_reg.tn_aobj; 866 vm_page_t m; 867 868 /* 869 * free "backing store" 870 */ 871 VM_OBJECT_LOCK(uobj); 872 if (newpages < oldpages) { 873 swap_pager_freespace(uobj, 874 newpages, oldpages - newpages); 875 vm_object_page_remove(uobj, 876 OFF_TO_IDX(newsize + PAGE_MASK), 0, FALSE); 877 } 878 879 /* 880 * zero out the truncated part of the last page. 881 */ 882 883 if (zerolen > 0) { 884 m = vm_page_grab(uobj, OFF_TO_IDX(newsize), 885 VM_ALLOC_NORMAL | VM_ALLOC_RETRY); 886 pmap_zero_page_area(m, PAGE_SIZE - zerolen, 887 zerolen); 888 vm_page_wakeup(m); 889 } 890 VM_OBJECT_UNLOCK(uobj); 891 892 } 893 894 error = 0; 895 896 out: 897 return error; 898 } 899 900 /* --------------------------------------------------------------------- */ 901 902 /* 903 * Change flags of the given vnode. 904 * Caller should execute tmpfs_update on vp after a successful execution. 905 * The vnode must be locked on entry and remain locked on exit. 906 */ 907 int 908 tmpfs_chflags(struct vnode *vp, int flags, struct ucred *cred, struct thread *p) 909 { 910 int error; 911 struct tmpfs_node *node; 912 913 MPASS(VOP_ISLOCKED(vp, p)); 914 915 node = VP_TO_TMPFS_NODE(vp); 916 917 /* Disallow this operation if the file system is mounted read-only. */ 918 if (vp->v_mount->mnt_flag & MNT_RDONLY) 919 return EROFS; 920 921 /* 922 * Callers may only modify the file flags on objects they 923 * have VADMIN rights for. 924 */ 925 if ((error = VOP_ACCESS(vp, VADMIN, cred, p))) 926 return (error); 927 /* 928 * Unprivileged processes are not permitted to unset system 929 * flags, or modify flags if any system flags are set. 930 */ 931 if (!priv_check_cred(cred, PRIV_VFS_SYSFLAGS, 0)) { 932 if (node->tn_flags 933 & (SF_NOUNLINK | SF_IMMUTABLE | SF_APPEND)) { 934 error = securelevel_gt(cred, 0); 935 if (error) 936 return (error); 937 } 938 /* Snapshot flag cannot be set or cleared */ 939 if (((flags & SF_SNAPSHOT) != 0 && 940 (node->tn_flags & SF_SNAPSHOT) == 0) || 941 ((flags & SF_SNAPSHOT) == 0 && 942 (node->tn_flags & SF_SNAPSHOT) != 0)) 943 return (EPERM); 944 node->tn_flags = flags; 945 } else { 946 if (node->tn_flags 947 & (SF_NOUNLINK | SF_IMMUTABLE | SF_APPEND) || 948 (flags & UF_SETTABLE) != flags) 949 return (EPERM); 950 node->tn_flags &= SF_SETTABLE; 951 node->tn_flags |= (flags & UF_SETTABLE); 952 } 953 node->tn_status |= TMPFS_NODE_CHANGED; 954 955 MPASS(VOP_ISLOCKED(vp, p)); 956 957 return 0; 958 } 959 960 /* --------------------------------------------------------------------- */ 961 962 /* 963 * Change access mode on the given vnode. 964 * Caller should execute tmpfs_update on vp after a successful execution. 965 * The vnode must be locked on entry and remain locked on exit. 966 */ 967 int 968 tmpfs_chmod(struct vnode *vp, mode_t mode, struct ucred *cred, struct thread *p) 969 { 970 int error; 971 struct tmpfs_node *node; 972 973 MPASS(VOP_ISLOCKED(vp, p)); 974 975 node = VP_TO_TMPFS_NODE(vp); 976 977 /* Disallow this operation if the file system is mounted read-only. */ 978 if (vp->v_mount->mnt_flag & MNT_RDONLY) 979 return EROFS; 980 981 /* Immutable or append-only files cannot be modified, either. */ 982 if (node->tn_flags & (IMMUTABLE | APPEND)) 983 return EPERM; 984 985 /* 986 * To modify the permissions on a file, must possess VADMIN 987 * for that file. 988 */ 989 if ((error = VOP_ACCESS(vp, VADMIN, cred, p))) 990 return (error); 991 992 /* 993 * Privileged processes may set the sticky bit on non-directories, 994 * as well as set the setgid bit on a file with a group that the 995 * process is not a member of. 996 */ 997 if (vp->v_type != VDIR && (mode & S_ISTXT)) { 998 if (priv_check_cred(cred, PRIV_VFS_STICKYFILE, 0)) 999 return (EFTYPE); 1000 } 1001 if (!groupmember(node->tn_gid, cred) && (mode & S_ISGID)) { 1002 error = priv_check_cred(cred, PRIV_VFS_SETGID, 0); 1003 if (error) 1004 return (error); 1005 } 1006 1007 1008 node->tn_mode &= ~ALLPERMS; 1009 node->tn_mode |= mode & ALLPERMS; 1010 1011 node->tn_status |= TMPFS_NODE_CHANGED; 1012 1013 MPASS(VOP_ISLOCKED(vp, p)); 1014 1015 return 0; 1016 } 1017 1018 /* --------------------------------------------------------------------- */ 1019 1020 /* 1021 * Change ownership of the given vnode. At least one of uid or gid must 1022 * be different than VNOVAL. If one is set to that value, the attribute 1023 * is unchanged. 1024 * Caller should execute tmpfs_update on vp after a successful execution. 1025 * The vnode must be locked on entry and remain locked on exit. 1026 */ 1027 int 1028 tmpfs_chown(struct vnode *vp, uid_t uid, gid_t gid, struct ucred *cred, 1029 struct thread *p) 1030 { 1031 int error; 1032 struct tmpfs_node *node; 1033 uid_t ouid; 1034 gid_t ogid; 1035 1036 MPASS(VOP_ISLOCKED(vp, p)); 1037 1038 node = VP_TO_TMPFS_NODE(vp); 1039 1040 /* Assign default values if they are unknown. */ 1041 MPASS(uid != VNOVAL || gid != VNOVAL); 1042 if (uid == VNOVAL) 1043 uid = node->tn_uid; 1044 if (gid == VNOVAL) 1045 gid = node->tn_gid; 1046 MPASS(uid != VNOVAL && gid != VNOVAL); 1047 1048 /* Disallow this operation if the file system is mounted read-only. */ 1049 if (vp->v_mount->mnt_flag & MNT_RDONLY) 1050 return EROFS; 1051 1052 /* Immutable or append-only files cannot be modified, either. */ 1053 if (node->tn_flags & (IMMUTABLE | APPEND)) 1054 return EPERM; 1055 1056 /* 1057 * To modify the ownership of a file, must possess VADMIN for that 1058 * file. 1059 */ 1060 if ((error = VOP_ACCESS(vp, VADMIN, cred, p))) 1061 return (error); 1062 1063 /* 1064 * To change the owner of a file, or change the group of a file to a 1065 * group of which we are not a member, the caller must have 1066 * privilege. 1067 */ 1068 if ((uid != node->tn_uid || 1069 (gid != node->tn_gid && !groupmember(gid, cred))) && 1070 (error = priv_check_cred(cred, PRIV_VFS_CHOWN, 0))) 1071 return (error); 1072 1073 ogid = node->tn_gid; 1074 ouid = node->tn_uid; 1075 1076 node->tn_uid = uid; 1077 node->tn_gid = gid; 1078 1079 node->tn_status |= TMPFS_NODE_CHANGED; 1080 1081 if ((node->tn_mode & (S_ISUID | S_ISGID)) && (ouid != uid || ogid != gid)) { 1082 if (priv_check_cred(cred, PRIV_VFS_RETAINSUGID, 0)) 1083 node->tn_mode &= ~(S_ISUID | S_ISGID); 1084 } 1085 1086 MPASS(VOP_ISLOCKED(vp, p)); 1087 1088 return 0; 1089 } 1090 1091 /* --------------------------------------------------------------------- */ 1092 1093 /* 1094 * Change size of the given vnode. 1095 * Caller should execute tmpfs_update on vp after a successful execution. 1096 * The vnode must be locked on entry and remain locked on exit. 1097 */ 1098 int 1099 tmpfs_chsize(struct vnode *vp, u_quad_t size, struct ucred *cred, 1100 struct thread *p) 1101 { 1102 int error; 1103 struct tmpfs_node *node; 1104 1105 MPASS(VOP_ISLOCKED(vp, p)); 1106 1107 node = VP_TO_TMPFS_NODE(vp); 1108 1109 /* Decide whether this is a valid operation based on the file type. */ 1110 error = 0; 1111 switch (vp->v_type) { 1112 case VDIR: 1113 return EISDIR; 1114 1115 case VREG: 1116 if (vp->v_mount->mnt_flag & MNT_RDONLY) 1117 return EROFS; 1118 break; 1119 1120 case VBLK: 1121 /* FALLTHROUGH */ 1122 case VCHR: 1123 /* FALLTHROUGH */ 1124 case VFIFO: 1125 /* Allow modifications of special files even if in the file 1126 * system is mounted read-only (we are not modifying the 1127 * files themselves, but the objects they represent). */ 1128 return 0; 1129 1130 default: 1131 /* Anything else is unsupported. */ 1132 return EOPNOTSUPP; 1133 } 1134 1135 /* Immutable or append-only files cannot be modified, either. */ 1136 if (node->tn_flags & (IMMUTABLE | APPEND)) 1137 return EPERM; 1138 1139 error = tmpfs_truncate(vp, size); 1140 /* tmpfs_truncate will raise the NOTE_EXTEND and NOTE_ATTRIB kevents 1141 * for us, as will update tn_status; no need to do that here. */ 1142 1143 MPASS(VOP_ISLOCKED(vp, p)); 1144 1145 return error; 1146 } 1147 1148 /* --------------------------------------------------------------------- */ 1149 1150 /* 1151 * Change access and modification times of the given vnode. 1152 * Caller should execute tmpfs_update on vp after a successful execution. 1153 * The vnode must be locked on entry and remain locked on exit. 1154 */ 1155 int 1156 tmpfs_chtimes(struct vnode *vp, struct timespec *atime, struct timespec *mtime, 1157 struct timespec *birthtime, int vaflags, struct ucred *cred, struct thread *l) 1158 { 1159 int error; 1160 struct tmpfs_node *node; 1161 1162 MPASS(VOP_ISLOCKED(vp, l)); 1163 1164 node = VP_TO_TMPFS_NODE(vp); 1165 1166 /* Disallow this operation if the file system is mounted read-only. */ 1167 if (vp->v_mount->mnt_flag & MNT_RDONLY) 1168 return EROFS; 1169 1170 /* Immutable or append-only files cannot be modified, either. */ 1171 if (node->tn_flags & (IMMUTABLE | APPEND)) 1172 return EPERM; 1173 1174 /* Determine if the user have proper privilege to update time. */ 1175 if (vaflags & VA_UTIMES_NULL) { 1176 error = VOP_ACCESS(vp, VADMIN, cred, l); 1177 if (error) 1178 error = VOP_ACCESS(vp, VWRITE, cred, l); 1179 } else 1180 error = VOP_ACCESS(vp, VADMIN, cred, l); 1181 if (error) 1182 return (error); 1183 1184 if (atime->tv_sec != VNOVAL && atime->tv_nsec != VNOVAL) 1185 node->tn_status |= TMPFS_NODE_ACCESSED; 1186 1187 if (mtime->tv_sec != VNOVAL && mtime->tv_nsec != VNOVAL) 1188 node->tn_status |= TMPFS_NODE_MODIFIED; 1189 1190 if (birthtime->tv_nsec != VNOVAL && birthtime->tv_nsec != VNOVAL) 1191 node->tn_status |= TMPFS_NODE_MODIFIED; 1192 1193 tmpfs_itimes(vp, atime, mtime); 1194 1195 if (birthtime->tv_nsec != VNOVAL && birthtime->tv_nsec != VNOVAL) 1196 node->tn_birthtime = *birthtime; 1197 MPASS(VOP_ISLOCKED(vp, l)); 1198 1199 return 0; 1200 } 1201 1202 /* --------------------------------------------------------------------- */ 1203 /* Sync timestamps */ 1204 void 1205 tmpfs_itimes(struct vnode *vp, const struct timespec *acc, 1206 const struct timespec *mod) 1207 { 1208 struct tmpfs_node *node; 1209 struct timespec now; 1210 1211 node = VP_TO_TMPFS_NODE(vp); 1212 1213 if ((node->tn_status & (TMPFS_NODE_ACCESSED | TMPFS_NODE_MODIFIED | 1214 TMPFS_NODE_CHANGED)) == 0) 1215 return; 1216 1217 vfs_timestamp(&now); 1218 if (node->tn_status & TMPFS_NODE_ACCESSED) { 1219 if (acc == NULL) 1220 acc = &now; 1221 node->tn_atime = *acc; 1222 } 1223 if (node->tn_status & TMPFS_NODE_MODIFIED) { 1224 if (mod == NULL) 1225 mod = &now; 1226 node->tn_mtime = *mod; 1227 } 1228 if (node->tn_status & TMPFS_NODE_CHANGED) { 1229 node->tn_ctime = now; 1230 } 1231 node->tn_status &= 1232 ~(TMPFS_NODE_ACCESSED | TMPFS_NODE_MODIFIED | TMPFS_NODE_CHANGED); 1233 } 1234 1235 /* --------------------------------------------------------------------- */ 1236 1237 void 1238 tmpfs_update(struct vnode *vp) 1239 { 1240 1241 tmpfs_itimes(vp, NULL, NULL); 1242 } 1243 1244 /* --------------------------------------------------------------------- */ 1245 1246 int 1247 tmpfs_truncate(struct vnode *vp, off_t length) 1248 { 1249 boolean_t extended; 1250 int error; 1251 struct tmpfs_node *node; 1252 1253 node = VP_TO_TMPFS_NODE(vp); 1254 extended = length > node->tn_size; 1255 1256 if (length < 0) { 1257 error = EINVAL; 1258 goto out; 1259 } 1260 1261 if (node->tn_size == length) { 1262 error = 0; 1263 goto out; 1264 } 1265 1266 if (length > VFS_TO_TMPFS(vp->v_mount)->tm_maxfilesize) 1267 return (EFBIG); 1268 1269 error = tmpfs_reg_resize(vp, length); 1270 if (error == 0) { 1271 node->tn_status |= TMPFS_NODE_CHANGED | TMPFS_NODE_MODIFIED; 1272 } 1273 1274 out: 1275 tmpfs_update(vp); 1276 1277 return error; 1278 } 1279