1 /* $NetBSD: tmpfs_vfsops.c,v 1.10 2005/12/11 12:24:29 christos Exp $ */ 2 3 /*- 4 * SPDX-License-Identifier: BSD-2-Clause 5 * 6 * Copyright (c) 2005 The NetBSD Foundation, Inc. 7 * All rights reserved. 8 * 9 * This code is derived from software contributed to The NetBSD Foundation 10 * by Julio M. Merino Vidal, developed as part of Google's Summer of Code 11 * 2005 program. 12 * 13 * Redistribution and use in source and binary forms, with or without 14 * modification, are permitted provided that the following conditions 15 * are met: 16 * 1. Redistributions of source code must retain the above copyright 17 * notice, this list of conditions and the following disclaimer. 18 * 2. Redistributions in binary form must reproduce the above copyright 19 * notice, this list of conditions and the following disclaimer in the 20 * documentation and/or other materials provided with the distribution. 21 * 22 * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS 23 * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED 24 * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR 25 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS 26 * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR 27 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF 28 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS 29 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN 30 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) 31 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE 32 * POSSIBILITY OF SUCH DAMAGE. 33 */ 34 35 /* 36 * Efficient memory file system. 37 * 38 * tmpfs is a file system that uses FreeBSD's virtual memory 39 * sub-system to store file data and metadata in an efficient way. 40 * This means that it does not follow the structure of an on-disk file 41 * system because it simply does not need to. Instead, it uses 42 * memory-specific data structures and algorithms to automatically 43 * allocate and release resources. 44 */ 45 46 #include "opt_ddb.h" 47 #include "opt_tmpfs.h" 48 49 #include <sys/param.h> 50 #include <sys/systm.h> 51 #include <sys/dirent.h> 52 #include <sys/file.h> 53 #include <sys/limits.h> 54 #include <sys/lock.h> 55 #include <sys/mount.h> 56 #include <sys/mutex.h> 57 #include <sys/proc.h> 58 #include <sys/jail.h> 59 #include <sys/kernel.h> 60 #include <sys/rwlock.h> 61 #include <sys/stat.h> 62 #include <sys/sx.h> 63 #include <sys/sysctl.h> 64 #include <sys/vnode.h> 65 66 #include <vm/vm.h> 67 #include <vm/vm_param.h> 68 #include <vm/pmap.h> 69 #include <vm/vm_extern.h> 70 #include <vm/vm_map.h> 71 #include <vm/vm_object.h> 72 #include <vm/vm_param.h> 73 74 #include <fs/tmpfs/tmpfs.h> 75 76 /* 77 * Default permission for root node 78 */ 79 #define TMPFS_DEFAULT_ROOT_MODE (S_IRWXU|S_IRGRP|S_IXGRP|S_IROTH|S_IXOTH) 80 81 static MALLOC_DEFINE(M_TMPFSMNT, "tmpfs mount", "tmpfs mount structures"); 82 MALLOC_DEFINE(M_TMPFSNAME, "tmpfs name", "tmpfs file names"); 83 84 static int tmpfs_mount(struct mount *); 85 static int tmpfs_unmount(struct mount *, int); 86 static int tmpfs_root(struct mount *, int flags, struct vnode **); 87 static int tmpfs_fhtovp(struct mount *, struct fid *, int, 88 struct vnode **); 89 static int tmpfs_statfs(struct mount *, struct statfs *); 90 91 static const char *tmpfs_opts[] = { 92 "from", "easize", "size", "maxfilesize", "inodes", "uid", "gid", "mode", 93 "export", "union", "nonc", "nomtime", "nosymfollow", "pgread", NULL 94 }; 95 96 static int 97 tmpfs_update_mtime_lazy_filter(struct vnode *vp, void *arg) 98 { 99 struct vm_object *obj; 100 101 if (vp->v_type != VREG) 102 return (0); 103 104 obj = atomic_load_ptr(&vp->v_object); 105 if (obj == NULL) 106 return (0); 107 108 return (vm_object_mightbedirty_(obj)); 109 } 110 111 static void 112 tmpfs_update_mtime_lazy(struct mount *mp) 113 { 114 struct vnode *vp, *mvp; 115 116 MNT_VNODE_FOREACH_LAZY(vp, mp, mvp, tmpfs_update_mtime_lazy_filter, NULL) { 117 if (vget(vp, LK_EXCLUSIVE | LK_INTERLOCK) != 0) 118 continue; 119 tmpfs_check_mtime(vp); 120 vput(vp); 121 } 122 } 123 124 static void 125 tmpfs_update_mtime_all(struct mount *mp) 126 { 127 struct vnode *vp, *mvp; 128 129 if (VFS_TO_TMPFS(mp)->tm_nomtime) 130 return; 131 MNT_VNODE_FOREACH_ALL(vp, mp, mvp) { 132 if (vp->v_type != VREG) { 133 VI_UNLOCK(vp); 134 continue; 135 } 136 if (vget(vp, LK_EXCLUSIVE | LK_INTERLOCK) != 0) 137 continue; 138 tmpfs_check_mtime(vp); 139 tmpfs_update(vp); 140 vput(vp); 141 } 142 } 143 144 struct tmpfs_check_rw_maps_arg { 145 bool found; 146 }; 147 148 static bool 149 tmpfs_check_rw_maps_cb(struct mount *mp __unused, vm_map_t map __unused, 150 vm_map_entry_t entry __unused, void *arg) 151 { 152 struct tmpfs_check_rw_maps_arg *a; 153 154 a = arg; 155 a->found = true; 156 return (true); 157 } 158 159 /* 160 * Revoke write permissions from all mappings of regular files 161 * belonging to the specified tmpfs mount. 162 */ 163 static bool 164 tmpfs_revoke_rw_maps_cb(struct mount *mp __unused, vm_map_t map, 165 vm_map_entry_t entry, void *arg __unused) 166 { 167 168 /* 169 * XXXKIB: might be invalidate the mapping 170 * instead ? The process is not going to be 171 * happy in any case. 172 */ 173 entry->max_protection &= ~VM_PROT_WRITE; 174 if ((entry->protection & VM_PROT_WRITE) != 0) { 175 entry->protection &= ~VM_PROT_WRITE; 176 pmap_protect(map->pmap, entry->start, entry->end, 177 entry->protection); 178 } 179 return (false); 180 } 181 182 static void 183 tmpfs_all_rw_maps(struct mount *mp, bool (*cb)(struct mount *mp, vm_map_t, 184 vm_map_entry_t, void *), void *cb_arg) 185 { 186 struct proc *p; 187 struct vmspace *vm; 188 vm_map_t map; 189 vm_map_entry_t entry; 190 vm_object_t object; 191 struct vnode *vp; 192 int gen; 193 bool terminate; 194 195 terminate = false; 196 sx_slock(&allproc_lock); 197 again: 198 gen = allproc_gen; 199 FOREACH_PROC_IN_SYSTEM(p) { 200 PROC_LOCK(p); 201 if (p->p_state != PRS_NORMAL || (p->p_flag & (P_INEXEC | 202 P_SYSTEM | P_WEXIT)) != 0) { 203 PROC_UNLOCK(p); 204 continue; 205 } 206 vm = vmspace_acquire_ref(p); 207 _PHOLD(p); 208 PROC_UNLOCK(p); 209 if (vm == NULL) { 210 PRELE(p); 211 continue; 212 } 213 sx_sunlock(&allproc_lock); 214 map = &vm->vm_map; 215 216 vm_map_lock(map); 217 if (map->busy) 218 vm_map_wait_busy(map); 219 VM_MAP_ENTRY_FOREACH(entry, map) { 220 if ((entry->eflags & (MAP_ENTRY_GUARD | 221 MAP_ENTRY_IS_SUB_MAP | MAP_ENTRY_COW)) != 0 || 222 (entry->max_protection & VM_PROT_WRITE) == 0) 223 continue; 224 object = entry->object.vm_object; 225 if (object == NULL || object->type != tmpfs_pager_type) 226 continue; 227 /* 228 * No need to dig into shadow chain, mapping 229 * of the object not at top is readonly. 230 */ 231 232 VM_OBJECT_RLOCK(object); 233 if (object->type == OBJT_DEAD) { 234 VM_OBJECT_RUNLOCK(object); 235 continue; 236 } 237 MPASS(object->ref_count > 1); 238 if ((object->flags & OBJ_TMPFS) == 0) { 239 VM_OBJECT_RUNLOCK(object); 240 continue; 241 } 242 vp = VM_TO_TMPFS_VP(object); 243 if (vp->v_mount != mp) { 244 VM_OBJECT_RUNLOCK(object); 245 continue; 246 } 247 248 terminate = cb(mp, map, entry, cb_arg); 249 VM_OBJECT_RUNLOCK(object); 250 if (terminate) 251 break; 252 } 253 vm_map_unlock(map); 254 255 vmspace_free(vm); 256 sx_slock(&allproc_lock); 257 PRELE(p); 258 if (terminate) 259 break; 260 } 261 if (!terminate && gen != allproc_gen) 262 goto again; 263 sx_sunlock(&allproc_lock); 264 } 265 266 static bool 267 tmpfs_check_rw_maps(struct mount *mp) 268 { 269 struct tmpfs_check_rw_maps_arg ca; 270 271 ca.found = false; 272 tmpfs_all_rw_maps(mp, tmpfs_check_rw_maps_cb, &ca); 273 return (ca.found); 274 } 275 276 static int 277 tmpfs_rw_to_ro(struct mount *mp) 278 { 279 int error, flags; 280 bool forced; 281 282 forced = (mp->mnt_flag & MNT_FORCE) != 0; 283 flags = WRITECLOSE | (forced ? FORCECLOSE : 0); 284 285 if ((error = vn_start_write(NULL, &mp, V_WAIT)) != 0) 286 return (error); 287 error = vfs_write_suspend_umnt(mp); 288 if (error != 0) 289 return (error); 290 if (!forced && tmpfs_check_rw_maps(mp)) { 291 error = EBUSY; 292 goto out; 293 } 294 VFS_TO_TMPFS(mp)->tm_ronly = 1; 295 MNT_ILOCK(mp); 296 mp->mnt_flag |= MNT_RDONLY; 297 MNT_IUNLOCK(mp); 298 for (;;) { 299 tmpfs_all_rw_maps(mp, tmpfs_revoke_rw_maps_cb, NULL); 300 tmpfs_update_mtime_all(mp); 301 error = vflush(mp, 0, flags, curthread); 302 if (error != 0) { 303 VFS_TO_TMPFS(mp)->tm_ronly = 0; 304 MNT_ILOCK(mp); 305 mp->mnt_flag &= ~MNT_RDONLY; 306 MNT_IUNLOCK(mp); 307 goto out; 308 } 309 if (!tmpfs_check_rw_maps(mp)) 310 break; 311 } 312 out: 313 vfs_write_resume(mp, 0); 314 return (error); 315 } 316 317 static int 318 tmpfs_mount(struct mount *mp) 319 { 320 const size_t nodes_per_page = howmany(PAGE_SIZE, 321 sizeof(struct tmpfs_dirent) + sizeof(struct tmpfs_node)); 322 struct tmpfs_mount *tmp; 323 struct tmpfs_node *root; 324 struct vfsoptlist *opts; 325 int error; 326 bool nomtime, nonc, pgread; 327 /* Size counters. */ 328 u_quad_t pages; 329 off_t nodes_max, size_max, maxfilesize, ea_max_size; 330 331 /* Root node attributes. */ 332 uid_t root_uid; 333 gid_t root_gid; 334 mode_t root_mode; 335 336 struct vattr va; 337 338 opts = mp->mnt_optnew; 339 if (vfs_filteropt(opts, tmpfs_opts)) 340 return (EINVAL); 341 342 if (mp->mnt_flag & MNT_UPDATE) { 343 tmp = VFS_TO_TMPFS(mp); 344 345 /* 346 * These options cannot (yet) be modified on the fly, but 347 * mount(8) will still pass them when remounting, so we 348 * will silently ignore them as long as the value is 349 * unchanged. 350 */ 351 if (vfs_scanopt(opts, "gid", "%d", &root_gid) == 1 && 352 root_gid != tmp->tm_root->tn_gid) 353 return (EOPNOTSUPP); 354 if (vfs_scanopt(opts, "uid", "%d", &root_uid) == 1 && 355 root_uid != tmp->tm_root->tn_uid) 356 return (EOPNOTSUPP); 357 if (vfs_scanopt(opts, "mode", "%ho", &root_mode) == 1 && 358 (root_mode & S_IFMT) != (tmp->tm_root->tn_mode & S_IFMT)) 359 return (EOPNOTSUPP); 360 if (vfs_getopt_size(opts, "inodes", &nodes_max) == 0 && 361 nodes_max != 0 && nodes_max != tmp->tm_nodes_max) 362 return (EOPNOTSUPP); 363 if (vfs_getopt_size(opts, "size", &size_max) == 0 && 364 size_max != 0 && size_max != tmp->tm_size_max) 365 return (EOPNOTSUPP); 366 if (vfs_getopt_size(opts, "maxfilesize", &maxfilesize) == 0 && 367 maxfilesize != 0 && maxfilesize != tmp->tm_maxfilesize) 368 return (EOPNOTSUPP); 369 if (tmp->tm_nonc != 370 (vfs_getopt(opts, "nonc", NULL, NULL) == 0)) 371 return (EOPNOTSUPP); 372 if (tmp->tm_pgread != 373 (vfs_getopt(opts, "pgread", NULL, NULL) == 0)) 374 return (EOPNOTSUPP); 375 376 /* 377 * These options can be modified. 378 */ 379 if (vfs_getopt_size(opts, "easize", &ea_max_size) != 0) 380 tmp->tm_ea_memory_max = ea_max_size; 381 tmp->tm_nomtime = (vfs_getopt(opts, "nomtime", NULL, 0) == 0); 382 383 /* 384 * Handle read-write to read-only or vice versa. 385 */ 386 if (vfs_flagopt(opts, "ro", NULL, 0) && !tmp->tm_ronly) { 387 /* RW -> RO */ 388 return (tmpfs_rw_to_ro(mp)); 389 } 390 if (!vfs_flagopt(opts, "ro", NULL, 0) && tmp->tm_ronly) { 391 /* RO -> RW */ 392 tmp->tm_ronly = 0; 393 MNT_ILOCK(mp); 394 mp->mnt_flag &= ~MNT_RDONLY; 395 MNT_IUNLOCK(mp); 396 } 397 398 /* 399 * Check if fast path lookup is still supported. 400 */ 401 MNT_ILOCK(mp); 402 if (!tmp->tm_nonc && (mp->mnt_flag & MNT_UNION) == 0) { 403 mp->mnt_kern_flag |= MNTK_FPLOOKUP; 404 } else { 405 mp->mnt_kern_flag &= ~MNTK_FPLOOKUP; 406 } 407 MNT_IUNLOCK(mp); 408 return (0); 409 } 410 411 vn_lock(mp->mnt_vnodecovered, LK_SHARED | LK_RETRY); 412 error = VOP_GETATTR(mp->mnt_vnodecovered, &va, mp->mnt_cred); 413 VOP_UNLOCK(mp->mnt_vnodecovered); 414 if (error) 415 return (error); 416 417 if (mp->mnt_cred->cr_ruid != 0 || 418 vfs_scanopt(opts, "gid", "%d", &root_gid) != 1) 419 root_gid = va.va_gid; 420 if (mp->mnt_cred->cr_ruid != 0 || 421 vfs_scanopt(opts, "uid", "%d", &root_uid) != 1) 422 root_uid = va.va_uid; 423 if (mp->mnt_cred->cr_ruid != 0 || 424 vfs_scanopt(opts, "mode", "%ho", &root_mode) != 1) 425 root_mode = va.va_mode; 426 if (vfs_getopt_size(opts, "inodes", &nodes_max) != 0) 427 nodes_max = 0; 428 if (vfs_getopt_size(opts, "size", &size_max) != 0) 429 size_max = 0; 430 if (vfs_getopt_size(opts, "maxfilesize", &maxfilesize) != 0) 431 maxfilesize = 0; 432 if (vfs_getopt_size(opts, "easize", &ea_max_size) != 0) 433 ea_max_size = 0; 434 nonc = vfs_getopt(opts, "nonc", NULL, NULL) == 0; 435 nomtime = vfs_getopt(opts, "nomtime", NULL, NULL) == 0; 436 pgread = vfs_getopt(opts, "pgread", NULL, NULL) == 0; 437 438 /* Do not allow mounts if we do not have enough memory to preserve 439 * the minimum reserved pages. */ 440 if (tmpfs_mem_avail() < TMPFS_PAGES_MINRESERVED) 441 return (ENOSPC); 442 443 /* Get the maximum number of memory pages this file system is 444 * allowed to use, based on the maximum size the user passed in 445 * the mount structure. A value of zero is treated as if the 446 * maximum available space was requested. */ 447 if (size_max == 0 || size_max > OFF_MAX - PAGE_SIZE || 448 (SIZE_MAX < OFF_MAX && size_max / PAGE_SIZE >= SIZE_MAX)) 449 pages = SIZE_MAX; 450 else { 451 size_max = roundup(size_max, PAGE_SIZE); 452 pages = howmany(size_max, PAGE_SIZE); 453 } 454 MPASS(pages > 0); 455 456 if (nodes_max <= 3) { 457 if (pages < INT_MAX / nodes_per_page) 458 nodes_max = pages * nodes_per_page; 459 else 460 nodes_max = INT_MAX; 461 } 462 if (nodes_max > INT_MAX) 463 nodes_max = INT_MAX; 464 MPASS(nodes_max >= 3); 465 466 /* Allocate the tmpfs mount structure and fill it. */ 467 tmp = (struct tmpfs_mount *)malloc(sizeof(struct tmpfs_mount), 468 M_TMPFSMNT, M_WAITOK | M_ZERO); 469 470 mtx_init(&tmp->tm_allnode_lock, "tmpfs allnode lock", NULL, MTX_DEF); 471 tmp->tm_nodes_max = nodes_max; 472 tmp->tm_nodes_inuse = 0; 473 tmp->tm_ea_memory_inuse = 0; 474 tmp->tm_refcount = 1; 475 tmp->tm_maxfilesize = maxfilesize > 0 ? maxfilesize : OFF_MAX; 476 tmp->tm_ea_memory_max = ea_max_size > 0 ? 477 ea_max_size : TMPFS_EA_MEMORY_RESERVED; 478 LIST_INIT(&tmp->tm_nodes_used); 479 480 tmp->tm_size_max = size_max; 481 tmp->tm_pages_max = pages; 482 tmp->tm_pages_used = 0; 483 new_unrhdr64(&tmp->tm_ino_unr, 2); 484 tmp->tm_ronly = (mp->mnt_flag & MNT_RDONLY) != 0; 485 tmp->tm_nonc = nonc; 486 tmp->tm_nomtime = nomtime; 487 tmp->tm_pgread = pgread; 488 489 /* Allocate the root node. */ 490 error = tmpfs_alloc_node(mp, tmp, VDIR, root_uid, root_gid, 491 root_mode & ALLPERMS, NULL, NULL, VNOVAL, &root); 492 493 if (error != 0 || root == NULL) { 494 free(tmp, M_TMPFSMNT); 495 return (error); 496 } 497 KASSERT(root->tn_id == 2, 498 ("tmpfs root with invalid ino: %ju", (uintmax_t)root->tn_id)); 499 tmp->tm_root = root; 500 501 MNT_ILOCK(mp); 502 mp->mnt_flag |= MNT_LOCAL; 503 mp->mnt_kern_flag |= MNTK_LOOKUP_SHARED | MNTK_EXTENDED_SHARED | 504 MNTK_NOMSYNC; 505 if (!nonc && (mp->mnt_flag & MNT_UNION) == 0) 506 mp->mnt_kern_flag |= MNTK_FPLOOKUP; 507 MNT_IUNLOCK(mp); 508 509 mp->mnt_data = tmp; 510 mp->mnt_stat.f_namemax = MAXNAMLEN; 511 vfs_getnewfsid(mp); 512 vfs_mountedfrom(mp, "tmpfs"); 513 514 return (0); 515 } 516 517 /* ARGSUSED2 */ 518 static int 519 tmpfs_unmount(struct mount *mp, int mntflags) 520 { 521 struct tmpfs_mount *tmp; 522 struct tmpfs_node *node; 523 int error, flags; 524 525 flags = (mntflags & MNT_FORCE) != 0 ? FORCECLOSE : 0; 526 tmp = VFS_TO_TMPFS(mp); 527 528 /* Stop writers */ 529 error = vfs_write_suspend_umnt(mp); 530 if (error != 0) 531 return (error); 532 /* 533 * At this point, nodes cannot be destroyed by any other 534 * thread because write suspension is started. 535 */ 536 537 for (;;) { 538 error = vflush(mp, 0, flags, curthread); 539 if (error != 0) { 540 vfs_write_resume(mp, VR_START_WRITE); 541 return (error); 542 } 543 MNT_ILOCK(mp); 544 if (mp->mnt_nvnodelistsize == 0) { 545 MNT_IUNLOCK(mp); 546 break; 547 } 548 MNT_IUNLOCK(mp); 549 if ((mntflags & MNT_FORCE) == 0) { 550 vfs_write_resume(mp, VR_START_WRITE); 551 return (EBUSY); 552 } 553 } 554 555 TMPFS_LOCK(tmp); 556 while ((node = LIST_FIRST(&tmp->tm_nodes_used)) != NULL) { 557 TMPFS_NODE_LOCK(node); 558 if (node->tn_type == VDIR) 559 tmpfs_dir_destroy(tmp, node); 560 if (tmpfs_free_node_locked(tmp, node, true)) 561 TMPFS_LOCK(tmp); 562 else 563 TMPFS_NODE_UNLOCK(node); 564 } 565 566 mp->mnt_data = NULL; 567 tmpfs_free_tmp(tmp); 568 vfs_write_resume(mp, VR_START_WRITE); 569 570 return (0); 571 } 572 573 void 574 tmpfs_free_tmp(struct tmpfs_mount *tmp) 575 { 576 TMPFS_MP_ASSERT_LOCKED(tmp); 577 MPASS(tmp->tm_refcount > 0); 578 579 tmp->tm_refcount--; 580 if (tmp->tm_refcount > 0) { 581 TMPFS_UNLOCK(tmp); 582 return; 583 } 584 TMPFS_UNLOCK(tmp); 585 586 mtx_destroy(&tmp->tm_allnode_lock); 587 /* 588 * We cannot assert that tmp->tm_pages_used == 0 there, 589 * because tmpfs vm_objects might be still mapped by some 590 * process and outlive the mount due to reference counting. 591 */ 592 MPASS(tmp->tm_nodes_inuse == 0); 593 594 free(tmp, M_TMPFSMNT); 595 } 596 597 static int 598 tmpfs_root(struct mount *mp, int flags, struct vnode **vpp) 599 { 600 int error; 601 602 error = tmpfs_alloc_vp(mp, VFS_TO_TMPFS(mp)->tm_root, flags, vpp); 603 if (error == 0) 604 (*vpp)->v_vflag |= VV_ROOT; 605 return (error); 606 } 607 608 static int 609 tmpfs_fhtovp(struct mount *mp, struct fid *fhp, int flags, 610 struct vnode **vpp) 611 { 612 struct tmpfs_fid_data *tfd; 613 struct tmpfs_mount *tmp; 614 struct tmpfs_node *node; 615 int error; 616 617 if (fhp->fid_len != sizeof(*tfd)) 618 return (EINVAL); 619 620 tfd = (struct tmpfs_fid_data *)fhp; 621 622 tmp = VFS_TO_TMPFS(mp); 623 624 if (tfd->tfd_id >= tmp->tm_nodes_max) 625 return (EINVAL); 626 627 TMPFS_LOCK(tmp); 628 LIST_FOREACH(node, &tmp->tm_nodes_used, tn_entries) { 629 if (node->tn_id == tfd->tfd_id && 630 node->tn_gen == tfd->tfd_gen) { 631 tmpfs_ref_node(node); 632 break; 633 } 634 } 635 TMPFS_UNLOCK(tmp); 636 637 if (node != NULL) { 638 error = tmpfs_alloc_vp(mp, node, LK_EXCLUSIVE, vpp); 639 tmpfs_free_node(tmp, node); 640 } else 641 error = EINVAL; 642 return (error); 643 } 644 645 /* ARGSUSED2 */ 646 static int 647 tmpfs_statfs(struct mount *mp, struct statfs *sbp) 648 { 649 struct tmpfs_mount *tmp; 650 size_t used; 651 652 tmp = VFS_TO_TMPFS(mp); 653 654 sbp->f_iosize = PAGE_SIZE; 655 sbp->f_bsize = PAGE_SIZE; 656 657 used = tmpfs_pages_used(tmp); 658 if (tmp->tm_pages_max != ULONG_MAX) 659 sbp->f_blocks = tmp->tm_pages_max; 660 else 661 sbp->f_blocks = used + tmpfs_mem_avail(); 662 if (sbp->f_blocks <= used) 663 sbp->f_bavail = 0; 664 else 665 sbp->f_bavail = sbp->f_blocks - used; 666 sbp->f_bfree = sbp->f_bavail; 667 used = tmp->tm_nodes_inuse; 668 sbp->f_files = tmp->tm_nodes_max; 669 if (sbp->f_files <= used) 670 sbp->f_ffree = 0; 671 else 672 sbp->f_ffree = sbp->f_files - used; 673 /* sbp->f_owner = tmp->tn_uid; */ 674 675 return (0); 676 } 677 678 static int 679 tmpfs_sync(struct mount *mp, int waitfor) 680 { 681 682 if (waitfor == MNT_SUSPEND) { 683 MNT_ILOCK(mp); 684 mp->mnt_kern_flag |= MNTK_SUSPEND2 | MNTK_SUSPENDED; 685 MNT_IUNLOCK(mp); 686 } else if (waitfor == MNT_LAZY) { 687 tmpfs_update_mtime_lazy(mp); 688 } 689 return (0); 690 } 691 692 static int 693 tmpfs_init(struct vfsconf *conf) 694 { 695 int res; 696 697 res = tmpfs_subr_init(); 698 if (res != 0) 699 return (res); 700 memcpy(&tmpfs_fnops, &vnops, sizeof(struct fileops)); 701 tmpfs_fnops.fo_close = tmpfs_fo_close; 702 return (0); 703 } 704 705 static int 706 tmpfs_uninit(struct vfsconf *conf) 707 { 708 tmpfs_subr_uninit(); 709 return (0); 710 } 711 712 /* 713 * tmpfs vfs operations. 714 */ 715 struct vfsops tmpfs_vfsops = { 716 .vfs_mount = tmpfs_mount, 717 .vfs_unmount = tmpfs_unmount, 718 .vfs_root = vfs_cache_root, 719 .vfs_cachedroot = tmpfs_root, 720 .vfs_statfs = tmpfs_statfs, 721 .vfs_fhtovp = tmpfs_fhtovp, 722 .vfs_sync = tmpfs_sync, 723 .vfs_init = tmpfs_init, 724 .vfs_uninit = tmpfs_uninit, 725 }; 726 VFS_SET(tmpfs_vfsops, tmpfs, VFCF_JAIL); 727 728 #ifdef DDB 729 #include <ddb/ddb.h> 730 731 static void 732 db_print_tmpfs(struct mount *mp, struct tmpfs_mount *tmp) 733 { 734 db_printf("mp %p (%s) tmp %p\n", mp, 735 mp->mnt_stat.f_mntonname, tmp); 736 db_printf( 737 "\tsize max %ju pages max %lu pages used %lu\n" 738 "\tinodes max %ju inodes inuse %ju ea inuse %ju refcount %ju\n" 739 "\tmaxfilesize %ju r%c %snamecache %smtime\n", 740 (uintmax_t)tmp->tm_size_max, tmp->tm_pages_max, tmp->tm_pages_used, 741 (uintmax_t)tmp->tm_nodes_max, (uintmax_t)tmp->tm_nodes_inuse, 742 (uintmax_t)tmp->tm_ea_memory_inuse, (uintmax_t)tmp->tm_refcount, 743 (uintmax_t)tmp->tm_maxfilesize, 744 tmp->tm_ronly ? 'o' : 'w', tmp->tm_nonc ? "no" : "", 745 tmp->tm_nomtime ? "no" : ""); 746 } 747 748 DB_SHOW_COMMAND(tmpfs, db_show_tmpfs) 749 { 750 struct mount *mp; 751 struct tmpfs_mount *tmp; 752 753 if (have_addr) { 754 mp = (struct mount *)addr; 755 tmp = VFS_TO_TMPFS(mp); 756 db_print_tmpfs(mp, tmp); 757 return; 758 } 759 760 TAILQ_FOREACH(mp, &mountlist, mnt_list) { 761 if (strcmp(mp->mnt_stat.f_fstypename, tmpfs_vfsconf.vfc_name) == 762 0) { 763 tmp = VFS_TO_TMPFS(mp); 764 db_print_tmpfs(mp, tmp); 765 } 766 } 767 } 768 #endif /* DDB */ 769