1 /* $NetBSD: tmpfs_vfsops.c,v 1.10 2005/12/11 12:24:29 christos Exp $ */
4 * Copyright (c) 2005 The NetBSD Foundation, Inc.
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
11 * Redistribution and use in source and binary forms, with or without
12 * modification, are permitted provided that the following conditions
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.
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.
34 * Efficient memory file system.
36 * tmpfs is a file system that uses NetBSD's virtual memory sub-system
37 * (the well-known UVM) to store file data and metadata in an efficient
38 * way. This means that it does not follow the structure of an on-disk
39 * file system because it simply does not need to. Instead, it uses
40 * memory-specific data structures and algorithms to automatically
41 * allocate and release resources.
43 #include <sys/cdefs.h>
44 __FBSDID("$FreeBSD$");
46 #include <sys/param.h>
47 #include <sys/limits.h>
49 #include <sys/mutex.h>
52 #include <sys/kernel.h>
54 #include <sys/systm.h>
55 #include <sys/sysctl.h>
58 #include <vm/vm_object.h>
59 #include <vm/vm_param.h>
61 #include <fs/tmpfs/tmpfs.h>
64 * Default permission for root node
66 #define TMPFS_DEFAULT_ROOT_MODE (S_IRWXU|S_IRGRP|S_IXGRP|S_IROTH|S_IXOTH)
68 MALLOC_DEFINE(M_TMPFSMNT, "tmpfs mount", "tmpfs mount structures");
69 MALLOC_DEFINE(M_TMPFSNAME, "tmpfs name", "tmpfs file names");
71 static int tmpfs_mount(struct mount *);
72 static int tmpfs_unmount(struct mount *, int);
73 static int tmpfs_root(struct mount *, int flags, struct vnode **);
74 static int tmpfs_fhtovp(struct mount *, struct fid *, int,
76 static int tmpfs_statfs(struct mount *, struct statfs *);
78 static const char *tmpfs_opts[] = {
79 "from", "size", "maxfilesize", "inodes", "uid", "gid", "mode", "export",
83 static const char *tmpfs_updateopts[] = {
84 "from", "export", NULL
88 tmpfs_node_ctor(void *mem, int size, void *arg, int flags)
90 struct tmpfs_node *node = (struct tmpfs_node *)mem;
97 node->tn_vnode = NULL;
104 tmpfs_node_dtor(void *mem, int size, void *arg)
106 struct tmpfs_node *node = (struct tmpfs_node *)mem;
107 node->tn_type = VNON;
111 tmpfs_node_init(void *mem, int size, int flags)
113 struct tmpfs_node *node = (struct tmpfs_node *)mem;
116 mtx_init(&node->tn_interlock, "tmpfs node interlock", NULL, MTX_DEF);
117 node->tn_gen = arc4random();
123 tmpfs_node_fini(void *mem, int size)
125 struct tmpfs_node *node = (struct tmpfs_node *)mem;
127 mtx_destroy(&node->tn_interlock);
131 tmpfs_mount(struct mount *mp)
133 const size_t nodes_per_page = howmany(PAGE_SIZE,
134 sizeof(struct tmpfs_dirent) + sizeof(struct tmpfs_node));
135 struct tmpfs_mount *tmp;
136 struct tmpfs_node *root;
137 struct thread *td = curthread;
141 off_t nodes_max, size_max, maxfilesize;
143 /* Root node attributes. */
150 if (!prison_allow(td->td_ucred, PR_ALLOW_MOUNT_TMPFS))
153 if (vfs_filteropt(mp->mnt_optnew, tmpfs_opts))
156 if (mp->mnt_flag & MNT_UPDATE) {
157 /* Only support update mounts for certain options. */
158 if (vfs_filteropt(mp->mnt_optnew, tmpfs_updateopts) != 0)
160 if (vfs_flagopt(mp->mnt_optnew, "ro", NULL, 0) !=
161 ((struct tmpfs_mount *)mp->mnt_data)->tm_ronly)
166 vn_lock(mp->mnt_vnodecovered, LK_SHARED | LK_RETRY);
167 error = VOP_GETATTR(mp->mnt_vnodecovered, &va, mp->mnt_cred);
168 VOP_UNLOCK(mp->mnt_vnodecovered, 0);
172 if (mp->mnt_cred->cr_ruid != 0 ||
173 vfs_scanopt(mp->mnt_optnew, "gid", "%d", &root_gid) != 1)
174 root_gid = va.va_gid;
175 if (mp->mnt_cred->cr_ruid != 0 ||
176 vfs_scanopt(mp->mnt_optnew, "uid", "%d", &root_uid) != 1)
177 root_uid = va.va_uid;
178 if (mp->mnt_cred->cr_ruid != 0 ||
179 vfs_scanopt(mp->mnt_optnew, "mode", "%ho", &root_mode) != 1)
180 root_mode = va.va_mode;
181 if (vfs_getopt_size(mp->mnt_optnew, "inodes", &nodes_max) != 0)
183 if (vfs_getopt_size(mp->mnt_optnew, "size", &size_max) != 0)
185 if (vfs_getopt_size(mp->mnt_optnew, "maxfilesize", &maxfilesize) != 0)
188 /* Do not allow mounts if we do not have enough memory to preserve
189 * the minimum reserved pages. */
190 if (tmpfs_mem_avail() < TMPFS_PAGES_MINRESERVED)
193 /* Get the maximum number of memory pages this file system is
194 * allowed to use, based on the maximum size the user passed in
195 * the mount structure. A value of zero is treated as if the
196 * maximum available space was requested. */
197 if (size_max == 0 || size_max > OFF_MAX - PAGE_SIZE ||
198 (SIZE_MAX < OFF_MAX && size_max / PAGE_SIZE >= SIZE_MAX))
201 size_max = roundup(size_max, PAGE_SIZE);
202 pages = howmany(size_max, PAGE_SIZE);
206 if (nodes_max <= 3) {
207 if (pages < INT_MAX / nodes_per_page)
208 nodes_max = pages * nodes_per_page;
212 if (nodes_max > INT_MAX)
214 MPASS(nodes_max >= 3);
216 /* Allocate the tmpfs mount structure and fill it. */
217 tmp = (struct tmpfs_mount *)malloc(sizeof(struct tmpfs_mount),
218 M_TMPFSMNT, M_WAITOK | M_ZERO);
220 mtx_init(&tmp->allnode_lock, "tmpfs allnode lock", NULL, MTX_DEF);
221 tmp->tm_nodes_max = nodes_max;
222 tmp->tm_nodes_inuse = 0;
223 tmp->tm_maxfilesize = maxfilesize > 0 ? maxfilesize : OFF_MAX;
224 LIST_INIT(&tmp->tm_nodes_used);
226 tmp->tm_pages_max = pages;
227 tmp->tm_pages_used = 0;
228 tmp->tm_ino_unr = new_unrhdr(2, INT_MAX, &tmp->allnode_lock);
229 tmp->tm_dirent_pool = uma_zcreate("TMPFS dirent",
230 sizeof(struct tmpfs_dirent),
231 NULL, NULL, NULL, NULL,
233 tmp->tm_node_pool = uma_zcreate("TMPFS node",
234 sizeof(struct tmpfs_node),
235 tmpfs_node_ctor, tmpfs_node_dtor,
236 tmpfs_node_init, tmpfs_node_fini,
238 tmp->tm_ronly = (mp->mnt_flag & MNT_RDONLY) != 0;
240 /* Allocate the root node. */
241 error = tmpfs_alloc_node(mp, tmp, VDIR, root_uid,
242 root_gid, root_mode & ALLPERMS, NULL, NULL,
245 if (error != 0 || root == NULL) {
246 uma_zdestroy(tmp->tm_node_pool);
247 uma_zdestroy(tmp->tm_dirent_pool);
248 delete_unrhdr(tmp->tm_ino_unr);
249 free(tmp, M_TMPFSMNT);
252 KASSERT(root->tn_id == 2,
253 ("tmpfs root with invalid ino: %ju", (uintmax_t)root->tn_id));
257 mp->mnt_flag |= MNT_LOCAL;
261 mp->mnt_stat.f_namemax = MAXNAMLEN;
263 vfs_mountedfrom(mp, "tmpfs");
270 tmpfs_unmount(struct mount *mp, int mntflags)
272 struct tmpfs_mount *tmp;
273 struct tmpfs_node *node;
276 flags = (mntflags & MNT_FORCE) != 0 ? FORCECLOSE : 0;
277 tmp = VFS_TO_TMPFS(mp);
280 error = vfs_write_suspend_umnt(mp);
284 * At this point, nodes cannot be destroyed by any other
285 * thread because write suspension is started.
289 error = vflush(mp, 0, flags, curthread);
291 vfs_write_resume(mp, VR_START_WRITE);
295 if (mp->mnt_nvnodelistsize == 0) {
300 if ((mntflags & MNT_FORCE) == 0) {
301 vfs_write_resume(mp, VR_START_WRITE);
307 while ((node = LIST_FIRST(&tmp->tm_nodes_used)) != NULL) {
309 if (node->tn_type == VDIR)
310 tmpfs_dir_destroy(tmp, node);
311 tmpfs_free_node(tmp, node);
316 uma_zdestroy(tmp->tm_dirent_pool);
317 uma_zdestroy(tmp->tm_node_pool);
318 delete_unrhdr(tmp->tm_ino_unr);
320 mtx_destroy(&tmp->allnode_lock);
321 MPASS(tmp->tm_pages_used == 0);
322 MPASS(tmp->tm_nodes_inuse == 0);
324 /* Throw away the tmpfs_mount structure. */
325 free(mp->mnt_data, M_TMPFSMNT);
327 vfs_write_resume(mp, VR_START_WRITE);
330 mp->mnt_flag &= ~MNT_LOCAL;
337 tmpfs_root(struct mount *mp, int flags, struct vnode **vpp)
340 error = tmpfs_alloc_vp(mp, VFS_TO_TMPFS(mp)->tm_root, flags, vpp);
343 (*vpp)->v_vflag |= VV_ROOT;
349 tmpfs_fhtovp(struct mount *mp, struct fid *fhp, int flags,
353 struct tmpfs_fid *tfhp;
354 struct tmpfs_mount *tmp;
355 struct tmpfs_node *node;
357 tmp = VFS_TO_TMPFS(mp);
359 tfhp = (struct tmpfs_fid *)fhp;
360 if (tfhp->tf_len != sizeof(struct tmpfs_fid))
363 if (tfhp->tf_id >= tmp->tm_nodes_max)
369 LIST_FOREACH(node, &tmp->tm_nodes_used, tn_entries) {
370 if (node->tn_id == tfhp->tf_id &&
371 node->tn_gen == tfhp->tf_gen) {
379 return (tmpfs_alloc_vp(mp, node, LK_EXCLUSIVE, vpp));
386 tmpfs_statfs(struct mount *mp, struct statfs *sbp)
388 struct tmpfs_mount *tmp;
391 tmp = VFS_TO_TMPFS(mp);
393 sbp->f_iosize = PAGE_SIZE;
394 sbp->f_bsize = PAGE_SIZE;
396 used = tmpfs_pages_used(tmp);
397 if (tmp->tm_pages_max != SIZE_MAX)
398 sbp->f_blocks = tmp->tm_pages_max;
400 sbp->f_blocks = used + tmpfs_mem_avail();
401 if (sbp->f_blocks <= used)
404 sbp->f_bavail = sbp->f_blocks - used;
405 sbp->f_bfree = sbp->f_bavail;
406 used = tmp->tm_nodes_inuse;
407 sbp->f_files = tmp->tm_nodes_max;
408 if (sbp->f_files <= used)
411 sbp->f_ffree = sbp->f_files - used;
412 /* sbp->f_owner = tmp->tn_uid; */
418 tmpfs_sync(struct mount *mp, int waitfor)
421 if (waitfor == MNT_SUSPEND) {
423 mp->mnt_kern_flag |= MNTK_SUSPEND2 | MNTK_SUSPENDED;
430 * tmpfs vfs operations.
433 struct vfsops tmpfs_vfsops = {
434 .vfs_mount = tmpfs_mount,
435 .vfs_unmount = tmpfs_unmount,
436 .vfs_root = tmpfs_root,
437 .vfs_statfs = tmpfs_statfs,
438 .vfs_fhtovp = tmpfs_fhtovp,
439 .vfs_sync = tmpfs_sync,
441 VFS_SET(tmpfs_vfsops, tmpfs, VFCF_JAIL);