1 /* $NetBSD: tmpfs_vfsops.c,v 1.10 2005/12/11 12:24:29 christos Exp $ */
4 * SPDX-License-Identifier: BSD-2-Clause-NetBSD
6 * Copyright (c) 2005 The NetBSD Foundation, Inc.
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
13 * Redistribution and use in source and binary forms, with or without
14 * modification, are permitted provided that the following conditions
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.
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.
36 * Efficient memory file system.
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.
46 #include "opt_tmpfs.h"
48 #include <sys/cdefs.h>
49 __FBSDID("$FreeBSD$");
51 #include <sys/param.h>
52 #include <sys/systm.h>
53 #include <sys/dirent.h>
55 #include <sys/limits.h>
57 #include <sys/mount.h>
58 #include <sys/mutex.h>
61 #include <sys/kernel.h>
62 #include <sys/rwlock.h>
65 #include <sys/sysctl.h>
66 #include <sys/vnode.h>
69 #include <vm/vm_param.h>
71 #include <vm/vm_extern.h>
72 #include <vm/vm_map.h>
73 #include <vm/vm_object.h>
74 #include <vm/vm_param.h>
76 #include <fs/tmpfs/tmpfs.h>
79 * Default permission for root node
81 #define TMPFS_DEFAULT_ROOT_MODE (S_IRWXU|S_IRGRP|S_IXGRP|S_IROTH|S_IXOTH)
83 static MALLOC_DEFINE(M_TMPFSMNT, "tmpfs mount", "tmpfs mount structures");
84 MALLOC_DEFINE(M_TMPFSNAME, "tmpfs name", "tmpfs file names");
86 static int tmpfs_mount(struct mount *);
87 static int tmpfs_unmount(struct mount *, int);
88 static int tmpfs_root(struct mount *, int flags, struct vnode **);
89 static int tmpfs_fhtovp(struct mount *, struct fid *, int,
91 static int tmpfs_statfs(struct mount *, struct statfs *);
93 static const char *tmpfs_opts[] = {
94 "from", "size", "maxfilesize", "inodes", "uid", "gid", "mode", "export",
95 "union", "nonc", "nomtime", NULL
98 static const char *tmpfs_updateopts[] = {
99 "from", "export", "nomtime", "size", NULL
103 tmpfs_update_mtime_lazy_filter(struct vnode *vp, void *arg)
105 struct vm_object *obj;
107 if (vp->v_type != VREG)
110 obj = atomic_load_ptr(&vp->v_object);
114 return (vm_object_mightbedirty_(obj));
118 tmpfs_update_mtime_lazy(struct mount *mp)
120 struct vnode *vp, *mvp;
122 MNT_VNODE_FOREACH_LAZY(vp, mp, mvp, tmpfs_update_mtime_lazy_filter, NULL) {
123 if (vget(vp, LK_EXCLUSIVE | LK_INTERLOCK) != 0)
125 tmpfs_check_mtime(vp);
131 tmpfs_update_mtime_all(struct mount *mp)
133 struct vnode *vp, *mvp;
135 if (VFS_TO_TMPFS(mp)->tm_nomtime)
137 MNT_VNODE_FOREACH_ALL(vp, mp, mvp) {
138 if (vp->v_type != VREG) {
142 if (vget(vp, LK_EXCLUSIVE | LK_INTERLOCK) != 0)
144 tmpfs_check_mtime(vp);
150 struct tmpfs_check_rw_maps_arg {
155 tmpfs_check_rw_maps_cb(struct mount *mp __unused, vm_map_t map __unused,
156 vm_map_entry_t entry __unused, void *arg)
158 struct tmpfs_check_rw_maps_arg *a;
166 * Revoke write permissions from all mappings of regular files
167 * belonging to the specified tmpfs mount.
170 tmpfs_revoke_rw_maps_cb(struct mount *mp __unused, vm_map_t map,
171 vm_map_entry_t entry, void *arg __unused)
175 * XXXKIB: might be invalidate the mapping
176 * instead ? The process is not going to be
179 entry->max_protection &= ~VM_PROT_WRITE;
180 if ((entry->protection & VM_PROT_WRITE) != 0) {
181 entry->protection &= ~VM_PROT_WRITE;
182 pmap_protect(map->pmap, entry->start, entry->end,
189 tmpfs_all_rw_maps(struct mount *mp, bool (*cb)(struct mount *mp, vm_map_t,
190 vm_map_entry_t, void *), void *cb_arg)
195 vm_map_entry_t entry;
202 sx_slock(&allproc_lock);
205 FOREACH_PROC_IN_SYSTEM(p) {
207 if (p->p_state != PRS_NORMAL || (p->p_flag & (P_INEXEC |
208 P_SYSTEM | P_WEXIT)) != 0) {
212 vm = vmspace_acquire_ref(p);
219 sx_sunlock(&allproc_lock);
224 vm_map_wait_busy(map);
225 VM_MAP_ENTRY_FOREACH(entry, map) {
226 if ((entry->eflags & (MAP_ENTRY_GUARD |
227 MAP_ENTRY_IS_SUB_MAP | MAP_ENTRY_COW)) != 0 ||
228 (entry->max_protection & VM_PROT_WRITE) == 0)
230 object = entry->object.vm_object;
231 if (object == NULL || object->type != tmpfs_pager_type)
234 * No need to dig into shadow chain, mapping
235 * of the object not at top is readonly.
238 VM_OBJECT_RLOCK(object);
239 if (object->type == OBJT_DEAD) {
240 VM_OBJECT_RUNLOCK(object);
243 MPASS(object->ref_count > 1);
244 if ((object->flags & OBJ_TMPFS) == 0) {
245 VM_OBJECT_RUNLOCK(object);
248 vp = object->un_pager.swp.swp_tmpfs;
249 if (vp->v_mount != mp) {
250 VM_OBJECT_RUNLOCK(object);
254 terminate = cb(mp, map, entry, cb_arg);
255 VM_OBJECT_RUNLOCK(object);
262 sx_slock(&allproc_lock);
267 if (!terminate && gen != allproc_gen)
269 sx_sunlock(&allproc_lock);
273 tmpfs_check_rw_maps(struct mount *mp)
275 struct tmpfs_check_rw_maps_arg ca;
278 tmpfs_all_rw_maps(mp, tmpfs_check_rw_maps_cb, &ca);
283 tmpfs_rw_to_ro(struct mount *mp)
288 forced = (mp->mnt_flag & MNT_FORCE) != 0;
289 flags = WRITECLOSE | (forced ? FORCECLOSE : 0);
291 if ((error = vn_start_write(NULL, &mp, V_WAIT)) != 0)
293 error = vfs_write_suspend_umnt(mp);
296 if (!forced && tmpfs_check_rw_maps(mp)) {
300 VFS_TO_TMPFS(mp)->tm_ronly = 1;
302 mp->mnt_flag |= MNT_RDONLY;
305 tmpfs_all_rw_maps(mp, tmpfs_revoke_rw_maps_cb, NULL);
306 tmpfs_update_mtime_all(mp);
307 error = vflush(mp, 0, flags, curthread);
309 VFS_TO_TMPFS(mp)->tm_ronly = 0;
311 mp->mnt_flag &= ~MNT_RDONLY;
315 if (!tmpfs_check_rw_maps(mp))
319 vfs_write_resume(mp, 0);
324 tmpfs_mount(struct mount *mp)
326 const size_t nodes_per_page = howmany(PAGE_SIZE,
327 sizeof(struct tmpfs_dirent) + sizeof(struct tmpfs_node));
328 struct tmpfs_mount *tmp;
329 struct tmpfs_node *root;
334 off_t nodes_max, size_max, maxfilesize;
336 /* Root node attributes. */
343 if (vfs_filteropt(mp->mnt_optnew, tmpfs_opts))
346 if (mp->mnt_flag & MNT_UPDATE) {
347 /* Only support update mounts for certain options. */
348 if (vfs_filteropt(mp->mnt_optnew, tmpfs_updateopts) != 0)
350 tmp = VFS_TO_TMPFS(mp);
351 if (vfs_getopt_size(mp->mnt_optnew, "size", &size_max) == 0) {
353 * On-the-fly resizing is not supported (yet). We still
354 * need to have "size" listed as "supported", otherwise
355 * trying to update fs that is listed in fstab with size
356 * parameter, say trying to change rw to ro or vice
357 * versa, would cause vfs_filteropt() to bail.
359 if (size_max != tmp->tm_size_max)
362 if (vfs_flagopt(mp->mnt_optnew, "ro", NULL, 0) &&
365 return (tmpfs_rw_to_ro(mp));
366 } else if (!vfs_flagopt(mp->mnt_optnew, "ro", NULL, 0) &&
371 mp->mnt_flag &= ~MNT_RDONLY;
374 tmp->tm_nomtime = vfs_getopt(mp->mnt_optnew, "nomtime", NULL,
377 if ((mp->mnt_flag & MNT_UNION) == 0) {
378 mp->mnt_kern_flag |= MNTK_FPLOOKUP;
380 mp->mnt_kern_flag &= ~MNTK_FPLOOKUP;
386 vn_lock(mp->mnt_vnodecovered, LK_SHARED | LK_RETRY);
387 error = VOP_GETATTR(mp->mnt_vnodecovered, &va, mp->mnt_cred);
388 VOP_UNLOCK(mp->mnt_vnodecovered);
392 if (mp->mnt_cred->cr_ruid != 0 ||
393 vfs_scanopt(mp->mnt_optnew, "gid", "%d", &root_gid) != 1)
394 root_gid = va.va_gid;
395 if (mp->mnt_cred->cr_ruid != 0 ||
396 vfs_scanopt(mp->mnt_optnew, "uid", "%d", &root_uid) != 1)
397 root_uid = va.va_uid;
398 if (mp->mnt_cred->cr_ruid != 0 ||
399 vfs_scanopt(mp->mnt_optnew, "mode", "%ho", &root_mode) != 1)
400 root_mode = va.va_mode;
401 if (vfs_getopt_size(mp->mnt_optnew, "inodes", &nodes_max) != 0)
403 if (vfs_getopt_size(mp->mnt_optnew, "size", &size_max) != 0)
405 if (vfs_getopt_size(mp->mnt_optnew, "maxfilesize", &maxfilesize) != 0)
407 nonc = vfs_getopt(mp->mnt_optnew, "nonc", NULL, NULL) == 0;
408 nomtime = vfs_getopt(mp->mnt_optnew, "nomtime", NULL, NULL) == 0;
410 /* Do not allow mounts if we do not have enough memory to preserve
411 * the minimum reserved pages. */
412 if (tmpfs_mem_avail() < TMPFS_PAGES_MINRESERVED)
415 /* Get the maximum number of memory pages this file system is
416 * allowed to use, based on the maximum size the user passed in
417 * the mount structure. A value of zero is treated as if the
418 * maximum available space was requested. */
419 if (size_max == 0 || size_max > OFF_MAX - PAGE_SIZE ||
420 (SIZE_MAX < OFF_MAX && size_max / PAGE_SIZE >= SIZE_MAX))
423 size_max = roundup(size_max, PAGE_SIZE);
424 pages = howmany(size_max, PAGE_SIZE);
428 if (nodes_max <= 3) {
429 if (pages < INT_MAX / nodes_per_page)
430 nodes_max = pages * nodes_per_page;
434 if (nodes_max > INT_MAX)
436 MPASS(nodes_max >= 3);
438 /* Allocate the tmpfs mount structure and fill it. */
439 tmp = (struct tmpfs_mount *)malloc(sizeof(struct tmpfs_mount),
440 M_TMPFSMNT, M_WAITOK | M_ZERO);
442 mtx_init(&tmp->tm_allnode_lock, "tmpfs allnode lock", NULL, MTX_DEF);
443 tmp->tm_nodes_max = nodes_max;
444 tmp->tm_nodes_inuse = 0;
445 tmp->tm_refcount = 1;
446 tmp->tm_maxfilesize = maxfilesize > 0 ? maxfilesize : OFF_MAX;
447 LIST_INIT(&tmp->tm_nodes_used);
449 tmp->tm_size_max = size_max;
450 tmp->tm_pages_max = pages;
451 tmp->tm_pages_used = 0;
452 new_unrhdr64(&tmp->tm_ino_unr, 2);
453 tmp->tm_ronly = (mp->mnt_flag & MNT_RDONLY) != 0;
455 tmp->tm_nomtime = nomtime;
457 /* Allocate the root node. */
458 error = tmpfs_alloc_node(mp, tmp, VDIR, root_uid, root_gid,
459 root_mode & ALLPERMS, NULL, NULL, VNOVAL, &root);
461 if (error != 0 || root == NULL) {
462 free(tmp, M_TMPFSMNT);
465 KASSERT(root->tn_id == 2,
466 ("tmpfs root with invalid ino: %ju", (uintmax_t)root->tn_id));
470 mp->mnt_flag |= MNT_LOCAL;
471 mp->mnt_kern_flag |= MNTK_LOOKUP_SHARED | MNTK_EXTENDED_SHARED |
472 MNTK_TEXT_REFS | MNTK_NOMSYNC;
473 if (!nonc && (mp->mnt_flag & MNT_UNION) == 0)
474 mp->mnt_kern_flag |= MNTK_FPLOOKUP;
478 mp->mnt_stat.f_namemax = MAXNAMLEN;
480 vfs_mountedfrom(mp, "tmpfs");
487 tmpfs_unmount(struct mount *mp, int mntflags)
489 struct tmpfs_mount *tmp;
490 struct tmpfs_node *node;
493 flags = (mntflags & MNT_FORCE) != 0 ? FORCECLOSE : 0;
494 tmp = VFS_TO_TMPFS(mp);
497 error = vfs_write_suspend_umnt(mp);
501 * At this point, nodes cannot be destroyed by any other
502 * thread because write suspension is started.
506 error = vflush(mp, 0, flags, curthread);
508 vfs_write_resume(mp, VR_START_WRITE);
512 if (mp->mnt_nvnodelistsize == 0) {
517 if ((mntflags & MNT_FORCE) == 0) {
518 vfs_write_resume(mp, VR_START_WRITE);
524 while ((node = LIST_FIRST(&tmp->tm_nodes_used)) != NULL) {
525 TMPFS_NODE_LOCK(node);
526 if (node->tn_type == VDIR)
527 tmpfs_dir_destroy(tmp, node);
528 if (tmpfs_free_node_locked(tmp, node, true))
531 TMPFS_NODE_UNLOCK(node);
536 vfs_write_resume(mp, VR_START_WRITE);
539 mp->mnt_flag &= ~MNT_LOCAL;
546 tmpfs_free_tmp(struct tmpfs_mount *tmp)
548 TMPFS_MP_ASSERT_LOCKED(tmp);
549 MPASS(tmp->tm_refcount > 0);
552 if (tmp->tm_refcount > 0) {
558 mtx_destroy(&tmp->tm_allnode_lock);
559 MPASS(tmp->tm_pages_used == 0);
560 MPASS(tmp->tm_nodes_inuse == 0);
562 free(tmp, M_TMPFSMNT);
566 tmpfs_root(struct mount *mp, int flags, struct vnode **vpp)
570 error = tmpfs_alloc_vp(mp, VFS_TO_TMPFS(mp)->tm_root, flags, vpp);
572 (*vpp)->v_vflag |= VV_ROOT;
577 tmpfs_fhtovp(struct mount *mp, struct fid *fhp, int flags,
580 struct tmpfs_fid_data tfd;
581 struct tmpfs_mount *tmp;
582 struct tmpfs_node *node;
585 if (fhp->fid_len != sizeof(tfd))
589 * Copy from fid_data onto the stack to avoid unaligned pointer use.
590 * See the comment in sys/mount.h on struct fid for details.
592 memcpy(&tfd, fhp->fid_data, fhp->fid_len);
594 tmp = VFS_TO_TMPFS(mp);
596 if (tfd.tfd_id >= tmp->tm_nodes_max)
600 LIST_FOREACH(node, &tmp->tm_nodes_used, tn_entries) {
601 if (node->tn_id == tfd.tfd_id &&
602 node->tn_gen == tfd.tfd_gen) {
603 tmpfs_ref_node(node);
610 error = tmpfs_alloc_vp(mp, node, LK_EXCLUSIVE, vpp);
611 tmpfs_free_node(tmp, node);
619 tmpfs_statfs(struct mount *mp, struct statfs *sbp)
621 struct tmpfs_mount *tmp;
624 tmp = VFS_TO_TMPFS(mp);
626 sbp->f_iosize = PAGE_SIZE;
627 sbp->f_bsize = PAGE_SIZE;
629 used = tmpfs_pages_used(tmp);
630 if (tmp->tm_pages_max != ULONG_MAX)
631 sbp->f_blocks = tmp->tm_pages_max;
633 sbp->f_blocks = used + tmpfs_mem_avail();
634 if (sbp->f_blocks <= used)
637 sbp->f_bavail = sbp->f_blocks - used;
638 sbp->f_bfree = sbp->f_bavail;
639 used = tmp->tm_nodes_inuse;
640 sbp->f_files = tmp->tm_nodes_max;
641 if (sbp->f_files <= used)
644 sbp->f_ffree = sbp->f_files - used;
645 /* sbp->f_owner = tmp->tn_uid; */
651 tmpfs_sync(struct mount *mp, int waitfor)
654 if (waitfor == MNT_SUSPEND) {
656 mp->mnt_kern_flag |= MNTK_SUSPEND2 | MNTK_SUSPENDED;
658 } else if (waitfor == MNT_LAZY) {
659 tmpfs_update_mtime_lazy(mp);
665 tmpfs_init(struct vfsconf *conf)
669 res = tmpfs_subr_init();
672 memcpy(&tmpfs_fnops, &vnops, sizeof(struct fileops));
673 tmpfs_fnops.fo_close = tmpfs_fo_close;
678 tmpfs_uninit(struct vfsconf *conf)
685 * tmpfs vfs operations.
687 struct vfsops tmpfs_vfsops = {
688 .vfs_mount = tmpfs_mount,
689 .vfs_unmount = tmpfs_unmount,
690 .vfs_root = vfs_cache_root,
691 .vfs_cachedroot = tmpfs_root,
692 .vfs_statfs = tmpfs_statfs,
693 .vfs_fhtovp = tmpfs_fhtovp,
694 .vfs_sync = tmpfs_sync,
695 .vfs_init = tmpfs_init,
696 .vfs_uninit = tmpfs_uninit,
698 VFS_SET(tmpfs_vfsops, tmpfs, VFCF_JAIL);