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/namei.h> 41 #include <sys/priv.h> 42 #include <sys/proc.h> 43 #include <sys/stat.h> 44 #include <sys/systm.h> 45 #include <sys/vnode.h> 46 #include <sys/vmmeter.h> 47 48 #include <vm/vm.h> 49 #include <vm/vm_object.h> 50 #include <vm/vm_page.h> 51 #include <vm/vm_pager.h> 52 #include <vm/vm_extern.h> 53 54 #include <fs/tmpfs/tmpfs.h> 55 #include <fs/tmpfs/tmpfs_fifoops.h> 56 #include <fs/tmpfs/tmpfs_vnops.h> 57 58 /* --------------------------------------------------------------------- */ 59 60 /* 61 * Allocates a new node of type 'type' inside the 'tmp' mount point, with 62 * its owner set to 'uid', its group to 'gid' and its mode set to 'mode', 63 * using the credentials of the process 'p'. 64 * 65 * If the node type is set to 'VDIR', then the parent parameter must point 66 * to the parent directory of the node being created. It may only be NULL 67 * while allocating the root node. 68 * 69 * If the node type is set to 'VBLK' or 'VCHR', then the rdev parameter 70 * specifies the device the node represents. 71 * 72 * If the node type is set to 'VLNK', then the parameter target specifies 73 * the file name of the target file for the symbolic link that is being 74 * created. 75 * 76 * Note that new nodes are retrieved from the available list if it has 77 * items or, if it is empty, from the node pool as long as there is enough 78 * space to create them. 79 * 80 * Returns zero on success or an appropriate error code on failure. 81 */ 82 int 83 tmpfs_alloc_node(struct tmpfs_mount *tmp, enum vtype type, 84 uid_t uid, gid_t gid, mode_t mode, struct tmpfs_node *parent, 85 char *target, dev_t rdev, struct tmpfs_node **node) 86 { 87 struct tmpfs_node *nnode; 88 89 /* If the root directory of the 'tmp' file system is not yet 90 * allocated, this must be the request to do it. */ 91 MPASS(IMPLIES(tmp->tm_root == NULL, parent == NULL && type == VDIR)); 92 93 MPASS(IFF(type == VLNK, target != NULL)); 94 MPASS(IFF(type == VBLK || type == VCHR, rdev != VNOVAL)); 95 96 if (tmp->tm_nodes_inuse > tmp->tm_nodes_max) 97 return (ENOSPC); 98 99 nnode = (struct tmpfs_node *)uma_zalloc_arg( 100 tmp->tm_node_pool, tmp, M_WAITOK); 101 102 /* Generic initialization. */ 103 nnode->tn_type = type; 104 vfs_timestamp(&nnode->tn_atime); 105 nnode->tn_birthtime = nnode->tn_ctime = nnode->tn_mtime = 106 nnode->tn_atime; 107 nnode->tn_uid = uid; 108 nnode->tn_gid = gid; 109 nnode->tn_mode = mode; 110 nnode->tn_id = alloc_unr(tmp->tm_ino_unr); 111 112 /* Type-specific initialization. */ 113 switch (nnode->tn_type) { 114 case VBLK: 115 case VCHR: 116 nnode->tn_rdev = rdev; 117 break; 118 119 case VDIR: 120 TAILQ_INIT(&nnode->tn_dir.tn_dirhead); 121 MPASS(parent != nnode); 122 MPASS(IMPLIES(parent == NULL, tmp->tm_root == NULL)); 123 nnode->tn_dir.tn_parent = (parent == NULL) ? nnode : parent; 124 nnode->tn_dir.tn_readdir_lastn = 0; 125 nnode->tn_dir.tn_readdir_lastp = NULL; 126 nnode->tn_links++; 127 nnode->tn_dir.tn_parent->tn_links++; 128 break; 129 130 case VFIFO: 131 /* FALLTHROUGH */ 132 case VSOCK: 133 break; 134 135 case VLNK: 136 MPASS(strlen(target) < MAXPATHLEN); 137 nnode->tn_size = strlen(target); 138 nnode->tn_link = malloc(nnode->tn_size, M_TMPFSNAME, 139 M_WAITOK); 140 memcpy(nnode->tn_link, target, nnode->tn_size); 141 break; 142 143 case VREG: 144 nnode->tn_reg.tn_aobj = 145 vm_pager_allocate(OBJT_SWAP, NULL, 0, VM_PROT_DEFAULT, 0); 146 nnode->tn_reg.tn_aobj_pages = 0; 147 break; 148 149 default: 150 panic("tmpfs_alloc_node: type %p %d", nnode, (int)nnode->tn_type); 151 } 152 153 TMPFS_LOCK(tmp); 154 LIST_INSERT_HEAD(&tmp->tm_nodes_used, nnode, tn_entries); 155 tmp->tm_nodes_inuse++; 156 TMPFS_UNLOCK(tmp); 157 158 *node = nnode; 159 return 0; 160 } 161 162 /* --------------------------------------------------------------------- */ 163 164 /* 165 * Destroys the node pointed to by node from the file system 'tmp'. 166 * If the node does not belong to the given mount point, the results are 167 * unpredicted. 168 * 169 * If the node references a directory; no entries are allowed because 170 * their removal could need a recursive algorithm, something forbidden in 171 * kernel space. Furthermore, there is not need to provide such 172 * functionality (recursive removal) because the only primitives offered 173 * to the user are the removal of empty directories and the deletion of 174 * individual files. 175 * 176 * Note that nodes are not really deleted; in fact, when a node has been 177 * allocated, it cannot be deleted during the whole life of the file 178 * system. Instead, they are moved to the available list and remain there 179 * until reused. 180 */ 181 void 182 tmpfs_free_node(struct tmpfs_mount *tmp, struct tmpfs_node *node) 183 { 184 size_t pages = 0; 185 186 #ifdef INVARIANTS 187 TMPFS_NODE_LOCK(node); 188 MPASS(node->tn_vnode == NULL); 189 TMPFS_NODE_UNLOCK(node); 190 #endif 191 192 TMPFS_LOCK(tmp); 193 LIST_REMOVE(node, tn_entries); 194 tmp->tm_nodes_inuse--; 195 TMPFS_UNLOCK(tmp); 196 197 switch (node->tn_type) { 198 case VNON: 199 /* Do not do anything. VNON is provided to let the 200 * allocation routine clean itself easily by avoiding 201 * duplicating code in it. */ 202 /* FALLTHROUGH */ 203 case VBLK: 204 /* FALLTHROUGH */ 205 case VCHR: 206 /* FALLTHROUGH */ 207 case VDIR: 208 /* FALLTHROUGH */ 209 case VFIFO: 210 /* FALLTHROUGH */ 211 case VSOCK: 212 break; 213 214 case VLNK: 215 free(node->tn_link, M_TMPFSNAME); 216 break; 217 218 case VREG: 219 if (node->tn_reg.tn_aobj != NULL) 220 vm_object_deallocate(node->tn_reg.tn_aobj); 221 pages = node->tn_reg.tn_aobj_pages; 222 break; 223 224 default: 225 panic("tmpfs_free_node: type %p %d", node, (int)node->tn_type); 226 } 227 228 free_unr(tmp->tm_ino_unr, node->tn_id); 229 uma_zfree(tmp->tm_node_pool, node); 230 231 TMPFS_LOCK(tmp); 232 tmp->tm_pages_used -= pages; 233 TMPFS_UNLOCK(tmp); 234 } 235 236 /* --------------------------------------------------------------------- */ 237 238 /* 239 * Allocates a new directory entry for the node node with a name of name. 240 * The new directory entry is returned in *de. 241 * 242 * The link count of node is increased by one to reflect the new object 243 * referencing it. 244 * 245 * Returns zero on success or an appropriate error code on failure. 246 */ 247 int 248 tmpfs_alloc_dirent(struct tmpfs_mount *tmp, struct tmpfs_node *node, 249 const char *name, uint16_t len, struct tmpfs_dirent **de) 250 { 251 struct tmpfs_dirent *nde; 252 253 nde = (struct tmpfs_dirent *)uma_zalloc( 254 tmp->tm_dirent_pool, M_WAITOK); 255 nde->td_name = malloc(len, M_TMPFSNAME, M_WAITOK); 256 nde->td_namelen = len; 257 memcpy(nde->td_name, name, len); 258 259 nde->td_node = node; 260 node->tn_links++; 261 262 *de = nde; 263 264 return 0; 265 } 266 267 /* --------------------------------------------------------------------- */ 268 269 /* 270 * Frees a directory entry. It is the caller's responsibility to destroy 271 * the node referenced by it if needed. 272 * 273 * The link count of node is decreased by one to reflect the removal of an 274 * object that referenced it. This only happens if 'node_exists' is true; 275 * otherwise the function will not access the node referred to by the 276 * directory entry, as it may already have been released from the outside. 277 */ 278 void 279 tmpfs_free_dirent(struct tmpfs_mount *tmp, struct tmpfs_dirent *de, 280 boolean_t node_exists) 281 { 282 if (node_exists) { 283 struct tmpfs_node *node; 284 285 node = de->td_node; 286 287 MPASS(node->tn_links > 0); 288 node->tn_links--; 289 } 290 291 free(de->td_name, M_TMPFSNAME); 292 uma_zfree(tmp->tm_dirent_pool, de); 293 } 294 295 /* --------------------------------------------------------------------- */ 296 297 /* 298 * Allocates a new vnode for the node node or returns a new reference to 299 * an existing one if the node had already a vnode referencing it. The 300 * resulting locked vnode is returned in *vpp. 301 * 302 * Returns zero on success or an appropriate error code on failure. 303 */ 304 int 305 tmpfs_alloc_vp(struct mount *mp, struct tmpfs_node *node, int lkflag, 306 struct vnode **vpp) 307 { 308 int error = 0; 309 struct vnode *vp; 310 311 loop: 312 TMPFS_NODE_LOCK(node); 313 if ((vp = node->tn_vnode) != NULL) { 314 VI_LOCK(vp); 315 TMPFS_NODE_UNLOCK(node); 316 vholdl(vp); 317 (void) vget(vp, lkflag | LK_INTERLOCK | LK_RETRY, curthread); 318 vdrop(vp); 319 320 /* 321 * Make sure the vnode is still there after 322 * getting the interlock to avoid racing a free. 323 */ 324 if (node->tn_vnode == NULL || node->tn_vnode != vp) { 325 vput(vp); 326 goto loop; 327 } 328 329 goto out; 330 } 331 332 /* 333 * otherwise lock the vp list while we call getnewvnode 334 * since that can block. 335 */ 336 if (node->tn_vpstate & TMPFS_VNODE_ALLOCATING) { 337 node->tn_vpstate |= TMPFS_VNODE_WANT; 338 error = msleep((caddr_t) &node->tn_vpstate, 339 TMPFS_NODE_MTX(node), PDROP | PCATCH, 340 "tmpfs_alloc_vp", 0); 341 if (error) 342 return error; 343 344 goto loop; 345 } else 346 node->tn_vpstate |= TMPFS_VNODE_ALLOCATING; 347 348 TMPFS_NODE_UNLOCK(node); 349 350 /* Get a new vnode and associate it with our node. */ 351 error = getnewvnode("tmpfs", mp, &tmpfs_vnodeop_entries, &vp); 352 if (error != 0) 353 goto unlock; 354 MPASS(vp != NULL); 355 356 (void) vn_lock(vp, lkflag | LK_RETRY); 357 358 vp->v_data = node; 359 vp->v_type = node->tn_type; 360 361 /* Type-specific initialization. */ 362 switch (node->tn_type) { 363 case VBLK: 364 /* FALLTHROUGH */ 365 case VCHR: 366 /* FALLTHROUGH */ 367 case VLNK: 368 /* FALLTHROUGH */ 369 case VREG: 370 /* FALLTHROUGH */ 371 case VSOCK: 372 break; 373 case VFIFO: 374 vp->v_op = &tmpfs_fifoop_entries; 375 break; 376 case VDIR: 377 if (node->tn_dir.tn_parent == node) 378 vp->v_vflag |= VV_ROOT; 379 break; 380 381 default: 382 panic("tmpfs_alloc_vp: type %p %d", node, (int)node->tn_type); 383 } 384 385 vnode_pager_setsize(vp, node->tn_size); 386 error = insmntque(vp, mp); 387 if (error) 388 vp = NULL; 389 390 unlock: 391 TMPFS_NODE_LOCK(node); 392 393 MPASS(node->tn_vpstate & TMPFS_VNODE_ALLOCATING); 394 node->tn_vpstate &= ~TMPFS_VNODE_ALLOCATING; 395 node->tn_vnode = vp; 396 397 if (node->tn_vpstate & TMPFS_VNODE_WANT) { 398 node->tn_vpstate &= ~TMPFS_VNODE_WANT; 399 TMPFS_NODE_UNLOCK(node); 400 wakeup((caddr_t) &node->tn_vpstate); 401 } else 402 TMPFS_NODE_UNLOCK(node); 403 404 out: 405 *vpp = vp; 406 407 MPASS(IFF(error == 0, *vpp != NULL && VOP_ISLOCKED(*vpp))); 408 #ifdef INVARIANTS 409 TMPFS_NODE_LOCK(node); 410 MPASS(*vpp == node->tn_vnode); 411 TMPFS_NODE_UNLOCK(node); 412 #endif 413 414 return error; 415 } 416 417 /* --------------------------------------------------------------------- */ 418 419 /* 420 * Destroys the association between the vnode vp and the node it 421 * references. 422 */ 423 void 424 tmpfs_free_vp(struct vnode *vp) 425 { 426 struct tmpfs_node *node; 427 428 node = VP_TO_TMPFS_NODE(vp); 429 430 TMPFS_NODE_LOCK(node); 431 node->tn_vnode = NULL; 432 vp->v_data = NULL; 433 TMPFS_NODE_UNLOCK(node); 434 } 435 436 /* --------------------------------------------------------------------- */ 437 438 /* 439 * Allocates a new file of type 'type' and adds it to the parent directory 440 * 'dvp'; this addition is done using the component name given in 'cnp'. 441 * The ownership of the new file is automatically assigned based on the 442 * credentials of the caller (through 'cnp'), the group is set based on 443 * the parent directory and the mode is determined from the 'vap' argument. 444 * If successful, *vpp holds a vnode to the newly created file and zero 445 * is returned. Otherwise *vpp is NULL and the function returns an 446 * appropriate error code. 447 */ 448 int 449 tmpfs_alloc_file(struct vnode *dvp, struct vnode **vpp, struct vattr *vap, 450 struct componentname *cnp, char *target) 451 { 452 int error; 453 struct tmpfs_dirent *de; 454 struct tmpfs_mount *tmp; 455 struct tmpfs_node *dnode; 456 struct tmpfs_node *node; 457 struct tmpfs_node *parent; 458 459 MPASS(VOP_ISLOCKED(dvp)); 460 MPASS(cnp->cn_flags & HASBUF); 461 462 tmp = VFS_TO_TMPFS(dvp->v_mount); 463 dnode = VP_TO_TMPFS_DIR(dvp); 464 *vpp = NULL; 465 466 /* If the entry we are creating is a directory, we cannot overflow 467 * the number of links of its parent, because it will get a new 468 * link. */ 469 if (vap->va_type == VDIR) { 470 /* Ensure that we do not overflow the maximum number of links 471 * imposed by the system. */ 472 MPASS(dnode->tn_links <= LINK_MAX); 473 if (dnode->tn_links == LINK_MAX) { 474 error = EMLINK; 475 goto out; 476 } 477 478 parent = dnode; 479 MPASS(parent != NULL); 480 } else 481 parent = NULL; 482 483 /* Allocate a node that represents the new file. */ 484 error = tmpfs_alloc_node(tmp, vap->va_type, cnp->cn_cred->cr_uid, 485 dnode->tn_gid, vap->va_mode, parent, target, vap->va_rdev, &node); 486 if (error != 0) 487 goto out; 488 489 /* Allocate a directory entry that points to the new file. */ 490 error = tmpfs_alloc_dirent(tmp, node, cnp->cn_nameptr, cnp->cn_namelen, 491 &de); 492 if (error != 0) { 493 tmpfs_free_node(tmp, node); 494 goto out; 495 } 496 497 /* Allocate a vnode for the new file. */ 498 error = tmpfs_alloc_vp(dvp->v_mount, node, LK_EXCLUSIVE, vpp); 499 if (error != 0) { 500 tmpfs_free_dirent(tmp, de, TRUE); 501 tmpfs_free_node(tmp, node); 502 goto out; 503 } 504 505 /* Now that all required items are allocated, we can proceed to 506 * insert the new node into the directory, an operation that 507 * cannot fail. */ 508 tmpfs_dir_attach(dvp, de); 509 510 out: 511 512 return error; 513 } 514 515 /* --------------------------------------------------------------------- */ 516 517 /* 518 * Attaches the directory entry de to the directory represented by vp. 519 * Note that this does not change the link count of the node pointed by 520 * the directory entry, as this is done by tmpfs_alloc_dirent. 521 */ 522 void 523 tmpfs_dir_attach(struct vnode *vp, struct tmpfs_dirent *de) 524 { 525 struct tmpfs_node *dnode; 526 527 ASSERT_VOP_ELOCKED(vp, __func__); 528 dnode = VP_TO_TMPFS_DIR(vp); 529 TAILQ_INSERT_TAIL(&dnode->tn_dir.tn_dirhead, de, td_entries); 530 dnode->tn_size += sizeof(struct tmpfs_dirent); 531 dnode->tn_status |= TMPFS_NODE_ACCESSED | TMPFS_NODE_CHANGED | \ 532 TMPFS_NODE_MODIFIED; 533 } 534 535 /* --------------------------------------------------------------------- */ 536 537 /* 538 * Detaches the directory entry de from the directory represented by vp. 539 * Note that this does not change the link count of the node pointed by 540 * the directory entry, as this is done by tmpfs_free_dirent. 541 */ 542 void 543 tmpfs_dir_detach(struct vnode *vp, struct tmpfs_dirent *de) 544 { 545 struct tmpfs_node *dnode; 546 547 ASSERT_VOP_ELOCKED(vp, __func__); 548 dnode = VP_TO_TMPFS_DIR(vp); 549 550 if (dnode->tn_dir.tn_readdir_lastp == de) { 551 dnode->tn_dir.tn_readdir_lastn = 0; 552 dnode->tn_dir.tn_readdir_lastp = NULL; 553 } 554 555 TAILQ_REMOVE(&dnode->tn_dir.tn_dirhead, de, td_entries); 556 dnode->tn_size -= sizeof(struct tmpfs_dirent); 557 dnode->tn_status |= TMPFS_NODE_ACCESSED | TMPFS_NODE_CHANGED | \ 558 TMPFS_NODE_MODIFIED; 559 } 560 561 /* --------------------------------------------------------------------- */ 562 563 /* 564 * Looks for a directory entry in the directory represented by node. 565 * 'cnp' describes the name of the entry to look for. Note that the . 566 * and .. components are not allowed as they do not physically exist 567 * within directories. 568 * 569 * Returns a pointer to the entry when found, otherwise NULL. 570 */ 571 struct tmpfs_dirent * 572 tmpfs_dir_lookup(struct tmpfs_node *node, struct tmpfs_node *f, 573 struct componentname *cnp) 574 { 575 boolean_t found; 576 struct tmpfs_dirent *de; 577 578 MPASS(IMPLIES(cnp->cn_namelen == 1, cnp->cn_nameptr[0] != '.')); 579 MPASS(IMPLIES(cnp->cn_namelen == 2, !(cnp->cn_nameptr[0] == '.' && 580 cnp->cn_nameptr[1] == '.'))); 581 TMPFS_VALIDATE_DIR(node); 582 583 found = 0; 584 TAILQ_FOREACH(de, &node->tn_dir.tn_dirhead, td_entries) { 585 if (f != NULL && de->td_node != f) 586 continue; 587 MPASS(cnp->cn_namelen < 0xffff); 588 if (de->td_namelen == (uint16_t)cnp->cn_namelen && 589 bcmp(de->td_name, cnp->cn_nameptr, de->td_namelen) == 0) { 590 found = 1; 591 break; 592 } 593 } 594 node->tn_status |= TMPFS_NODE_ACCESSED; 595 596 return found ? de : NULL; 597 } 598 599 /* --------------------------------------------------------------------- */ 600 601 /* 602 * Helper function for tmpfs_readdir. Creates a '.' entry for the given 603 * directory and returns it in the uio space. The function returns 0 604 * on success, -1 if there was not enough space in the uio structure to 605 * hold the directory entry or an appropriate error code if another 606 * error happens. 607 */ 608 int 609 tmpfs_dir_getdotdent(struct tmpfs_node *node, struct uio *uio) 610 { 611 int error; 612 struct dirent dent; 613 614 TMPFS_VALIDATE_DIR(node); 615 MPASS(uio->uio_offset == TMPFS_DIRCOOKIE_DOT); 616 617 dent.d_fileno = node->tn_id; 618 dent.d_type = DT_DIR; 619 dent.d_namlen = 1; 620 dent.d_name[0] = '.'; 621 dent.d_name[1] = '\0'; 622 dent.d_reclen = GENERIC_DIRSIZ(&dent); 623 624 if (dent.d_reclen > uio->uio_resid) 625 error = -1; 626 else { 627 error = uiomove(&dent, dent.d_reclen, uio); 628 if (error == 0) 629 uio->uio_offset = TMPFS_DIRCOOKIE_DOTDOT; 630 } 631 632 node->tn_status |= TMPFS_NODE_ACCESSED; 633 634 return error; 635 } 636 637 /* --------------------------------------------------------------------- */ 638 639 /* 640 * Helper function for tmpfs_readdir. Creates a '..' entry for the given 641 * directory and returns it in the uio space. The function returns 0 642 * on success, -1 if there was not enough space in the uio structure to 643 * hold the directory entry or an appropriate error code if another 644 * error happens. 645 */ 646 int 647 tmpfs_dir_getdotdotdent(struct tmpfs_node *node, struct uio *uio) 648 { 649 int error; 650 struct dirent dent; 651 652 TMPFS_VALIDATE_DIR(node); 653 MPASS(uio->uio_offset == TMPFS_DIRCOOKIE_DOTDOT); 654 655 dent.d_fileno = node->tn_dir.tn_parent->tn_id; 656 dent.d_type = DT_DIR; 657 dent.d_namlen = 2; 658 dent.d_name[0] = '.'; 659 dent.d_name[1] = '.'; 660 dent.d_name[2] = '\0'; 661 dent.d_reclen = GENERIC_DIRSIZ(&dent); 662 663 if (dent.d_reclen > uio->uio_resid) 664 error = -1; 665 else { 666 error = uiomove(&dent, dent.d_reclen, uio); 667 if (error == 0) { 668 struct tmpfs_dirent *de; 669 670 de = TAILQ_FIRST(&node->tn_dir.tn_dirhead); 671 if (de == NULL) 672 uio->uio_offset = TMPFS_DIRCOOKIE_EOF; 673 else 674 uio->uio_offset = tmpfs_dircookie(de); 675 } 676 } 677 678 node->tn_status |= TMPFS_NODE_ACCESSED; 679 680 return error; 681 } 682 683 /* --------------------------------------------------------------------- */ 684 685 /* 686 * Lookup a directory entry by its associated cookie. 687 */ 688 struct tmpfs_dirent * 689 tmpfs_dir_lookupbycookie(struct tmpfs_node *node, off_t cookie) 690 { 691 struct tmpfs_dirent *de; 692 693 if (cookie == node->tn_dir.tn_readdir_lastn && 694 node->tn_dir.tn_readdir_lastp != NULL) { 695 return node->tn_dir.tn_readdir_lastp; 696 } 697 698 TAILQ_FOREACH(de, &node->tn_dir.tn_dirhead, td_entries) { 699 if (tmpfs_dircookie(de) == cookie) { 700 break; 701 } 702 } 703 704 return de; 705 } 706 707 /* --------------------------------------------------------------------- */ 708 709 /* 710 * Helper function for tmpfs_readdir. Returns as much directory entries 711 * as can fit in the uio space. The read starts at uio->uio_offset. 712 * The function returns 0 on success, -1 if there was not enough space 713 * in the uio structure to hold the directory entry or an appropriate 714 * error code if another error happens. 715 */ 716 int 717 tmpfs_dir_getdents(struct tmpfs_node *node, struct uio *uio, off_t *cntp) 718 { 719 int error; 720 off_t startcookie; 721 struct tmpfs_dirent *de; 722 723 TMPFS_VALIDATE_DIR(node); 724 725 /* Locate the first directory entry we have to return. We have cached 726 * the last readdir in the node, so use those values if appropriate. 727 * Otherwise do a linear scan to find the requested entry. */ 728 startcookie = uio->uio_offset; 729 MPASS(startcookie != TMPFS_DIRCOOKIE_DOT); 730 MPASS(startcookie != TMPFS_DIRCOOKIE_DOTDOT); 731 if (startcookie == TMPFS_DIRCOOKIE_EOF) { 732 return 0; 733 } else { 734 de = tmpfs_dir_lookupbycookie(node, startcookie); 735 } 736 if (de == NULL) { 737 return EINVAL; 738 } 739 740 /* Read as much entries as possible; i.e., until we reach the end of 741 * the directory or we exhaust uio space. */ 742 do { 743 struct dirent d; 744 745 /* Create a dirent structure representing the current 746 * tmpfs_node and fill it. */ 747 d.d_fileno = de->td_node->tn_id; 748 switch (de->td_node->tn_type) { 749 case VBLK: 750 d.d_type = DT_BLK; 751 break; 752 753 case VCHR: 754 d.d_type = DT_CHR; 755 break; 756 757 case VDIR: 758 d.d_type = DT_DIR; 759 break; 760 761 case VFIFO: 762 d.d_type = DT_FIFO; 763 break; 764 765 case VLNK: 766 d.d_type = DT_LNK; 767 break; 768 769 case VREG: 770 d.d_type = DT_REG; 771 break; 772 773 case VSOCK: 774 d.d_type = DT_SOCK; 775 break; 776 777 default: 778 panic("tmpfs_dir_getdents: type %p %d", 779 de->td_node, (int)de->td_node->tn_type); 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)); 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)); 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)); 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)); 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)); 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)); 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)); 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)); 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)); 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)); 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 int error; 1250 struct tmpfs_node *node; 1251 1252 node = VP_TO_TMPFS_NODE(vp); 1253 1254 if (length < 0) { 1255 error = EINVAL; 1256 goto out; 1257 } 1258 1259 if (node->tn_size == length) { 1260 error = 0; 1261 goto out; 1262 } 1263 1264 if (length > VFS_TO_TMPFS(vp->v_mount)->tm_maxfilesize) 1265 return (EFBIG); 1266 1267 error = tmpfs_reg_resize(vp, length); 1268 if (error == 0) { 1269 node->tn_status |= TMPFS_NODE_CHANGED | TMPFS_NODE_MODIFIED; 1270 } 1271 1272 out: 1273 tmpfs_update(vp); 1274 1275 return error; 1276 } 1277