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