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