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