1 /* $NetBSD: tmpfs_subr.c,v 1.61 2010/11/30 10:43:04 dholland Exp $ */ 2 3 /* 4 * Copyright (c) 2005, 2006, 2007 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 37 #include <sys/cdefs.h> 38 __KERNEL_RCSID(0, "$NetBSD: tmpfs_subr.c,v 1.61 2010/11/30 10:43:04 dholland Exp $"); 39 40 #include <sys/param.h> 41 #include <sys/dirent.h> 42 #include <sys/event.h> 43 #include <sys/kmem.h> 44 #include <sys/mount.h> 45 #include <sys/namei.h> 46 #include <sys/time.h> 47 #include <sys/stat.h> 48 #include <sys/systm.h> 49 #include <sys/swap.h> 50 #include <sys/vnode.h> 51 #include <sys/kauth.h> 52 #include <sys/proc.h> 53 #include <sys/atomic.h> 54 55 #include <uvm/uvm.h> 56 57 #include <miscfs/specfs/specdev.h> 58 #include <miscfs/genfs/genfs.h> 59 #include <fs/tmpfs/tmpfs.h> 60 #include <fs/tmpfs/tmpfs_fifoops.h> 61 #include <fs/tmpfs/tmpfs_specops.h> 62 #include <fs/tmpfs/tmpfs_vnops.h> 63 64 /* --------------------------------------------------------------------- */ 65 66 /* 67 * Allocates a new node of type 'type' inside the 'tmp' mount point, with 68 * its owner set to 'uid', its group to 'gid' and its mode set to 'mode', 69 * using the credentials of the process 'p'. 70 * 71 * If the node type is set to 'VDIR', then the parent parameter must point 72 * to the parent directory of the node being created. It may only be NULL 73 * while allocating the root node. 74 * 75 * If the node type is set to 'VBLK' or 'VCHR', then the rdev parameter 76 * specifies the device the node represents. 77 * 78 * If the node type is set to 'VLNK', then the parameter target specifies 79 * the file name of the target file for the symbolic link that is being 80 * created. 81 * 82 * Note that new nodes are retrieved from the available list if it has 83 * items or, if it is empty, from the node pool as long as there is enough 84 * space to create them. 85 * 86 * Returns zero on success or an appropriate error code on failure. 87 */ 88 int 89 tmpfs_alloc_node(struct tmpfs_mount *tmp, enum vtype type, 90 uid_t uid, gid_t gid, mode_t mode, struct tmpfs_node *parent, 91 char *target, dev_t rdev, struct tmpfs_node **node) 92 { 93 struct tmpfs_node *nnode; 94 95 /* If the root directory of the 'tmp' file system is not yet 96 * allocated, this must be the request to do it. */ 97 KASSERT(IMPLIES(tmp->tm_root == NULL, parent == NULL && type == VDIR)); 98 99 KASSERT(IFF(type == VLNK, target != NULL)); 100 KASSERT(IFF(type == VBLK || type == VCHR, rdev != VNOVAL)); 101 102 KASSERT(uid != VNOVAL && gid != VNOVAL && mode != VNOVAL); 103 104 nnode = NULL; 105 if (atomic_inc_uint_nv(&tmp->tm_nodes_cnt) >= tmp->tm_nodes_max) { 106 atomic_dec_uint(&tmp->tm_nodes_cnt); 107 return ENOSPC; 108 } 109 110 nnode = tmpfs_node_get(tmp); 111 if (nnode == NULL) { 112 atomic_dec_uint(&tmp->tm_nodes_cnt); 113 return ENOSPC; 114 } 115 116 /* 117 * XXX Where the pool is backed by a map larger than (4GB * 118 * sizeof(*nnode)), this may produce duplicate inode numbers 119 * for applications that do not understand 64-bit ino_t. 120 */ 121 nnode->tn_id = (ino_t)((uintptr_t)nnode / sizeof(*nnode)); 122 nnode->tn_gen = arc4random(); 123 124 /* Generic initialization. */ 125 nnode->tn_type = type; 126 nnode->tn_size = 0; 127 nnode->tn_status = 0; 128 nnode->tn_flags = 0; 129 nnode->tn_links = 0; 130 131 vfs_timestamp(&nnode->tn_atime); 132 nnode->tn_birthtime = nnode->tn_atime; 133 nnode->tn_ctime = nnode->tn_atime; 134 nnode->tn_mtime = nnode->tn_atime; 135 136 nnode->tn_uid = uid; 137 nnode->tn_gid = gid; 138 nnode->tn_mode = mode; 139 nnode->tn_lockf = NULL; 140 nnode->tn_vnode = NULL; 141 142 /* Type-specific initialization. */ 143 switch (nnode->tn_type) { 144 case VBLK: 145 case VCHR: 146 nnode->tn_spec.tn_dev.tn_rdev = rdev; 147 break; 148 149 case VDIR: 150 TAILQ_INIT(&nnode->tn_spec.tn_dir.tn_dir); 151 nnode->tn_spec.tn_dir.tn_parent = 152 (parent == NULL) ? nnode : parent; 153 nnode->tn_spec.tn_dir.tn_readdir_lastn = 0; 154 nnode->tn_spec.tn_dir.tn_readdir_lastp = NULL; 155 nnode->tn_links++; 156 break; 157 158 case VFIFO: 159 /* FALLTHROUGH */ 160 case VSOCK: 161 break; 162 163 case VLNK: 164 KASSERT(strlen(target) < MAXPATHLEN); 165 nnode->tn_size = strlen(target); 166 nnode->tn_spec.tn_lnk.tn_link = 167 tmpfs_strname_alloc(tmp, nnode->tn_size); 168 if (nnode->tn_spec.tn_lnk.tn_link == NULL) { 169 atomic_dec_uint(&tmp->tm_nodes_cnt); 170 tmpfs_node_put(tmp, nnode); 171 return ENOSPC; 172 } 173 memcpy(nnode->tn_spec.tn_lnk.tn_link, target, nnode->tn_size); 174 break; 175 176 case VREG: 177 nnode->tn_spec.tn_reg.tn_aobj = 178 uao_create(INT32_MAX - PAGE_SIZE, 0); 179 nnode->tn_spec.tn_reg.tn_aobj_pages = 0; 180 break; 181 182 default: 183 KASSERT(0); 184 } 185 186 mutex_init(&nnode->tn_vlock, MUTEX_DEFAULT, IPL_NONE); 187 188 mutex_enter(&tmp->tm_lock); 189 LIST_INSERT_HEAD(&tmp->tm_nodes, nnode, tn_entries); 190 mutex_exit(&tmp->tm_lock); 191 192 *node = nnode; 193 return 0; 194 } 195 196 /* --------------------------------------------------------------------- */ 197 198 /* 199 * Destroys the node pointed to by node from the file system 'tmp'. 200 * If the node does not belong to the given mount point, the results are 201 * unpredicted. 202 * 203 * If the node references a directory; no entries are allowed because 204 * their removal could need a recursive algorithm, something forbidden in 205 * kernel space. Furthermore, there is not need to provide such 206 * functionality (recursive removal) because the only primitives offered 207 * to the user are the removal of empty directories and the deletion of 208 * individual files. 209 * 210 * Note that nodes are not really deleted; in fact, when a node has been 211 * allocated, it cannot be deleted during the whole life of the file 212 * system. Instead, they are moved to the available list and remain there 213 * until reused. 214 */ 215 void 216 tmpfs_free_node(struct tmpfs_mount *tmp, struct tmpfs_node *node) 217 { 218 size_t objsz; 219 220 mutex_enter(&tmp->tm_lock); 221 LIST_REMOVE(node, tn_entries); 222 mutex_exit(&tmp->tm_lock); 223 atomic_dec_uint(&tmp->tm_nodes_cnt); 224 225 switch (node->tn_type) { 226 case VLNK: 227 tmpfs_strname_free(tmp, node->tn_spec.tn_lnk.tn_link, 228 node->tn_size); 229 break; 230 case VREG: 231 /* 232 * Calculate the size of node data, decrease the used-memory 233 * counter, and destroy the memory object (if any). 234 */ 235 objsz = PAGE_SIZE * node->tn_spec.tn_reg.tn_aobj_pages; 236 if (objsz != 0) { 237 tmpfs_mem_decr(tmp, objsz); 238 } 239 if (node->tn_spec.tn_reg.tn_aobj != NULL) { 240 uao_detach(node->tn_spec.tn_reg.tn_aobj); 241 } 242 break; 243 default: 244 break; 245 } 246 247 mutex_destroy(&node->tn_vlock); 248 tmpfs_node_put(tmp, node); 249 } 250 251 /* --------------------------------------------------------------------- */ 252 253 /* 254 * Allocates a new directory entry for the node node with a name of name. 255 * The new directory entry is returned in *de. 256 * 257 * The link count of node is increased by one to reflect the new object 258 * referencing it. This takes care of notifying kqueue listeners about 259 * this change. 260 * 261 * Returns zero on success or an appropriate error code on failure. 262 */ 263 int 264 tmpfs_alloc_dirent(struct tmpfs_mount *tmp, struct tmpfs_node *node, 265 const char *name, uint16_t len, struct tmpfs_dirent **de) 266 { 267 struct tmpfs_dirent *nde; 268 269 nde = tmpfs_dirent_get(tmp); 270 if (nde == NULL) 271 return ENOSPC; 272 273 nde->td_name = tmpfs_strname_alloc(tmp, len); 274 if (nde->td_name == NULL) { 275 tmpfs_dirent_put(tmp, nde); 276 return ENOSPC; 277 } 278 nde->td_namelen = len; 279 memcpy(nde->td_name, name, len); 280 nde->td_node = node; 281 282 node->tn_links++; 283 if (node->tn_links > 1 && node->tn_vnode != NULL) 284 VN_KNOTE(node->tn_vnode, NOTE_LINK); 285 *de = nde; 286 287 return 0; 288 } 289 290 /* --------------------------------------------------------------------- */ 291 292 /* 293 * Frees a directory entry. It is the caller's responsibility to destroy 294 * the node referenced by it if needed. 295 * 296 * The link count of node is decreased by one to reflect the removal of an 297 * object that referenced it. This only happens if 'node_exists' is true; 298 * otherwise the function will not access the node referred to by the 299 * directory entry, as it may already have been released from the outside. 300 * 301 * Interested parties (kqueue) are notified of the link count change; note 302 * that this can include both the node pointed to by the directory entry 303 * as well as its parent. 304 */ 305 void 306 tmpfs_free_dirent(struct tmpfs_mount *tmp, struct tmpfs_dirent *de, 307 bool node_exists) 308 { 309 if (node_exists) { 310 struct tmpfs_node *node; 311 312 node = de->td_node; 313 314 KASSERT(node->tn_links > 0); 315 node->tn_links--; 316 if (node->tn_vnode != NULL) 317 VN_KNOTE(node->tn_vnode, node->tn_links == 0 ? 318 NOTE_DELETE : NOTE_LINK); 319 if (node->tn_type == VDIR) 320 VN_KNOTE(node->tn_spec.tn_dir.tn_parent->tn_vnode, 321 NOTE_LINK); 322 } 323 324 tmpfs_strname_free(tmp, de->td_name, de->td_namelen); 325 tmpfs_dirent_put(tmp, de); 326 } 327 328 /* --------------------------------------------------------------------- */ 329 330 /* 331 * Allocates a new vnode for the node node or returns a new reference to 332 * an existing one if the node had already a vnode referencing it. The 333 * resulting locked vnode is returned in *vpp. 334 * 335 * Returns zero on success or an appropriate error code on failure. 336 */ 337 int 338 tmpfs_alloc_vp(struct mount *mp, struct tmpfs_node *node, struct vnode **vpp) 339 { 340 int error; 341 struct vnode *vp; 342 343 /* If there is already a vnode, then lock it. */ 344 for (;;) { 345 mutex_enter(&node->tn_vlock); 346 if ((vp = node->tn_vnode) != NULL) { 347 mutex_enter(&vp->v_interlock); 348 mutex_exit(&node->tn_vlock); 349 error = vget(vp, LK_EXCLUSIVE); 350 if (error == ENOENT) { 351 /* vnode was reclaimed. */ 352 continue; 353 } 354 *vpp = vp; 355 return error; 356 } 357 break; 358 } 359 360 /* Get a new vnode and associate it with our node. */ 361 error = getnewvnode(VT_TMPFS, mp, tmpfs_vnodeop_p, &vp); 362 if (error != 0) { 363 mutex_exit(&node->tn_vlock); 364 return error; 365 } 366 367 error = vn_lock(vp, LK_EXCLUSIVE | LK_RETRY); 368 if (error != 0) { 369 mutex_exit(&node->tn_vlock); 370 ungetnewvnode(vp); 371 return error; 372 } 373 374 vp->v_type = node->tn_type; 375 376 /* Type-specific initialization. */ 377 switch (node->tn_type) { 378 case VBLK: 379 /* FALLTHROUGH */ 380 case VCHR: 381 vp->v_op = tmpfs_specop_p; 382 spec_node_init(vp, node->tn_spec.tn_dev.tn_rdev); 383 break; 384 385 case VDIR: 386 vp->v_vflag |= node->tn_spec.tn_dir.tn_parent == node ? 387 VV_ROOT : 0; 388 break; 389 390 case VFIFO: 391 vp->v_op = tmpfs_fifoop_p; 392 break; 393 394 case VLNK: 395 /* FALLTHROUGH */ 396 case VREG: 397 /* FALLTHROUGH */ 398 case VSOCK: 399 break; 400 401 default: 402 KASSERT(0); 403 } 404 405 uvm_vnp_setsize(vp, node->tn_size); 406 vp->v_data = node; 407 node->tn_vnode = vp; 408 mutex_exit(&node->tn_vlock); 409 *vpp = vp; 410 411 KASSERT(IFF(error == 0, *vpp != NULL && VOP_ISLOCKED(*vpp))); 412 KASSERT(*vpp == node->tn_vnode); 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 mutex_enter(&node->tn_vlock); 431 node->tn_vnode = NULL; 432 mutex_exit(&node->tn_vlock); 433 vp->v_data = NULL; 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 KASSERT(VOP_ISLOCKED(dvp)); 460 461 tmp = VFS_TO_TMPFS(dvp->v_mount); 462 dnode = VP_TO_TMPFS_DIR(dvp); 463 *vpp = NULL; 464 465 /* If the entry we are creating is a directory, we cannot overflow 466 * the number of links of its parent, because it will get a new 467 * link. */ 468 if (vap->va_type == VDIR) { 469 /* Ensure that we do not overflow the maximum number of links 470 * imposed by the system. */ 471 KASSERT(dnode->tn_links <= LINK_MAX); 472 if (dnode->tn_links == LINK_MAX) { 473 error = EMLINK; 474 goto out; 475 } 476 477 parent = dnode; 478 } else 479 parent = NULL; 480 481 /* Allocate a node that represents the new file. */ 482 error = tmpfs_alloc_node(tmp, vap->va_type, kauth_cred_geteuid(cnp->cn_cred), 483 dnode->tn_gid, vap->va_mode, parent, target, vap->va_rdev, &node); 484 if (error != 0) 485 goto out; 486 487 /* Allocate a directory entry that points to the new file. */ 488 error = tmpfs_alloc_dirent(tmp, node, cnp->cn_nameptr, cnp->cn_namelen, 489 &de); 490 if (error != 0) { 491 tmpfs_free_node(tmp, node); 492 goto out; 493 } 494 495 /* Allocate a vnode for the new file. */ 496 error = tmpfs_alloc_vp(dvp->v_mount, node, vpp); 497 if (error != 0) { 498 tmpfs_free_dirent(tmp, de, true); 499 tmpfs_free_node(tmp, node); 500 goto out; 501 } 502 503 /* Now that all required items are allocated, we can proceed to 504 * insert the new node into the directory, an operation that 505 * cannot fail. */ 506 tmpfs_dir_attach(dvp, de); 507 if (vap->va_type == VDIR) { 508 VN_KNOTE(dvp, NOTE_LINK); 509 dnode->tn_links++; 510 KASSERT(dnode->tn_links <= LINK_MAX); 511 } 512 513 out: 514 vput(dvp); 515 516 KASSERT(IFF(error == 0, *vpp != NULL)); 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 * As the "parent" directory changes, interested parties are notified of 529 * a write to it. 530 */ 531 void 532 tmpfs_dir_attach(struct vnode *vp, struct tmpfs_dirent *de) 533 { 534 struct tmpfs_node *dnode; 535 536 KASSERT(VOP_ISLOCKED(vp)); 537 dnode = VP_TO_TMPFS_DIR(vp); 538 539 TAILQ_INSERT_TAIL(&dnode->tn_spec.tn_dir.tn_dir, de, td_entries); 540 dnode->tn_size += sizeof(struct tmpfs_dirent); 541 dnode->tn_status |= TMPFS_NODE_ACCESSED | TMPFS_NODE_CHANGED | \ 542 TMPFS_NODE_MODIFIED; 543 uvm_vnp_setsize(vp, dnode->tn_size); 544 545 VN_KNOTE(vp, NOTE_WRITE); 546 } 547 548 /* --------------------------------------------------------------------- */ 549 550 /* 551 * Detaches the directory entry de from the directory represented by vp. 552 * Note that this does not change the link count of the node pointed by 553 * the directory entry, as this is done by tmpfs_free_dirent. 554 * 555 * As the "parent" directory changes, interested parties are notified of 556 * a write to it. 557 */ 558 void 559 tmpfs_dir_detach(struct vnode *vp, struct tmpfs_dirent *de) 560 { 561 struct tmpfs_node *dnode; 562 563 KASSERT(VOP_ISLOCKED(vp)); 564 dnode = VP_TO_TMPFS_DIR(vp); 565 566 if (dnode->tn_spec.tn_dir.tn_readdir_lastp == de) { 567 dnode->tn_spec.tn_dir.tn_readdir_lastn = 0; 568 dnode->tn_spec.tn_dir.tn_readdir_lastp = NULL; 569 } 570 571 TAILQ_REMOVE(&dnode->tn_spec.tn_dir.tn_dir, de, td_entries); 572 dnode->tn_size -= sizeof(struct tmpfs_dirent); 573 dnode->tn_status |= TMPFS_NODE_ACCESSED | TMPFS_NODE_CHANGED | \ 574 TMPFS_NODE_MODIFIED; 575 uvm_vnp_setsize(vp, dnode->tn_size); 576 577 VN_KNOTE(vp, NOTE_WRITE); 578 } 579 580 /* --------------------------------------------------------------------- */ 581 582 /* 583 * Looks for a directory entry in the directory represented by node. 584 * 'cnp' describes the name of the entry to look for. Note that the . 585 * and .. components are not allowed as they do not physically exist 586 * within directories. 587 * 588 * Returns a pointer to the entry when found, otherwise NULL. 589 */ 590 struct tmpfs_dirent * 591 tmpfs_dir_lookup(struct tmpfs_node *node, struct componentname *cnp) 592 { 593 struct tmpfs_dirent *de; 594 595 KASSERT(VOP_ISLOCKED(node->tn_vnode)); 596 KASSERT(IMPLIES(cnp->cn_namelen == 1, cnp->cn_nameptr[0] != '.')); 597 KASSERT(IMPLIES(cnp->cn_namelen == 2, !(cnp->cn_nameptr[0] == '.' && 598 cnp->cn_nameptr[1] == '.'))); 599 TMPFS_VALIDATE_DIR(node); 600 601 node->tn_status |= TMPFS_NODE_ACCESSED; 602 603 TAILQ_FOREACH(de, &node->tn_spec.tn_dir.tn_dir, td_entries) { 604 KASSERT(cnp->cn_namelen < 0xffff); 605 if (de->td_namelen == (uint16_t)cnp->cn_namelen && 606 memcmp(de->td_name, cnp->cn_nameptr, de->td_namelen) == 0) { 607 break; 608 } 609 } 610 611 return de; 612 } 613 614 /* --------------------------------------------------------------------- */ 615 616 /* 617 * Helper function for tmpfs_readdir. Creates a '.' entry for the given 618 * directory and returns it in the uio space. The function returns 0 619 * on success, -1 if there was not enough space in the uio structure to 620 * hold the directory entry or an appropriate error code if another 621 * error happens. 622 */ 623 int 624 tmpfs_dir_getdotdent(struct tmpfs_node *node, struct uio *uio) 625 { 626 int error; 627 struct dirent *dentp; 628 629 TMPFS_VALIDATE_DIR(node); 630 KASSERT(uio->uio_offset == TMPFS_DIRCOOKIE_DOT); 631 632 dentp = kmem_alloc(sizeof(struct dirent), KM_SLEEP); 633 634 dentp->d_fileno = node->tn_id; 635 dentp->d_type = DT_DIR; 636 dentp->d_namlen = 1; 637 dentp->d_name[0] = '.'; 638 dentp->d_name[1] = '\0'; 639 dentp->d_reclen = _DIRENT_SIZE(dentp); 640 641 if (dentp->d_reclen > uio->uio_resid) 642 error = -1; 643 else { 644 error = uiomove(dentp, dentp->d_reclen, uio); 645 if (error == 0) 646 uio->uio_offset = TMPFS_DIRCOOKIE_DOTDOT; 647 } 648 649 node->tn_status |= TMPFS_NODE_ACCESSED; 650 651 kmem_free(dentp, sizeof(struct dirent)); 652 return error; 653 } 654 655 /* --------------------------------------------------------------------- */ 656 657 /* 658 * Helper function for tmpfs_readdir. Creates a '..' entry for the given 659 * directory and returns it in the uio space. The function returns 0 660 * on success, -1 if there was not enough space in the uio structure to 661 * hold the directory entry or an appropriate error code if another 662 * error happens. 663 */ 664 int 665 tmpfs_dir_getdotdotdent(struct tmpfs_node *node, struct uio *uio) 666 { 667 int error; 668 struct dirent *dentp; 669 670 TMPFS_VALIDATE_DIR(node); 671 KASSERT(uio->uio_offset == TMPFS_DIRCOOKIE_DOTDOT); 672 673 dentp = kmem_alloc(sizeof(struct dirent), KM_SLEEP); 674 675 dentp->d_fileno = node->tn_spec.tn_dir.tn_parent->tn_id; 676 dentp->d_type = DT_DIR; 677 dentp->d_namlen = 2; 678 dentp->d_name[0] = '.'; 679 dentp->d_name[1] = '.'; 680 dentp->d_name[2] = '\0'; 681 dentp->d_reclen = _DIRENT_SIZE(dentp); 682 683 if (dentp->d_reclen > uio->uio_resid) 684 error = -1; 685 else { 686 error = uiomove(dentp, dentp->d_reclen, uio); 687 if (error == 0) { 688 struct tmpfs_dirent *de; 689 690 de = TAILQ_FIRST(&node->tn_spec.tn_dir.tn_dir); 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 kmem_free(dentp, sizeof(struct dirent)); 701 return error; 702 } 703 704 /* --------------------------------------------------------------------- */ 705 706 /* 707 * Lookup a directory entry by its associated cookie. 708 */ 709 struct tmpfs_dirent * 710 tmpfs_dir_lookupbycookie(struct tmpfs_node *node, off_t cookie) 711 { 712 struct tmpfs_dirent *de; 713 714 KASSERT(VOP_ISLOCKED(node->tn_vnode)); 715 716 if (cookie == node->tn_spec.tn_dir.tn_readdir_lastn && 717 node->tn_spec.tn_dir.tn_readdir_lastp != NULL) { 718 return node->tn_spec.tn_dir.tn_readdir_lastp; 719 } 720 721 TAILQ_FOREACH(de, &node->tn_spec.tn_dir.tn_dir, td_entries) { 722 if (tmpfs_dircookie(de) == cookie) { 723 break; 724 } 725 } 726 727 return de; 728 } 729 730 /* --------------------------------------------------------------------- */ 731 732 /* 733 * Helper function for tmpfs_readdir. Returns as much directory entries 734 * as can fit in the uio space. The read starts at uio->uio_offset. 735 * The function returns 0 on success, -1 if there was not enough space 736 * in the uio structure to hold the directory entry or an appropriate 737 * error code if another error happens. 738 */ 739 int 740 tmpfs_dir_getdents(struct tmpfs_node *node, struct uio *uio, off_t *cntp) 741 { 742 int error; 743 off_t startcookie; 744 struct dirent *dentp; 745 struct tmpfs_dirent *de; 746 747 KASSERT(VOP_ISLOCKED(node->tn_vnode)); 748 TMPFS_VALIDATE_DIR(node); 749 750 /* Locate the first directory entry we have to return. We have cached 751 * the last readdir in the node, so use those values if appropriate. 752 * Otherwise do a linear scan to find the requested entry. */ 753 startcookie = uio->uio_offset; 754 KASSERT(startcookie != TMPFS_DIRCOOKIE_DOT); 755 KASSERT(startcookie != TMPFS_DIRCOOKIE_DOTDOT); 756 if (startcookie == TMPFS_DIRCOOKIE_EOF) { 757 return 0; 758 } else { 759 de = tmpfs_dir_lookupbycookie(node, startcookie); 760 } 761 if (de == NULL) { 762 return EINVAL; 763 } 764 765 dentp = kmem_alloc(sizeof(struct dirent), KM_SLEEP); 766 767 /* Read as much entries as possible; i.e., until we reach the end of 768 * the directory or we exhaust uio space. */ 769 do { 770 /* Create a dirent structure representing the current 771 * tmpfs_node and fill it. */ 772 dentp->d_fileno = de->td_node->tn_id; 773 switch (de->td_node->tn_type) { 774 case VBLK: 775 dentp->d_type = DT_BLK; 776 break; 777 778 case VCHR: 779 dentp->d_type = DT_CHR; 780 break; 781 782 case VDIR: 783 dentp->d_type = DT_DIR; 784 break; 785 786 case VFIFO: 787 dentp->d_type = DT_FIFO; 788 break; 789 790 case VLNK: 791 dentp->d_type = DT_LNK; 792 break; 793 794 case VREG: 795 dentp->d_type = DT_REG; 796 break; 797 798 case VSOCK: 799 dentp->d_type = DT_SOCK; 800 break; 801 802 default: 803 KASSERT(0); 804 } 805 dentp->d_namlen = de->td_namelen; 806 KASSERT(de->td_namelen < sizeof(dentp->d_name)); 807 (void)memcpy(dentp->d_name, de->td_name, de->td_namelen); 808 dentp->d_name[de->td_namelen] = '\0'; 809 dentp->d_reclen = _DIRENT_SIZE(dentp); 810 811 /* Stop reading if the directory entry we are treating is 812 * bigger than the amount of data that can be returned. */ 813 if (dentp->d_reclen > uio->uio_resid) { 814 error = -1; 815 break; 816 } 817 818 /* Copy the new dirent structure into the output buffer and 819 * advance pointers. */ 820 error = uiomove(dentp, dentp->d_reclen, uio); 821 822 (*cntp)++; 823 de = TAILQ_NEXT(de, td_entries); 824 } while (error == 0 && uio->uio_resid > 0 && de != NULL); 825 826 /* Update the offset and cache. */ 827 if (de == NULL) { 828 uio->uio_offset = TMPFS_DIRCOOKIE_EOF; 829 node->tn_spec.tn_dir.tn_readdir_lastn = 0; 830 node->tn_spec.tn_dir.tn_readdir_lastp = NULL; 831 } else { 832 node->tn_spec.tn_dir.tn_readdir_lastn = uio->uio_offset = 833 tmpfs_dircookie(de); 834 node->tn_spec.tn_dir.tn_readdir_lastp = de; 835 } 836 837 node->tn_status |= TMPFS_NODE_ACCESSED; 838 839 kmem_free(dentp, sizeof(struct dirent)); 840 return error; 841 } 842 843 /* --------------------------------------------------------------------- */ 844 845 /* 846 * Resizes the aobj associated to the regular file pointed to by vp to 847 * the size newsize. 'vp' must point to a vnode that represents a regular 848 * file. 'newsize' must be positive. 849 * 850 * If the file is extended, the appropriate kevent is raised. This does 851 * not rise a write event though because resizing is not the same as 852 * writing. 853 * 854 * Returns zero on success or an appropriate error code on failure. 855 */ 856 int 857 tmpfs_reg_resize(struct vnode *vp, off_t newsize) 858 { 859 size_t newpages, oldpages; 860 struct tmpfs_mount *tmp; 861 struct tmpfs_node *node; 862 off_t oldsize; 863 864 KASSERT(vp->v_type == VREG); 865 KASSERT(newsize >= 0); 866 867 node = VP_TO_TMPFS_NODE(vp); 868 tmp = VFS_TO_TMPFS(vp->v_mount); 869 870 oldsize = node->tn_size; 871 oldpages = round_page(oldsize) >> PAGE_SHIFT; 872 newpages = round_page(newsize) >> PAGE_SHIFT; 873 KASSERT(oldpages == node->tn_spec.tn_reg.tn_aobj_pages); 874 875 if (newpages > oldpages) { 876 /* Increase the used-memory counter if getting extra pages. */ 877 if (!tmpfs_mem_incr(tmp, (newpages - oldpages) << PAGE_SHIFT)) { 878 return ENOSPC; 879 } 880 } else if (newsize < oldsize) { 881 int zerolen = MIN(round_page(newsize), node->tn_size) - newsize; 882 883 /* Zero out the truncated part of the last page. */ 884 uvm_vnp_zerorange(vp, newsize, zerolen); 885 } 886 887 node->tn_spec.tn_reg.tn_aobj_pages = newpages; 888 node->tn_size = newsize; 889 uvm_vnp_setsize(vp, newsize); 890 891 /* 892 * Free "backing store". 893 */ 894 if (newpages < oldpages) { 895 struct uvm_object *uobj; 896 897 uobj = node->tn_spec.tn_reg.tn_aobj; 898 899 mutex_enter(&uobj->vmobjlock); 900 uao_dropswap_range(uobj, newpages, oldpages); 901 mutex_exit(&uobj->vmobjlock); 902 903 /* Decrease the used-memory counter. */ 904 tmpfs_mem_decr(tmp, (oldpages - newpages) << PAGE_SHIFT); 905 } 906 907 if (newsize > oldsize) 908 VN_KNOTE(vp, NOTE_EXTEND); 909 910 return 0; 911 } 912 913 /* 914 * Change flags of the given vnode. 915 * Caller should execute tmpfs_update on vp after a successful execution. 916 * The vnode must be locked on entry and remain locked on exit. 917 */ 918 int 919 tmpfs_chflags(struct vnode *vp, int flags, kauth_cred_t cred, struct lwp *l) 920 { 921 int error; 922 struct tmpfs_node *node; 923 kauth_action_t action = KAUTH_VNODE_WRITE_FLAGS; 924 int fs_decision = 0; 925 926 KASSERT(VOP_ISLOCKED(vp)); 927 928 node = VP_TO_TMPFS_NODE(vp); 929 930 /* Disallow this operation if the file system is mounted read-only. */ 931 if (vp->v_mount->mnt_flag & MNT_RDONLY) 932 return EROFS; 933 934 if (kauth_cred_geteuid(cred) != node->tn_uid) 935 fs_decision = EACCES; 936 937 /* 938 * If the new flags have non-user flags that are different than 939 * those on the node, we need special permission to change them. 940 */ 941 if ((flags & SF_SETTABLE) != (node->tn_flags & SF_SETTABLE)) { 942 action |= KAUTH_VNODE_WRITE_SYSFLAGS; 943 if (!fs_decision) 944 fs_decision = EPERM; 945 } 946 947 /* 948 * Indicate that this node's flags have system attributes in them if 949 * that's the case. 950 */ 951 if (node->tn_flags & (SF_IMMUTABLE | SF_APPEND)) { 952 action |= KAUTH_VNODE_HAS_SYSFLAGS; 953 } 954 955 error = kauth_authorize_vnode(cred, action, vp, NULL, fs_decision); 956 if (error) 957 return error; 958 959 /* 960 * Set the flags. If we're not setting non-user flags, be careful not 961 * to overwrite them. 962 * 963 * XXX: Can't we always assign here? if the system flags are different, 964 * the code above should catch attempts to change them without 965 * proper permissions, and if we're here it means it's okay to 966 * change them... 967 */ 968 if (action & KAUTH_VNODE_WRITE_SYSFLAGS) { 969 node->tn_flags = flags; 970 } else { 971 /* Clear all user-settable flags and re-set them. */ 972 node->tn_flags &= SF_SETTABLE; 973 node->tn_flags |= (flags & UF_SETTABLE); 974 } 975 976 node->tn_status |= TMPFS_NODE_CHANGED; 977 VN_KNOTE(vp, NOTE_ATTRIB); 978 979 KASSERT(VOP_ISLOCKED(vp)); 980 981 return 0; 982 } 983 984 /* --------------------------------------------------------------------- */ 985 986 /* 987 * Change access mode on the given vnode. 988 * Caller should execute tmpfs_update on vp after a successful execution. 989 * The vnode must be locked on entry and remain locked on exit. 990 */ 991 int 992 tmpfs_chmod(struct vnode *vp, mode_t mode, kauth_cred_t cred, struct lwp *l) 993 { 994 int error; 995 struct tmpfs_node *node; 996 997 KASSERT(VOP_ISLOCKED(vp)); 998 999 node = VP_TO_TMPFS_NODE(vp); 1000 1001 /* Disallow this operation if the file system is mounted read-only. */ 1002 if (vp->v_mount->mnt_flag & MNT_RDONLY) 1003 return EROFS; 1004 1005 /* Immutable or append-only files cannot be modified, either. */ 1006 if (node->tn_flags & (IMMUTABLE | APPEND)) 1007 return EPERM; 1008 1009 error = genfs_can_chmod(vp, cred, node->tn_uid, node->tn_gid, 1010 mode); 1011 1012 error = kauth_authorize_vnode(cred, KAUTH_VNODE_WRITE_SECURITY, vp, 1013 NULL, error); 1014 if (error) 1015 return (error); 1016 1017 node->tn_mode = (mode & ALLPERMS); 1018 1019 node->tn_status |= TMPFS_NODE_CHANGED; 1020 VN_KNOTE(vp, NOTE_ATTRIB); 1021 1022 KASSERT(VOP_ISLOCKED(vp)); 1023 1024 return 0; 1025 } 1026 1027 /* --------------------------------------------------------------------- */ 1028 1029 /* 1030 * Change ownership of the given vnode. At least one of uid or gid must 1031 * be different than VNOVAL. If one is set to that value, the attribute 1032 * is unchanged. 1033 * Caller should execute tmpfs_update on vp after a successful execution. 1034 * The vnode must be locked on entry and remain locked on exit. 1035 */ 1036 int 1037 tmpfs_chown(struct vnode *vp, uid_t uid, gid_t gid, kauth_cred_t cred, 1038 struct lwp *l) 1039 { 1040 int error; 1041 struct tmpfs_node *node; 1042 1043 KASSERT(VOP_ISLOCKED(vp)); 1044 1045 node = VP_TO_TMPFS_NODE(vp); 1046 1047 /* Assign default values if they are unknown. */ 1048 KASSERT(uid != VNOVAL || gid != VNOVAL); 1049 if (uid == VNOVAL) 1050 uid = node->tn_uid; 1051 if (gid == VNOVAL) 1052 gid = node->tn_gid; 1053 KASSERT(uid != VNOVAL && gid != VNOVAL); 1054 1055 /* Disallow this operation if the file system is mounted read-only. */ 1056 if (vp->v_mount->mnt_flag & MNT_RDONLY) 1057 return EROFS; 1058 1059 /* Immutable or append-only files cannot be modified, either. */ 1060 if (node->tn_flags & (IMMUTABLE | APPEND)) 1061 return EPERM; 1062 1063 error = genfs_can_chown(vp, cred, node->tn_uid, node->tn_gid, uid, 1064 gid); 1065 1066 error = kauth_authorize_vnode(cred, KAUTH_VNODE_CHANGE_OWNERSHIP, vp, 1067 NULL, error); 1068 if (error) 1069 return (error); 1070 1071 node->tn_uid = uid; 1072 node->tn_gid = gid; 1073 1074 node->tn_status |= TMPFS_NODE_CHANGED; 1075 VN_KNOTE(vp, NOTE_ATTRIB); 1076 1077 KASSERT(VOP_ISLOCKED(vp)); 1078 1079 return 0; 1080 } 1081 1082 /* --------------------------------------------------------------------- */ 1083 1084 /* 1085 * Change size of the given vnode. 1086 * Caller should execute tmpfs_update on vp after a successful execution. 1087 * The vnode must be locked on entry and remain locked on exit. 1088 */ 1089 int 1090 tmpfs_chsize(struct vnode *vp, u_quad_t size, kauth_cred_t cred, 1091 struct lwp *l) 1092 { 1093 int error; 1094 struct tmpfs_node *node; 1095 1096 KASSERT(VOP_ISLOCKED(vp)); 1097 1098 node = VP_TO_TMPFS_NODE(vp); 1099 1100 /* Decide whether this is a valid operation based on the file type. */ 1101 error = 0; 1102 switch (vp->v_type) { 1103 case VDIR: 1104 return EISDIR; 1105 1106 case VREG: 1107 if (vp->v_mount->mnt_flag & MNT_RDONLY) 1108 return EROFS; 1109 break; 1110 1111 case VBLK: 1112 /* FALLTHROUGH */ 1113 case VCHR: 1114 /* FALLTHROUGH */ 1115 case VFIFO: 1116 /* Allow modifications of special files even if in the file 1117 * system is mounted read-only (we are not modifying the 1118 * files themselves, but the objects they represent). */ 1119 return 0; 1120 1121 default: 1122 /* Anything else is unsupported. */ 1123 return EOPNOTSUPP; 1124 } 1125 1126 /* Immutable or append-only files cannot be modified, either. */ 1127 if (node->tn_flags & (IMMUTABLE | APPEND)) 1128 return EPERM; 1129 1130 error = tmpfs_truncate(vp, size); 1131 /* tmpfs_truncate will raise the NOTE_EXTEND and NOTE_ATTRIB kevents 1132 * for us, as will update tn_status; no need to do that here. */ 1133 1134 KASSERT(VOP_ISLOCKED(vp)); 1135 1136 return error; 1137 } 1138 1139 /* --------------------------------------------------------------------- */ 1140 1141 /* 1142 * Change access and modification times of the given vnode. 1143 * Caller should execute tmpfs_update on vp after a successful execution. 1144 * The vnode must be locked on entry and remain locked on exit. 1145 */ 1146 int 1147 tmpfs_chtimes(struct vnode *vp, const struct timespec *atime, 1148 const struct timespec *mtime, const struct timespec *btime, 1149 int vaflags, kauth_cred_t cred, struct lwp *l) 1150 { 1151 int error; 1152 struct tmpfs_node *node; 1153 1154 KASSERT(VOP_ISLOCKED(vp)); 1155 1156 node = VP_TO_TMPFS_NODE(vp); 1157 1158 /* Disallow this operation if the file system is mounted read-only. */ 1159 if (vp->v_mount->mnt_flag & MNT_RDONLY) 1160 return EROFS; 1161 1162 /* Immutable or append-only files cannot be modified, either. */ 1163 if (node->tn_flags & (IMMUTABLE | APPEND)) 1164 return EPERM; 1165 1166 error = genfs_can_chtimes(vp, vaflags, node->tn_uid, cred); 1167 1168 error = kauth_authorize_vnode(cred, KAUTH_VNODE_WRITE_TIMES, vp, NULL, 1169 error); 1170 if (error) 1171 return (error); 1172 1173 if (atime->tv_sec != VNOVAL && atime->tv_nsec != VNOVAL) 1174 node->tn_status |= TMPFS_NODE_ACCESSED; 1175 1176 if (mtime->tv_sec != VNOVAL && mtime->tv_nsec != VNOVAL) 1177 node->tn_status |= TMPFS_NODE_MODIFIED; 1178 1179 if (btime->tv_sec == VNOVAL && btime->tv_nsec == VNOVAL) 1180 btime = NULL; 1181 1182 tmpfs_update(vp, atime, mtime, btime, 0); 1183 VN_KNOTE(vp, NOTE_ATTRIB); 1184 1185 KASSERT(VOP_ISLOCKED(vp)); 1186 1187 return 0; 1188 } 1189 1190 /* --------------------------------------------------------------------- */ 1191 1192 /* Sync timestamps */ 1193 void 1194 tmpfs_itimes(struct vnode *vp, const struct timespec *acc, 1195 const struct timespec *mod, const struct timespec *birth) 1196 { 1197 struct tmpfs_node *node; 1198 struct timespec nowtm; 1199 1200 node = VP_TO_TMPFS_NODE(vp); 1201 1202 if ((node->tn_status & (TMPFS_NODE_ACCESSED | TMPFS_NODE_MODIFIED | 1203 TMPFS_NODE_CHANGED)) == 0) 1204 return; 1205 1206 if (birth != NULL) { 1207 node->tn_birthtime = *birth; 1208 } 1209 vfs_timestamp(&nowtm); 1210 1211 if (node->tn_status & TMPFS_NODE_ACCESSED) { 1212 node->tn_atime = acc ? *acc : nowtm; 1213 } 1214 if (node->tn_status & TMPFS_NODE_MODIFIED) { 1215 node->tn_mtime = mod ? *mod : nowtm; 1216 } 1217 if (node->tn_status & TMPFS_NODE_CHANGED) { 1218 node->tn_ctime = nowtm; 1219 } 1220 1221 node->tn_status &= 1222 ~(TMPFS_NODE_ACCESSED | TMPFS_NODE_MODIFIED | TMPFS_NODE_CHANGED); 1223 } 1224 1225 /* --------------------------------------------------------------------- */ 1226 1227 void 1228 tmpfs_update(struct vnode *vp, const struct timespec *acc, 1229 const struct timespec *mod, const struct timespec *birth, int flags) 1230 { 1231 1232 struct tmpfs_node *node; 1233 1234 KASSERT(VOP_ISLOCKED(vp)); 1235 1236 node = VP_TO_TMPFS_NODE(vp); 1237 1238 #if 0 1239 if (flags & UPDATE_CLOSE) 1240 ; /* XXX Need to do anything special? */ 1241 #endif 1242 1243 tmpfs_itimes(vp, acc, mod, birth); 1244 1245 KASSERT(VOP_ISLOCKED(vp)); 1246 } 1247 1248 /* --------------------------------------------------------------------- */ 1249 1250 int 1251 tmpfs_truncate(struct vnode *vp, off_t length) 1252 { 1253 bool extended; 1254 int error; 1255 struct tmpfs_node *node; 1256 1257 node = VP_TO_TMPFS_NODE(vp); 1258 extended = length > node->tn_size; 1259 1260 if (length < 0) { 1261 error = EINVAL; 1262 goto out; 1263 } 1264 1265 if (node->tn_size == length) { 1266 error = 0; 1267 goto out; 1268 } 1269 1270 error = tmpfs_reg_resize(vp, length); 1271 if (error == 0) 1272 node->tn_status |= TMPFS_NODE_CHANGED | TMPFS_NODE_MODIFIED; 1273 1274 out: 1275 tmpfs_update(vp, NULL, NULL, NULL, 0); 1276 1277 return error; 1278 } 1279