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>
45 #include <sys/param.h>
46 #include <sys/limits.h>
48 #include <sys/mutex.h>
49 #include <sys/kernel.h>
51 #include <sys/systm.h>
52 #include <sys/sysctl.h>
53 #include <sys/objcache.h>
56 #include <vm/vm_object.h>
57 #include <vm/vm_param.h>
59 #include <vfs/tmpfs/tmpfs.h>
60 #include <vfs/tmpfs/tmpfs_vnops.h>
61 #include <vfs/tmpfs/tmpfs_args.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");
70 MALLOC_DEFINE(M_TMPFS_DIRENT, "tmpfs dirent", "tmpfs dirent structures");
71 MALLOC_DEFINE(M_TMPFS_NODE, "tmpfs node", "tmpfs node structures");
73 /* --------------------------------------------------------------------- */
75 static int tmpfs_mount(struct mount *, char *, caddr_t, struct ucred *);
76 static int tmpfs_unmount(struct mount *, int);
77 static int tmpfs_root(struct mount *, struct vnode **);
78 static int tmpfs_fhtovp(struct mount *, struct vnode *, struct fid *, struct vnode **);
79 static int tmpfs_statfs(struct mount *, struct statfs *, struct ucred *cred);
81 /* --------------------------------------------------------------------- */
83 tmpfs_node_ctor(void *obj, void *privdata, int flags)
85 struct tmpfs_node *node = (struct tmpfs_node *)obj;
92 node->tn_vnode = NULL;
93 node->tn_vpstate = TMPFS_VNODE_WANT;
94 bzero(&node->tn_spec, sizeof(node->tn_spec));
100 tmpfs_node_dtor(void *obj, void *privdata)
102 struct tmpfs_node *node = (struct tmpfs_node *)obj;
103 node->tn_type = VNON;
104 node->tn_vpstate = TMPFS_VNODE_DOOMED;
108 tmpfs_node_init(void *args, int flags)
110 struct tmpfs_node *node = (struct tmpfs_node *)objcache_malloc_alloc(args, flags);
113 lockinit(&node->tn_interlock, "tmpfs node interlock", 0, LK_CANRECURSE);
114 node->tn_gen = karc4random();
120 tmpfs_node_fini(void *obj, void *args)
122 struct tmpfs_node *node = (struct tmpfs_node *)obj;
123 lockuninit(&node->tn_interlock);
124 objcache_malloc_free(obj, args);
127 struct objcache_malloc_args tmpfs_dirent_pool_malloc_args =
128 { sizeof(struct tmpfs_dirent), M_TMPFS_DIRENT };
129 struct objcache_malloc_args tmpfs_node_pool_malloc_args =
130 { sizeof(struct tmpfs_node), M_TMPFS_NODE };
133 tmpfs_mount(struct mount *mp, char *path, caddr_t data, struct ucred *cred)
135 struct tmpfs_mount *tmp;
136 struct tmpfs_node *root;
137 struct tmpfs_args args;
139 vm_pindex_t pages_limit;
149 /* Root node attributes. */
150 uid_t root_uid = cred->cr_uid;
151 gid_t root_gid = cred->cr_gid;
152 mode_t root_mode = (VREAD | VWRITE);
154 if (mp->mnt_flag & MNT_UPDATE) {
155 /* XXX: There is no support yet to update file system
156 * settings. Should be added. */
164 bzero(&args, sizeof(args));
171 error = copyin(data, &args, sizeof(args));
175 size_max = args.ta_size_max;
176 nodes_max = args.ta_nodes_max;
177 maxfsize_max = args.ta_maxfsize_max;
178 root_uid = args.ta_root_uid;
179 root_gid = args.ta_root_gid;
180 root_mode = args.ta_root_mode;
184 * If mount by non-root, then verify that user has necessary
185 * permissions on the device.
187 if (cred->cr_uid != 0) {
189 if ((mp->mnt_flag & MNT_RDONLY) == 0)
193 pages_limit = vm_swap_max + vmstats.v_page_count / 2;
196 pages = pages_limit / 2;
197 else if (size_max < PAGE_SIZE)
199 else if (OFF_TO_IDX(size_max) > pages_limit)
202 pages = OFF_TO_IDX(size_max);
205 nodes = 3 + pages * PAGE_SIZE / 1024;
206 else if (nodes_max < 3)
208 else if (nodes_max > pages)
213 maxfsize = IDX_TO_OFF(pages_limit);
214 if (maxfsize_max != 0 && maxfsize > maxfsize_max)
215 maxfsize = maxfsize_max;
217 /* Allocate the tmpfs mount structure and fill it. */
218 tmp = kmalloc(sizeof(*tmp), M_TMPFSMNT, M_WAITOK | M_ZERO);
220 lockinit(&(tmp->allnode_lock), "tmpfs allnode lock", 0, LK_CANRECURSE);
221 tmp->tm_nodes_max = nodes;
222 tmp->tm_nodes_inuse = 0;
223 tmp->tm_maxfilesize = maxfsize;
224 LIST_INIT(&tmp->tm_nodes_used);
226 tmp->tm_pages_max = pages;
227 tmp->tm_pages_used = 0;
229 kmalloc_raise_limit(M_TMPFS_NODE, sizeof(struct tmpfs_node) *
232 tmp->tm_dirent_pool = objcache_create( "tmpfs dirent cache",
235 objcache_malloc_alloc, objcache_malloc_free,
236 &tmpfs_dirent_pool_malloc_args);
237 tmp->tm_node_pool = objcache_create( "tmpfs node cache",
239 tmpfs_node_ctor, tmpfs_node_dtor, NULL,
240 tmpfs_node_init, tmpfs_node_fini,
241 &tmpfs_node_pool_malloc_args);
243 /* Allocate the root node. */
244 error = tmpfs_alloc_node(tmp, VDIR, root_uid, root_gid,
245 root_mode & ALLPERMS, NULL, NULL,
246 VNOVAL, VNOVAL, &root);
249 * We are backed by swap, set snocache chflags flag so we
250 * don't trip over swapcache.
252 root->tn_flags = SF_NOCACHE;
254 if (error != 0 || root == NULL) {
255 objcache_destroy(tmp->tm_node_pool);
256 objcache_destroy(tmp->tm_dirent_pool);
257 kfree(tmp, M_TMPFSMNT);
260 KASSERT(root->tn_id >= 0, ("tmpfs root with invalid ino: %d", (int)root->tn_id));
263 mp->mnt_flag |= MNT_LOCAL;
265 mp->mnt_kern_flag |= MNTK_RD_MPSAFE | MNTK_WR_MPSAFE | MNTK_GA_MPSAFE |
266 MNTK_IN_MPSAFE | MNTK_SG_MPSAFE;
268 mp->mnt_kern_flag |= MNTK_NOMSYNC;
269 mp->mnt_data = (qaddr_t)tmp;
273 vfs_add_vnodeops(mp, &tmpfs_vnode_vops, &mp->mnt_vn_norm_ops);
274 vfs_add_vnodeops(mp, &tmpfs_fifo_vops, &mp->mnt_vn_fifo_ops);
276 copystr("tmpfs", mp->mnt_stat.f_mntfromname, MNAMELEN - 1, &size);
277 bzero(mp->mnt_stat.f_mntfromname +size, MNAMELEN - size);
278 bzero(mp->mnt_stat.f_mntonname, sizeof(mp->mnt_stat.f_mntonname));
279 copyinstr(path, mp->mnt_stat.f_mntonname,
280 sizeof(mp->mnt_stat.f_mntonname) -1,
283 tmpfs_statfs(mp, &mp->mnt_stat, cred);
288 /* --------------------------------------------------------------------- */
292 tmpfs_unmount(struct mount *mp, int mntflags)
297 struct tmpfs_mount *tmp;
298 struct tmpfs_node *node;
300 /* Handle forced unmounts. */
301 if (mntflags & MNT_FORCE)
304 tmp = VFS_TO_TMPFS(mp);
307 * Finalize all pending I/O. In the case of tmpfs we want
308 * to throw all the data away so clean out the buffer cache
309 * and vm objects before calling vflush().
311 LIST_FOREACH(node, &tmp->tm_nodes_used, tn_entries) {
312 if (node->tn_type == VREG && node->tn_vnode) {
314 TMPFS_NODE_LOCK(node);
315 vx_get(node->tn_vnode);
316 tmpfs_truncate(node->tn_vnode, 0);
317 vx_put(node->tn_vnode);
318 TMPFS_NODE_UNLOCK(node);
322 error = vflush(mp, 0, flags);
327 * First pass get rid of all the directory entries and
328 * vnode associations. The directory structure will
329 * remain via the extra link count representing tn_dir.tn_parent.
331 * No vnodes should remain after the vflush above.
333 LIST_FOREACH(node, &tmp->tm_nodes_used, tn_entries) {
335 TMPFS_NODE_LOCK(node);
336 if (node->tn_type == VDIR) {
337 struct tmpfs_dirent *de;
339 while (!TAILQ_EMPTY(&node->tn_dir.tn_dirhead)) {
340 de = TAILQ_FIRST(&node->tn_dir.tn_dirhead);
341 tmpfs_dir_detach(node, de);
342 tmpfs_free_dirent(tmp, de);
343 node->tn_size -= sizeof(struct tmpfs_dirent);
346 KKASSERT(node->tn_vnode == NULL);
352 node->tn_vnode = NULL;
355 TMPFS_NODE_UNLOCK(node);
360 * Now get rid of all nodes. We can remove any node with a
361 * link count of 0 or any directory node with a link count of
362 * 1. The parents will not be destroyed until all their children
363 * have been destroyed.
365 * Recursion in tmpfs_free_node() can further modify the list so
366 * we cannot use a next pointer here.
368 * The root node will be destroyed by this loop (it will be last).
370 while (!LIST_EMPTY(&tmp->tm_nodes_used)) {
372 LIST_FOREACH(node, &tmp->tm_nodes_used, tn_entries) {
373 if (node->tn_links == 0 ||
374 (node->tn_links == 1 && node->tn_type == VDIR)) {
375 TMPFS_NODE_LOCK(node);
376 tmpfs_free_node(tmp, node);
383 kprintf("tmpfs: Cannot free entire node tree!");
388 KKASSERT(tmp->tm_root == NULL);
390 objcache_destroy(tmp->tm_dirent_pool);
391 objcache_destroy(tmp->tm_node_pool);
393 lockuninit(&tmp->allnode_lock);
394 KKASSERT(tmp->tm_pages_used == 0);
395 KKASSERT(tmp->tm_nodes_inuse == 0);
397 /* Throw away the tmpfs_mount structure. */
398 kfree(tmp, M_TMPFSMNT);
401 mp->mnt_flag &= ~MNT_LOCAL;
405 /* --------------------------------------------------------------------- */
408 tmpfs_root(struct mount *mp, struct vnode **vpp)
410 struct tmpfs_mount *tmp;
413 tmp = VFS_TO_TMPFS(mp);
414 if (tmp->tm_root == NULL) {
415 kprintf("tmpfs_root: called without root node %p\n", mp);
420 error = tmpfs_alloc_vp(mp, tmp->tm_root, LK_EXCLUSIVE, vpp);
421 (*vpp)->v_flag |= VROOT;
422 (*vpp)->v_type = VDIR;
427 /* --------------------------------------------------------------------- */
430 tmpfs_fhtovp(struct mount *mp, struct vnode *rootvp, struct fid *fhp, struct vnode **vpp)
433 struct tmpfs_fid *tfhp;
434 struct tmpfs_mount *tmp;
435 struct tmpfs_node *node;
437 tmp = VFS_TO_TMPFS(mp);
439 tfhp = (struct tmpfs_fid *)fhp;
440 if (tfhp->tf_len != sizeof(struct tmpfs_fid))
443 if (tfhp->tf_id >= tmp->tm_nodes_max)
449 LIST_FOREACH(node, &tmp->tm_nodes_used, tn_entries) {
450 if (node->tn_id == tfhp->tf_id &&
451 node->tn_gen == tfhp->tf_gen) {
459 return (tmpfs_alloc_vp(mp, node, LK_EXCLUSIVE, vpp));
464 /* --------------------------------------------------------------------- */
468 tmpfs_statfs(struct mount *mp, struct statfs *sbp, struct ucred *cred)
470 fsfilcnt_t freenodes;
471 struct tmpfs_mount *tmp;
473 tmp = VFS_TO_TMPFS(mp);
475 sbp->f_iosize = PAGE_SIZE;
476 sbp->f_bsize = PAGE_SIZE;
478 sbp->f_blocks = tmp->tm_pages_max;
479 sbp->f_bavail = tmp->tm_pages_max - tmp->tm_pages_used;
480 sbp->f_bfree = sbp->f_bavail;
482 freenodes = tmp->tm_nodes_max - tmp->tm_nodes_inuse;
484 sbp->f_files = freenodes + tmp->tm_nodes_inuse;
485 sbp->f_ffree = freenodes;
486 sbp->f_owner = tmp->tm_root->tn_uid;
491 /* --------------------------------------------------------------------- */
494 * tmpfs vfs operations.
497 static struct vfsops tmpfs_vfsops = {
498 .vfs_mount = tmpfs_mount,
499 .vfs_unmount = tmpfs_unmount,
500 .vfs_root = tmpfs_root,
501 .vfs_statfs = tmpfs_statfs,
502 .vfs_fhtovp = tmpfs_fhtovp,
503 .vfs_sync = vfs_stdsync
506 VFS_SET(tmpfs_vfsops, tmpfs, 0);