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