1 /* $NetBSD: tmpfs_subr.c,v 1.112 2020/05/17 19:39:15 ad Exp $ */ 2 3 /* 4 * Copyright (c) 2005-2020 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, and by Mindaugas Rasiukevicius. 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: interfaces for inode and directory entry 35 * construction, destruction and manipulation. 36 * 37 * Reference counting 38 * 39 * The link count of inode (tmpfs_node_t::tn_links) is used as a 40 * reference counter. However, it has slightly different semantics. 41 * 42 * For directories - link count represents directory entries, which 43 * refer to the directories. In other words, it represents the count 44 * of sub-directories. It also takes into account the virtual '.' 45 * entry (which has no real entry in the list). For files - link count 46 * represents the hard links. Since only empty directories can be 47 * removed - link count aligns the reference counting requirements 48 * enough. Note: to check whether directory is not empty, the inode 49 * size (tmpfs_node_t::tn_size) can be used. 50 * 51 * The inode itself, as an object, gathers its first reference when 52 * directory entry is attached via tmpfs_dir_attach(9). For instance, 53 * after regular tmpfs_create(), a file would have a link count of 1, 54 * while directory after tmpfs_mkdir() would have 2 (due to '.'). 55 * 56 * Reclamation 57 * 58 * It should be noted that tmpfs inodes rely on a combination of vnode 59 * reference counting and link counting. That is, an inode can only be 60 * destroyed if its associated vnode is inactive. The destruction is 61 * done on vnode reclamation i.e. tmpfs_reclaim(). It should be noted 62 * that tmpfs_node_t::tn_links being 0 is a destruction criterion. 63 * 64 * If an inode has references within the file system (tn_links > 0) and 65 * its inactive vnode gets reclaimed/recycled - then the association is 66 * broken in tmpfs_reclaim(). In such case, an inode will always pass 67 * tmpfs_lookup() and thus vcache_get() to associate a new vnode. 68 * 69 * Lock order 70 * 71 * vnode_t::v_vlock -> 72 * vnode_t::v_interlock 73 */ 74 75 #include <sys/cdefs.h> 76 __KERNEL_RCSID(0, "$NetBSD: tmpfs_subr.c,v 1.112 2020/05/17 19:39:15 ad Exp $"); 77 78 #include <sys/param.h> 79 #include <sys/cprng.h> 80 #include <sys/dirent.h> 81 #include <sys/event.h> 82 #include <sys/kmem.h> 83 #include <sys/mount.h> 84 #include <sys/namei.h> 85 #include <sys/time.h> 86 #include <sys/stat.h> 87 #include <sys/systm.h> 88 #include <sys/vnode.h> 89 #include <sys/kauth.h> 90 #include <sys/atomic.h> 91 92 #include <uvm/uvm.h> 93 94 #include <miscfs/specfs/specdev.h> 95 #include <miscfs/genfs/genfs.h> 96 #include <fs/tmpfs/tmpfs.h> 97 #include <fs/tmpfs/tmpfs_fifoops.h> 98 #include <fs/tmpfs/tmpfs_specops.h> 99 #include <fs/tmpfs/tmpfs_vnops.h> 100 101 static void tmpfs_dir_putseq(tmpfs_node_t *, tmpfs_dirent_t *); 102 103 /* 104 * Initialize vnode with tmpfs node. 105 */ 106 static void 107 tmpfs_init_vnode(struct vnode *vp, tmpfs_node_t *node) 108 { 109 krwlock_t *slock; 110 111 KASSERT(node->tn_vnode == NULL); 112 113 /* Share the interlock with the node. */ 114 if (node->tn_type == VREG) { 115 slock = node->tn_spec.tn_reg.tn_aobj->vmobjlock; 116 rw_obj_hold(slock); 117 uvm_obj_setlock(&vp->v_uobj, slock); 118 } 119 120 vp->v_tag = VT_TMPFS; 121 vp->v_type = node->tn_type; 122 123 /* Type-specific initialization. */ 124 switch (vp->v_type) { 125 case VBLK: 126 case VCHR: 127 vp->v_op = tmpfs_specop_p; 128 spec_node_init(vp, node->tn_spec.tn_dev.tn_rdev); 129 break; 130 case VFIFO: 131 vp->v_op = tmpfs_fifoop_p; 132 break; 133 case VDIR: 134 if (node->tn_spec.tn_dir.tn_parent == node) 135 vp->v_vflag |= VV_ROOT; 136 /* FALLTHROUGH */ 137 case VLNK: 138 case VREG: 139 case VSOCK: 140 vp->v_op = tmpfs_vnodeop_p; 141 break; 142 default: 143 panic("bad node type %d", vp->v_type); 144 break; 145 } 146 147 vp->v_data = node; 148 node->tn_vnode = vp; 149 uvm_vnp_setsize(vp, node->tn_size); 150 KASSERT(node->tn_mode != VNOVAL); 151 cache_enter_id(vp, node->tn_mode, node->tn_uid, node->tn_gid, true); 152 } 153 154 /* 155 * tmpfs_loadvnode: initialise a vnode for a specified inode. 156 */ 157 int 158 tmpfs_loadvnode(struct mount *mp, struct vnode *vp, 159 const void *key, size_t key_len, const void **new_key) 160 { 161 tmpfs_node_t *node; 162 163 KASSERT(key_len == sizeof(node)); 164 memcpy(&node, key, key_len); 165 166 if (node->tn_links == 0) 167 return ENOENT; 168 169 tmpfs_init_vnode(vp, node); 170 171 *new_key = &vp->v_data; 172 173 return 0; 174 } 175 176 /* 177 * tmpfs_newvnode: allocate a new inode of a specified type and 178 * attach the vonode. 179 */ 180 int 181 tmpfs_newvnode(struct mount *mp, struct vnode *dvp, struct vnode *vp, 182 struct vattr *vap, kauth_cred_t cred, void *extra, 183 size_t *key_len, const void **new_key) 184 { 185 tmpfs_mount_t *tmp = VFS_TO_TMPFS(mp); 186 tmpfs_node_t *node, *dnode; 187 188 if (dvp != NULL) { 189 KASSERT(VOP_ISLOCKED(dvp)); 190 dnode = VP_TO_TMPFS_DIR(dvp); 191 if (dnode->tn_links == 0) 192 return ENOENT; 193 if (vap->va_type == VDIR) { 194 /* Check for maximum links limit. */ 195 if (dnode->tn_links == LINK_MAX) 196 return EMLINK; 197 KASSERT(dnode->tn_links < LINK_MAX); 198 } 199 } else 200 dnode = NULL; 201 202 node = tmpfs_node_get(tmp); 203 if (node == NULL) 204 return ENOSPC; 205 206 /* Initially, no references and no associations. */ 207 node->tn_links = 0; 208 node->tn_vnode = NULL; 209 node->tn_holdcount = 0; 210 node->tn_dirent_hint = NULL; 211 212 /* 213 * XXX Where the pool is backed by a map larger than (4GB * 214 * sizeof(*node)), this may produce duplicate inode numbers 215 * for applications that do not understand 64-bit ino_t. 216 */ 217 node->tn_id = (ino_t)((uintptr_t)node / sizeof(*node)); 218 /* 219 * Make sure the generation number is not zero. 220 * tmpfs_inactive() uses generation zero to mark dead nodes. 221 */ 222 do { 223 node->tn_gen = TMPFS_NODE_GEN_MASK & cprng_fast32(); 224 } while (node->tn_gen == 0); 225 226 /* Generic initialization. */ 227 KASSERT((int)vap->va_type != VNOVAL); 228 node->tn_type = vap->va_type; 229 node->tn_size = 0; 230 node->tn_flags = 0; 231 node->tn_lockf = NULL; 232 233 node->tn_tflags = 0; 234 vfs_timestamp(&node->tn_atime); 235 node->tn_birthtime = node->tn_atime; 236 node->tn_ctime = node->tn_atime; 237 node->tn_mtime = node->tn_atime; 238 mutex_init(&node->tn_timelock, MUTEX_DEFAULT, IPL_NONE); 239 240 if (dvp == NULL) { 241 KASSERT(vap->va_uid != VNOVAL && vap->va_gid != VNOVAL); 242 node->tn_uid = vap->va_uid; 243 node->tn_gid = vap->va_gid; 244 vp->v_vflag |= VV_ROOT; 245 } else { 246 KASSERT(dnode != NULL); 247 node->tn_uid = kauth_cred_geteuid(cred); 248 node->tn_gid = dnode->tn_gid; 249 } 250 KASSERT(vap->va_mode != VNOVAL); 251 node->tn_mode = vap->va_mode; 252 253 /* Type-specific initialization. */ 254 switch (node->tn_type) { 255 case VBLK: 256 case VCHR: 257 /* Character/block special device. */ 258 KASSERT(vap->va_rdev != VNOVAL); 259 node->tn_spec.tn_dev.tn_rdev = vap->va_rdev; 260 break; 261 case VDIR: 262 /* Directory. */ 263 TAILQ_INIT(&node->tn_spec.tn_dir.tn_dir); 264 node->tn_spec.tn_dir.tn_parent = NULL; 265 node->tn_spec.tn_dir.tn_seq_arena = NULL; 266 node->tn_spec.tn_dir.tn_next_seq = TMPFS_DIRSEQ_START; 267 node->tn_spec.tn_dir.tn_readdir_lastp = NULL; 268 269 /* Extra link count for the virtual '.' entry. */ 270 node->tn_links++; 271 break; 272 case VFIFO: 273 case VSOCK: 274 break; 275 case VLNK: 276 node->tn_size = 0; 277 node->tn_spec.tn_lnk.tn_link = NULL; 278 break; 279 case VREG: 280 /* Regular file. Create an underlying UVM object. */ 281 node->tn_spec.tn_reg.tn_aobj = 282 uao_create(INT64_MAX - PAGE_SIZE, 0); 283 node->tn_spec.tn_reg.tn_aobj_pages = 0; 284 break; 285 default: 286 panic("bad node type %d", vp->v_type); 287 break; 288 } 289 290 tmpfs_init_vnode(vp, node); 291 292 mutex_enter(&tmp->tm_lock); 293 LIST_INSERT_HEAD(&tmp->tm_nodes, node, tn_entries); 294 mutex_exit(&tmp->tm_lock); 295 296 *key_len = sizeof(vp->v_data); 297 *new_key = &vp->v_data; 298 299 return 0; 300 } 301 302 /* 303 * tmpfs_free_node: remove the inode from a list in the mount point and 304 * destroy the inode structures. 305 */ 306 void 307 tmpfs_free_node(tmpfs_mount_t *tmp, tmpfs_node_t *node) 308 { 309 size_t objsz; 310 uint32_t hold; 311 312 mutex_enter(&tmp->tm_lock); 313 hold = atomic_or_32_nv(&node->tn_holdcount, TMPFS_NODE_RECLAIMED); 314 /* Defer destruction to last thread holding this node. */ 315 if (hold != TMPFS_NODE_RECLAIMED) { 316 mutex_exit(&tmp->tm_lock); 317 return; 318 } 319 LIST_REMOVE(node, tn_entries); 320 mutex_exit(&tmp->tm_lock); 321 322 switch (node->tn_type) { 323 case VLNK: 324 if (node->tn_size > 0) { 325 tmpfs_strname_free(tmp, node->tn_spec.tn_lnk.tn_link, 326 node->tn_size); 327 } 328 break; 329 case VREG: 330 /* 331 * Calculate the size of inode data, decrease the used-memory 332 * counter, and destroy the unerlying UVM object (if any). 333 */ 334 objsz = PAGE_SIZE * node->tn_spec.tn_reg.tn_aobj_pages; 335 if (objsz != 0) { 336 tmpfs_mem_decr(tmp, objsz); 337 } 338 if (node->tn_spec.tn_reg.tn_aobj != NULL) { 339 uao_detach(node->tn_spec.tn_reg.tn_aobj); 340 } 341 break; 342 case VDIR: 343 KASSERT(node->tn_size == 0); 344 KASSERT(node->tn_spec.tn_dir.tn_seq_arena == NULL); 345 KASSERT(TAILQ_EMPTY(&node->tn_spec.tn_dir.tn_dir)); 346 KASSERT(node->tn_spec.tn_dir.tn_parent == NULL || 347 node == tmp->tm_root); 348 break; 349 default: 350 break; 351 } 352 KASSERT(node->tn_vnode == NULL); 353 KASSERT(node->tn_links == 0); 354 355 mutex_destroy(&node->tn_timelock); 356 tmpfs_node_put(tmp, node); 357 } 358 359 /* 360 * tmpfs_construct_node: allocate a new file of specified type and adds it 361 * into the parent directory. 362 * 363 * => Credentials of the caller are used. 364 */ 365 int 366 tmpfs_construct_node(vnode_t *dvp, vnode_t **vpp, struct vattr *vap, 367 struct componentname *cnp, char *target) 368 { 369 tmpfs_mount_t *tmp = VFS_TO_TMPFS(dvp->v_mount); 370 tmpfs_node_t *dnode = VP_TO_TMPFS_DIR(dvp), *node; 371 tmpfs_dirent_t *de, *wde; 372 char *slink = NULL; 373 int ssize = 0; 374 int error; 375 376 /* Allocate symlink target. */ 377 if (target != NULL) { 378 KASSERT(vap->va_type == VLNK); 379 ssize = strlen(target); 380 KASSERT(ssize < MAXPATHLEN); 381 if (ssize > 0) { 382 slink = tmpfs_strname_alloc(tmp, ssize); 383 if (slink == NULL) 384 return ENOSPC; 385 memcpy(slink, target, ssize); 386 } 387 } 388 389 /* Allocate a directory entry that points to the new file. */ 390 error = tmpfs_alloc_dirent(tmp, cnp->cn_nameptr, cnp->cn_namelen, &de); 391 if (error) { 392 if (slink != NULL) 393 tmpfs_strname_free(tmp, slink, ssize); 394 return error; 395 } 396 397 /* Allocate a vnode that represents the new file. */ 398 error = vcache_new(dvp->v_mount, dvp, vap, cnp->cn_cred, NULL, vpp); 399 if (error) { 400 if (slink != NULL) 401 tmpfs_strname_free(tmp, slink, ssize); 402 tmpfs_free_dirent(tmp, de); 403 return error; 404 } 405 error = vn_lock(*vpp, LK_EXCLUSIVE); 406 if (error) { 407 vrele(*vpp); 408 *vpp = NULL; 409 if (slink != NULL) 410 tmpfs_strname_free(tmp, slink, ssize); 411 tmpfs_free_dirent(tmp, de); 412 return error; 413 } 414 415 node = VP_TO_TMPFS_NODE(*vpp); 416 417 if (slink != NULL) { 418 node->tn_spec.tn_lnk.tn_link = slink; 419 node->tn_size = ssize; 420 } 421 422 /* Remove whiteout before adding the new entry. */ 423 if (cnp->cn_flags & ISWHITEOUT) { 424 wde = tmpfs_dir_lookup(dnode, cnp); 425 KASSERT(wde != NULL && wde->td_node == TMPFS_NODE_WHITEOUT); 426 tmpfs_dir_detach(dnode, wde); 427 tmpfs_free_dirent(tmp, wde); 428 } 429 430 /* Associate inode and attach the entry into the directory. */ 431 tmpfs_dir_attach(dnode, de, node); 432 433 /* Make node opaque if requested. */ 434 if (cnp->cn_flags & ISWHITEOUT) 435 node->tn_flags |= UF_OPAQUE; 436 437 /* Update the parent's timestamps. */ 438 tmpfs_update(dvp, TMPFS_UPDATE_MTIME | TMPFS_UPDATE_CTIME); 439 440 VOP_UNLOCK(*vpp); 441 442 cache_enter(dvp, *vpp, cnp->cn_nameptr, cnp->cn_namelen, cnp->cn_flags); 443 return 0; 444 } 445 446 /* 447 * tmpfs_alloc_dirent: allocates a new directory entry for the inode. 448 * The directory entry contains a path name component. 449 */ 450 int 451 tmpfs_alloc_dirent(tmpfs_mount_t *tmp, const char *name, uint16_t len, 452 tmpfs_dirent_t **de) 453 { 454 tmpfs_dirent_t *nde; 455 456 nde = tmpfs_dirent_get(tmp); 457 if (nde == NULL) 458 return ENOSPC; 459 460 nde->td_name = tmpfs_strname_alloc(tmp, len); 461 if (nde->td_name == NULL) { 462 tmpfs_dirent_put(tmp, nde); 463 return ENOSPC; 464 } 465 nde->td_namelen = len; 466 memcpy(nde->td_name, name, len); 467 nde->td_seq = TMPFS_DIRSEQ_NONE; 468 nde->td_node = NULL; /* for asserts */ 469 470 *de = nde; 471 return 0; 472 } 473 474 /* 475 * tmpfs_free_dirent: free a directory entry. 476 */ 477 void 478 tmpfs_free_dirent(tmpfs_mount_t *tmp, tmpfs_dirent_t *de) 479 { 480 KASSERT(de->td_node == NULL); 481 KASSERT(de->td_seq == TMPFS_DIRSEQ_NONE); 482 tmpfs_strname_free(tmp, de->td_name, de->td_namelen); 483 tmpfs_dirent_put(tmp, de); 484 } 485 486 /* 487 * tmpfs_dir_attach: associate directory entry with a specified inode, 488 * and attach the entry into the directory, specified by vnode. 489 * 490 * => Increases link count on the associated node. 491 * => Increases link count on directory node if our node is VDIR. 492 * => It is caller's responsibility to check for the LINK_MAX limit. 493 * => Triggers kqueue events here. 494 */ 495 void 496 tmpfs_dir_attach(tmpfs_node_t *dnode, tmpfs_dirent_t *de, tmpfs_node_t *node) 497 { 498 vnode_t *dvp = dnode->tn_vnode; 499 int events = NOTE_WRITE; 500 501 KASSERT(dvp != NULL); 502 KASSERT(VOP_ISLOCKED(dvp)); 503 504 /* Get a new sequence number. */ 505 KASSERT(de->td_seq == TMPFS_DIRSEQ_NONE); 506 de->td_seq = tmpfs_dir_getseq(dnode, de); 507 508 /* Associate directory entry and the inode. */ 509 de->td_node = node; 510 if (node != TMPFS_NODE_WHITEOUT) { 511 KASSERT(node->tn_links < LINK_MAX); 512 node->tn_links++; 513 514 /* Save the hint (might overwrite). */ 515 node->tn_dirent_hint = de; 516 } else if ((dnode->tn_gen & TMPFS_WHITEOUT_BIT) == 0) { 517 /* Flag that there are whiteout entries. */ 518 atomic_or_32(&dnode->tn_gen, TMPFS_WHITEOUT_BIT); 519 } 520 521 /* Insert the entry to the directory (parent of inode). */ 522 TAILQ_INSERT_TAIL(&dnode->tn_spec.tn_dir.tn_dir, de, td_entries); 523 dnode->tn_size += sizeof(tmpfs_dirent_t); 524 uvm_vnp_setsize(dvp, dnode->tn_size); 525 526 if (node != TMPFS_NODE_WHITEOUT && node->tn_type == VDIR) { 527 /* Set parent. */ 528 KASSERT(node->tn_spec.tn_dir.tn_parent == NULL); 529 node->tn_spec.tn_dir.tn_parent = dnode; 530 531 /* Increase the link count of parent. */ 532 KASSERT(dnode->tn_links < LINK_MAX); 533 dnode->tn_links++; 534 events |= NOTE_LINK; 535 536 TMPFS_VALIDATE_DIR(node); 537 } 538 VN_KNOTE(dvp, events); 539 } 540 541 /* 542 * tmpfs_dir_detach: disassociate directory entry and its inode, 543 * and detach the entry from the directory, specified by vnode. 544 * 545 * => Decreases link count on the associated node. 546 * => Decreases the link count on directory node, if our node is VDIR. 547 * => Triggers kqueue events here. 548 * 549 * => Note: dvp and vp may be NULL only if called by tmpfs_unmount(). 550 */ 551 void 552 tmpfs_dir_detach(tmpfs_node_t *dnode, tmpfs_dirent_t *de) 553 { 554 tmpfs_node_t *node = de->td_node; 555 vnode_t *vp, *dvp = dnode->tn_vnode; 556 int events = NOTE_WRITE; 557 558 KASSERT(dvp == NULL || VOP_ISLOCKED(dvp)); 559 560 if (__predict_true(node != TMPFS_NODE_WHITEOUT)) { 561 /* Deassociate the inode and entry. */ 562 node->tn_dirent_hint = NULL; 563 564 KASSERT(node->tn_links > 0); 565 node->tn_links--; 566 567 if ((vp = node->tn_vnode) != NULL) { 568 KASSERT(VOP_ISLOCKED(vp)); 569 VN_KNOTE(vp, node->tn_links ? NOTE_LINK : NOTE_DELETE); 570 } 571 572 /* If directory - decrease the link count of parent. */ 573 if (node->tn_type == VDIR) { 574 KASSERT(node->tn_spec.tn_dir.tn_parent == dnode); 575 node->tn_spec.tn_dir.tn_parent = NULL; 576 577 KASSERT(dnode->tn_links > 0); 578 dnode->tn_links--; 579 events |= NOTE_LINK; 580 } 581 } 582 de->td_node = NULL; 583 584 /* Remove the entry from the directory. */ 585 if (dnode->tn_spec.tn_dir.tn_readdir_lastp == de) { 586 dnode->tn_spec.tn_dir.tn_readdir_lastp = NULL; 587 } 588 TAILQ_REMOVE(&dnode->tn_spec.tn_dir.tn_dir, de, td_entries); 589 dnode->tn_size -= sizeof(tmpfs_dirent_t); 590 tmpfs_dir_putseq(dnode, de); 591 592 if (dvp) { 593 uvm_vnp_setsize(dvp, dnode->tn_size); 594 VN_KNOTE(dvp, events); 595 } 596 } 597 598 /* 599 * tmpfs_dir_lookup: find a directory entry in the specified inode. 600 * 601 * Note that the . and .. components are not allowed as they do not 602 * physically exist within directories. 603 */ 604 tmpfs_dirent_t * 605 tmpfs_dir_lookup(tmpfs_node_t *node, struct componentname *cnp) 606 { 607 const char *name = cnp->cn_nameptr; 608 const uint16_t nlen = cnp->cn_namelen; 609 tmpfs_dirent_t *de; 610 611 KASSERT(VOP_ISLOCKED(node->tn_vnode)); 612 KASSERT(nlen != 1 || !(name[0] == '.')); 613 KASSERT(nlen != 2 || !(name[0] == '.' && name[1] == '.')); 614 TMPFS_VALIDATE_DIR(node); 615 616 TAILQ_FOREACH(de, &node->tn_spec.tn_dir.tn_dir, td_entries) { 617 if (de->td_namelen != nlen) 618 continue; 619 if (memcmp(de->td_name, name, nlen) != 0) 620 continue; 621 break; 622 } 623 return de; 624 } 625 626 /* 627 * tmpfs_dir_cached: get a cached directory entry if it is valid. Used to 628 * avoid unnecessary tmpfs_dir_lookup(). 629 * 630 * => The vnode must be locked. 631 */ 632 tmpfs_dirent_t * 633 tmpfs_dir_cached(tmpfs_node_t *node) 634 { 635 tmpfs_dirent_t *de = node->tn_dirent_hint; 636 637 KASSERT(VOP_ISLOCKED(node->tn_vnode)); 638 639 if (de == NULL) { 640 return NULL; 641 } 642 KASSERT(de->td_node == node); 643 644 /* 645 * Directories always have a valid hint. For files, check if there 646 * are any hard links. If there are - hint might be invalid. 647 */ 648 return (node->tn_type != VDIR && node->tn_links > 1) ? NULL : de; 649 } 650 651 /* 652 * tmpfs_dir_getseq: get a per-directory sequence number for the entry. 653 * 654 * => Shall not be larger than 2^31 for linux32 compatibility. 655 */ 656 uint32_t 657 tmpfs_dir_getseq(tmpfs_node_t *dnode, tmpfs_dirent_t *de) 658 { 659 uint32_t seq = de->td_seq; 660 vmem_t *seq_arena; 661 vmem_addr_t off; 662 int error __diagused; 663 664 TMPFS_VALIDATE_DIR(dnode); 665 666 if (__predict_true(seq != TMPFS_DIRSEQ_NONE)) { 667 /* Already set. */ 668 KASSERT(seq >= TMPFS_DIRSEQ_START); 669 return seq; 670 } 671 672 /* 673 * The "." and ".." and the end-of-directory have reserved numbers. 674 * The other sequence numbers are allocated as following: 675 * 676 * - The first half of the 2^31 is assigned incrementally. 677 * 678 * - If that range is exceeded, then the second half of 2^31 679 * is used, but managed by vmem(9). 680 */ 681 682 seq = dnode->tn_spec.tn_dir.tn_next_seq; 683 KASSERT(seq >= TMPFS_DIRSEQ_START); 684 685 if (__predict_true(seq < TMPFS_DIRSEQ_END)) { 686 /* First half: just increment and return. */ 687 dnode->tn_spec.tn_dir.tn_next_seq++; 688 return seq; 689 } 690 691 /* 692 * First half exceeded, use the second half. May need to create 693 * vmem(9) arena for the directory first. 694 */ 695 if ((seq_arena = dnode->tn_spec.tn_dir.tn_seq_arena) == NULL) { 696 seq_arena = vmem_create("tmpfscoo", 0, 697 TMPFS_DIRSEQ_END - 1, 1, NULL, NULL, NULL, 0, 698 VM_SLEEP, IPL_NONE); 699 dnode->tn_spec.tn_dir.tn_seq_arena = seq_arena; 700 KASSERT(seq_arena != NULL); 701 } 702 error = vmem_alloc(seq_arena, 1, VM_SLEEP | VM_BESTFIT, &off); 703 KASSERT(error == 0); 704 705 KASSERT(off < TMPFS_DIRSEQ_END); 706 seq = off | TMPFS_DIRSEQ_END; 707 return seq; 708 } 709 710 static void 711 tmpfs_dir_putseq(tmpfs_node_t *dnode, tmpfs_dirent_t *de) 712 { 713 vmem_t *seq_arena = dnode->tn_spec.tn_dir.tn_seq_arena; 714 uint32_t seq = de->td_seq; 715 716 TMPFS_VALIDATE_DIR(dnode); 717 718 if (seq == TMPFS_DIRSEQ_NONE || seq < TMPFS_DIRSEQ_END) { 719 /* First half (or no sequence number set yet). */ 720 KASSERT(de->td_seq >= TMPFS_DIRSEQ_START); 721 } else { 722 /* Second half. */ 723 KASSERT(seq_arena != NULL); 724 KASSERT(seq >= TMPFS_DIRSEQ_END); 725 seq &= ~TMPFS_DIRSEQ_END; 726 vmem_free(seq_arena, seq, 1); 727 } 728 de->td_seq = TMPFS_DIRSEQ_NONE; 729 730 /* Empty? We can reset. */ 731 if (seq_arena && dnode->tn_size == 0) { 732 dnode->tn_spec.tn_dir.tn_seq_arena = NULL; 733 dnode->tn_spec.tn_dir.tn_next_seq = TMPFS_DIRSEQ_START; 734 vmem_destroy(seq_arena); 735 } 736 } 737 738 /* 739 * tmpfs_dir_lookupbyseq: lookup a directory entry by the sequence number. 740 */ 741 tmpfs_dirent_t * 742 tmpfs_dir_lookupbyseq(tmpfs_node_t *node, off_t seq) 743 { 744 tmpfs_dirent_t *de = node->tn_spec.tn_dir.tn_readdir_lastp; 745 746 TMPFS_VALIDATE_DIR(node); 747 748 /* 749 * First, check the cache. If does not match - perform a lookup. 750 */ 751 if (de && de->td_seq == seq) { 752 KASSERT(de->td_seq >= TMPFS_DIRSEQ_START); 753 KASSERT(de->td_seq != TMPFS_DIRSEQ_NONE); 754 return de; 755 } 756 TAILQ_FOREACH(de, &node->tn_spec.tn_dir.tn_dir, td_entries) { 757 KASSERT(de->td_seq >= TMPFS_DIRSEQ_START); 758 KASSERT(de->td_seq != TMPFS_DIRSEQ_NONE); 759 if (de->td_seq == seq) 760 return de; 761 } 762 return NULL; 763 } 764 765 /* 766 * tmpfs_dir_getdotents: helper function for tmpfs_readdir() to get the 767 * dot meta entries, that is, "." or "..". Copy it to the UIO space. 768 */ 769 static int 770 tmpfs_dir_getdotents(tmpfs_node_t *node, struct dirent *dp, struct uio *uio) 771 { 772 tmpfs_dirent_t *de; 773 off_t next = 0; 774 int error; 775 776 switch (uio->uio_offset) { 777 case TMPFS_DIRSEQ_DOT: 778 dp->d_fileno = node->tn_id; 779 strlcpy(dp->d_name, ".", sizeof(dp->d_name)); 780 next = TMPFS_DIRSEQ_DOTDOT; 781 break; 782 case TMPFS_DIRSEQ_DOTDOT: 783 dp->d_fileno = node->tn_spec.tn_dir.tn_parent->tn_id; 784 strlcpy(dp->d_name, "..", sizeof(dp->d_name)); 785 de = TAILQ_FIRST(&node->tn_spec.tn_dir.tn_dir); 786 next = de ? tmpfs_dir_getseq(node, de) : TMPFS_DIRSEQ_EOF; 787 break; 788 default: 789 KASSERT(false); 790 } 791 dp->d_type = DT_DIR; 792 dp->d_namlen = strlen(dp->d_name); 793 dp->d_reclen = _DIRENT_SIZE(dp); 794 795 if (dp->d_reclen > uio->uio_resid) { 796 return EJUSTRETURN; 797 } 798 if ((error = uiomove(dp, dp->d_reclen, uio)) != 0) { 799 return error; 800 } 801 802 uio->uio_offset = next; 803 return error; 804 } 805 806 /* 807 * tmpfs_dir_getdents: helper function for tmpfs_readdir. 808 * 809 * => Returns as much directory entries as can fit in the uio space. 810 * => The read starts at uio->uio_offset. 811 */ 812 int 813 tmpfs_dir_getdents(tmpfs_node_t *node, struct uio *uio, off_t *cntp) 814 { 815 tmpfs_dirent_t *de; 816 struct dirent dent; 817 int error = 0; 818 819 KASSERT(VOP_ISLOCKED(node->tn_vnode)); 820 TMPFS_VALIDATE_DIR(node); 821 822 /* 823 * First check for the "." and ".." cases. 824 * Note: tmpfs_dir_getdotents() will "seek" for us. 825 */ 826 memset(&dent, 0, sizeof(dent)); 827 828 if (uio->uio_offset == TMPFS_DIRSEQ_DOT) { 829 if ((error = tmpfs_dir_getdotents(node, &dent, uio)) != 0) { 830 goto done; 831 } 832 (*cntp)++; 833 } 834 if (uio->uio_offset == TMPFS_DIRSEQ_DOTDOT) { 835 if ((error = tmpfs_dir_getdotents(node, &dent, uio)) != 0) { 836 goto done; 837 } 838 (*cntp)++; 839 } 840 841 /* Done if we reached the end. */ 842 if (uio->uio_offset == TMPFS_DIRSEQ_EOF) { 843 goto done; 844 } 845 846 /* Locate the directory entry given by the given sequence number. */ 847 de = tmpfs_dir_lookupbyseq(node, uio->uio_offset); 848 if (de == NULL) { 849 error = EINVAL; 850 goto done; 851 } 852 853 /* 854 * Read as many entries as possible; i.e., until we reach the end 855 * of the directory or we exhaust UIO space. 856 */ 857 do { 858 if (de->td_node == TMPFS_NODE_WHITEOUT) { 859 dent.d_fileno = 1; 860 dent.d_type = DT_WHT; 861 } else { 862 dent.d_fileno = de->td_node->tn_id; 863 dent.d_type = vtype2dt(de->td_node->tn_type); 864 } 865 dent.d_namlen = de->td_namelen; 866 KASSERT(de->td_namelen < sizeof(dent.d_name)); 867 memcpy(dent.d_name, de->td_name, de->td_namelen); 868 dent.d_name[de->td_namelen] = '\0'; 869 dent.d_reclen = _DIRENT_SIZE(&dent); 870 871 if (dent.d_reclen > uio->uio_resid) { 872 /* Exhausted UIO space. */ 873 error = EJUSTRETURN; 874 break; 875 } 876 877 /* Copy out the directory entry and continue. */ 878 error = uiomove(&dent, dent.d_reclen, uio); 879 if (error) { 880 break; 881 } 882 (*cntp)++; 883 de = TAILQ_NEXT(de, td_entries); 884 885 } while (uio->uio_resid > 0 && de); 886 887 /* Cache the last entry or clear and mark EOF. */ 888 uio->uio_offset = de ? tmpfs_dir_getseq(node, de) : TMPFS_DIRSEQ_EOF; 889 node->tn_spec.tn_dir.tn_readdir_lastp = de; 890 done: 891 tmpfs_update(node->tn_vnode, TMPFS_UPDATE_ATIME); 892 893 if (error == EJUSTRETURN) { 894 /* Exhausted UIO space - just return. */ 895 error = 0; 896 } 897 KASSERT(error >= 0); 898 return error; 899 } 900 901 /* 902 * tmpfs_reg_resize: resize the underlying UVM object associated with the 903 * specified regular file. 904 */ 905 int 906 tmpfs_reg_resize(struct vnode *vp, off_t newsize) 907 { 908 tmpfs_mount_t *tmp = VFS_TO_TMPFS(vp->v_mount); 909 tmpfs_node_t *node = VP_TO_TMPFS_NODE(vp); 910 struct uvm_object *uobj = node->tn_spec.tn_reg.tn_aobj; 911 size_t newpages, oldpages; 912 off_t oldsize; 913 914 KASSERT(vp->v_type == VREG); 915 KASSERT(newsize >= 0); 916 917 oldsize = node->tn_size; 918 oldpages = round_page(oldsize) >> PAGE_SHIFT; 919 newpages = round_page(newsize) >> PAGE_SHIFT; 920 KASSERT(oldpages == node->tn_spec.tn_reg.tn_aobj_pages); 921 922 if (newsize == oldsize) { 923 return 0; 924 } 925 926 if (newpages > oldpages) { 927 /* Increase the used-memory counter if getting extra pages. */ 928 if (!tmpfs_mem_incr(tmp, (newpages - oldpages) << PAGE_SHIFT)) { 929 return ENOSPC; 930 } 931 } else if (newsize < oldsize) { 932 size_t zerolen; 933 934 zerolen = MIN(round_page(newsize), node->tn_size) - newsize; 935 ubc_zerorange(uobj, newsize, zerolen, UBC_VNODE_FLAGS(vp)); 936 } 937 938 node->tn_spec.tn_reg.tn_aobj_pages = newpages; 939 node->tn_size = newsize; 940 uvm_vnp_setsize(vp, newsize); 941 942 /* 943 * Free "backing store". 944 */ 945 if (newpages < oldpages) { 946 rw_enter(uobj->vmobjlock, RW_WRITER); 947 uao_dropswap_range(uobj, newpages, oldpages); 948 rw_exit(uobj->vmobjlock); 949 950 /* Decrease the used-memory counter. */ 951 tmpfs_mem_decr(tmp, (oldpages - newpages) << PAGE_SHIFT); 952 } 953 if (newsize > oldsize) { 954 VN_KNOTE(vp, NOTE_EXTEND); 955 } 956 return 0; 957 } 958 959 /* 960 * tmpfs_chflags: change flags of the given vnode. 961 */ 962 int 963 tmpfs_chflags(vnode_t *vp, int flags, kauth_cred_t cred, lwp_t *l) 964 { 965 tmpfs_node_t *node = VP_TO_TMPFS_NODE(vp); 966 kauth_action_t action = KAUTH_VNODE_WRITE_FLAGS; 967 int error; 968 bool changing_sysflags = false; 969 970 KASSERT(VOP_ISLOCKED(vp)); 971 972 /* Disallow this operation if the file system is mounted read-only. */ 973 if (vp->v_mount->mnt_flag & MNT_RDONLY) 974 return EROFS; 975 976 /* 977 * If the new flags have non-user flags that are different than 978 * those on the node, we need special permission to change them. 979 */ 980 if ((flags & SF_SETTABLE) != (node->tn_flags & SF_SETTABLE)) { 981 action |= KAUTH_VNODE_WRITE_SYSFLAGS; 982 changing_sysflags = true; 983 } 984 985 /* 986 * Indicate that this node's flags have system attributes in them if 987 * that's the case. 988 */ 989 if (node->tn_flags & (SF_IMMUTABLE | SF_APPEND)) { 990 action |= KAUTH_VNODE_HAS_SYSFLAGS; 991 } 992 993 error = kauth_authorize_vnode(cred, action, vp, NULL, 994 genfs_can_chflags(vp, cred, node->tn_uid, changing_sysflags)); 995 if (error) 996 return error; 997 998 /* 999 * Set the flags. If we're not setting non-user flags, be careful not 1000 * to overwrite them. 1001 * 1002 * XXX: Can't we always assign here? if the system flags are different, 1003 * the code above should catch attempts to change them without 1004 * proper permissions, and if we're here it means it's okay to 1005 * change them... 1006 */ 1007 if (!changing_sysflags) { 1008 /* Clear all user-settable flags and re-set them. */ 1009 node->tn_flags &= SF_SETTABLE; 1010 node->tn_flags |= (flags & UF_SETTABLE); 1011 } else { 1012 node->tn_flags = flags; 1013 } 1014 tmpfs_update(vp, TMPFS_UPDATE_CTIME); 1015 VN_KNOTE(vp, NOTE_ATTRIB); 1016 return 0; 1017 } 1018 1019 /* 1020 * tmpfs_chmod: change access mode on the given vnode. 1021 */ 1022 int 1023 tmpfs_chmod(vnode_t *vp, mode_t mode, kauth_cred_t cred, lwp_t *l) 1024 { 1025 tmpfs_node_t *node = VP_TO_TMPFS_NODE(vp); 1026 int error; 1027 1028 KASSERT(VOP_ISLOCKED(vp)); 1029 1030 /* Disallow this operation if the file system is mounted read-only. */ 1031 if (vp->v_mount->mnt_flag & MNT_RDONLY) 1032 return EROFS; 1033 1034 /* Immutable or append-only files cannot be modified, either. */ 1035 if (node->tn_flags & (IMMUTABLE | APPEND)) 1036 return EPERM; 1037 1038 error = kauth_authorize_vnode(cred, KAUTH_VNODE_WRITE_SECURITY, vp, 1039 NULL, genfs_can_chmod(vp, cred, node->tn_uid, node->tn_gid, mode)); 1040 if (error) { 1041 return error; 1042 } 1043 node->tn_mode = (mode & ALLPERMS); 1044 tmpfs_update(vp, TMPFS_UPDATE_CTIME); 1045 VN_KNOTE(vp, NOTE_ATTRIB); 1046 cache_enter_id(vp, node->tn_mode, node->tn_uid, node->tn_gid, true); 1047 return 0; 1048 } 1049 1050 /* 1051 * tmpfs_chown: change ownership of the given vnode. 1052 * 1053 * => At least one of uid or gid must be different than VNOVAL. 1054 * => Attribute is unchanged for VNOVAL case. 1055 */ 1056 int 1057 tmpfs_chown(vnode_t *vp, uid_t uid, gid_t gid, kauth_cred_t cred, lwp_t *l) 1058 { 1059 tmpfs_node_t *node = VP_TO_TMPFS_NODE(vp); 1060 int error; 1061 1062 KASSERT(VOP_ISLOCKED(vp)); 1063 1064 /* Assign default values if they are unknown. */ 1065 KASSERT(uid != VNOVAL || gid != VNOVAL); 1066 if (uid == VNOVAL) { 1067 uid = node->tn_uid; 1068 } 1069 if (gid == VNOVAL) { 1070 gid = node->tn_gid; 1071 } 1072 1073 /* Disallow this operation if the file system is mounted read-only. */ 1074 if (vp->v_mount->mnt_flag & MNT_RDONLY) 1075 return EROFS; 1076 1077 /* Immutable or append-only files cannot be modified, either. */ 1078 if (node->tn_flags & (IMMUTABLE | APPEND)) 1079 return EPERM; 1080 1081 error = kauth_authorize_vnode(cred, KAUTH_VNODE_CHANGE_OWNERSHIP, vp, 1082 NULL, genfs_can_chown(vp, cred, node->tn_uid, node->tn_gid, uid, 1083 gid)); 1084 if (error) { 1085 return error; 1086 } 1087 node->tn_uid = uid; 1088 node->tn_gid = gid; 1089 tmpfs_update(vp, TMPFS_UPDATE_CTIME); 1090 VN_KNOTE(vp, NOTE_ATTRIB); 1091 cache_enter_id(vp, node->tn_mode, node->tn_uid, node->tn_gid, true); 1092 return 0; 1093 } 1094 1095 /* 1096 * tmpfs_chsize: change size of the given vnode. 1097 */ 1098 int 1099 tmpfs_chsize(vnode_t *vp, u_quad_t size, kauth_cred_t cred, lwp_t *l) 1100 { 1101 tmpfs_node_t *node = VP_TO_TMPFS_NODE(vp); 1102 const off_t length = size; 1103 int error; 1104 1105 KASSERT(VOP_ISLOCKED(vp)); 1106 1107 /* Decide whether this is a valid operation based on the file type. */ 1108 switch (vp->v_type) { 1109 case VDIR: 1110 return EISDIR; 1111 case VREG: 1112 if (vp->v_mount->mnt_flag & MNT_RDONLY) { 1113 return EROFS; 1114 } 1115 break; 1116 case VBLK: 1117 case VCHR: 1118 case VFIFO: 1119 /* 1120 * Allow modifications of special files even if in the file 1121 * system is mounted read-only (we are not modifying the 1122 * files themselves, but the objects they represent). 1123 */ 1124 return 0; 1125 default: 1126 return EOPNOTSUPP; 1127 } 1128 1129 /* Immutable or append-only files cannot be modified, either. */ 1130 if (node->tn_flags & (IMMUTABLE | APPEND)) { 1131 return EPERM; 1132 } 1133 1134 if (length < 0) { 1135 return EINVAL; 1136 } 1137 1138 /* Note: tmpfs_reg_resize() will raise NOTE_EXTEND and NOTE_ATTRIB. */ 1139 if (node->tn_size != length && 1140 (error = tmpfs_reg_resize(vp, length)) != 0) { 1141 return error; 1142 } 1143 tmpfs_update(vp, TMPFS_UPDATE_CTIME | TMPFS_UPDATE_MTIME); 1144 return 0; 1145 } 1146 1147 /* 1148 * tmpfs_chtimes: change access and modification times for vnode. 1149 */ 1150 int 1151 tmpfs_chtimes(vnode_t *vp, const struct timespec *atime, 1152 const struct timespec *mtime, const struct timespec *btime, 1153 int vaflags, kauth_cred_t cred, lwp_t *l) 1154 { 1155 tmpfs_node_t *node = VP_TO_TMPFS_NODE(vp); 1156 int error; 1157 1158 KASSERT(VOP_ISLOCKED(vp)); 1159 1160 /* Disallow this operation if the file system is mounted read-only. */ 1161 if (vp->v_mount->mnt_flag & MNT_RDONLY) 1162 return EROFS; 1163 1164 /* Immutable or append-only files cannot be modified, either. */ 1165 if (node->tn_flags & (IMMUTABLE | APPEND)) 1166 return EPERM; 1167 1168 error = kauth_authorize_vnode(cred, KAUTH_VNODE_WRITE_TIMES, vp, NULL, 1169 genfs_can_chtimes(vp, cred, node->tn_uid, vaflags)); 1170 if (error) 1171 return error; 1172 1173 mutex_enter(&node->tn_timelock); 1174 if (atime->tv_sec != VNOVAL) { 1175 atomic_and_uint(&node->tn_tflags, ~TMPFS_UPDATE_ATIME); 1176 node->tn_atime = *atime; 1177 } 1178 if (mtime->tv_sec != VNOVAL) { 1179 atomic_and_uint(&node->tn_tflags, ~TMPFS_UPDATE_MTIME); 1180 node->tn_mtime = *mtime; 1181 } 1182 if (btime->tv_sec != VNOVAL) { 1183 node->tn_birthtime = *btime; 1184 } 1185 mutex_exit(&node->tn_timelock); 1186 VN_KNOTE(vp, NOTE_ATTRIB); 1187 return 0; 1188 } 1189 1190 /* 1191 * tmpfs_update_locked: update the timestamps as indicated by the flags. 1192 */ 1193 void 1194 tmpfs_update_locked(vnode_t *vp, unsigned tflags) 1195 { 1196 tmpfs_node_t *node = VP_TO_TMPFS_NODE(vp); 1197 struct timespec nowtm; 1198 1199 KASSERT(mutex_owned(&node->tn_timelock)); 1200 1201 if ((tflags |= atomic_swap_uint(&node->tn_tflags, 0)) == 0) { 1202 return; 1203 } 1204 vfs_timestamp(&nowtm); 1205 1206 if (tflags & TMPFS_UPDATE_ATIME) { 1207 node->tn_atime = nowtm; 1208 } 1209 if (tflags & TMPFS_UPDATE_MTIME) { 1210 node->tn_mtime = nowtm; 1211 } 1212 if (tflags & TMPFS_UPDATE_CTIME) { 1213 node->tn_ctime = nowtm; 1214 } 1215 } 1216 1217 /* 1218 * tmpfs_update: update the timestamps as indicated by the flags. 1219 */ 1220 void 1221 tmpfs_update(vnode_t *vp, unsigned tflags) 1222 { 1223 tmpfs_node_t *node = VP_TO_TMPFS_NODE(vp); 1224 1225 if ((tflags | atomic_load_relaxed(&node->tn_tflags)) == 0) { 1226 return; 1227 } 1228 1229 mutex_enter(&node->tn_timelock); 1230 tmpfs_update_locked(vp, tflags); 1231 mutex_exit(&node->tn_timelock); 1232 } 1233 1234 /* 1235 * tmpfs_update_lazily: schedule a deferred timestamp update. 1236 */ 1237 void 1238 tmpfs_update_lazily(vnode_t *vp, unsigned tflags) 1239 { 1240 tmpfs_node_t *node = VP_TO_TMPFS_NODE(vp); 1241 unsigned cur; 1242 1243 cur = atomic_load_relaxed(&node->tn_tflags); 1244 if ((cur & tflags) != tflags) { 1245 atomic_or_uint(&node->tn_tflags, tflags); 1246 return; 1247 } 1248 } 1249