2007-06-16 01:56:05 +00:00
|
|
|
/* $NetBSD: tmpfs_vfsops.c,v 1.10 2005/12/11 12:24:29 christos Exp $ */
|
|
|
|
|
2008-09-03 18:53:48 +00:00
|
|
|
/*-
|
2017-11-27 15:15:37 +00:00
|
|
|
* SPDX-License-Identifier: BSD-2-Clause-NetBSD
|
|
|
|
*
|
2007-06-16 01:56:05 +00:00
|
|
|
* Copyright (c) 2005 The NetBSD Foundation, Inc.
|
|
|
|
* All rights reserved.
|
|
|
|
*
|
|
|
|
* This code is derived from software contributed to The NetBSD Foundation
|
|
|
|
* by Julio M. Merino Vidal, developed as part of Google's Summer of Code
|
|
|
|
* 2005 program.
|
|
|
|
*
|
|
|
|
* Redistribution and use in source and binary forms, with or without
|
|
|
|
* modification, are permitted provided that the following conditions
|
|
|
|
* are met:
|
|
|
|
* 1. Redistributions of source code must retain the above copyright
|
|
|
|
* notice, this list of conditions and the following disclaimer.
|
|
|
|
* 2. Redistributions in binary form must reproduce the above copyright
|
|
|
|
* notice, this list of conditions and the following disclaimer in the
|
|
|
|
* documentation and/or other materials provided with the distribution.
|
|
|
|
*
|
|
|
|
* THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
|
|
|
|
* ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
|
|
|
|
* TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
|
|
|
|
* PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
|
|
|
|
* BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
|
|
|
|
* CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
|
|
|
|
* SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
|
|
|
|
* INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
|
|
|
|
* CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
|
|
|
|
* ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
|
|
|
|
* POSSIBILITY OF SUCH DAMAGE.
|
|
|
|
*/
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Efficient memory file system.
|
|
|
|
*
|
2015-01-28 10:25:35 +00:00
|
|
|
* tmpfs is a file system that uses FreeBSD's virtual memory
|
|
|
|
* sub-system to store file data and metadata in an efficient way.
|
|
|
|
* This means that it does not follow the structure of an on-disk file
|
|
|
|
* system because it simply does not need to. Instead, it uses
|
2007-06-16 01:56:05 +00:00
|
|
|
* memory-specific data structures and algorithms to automatically
|
|
|
|
* allocate and release resources.
|
|
|
|
*/
|
|
|
|
#include <sys/cdefs.h>
|
|
|
|
__FBSDID("$FreeBSD$");
|
|
|
|
|
|
|
|
#include <sys/param.h>
|
2017-12-06 00:42:08 +00:00
|
|
|
#include <sys/dirent.h>
|
2007-07-08 15:56:12 +00:00
|
|
|
#include <sys/limits.h>
|
2007-06-16 01:56:05 +00:00
|
|
|
#include <sys/lock.h>
|
2017-12-06 00:42:08 +00:00
|
|
|
#include <sys/mount.h>
|
2007-06-16 01:56:05 +00:00
|
|
|
#include <sys/mutex.h>
|
2013-08-23 22:52:20 +00:00
|
|
|
#include <sys/proc.h>
|
|
|
|
#include <sys/jail.h>
|
2007-06-16 01:56:05 +00:00
|
|
|
#include <sys/kernel.h>
|
2015-01-28 10:37:23 +00:00
|
|
|
#include <sys/rwlock.h>
|
2007-06-16 01:56:05 +00:00
|
|
|
#include <sys/stat.h>
|
|
|
|
#include <sys/systm.h>
|
|
|
|
#include <sys/sysctl.h>
|
2017-12-06 00:42:08 +00:00
|
|
|
#include <sys/vnode.h>
|
2007-06-16 01:56:05 +00:00
|
|
|
|
|
|
|
#include <vm/vm.h>
|
|
|
|
#include <vm/vm_object.h>
|
|
|
|
#include <vm/vm_param.h>
|
|
|
|
|
|
|
|
#include <fs/tmpfs/tmpfs.h>
|
|
|
|
|
|
|
|
/*
|
2007-06-28 02:39:31 +00:00
|
|
|
* Default permission for root node
|
2007-06-16 01:56:05 +00:00
|
|
|
*/
|
|
|
|
#define TMPFS_DEFAULT_ROOT_MODE (S_IRWXU|S_IRGRP|S_IXGRP|S_IROTH|S_IXOTH)
|
|
|
|
|
|
|
|
MALLOC_DEFINE(M_TMPFSMNT, "tmpfs mount", "tmpfs mount structures");
|
2007-06-29 05:23:15 +00:00
|
|
|
MALLOC_DEFINE(M_TMPFSNAME, "tmpfs name", "tmpfs file names");
|
2007-06-16 01:56:05 +00:00
|
|
|
|
2009-05-11 15:33:26 +00:00
|
|
|
static int tmpfs_mount(struct mount *);
|
|
|
|
static int tmpfs_unmount(struct mount *, int);
|
|
|
|
static int tmpfs_root(struct mount *, int flags, struct vnode **);
|
2011-05-22 01:07:54 +00:00
|
|
|
static int tmpfs_fhtovp(struct mount *, struct fid *, int,
|
|
|
|
struct vnode **);
|
2009-05-11 15:33:26 +00:00
|
|
|
static int tmpfs_statfs(struct mount *, struct statfs *);
|
2015-07-05 22:37:33 +00:00
|
|
|
static void tmpfs_susp_clean(struct mount *);
|
2007-06-16 01:56:05 +00:00
|
|
|
|
|
|
|
static const char *tmpfs_opts[] = {
|
2010-01-29 12:09:14 +00:00
|
|
|
"from", "size", "maxfilesize", "inodes", "uid", "gid", "mode", "export",
|
2017-01-19 19:46:49 +00:00
|
|
|
"union", "nonc", NULL
|
2007-06-16 01:56:05 +00:00
|
|
|
};
|
|
|
|
|
2012-04-16 18:07:42 +00:00
|
|
|
static const char *tmpfs_updateopts[] = {
|
|
|
|
"from", "export", NULL
|
|
|
|
};
|
|
|
|
|
2007-06-25 18:46:13 +00:00
|
|
|
static int
|
|
|
|
tmpfs_node_ctor(void *mem, int size, void *arg, int flags)
|
|
|
|
{
|
|
|
|
struct tmpfs_node *node = (struct tmpfs_node *)mem;
|
2007-06-28 02:39:31 +00:00
|
|
|
|
2007-07-11 14:26:27 +00:00
|
|
|
node->tn_gen++;
|
2007-06-25 18:46:13 +00:00
|
|
|
node->tn_size = 0;
|
|
|
|
node->tn_status = 0;
|
|
|
|
node->tn_flags = 0;
|
|
|
|
node->tn_links = 0;
|
|
|
|
node->tn_vnode = NULL;
|
|
|
|
node->tn_vpstate = 0;
|
|
|
|
|
|
|
|
return (0);
|
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
|
|
|
tmpfs_node_dtor(void *mem, int size, void *arg)
|
|
|
|
{
|
|
|
|
struct tmpfs_node *node = (struct tmpfs_node *)mem;
|
|
|
|
node->tn_type = VNON;
|
|
|
|
}
|
|
|
|
|
2007-06-28 02:39:31 +00:00
|
|
|
static int
|
2007-06-25 18:46:13 +00:00
|
|
|
tmpfs_node_init(void *mem, int size, int flags)
|
|
|
|
{
|
|
|
|
struct tmpfs_node *node = (struct tmpfs_node *)mem;
|
|
|
|
node->tn_id = 0;
|
|
|
|
|
|
|
|
mtx_init(&node->tn_interlock, "tmpfs node interlock", NULL, MTX_DEF);
|
2007-07-11 14:26:27 +00:00
|
|
|
node->tn_gen = arc4random();
|
2007-06-28 02:39:31 +00:00
|
|
|
|
2007-06-25 18:46:13 +00:00
|
|
|
return (0);
|
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
|
|
|
tmpfs_node_fini(void *mem, int size)
|
|
|
|
{
|
|
|
|
struct tmpfs_node *node = (struct tmpfs_node *)mem;
|
2007-06-28 02:39:31 +00:00
|
|
|
|
2007-06-25 18:46:13 +00:00
|
|
|
mtx_destroy(&node->tn_interlock);
|
|
|
|
}
|
2007-06-16 01:56:05 +00:00
|
|
|
|
|
|
|
static int
|
2009-05-11 15:33:26 +00:00
|
|
|
tmpfs_mount(struct mount *mp)
|
2007-06-16 01:56:05 +00:00
|
|
|
{
|
2012-04-07 15:30:46 +00:00
|
|
|
const size_t nodes_per_page = howmany(PAGE_SIZE,
|
|
|
|
sizeof(struct tmpfs_dirent) + sizeof(struct tmpfs_node));
|
2007-06-16 01:56:05 +00:00
|
|
|
struct tmpfs_mount *tmp;
|
|
|
|
struct tmpfs_node *root;
|
2013-08-23 22:52:20 +00:00
|
|
|
struct thread *td = curthread;
|
2007-06-16 01:56:05 +00:00
|
|
|
int error;
|
2017-01-19 19:46:49 +00:00
|
|
|
bool nonc;
|
2007-07-08 15:56:12 +00:00
|
|
|
/* Size counters. */
|
2012-04-07 15:27:34 +00:00
|
|
|
u_quad_t pages;
|
|
|
|
off_t nodes_max, size_max, maxfilesize;
|
2007-07-08 15:56:12 +00:00
|
|
|
|
|
|
|
/* Root node attributes. */
|
2010-01-13 14:17:21 +00:00
|
|
|
uid_t root_uid;
|
|
|
|
gid_t root_gid;
|
|
|
|
mode_t root_mode;
|
2007-07-08 15:56:12 +00:00
|
|
|
|
2010-01-13 14:17:21 +00:00
|
|
|
struct vattr va;
|
2007-06-16 01:56:05 +00:00
|
|
|
|
2013-08-23 22:52:20 +00:00
|
|
|
if (!prison_allow(td->td_ucred, PR_ALLOW_MOUNT_TMPFS))
|
|
|
|
return (EPERM);
|
|
|
|
|
2007-06-16 01:56:05 +00:00
|
|
|
if (vfs_filteropt(mp->mnt_optnew, tmpfs_opts))
|
|
|
|
return (EINVAL);
|
|
|
|
|
|
|
|
if (mp->mnt_flag & MNT_UPDATE) {
|
2012-04-16 18:07:42 +00:00
|
|
|
/* Only support update mounts for certain options. */
|
|
|
|
if (vfs_filteropt(mp->mnt_optnew, tmpfs_updateopts) != 0)
|
|
|
|
return (EOPNOTSUPP);
|
|
|
|
if (vfs_flagopt(mp->mnt_optnew, "ro", NULL, 0) !=
|
|
|
|
((struct tmpfs_mount *)mp->mnt_data)->tm_ronly)
|
|
|
|
return (EOPNOTSUPP);
|
|
|
|
return (0);
|
2007-06-16 01:56:05 +00:00
|
|
|
}
|
|
|
|
|
2008-01-10 01:10:58 +00:00
|
|
|
vn_lock(mp->mnt_vnodecovered, LK_SHARED | LK_RETRY);
|
2008-08-28 15:23:18 +00:00
|
|
|
error = VOP_GETATTR(mp->mnt_vnodecovered, &va, mp->mnt_cred);
|
2008-01-13 14:44:15 +00:00
|
|
|
VOP_UNLOCK(mp->mnt_vnodecovered, 0);
|
2007-07-08 15:56:12 +00:00
|
|
|
if (error)
|
|
|
|
return (error);
|
|
|
|
|
|
|
|
if (mp->mnt_cred->cr_ruid != 0 ||
|
|
|
|
vfs_scanopt(mp->mnt_optnew, "gid", "%d", &root_gid) != 1)
|
|
|
|
root_gid = va.va_gid;
|
|
|
|
if (mp->mnt_cred->cr_ruid != 0 ||
|
|
|
|
vfs_scanopt(mp->mnt_optnew, "uid", "%d", &root_uid) != 1)
|
|
|
|
root_uid = va.va_uid;
|
|
|
|
if (mp->mnt_cred->cr_ruid != 0 ||
|
2007-11-12 18:57:33 +00:00
|
|
|
vfs_scanopt(mp->mnt_optnew, "mode", "%ho", &root_mode) != 1)
|
2007-07-08 15:56:12 +00:00
|
|
|
root_mode = va.va_mode;
|
2012-04-07 15:27:34 +00:00
|
|
|
if (vfs_getopt_size(mp->mnt_optnew, "inodes", &nodes_max) != 0)
|
2007-07-08 15:56:12 +00:00
|
|
|
nodes_max = 0;
|
2012-04-07 15:27:34 +00:00
|
|
|
if (vfs_getopt_size(mp->mnt_optnew, "size", &size_max) != 0)
|
2007-07-08 15:56:12 +00:00
|
|
|
size_max = 0;
|
2012-04-07 15:27:34 +00:00
|
|
|
if (vfs_getopt_size(mp->mnt_optnew, "maxfilesize", &maxfilesize) != 0)
|
2010-01-29 12:09:14 +00:00
|
|
|
maxfilesize = 0;
|
2017-01-19 19:46:49 +00:00
|
|
|
nonc = vfs_getopt(mp->mnt_optnew, "nonc", NULL, NULL) == 0;
|
2007-06-16 01:56:05 +00:00
|
|
|
|
|
|
|
/* Do not allow mounts if we do not have enough memory to preserve
|
|
|
|
* the minimum reserved pages. */
|
2012-04-07 15:23:51 +00:00
|
|
|
if (tmpfs_mem_avail() < TMPFS_PAGES_MINRESERVED)
|
2017-01-19 14:27:37 +00:00
|
|
|
return (ENOSPC);
|
2007-06-16 01:56:05 +00:00
|
|
|
|
|
|
|
/* Get the maximum number of memory pages this file system is
|
|
|
|
* allowed to use, based on the maximum size the user passed in
|
|
|
|
* the mount structure. A value of zero is treated as if the
|
|
|
|
* maximum available space was requested. */
|
2014-03-14 01:43:55 +00:00
|
|
|
if (size_max == 0 || size_max > OFF_MAX - PAGE_SIZE ||
|
2012-04-07 15:27:34 +00:00
|
|
|
(SIZE_MAX < OFF_MAX && size_max / PAGE_SIZE >= SIZE_MAX))
|
2007-06-16 01:56:05 +00:00
|
|
|
pages = SIZE_MAX;
|
2014-03-14 01:43:55 +00:00
|
|
|
else {
|
|
|
|
size_max = roundup(size_max, PAGE_SIZE);
|
2007-07-08 15:56:12 +00:00
|
|
|
pages = howmany(size_max, PAGE_SIZE);
|
2014-03-14 01:43:55 +00:00
|
|
|
}
|
2007-06-16 01:56:05 +00:00
|
|
|
MPASS(pages > 0);
|
|
|
|
|
2010-01-20 16:56:20 +00:00
|
|
|
if (nodes_max <= 3) {
|
2012-04-07 15:30:46 +00:00
|
|
|
if (pages < INT_MAX / nodes_per_page)
|
|
|
|
nodes_max = pages * nodes_per_page;
|
2010-01-20 16:56:20 +00:00
|
|
|
else
|
2012-04-07 15:30:46 +00:00
|
|
|
nodes_max = INT_MAX;
|
2012-04-07 15:27:34 +00:00
|
|
|
}
|
2012-04-07 15:30:46 +00:00
|
|
|
if (nodes_max > INT_MAX)
|
|
|
|
nodes_max = INT_MAX;
|
2012-04-07 15:27:34 +00:00
|
|
|
MPASS(nodes_max >= 3);
|
2007-06-16 01:56:05 +00:00
|
|
|
|
|
|
|
/* Allocate the tmpfs mount structure and fill it. */
|
|
|
|
tmp = (struct tmpfs_mount *)malloc(sizeof(struct tmpfs_mount),
|
|
|
|
M_TMPFSMNT, M_WAITOK | M_ZERO);
|
2007-06-28 02:39:31 +00:00
|
|
|
|
2017-01-19 16:01:36 +00:00
|
|
|
mtx_init(&tmp->tm_allnode_lock, "tmpfs allnode lock", NULL, MTX_DEF);
|
2012-04-07 15:27:34 +00:00
|
|
|
tmp->tm_nodes_max = nodes_max;
|
2007-06-16 01:56:05 +00:00
|
|
|
tmp->tm_nodes_inuse = 0;
|
2017-01-19 19:15:21 +00:00
|
|
|
tmp->tm_refcount = 1;
|
2012-04-07 15:27:34 +00:00
|
|
|
tmp->tm_maxfilesize = maxfilesize > 0 ? maxfilesize : OFF_MAX;
|
2007-06-16 01:56:05 +00:00
|
|
|
LIST_INIT(&tmp->tm_nodes_used);
|
2007-06-28 02:39:31 +00:00
|
|
|
|
2007-06-16 01:56:05 +00:00
|
|
|
tmp->tm_pages_max = pages;
|
|
|
|
tmp->tm_pages_used = 0;
|
2017-01-19 16:01:36 +00:00
|
|
|
tmp->tm_ino_unr = new_unrhdr(2, INT_MAX, &tmp->tm_allnode_lock);
|
2007-11-18 04:40:42 +00:00
|
|
|
tmp->tm_dirent_pool = uma_zcreate("TMPFS dirent",
|
2017-01-19 14:27:37 +00:00
|
|
|
sizeof(struct tmpfs_dirent), NULL, NULL, NULL, NULL,
|
2007-11-18 04:40:42 +00:00
|
|
|
UMA_ALIGN_PTR, 0);
|
|
|
|
tmp->tm_node_pool = uma_zcreate("TMPFS node",
|
2017-01-19 14:27:37 +00:00
|
|
|
sizeof(struct tmpfs_node), tmpfs_node_ctor, tmpfs_node_dtor,
|
|
|
|
tmpfs_node_init, tmpfs_node_fini, UMA_ALIGN_PTR, 0);
|
2012-04-16 18:07:42 +00:00
|
|
|
tmp->tm_ronly = (mp->mnt_flag & MNT_RDONLY) != 0;
|
2017-01-19 19:46:49 +00:00
|
|
|
tmp->tm_nonc = nonc;
|
2007-06-16 01:56:05 +00:00
|
|
|
|
|
|
|
/* Allocate the root node. */
|
2017-01-19 14:27:37 +00:00
|
|
|
error = tmpfs_alloc_node(mp, tmp, VDIR, root_uid, root_gid,
|
|
|
|
root_mode & ALLPERMS, NULL, NULL, VNOVAL, &root);
|
2007-06-16 01:56:05 +00:00
|
|
|
|
|
|
|
if (error != 0 || root == NULL) {
|
2017-01-19 14:27:37 +00:00
|
|
|
uma_zdestroy(tmp->tm_node_pool);
|
|
|
|
uma_zdestroy(tmp->tm_dirent_pool);
|
|
|
|
delete_unrhdr(tmp->tm_ino_unr);
|
|
|
|
free(tmp, M_TMPFSMNT);
|
|
|
|
return (error);
|
2007-06-16 01:56:05 +00:00
|
|
|
}
|
2012-09-27 23:30:49 +00:00
|
|
|
KASSERT(root->tn_id == 2,
|
|
|
|
("tmpfs root with invalid ino: %ju", (uintmax_t)root->tn_id));
|
2007-06-16 01:56:05 +00:00
|
|
|
tmp->tm_root = root;
|
|
|
|
|
|
|
|
MNT_ILOCK(mp);
|
|
|
|
mp->mnt_flag |= MNT_LOCAL;
|
2017-01-06 18:01:46 +00:00
|
|
|
mp->mnt_kern_flag |= MNTK_LOOKUP_SHARED | MNTK_EXTENDED_SHARED;
|
2007-06-16 01:56:05 +00:00
|
|
|
MNT_IUNLOCK(mp);
|
2007-06-28 02:39:31 +00:00
|
|
|
|
2007-06-16 01:56:05 +00:00
|
|
|
mp->mnt_data = tmp;
|
|
|
|
mp->mnt_stat.f_namemax = MAXNAMLEN;
|
|
|
|
vfs_getnewfsid(mp);
|
|
|
|
vfs_mountedfrom(mp, "tmpfs");
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* ARGSUSED2 */
|
|
|
|
static int
|
2009-05-11 15:33:26 +00:00
|
|
|
tmpfs_unmount(struct mount *mp, int mntflags)
|
2007-06-16 01:56:05 +00:00
|
|
|
{
|
|
|
|
struct tmpfs_mount *tmp;
|
|
|
|
struct tmpfs_node *node;
|
2014-07-14 09:52:33 +00:00
|
|
|
int error, flags;
|
2007-06-16 01:56:05 +00:00
|
|
|
|
2014-07-14 09:52:33 +00:00
|
|
|
flags = (mntflags & MNT_FORCE) != 0 ? FORCECLOSE : 0;
|
2007-06-16 01:56:05 +00:00
|
|
|
tmp = VFS_TO_TMPFS(mp);
|
2007-06-28 02:39:31 +00:00
|
|
|
|
2014-07-14 09:52:33 +00:00
|
|
|
/* Stop writers */
|
|
|
|
error = vfs_write_suspend_umnt(mp);
|
|
|
|
if (error != 0)
|
|
|
|
return (error);
|
|
|
|
/*
|
|
|
|
* At this point, nodes cannot be destroyed by any other
|
|
|
|
* thread because write suspension is started.
|
|
|
|
*/
|
|
|
|
|
|
|
|
for (;;) {
|
|
|
|
error = vflush(mp, 0, flags, curthread);
|
|
|
|
if (error != 0) {
|
|
|
|
vfs_write_resume(mp, VR_START_WRITE);
|
|
|
|
return (error);
|
|
|
|
}
|
|
|
|
MNT_ILOCK(mp);
|
|
|
|
if (mp->mnt_nvnodelistsize == 0) {
|
|
|
|
MNT_IUNLOCK(mp);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
MNT_IUNLOCK(mp);
|
|
|
|
if ((mntflags & MNT_FORCE) == 0) {
|
|
|
|
vfs_write_resume(mp, VR_START_WRITE);
|
|
|
|
return (EBUSY);
|
|
|
|
}
|
|
|
|
}
|
2007-06-16 01:56:05 +00:00
|
|
|
|
2014-07-14 09:52:33 +00:00
|
|
|
TMPFS_LOCK(tmp);
|
|
|
|
while ((node = LIST_FIRST(&tmp->tm_nodes_used)) != NULL) {
|
2017-01-19 19:15:21 +00:00
|
|
|
TMPFS_NODE_LOCK(node);
|
2013-01-06 22:15:44 +00:00
|
|
|
if (node->tn_type == VDIR)
|
|
|
|
tmpfs_dir_destroy(tmp, node);
|
2017-01-19 19:15:21 +00:00
|
|
|
if (tmpfs_free_node_locked(tmp, node, true))
|
|
|
|
TMPFS_LOCK(tmp);
|
|
|
|
else
|
|
|
|
TMPFS_NODE_UNLOCK(node);
|
|
|
|
}
|
|
|
|
|
|
|
|
mp->mnt_data = NULL;
|
|
|
|
tmpfs_free_tmp(tmp);
|
|
|
|
vfs_write_resume(mp, VR_START_WRITE);
|
|
|
|
|
|
|
|
MNT_ILOCK(mp);
|
|
|
|
mp->mnt_flag &= ~MNT_LOCAL;
|
|
|
|
MNT_IUNLOCK(mp);
|
|
|
|
|
|
|
|
return (0);
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
tmpfs_free_tmp(struct tmpfs_mount *tmp)
|
|
|
|
{
|
|
|
|
|
|
|
|
MPASS(tmp->tm_refcount > 0);
|
|
|
|
tmp->tm_refcount--;
|
|
|
|
if (tmp->tm_refcount > 0) {
|
|
|
|
TMPFS_UNLOCK(tmp);
|
|
|
|
return;
|
2007-06-16 01:56:05 +00:00
|
|
|
}
|
2014-07-14 09:52:33 +00:00
|
|
|
TMPFS_UNLOCK(tmp);
|
2007-06-16 01:56:05 +00:00
|
|
|
|
2007-06-25 18:46:13 +00:00
|
|
|
uma_zdestroy(tmp->tm_dirent_pool);
|
|
|
|
uma_zdestroy(tmp->tm_node_pool);
|
2017-10-23 15:43:38 +00:00
|
|
|
clear_unrhdr(tmp->tm_ino_unr);
|
2007-07-11 14:26:27 +00:00
|
|
|
delete_unrhdr(tmp->tm_ino_unr);
|
2007-06-16 01:56:05 +00:00
|
|
|
|
2017-01-19 16:01:36 +00:00
|
|
|
mtx_destroy(&tmp->tm_allnode_lock);
|
2007-06-16 01:56:05 +00:00
|
|
|
MPASS(tmp->tm_pages_used == 0);
|
2007-07-08 15:56:12 +00:00
|
|
|
MPASS(tmp->tm_nodes_inuse == 0);
|
2007-06-16 01:56:05 +00:00
|
|
|
|
2017-01-19 19:15:21 +00:00
|
|
|
free(tmp, M_TMPFSMNT);
|
2007-06-16 01:56:05 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
static int
|
2009-05-11 15:33:26 +00:00
|
|
|
tmpfs_root(struct mount *mp, int flags, struct vnode **vpp)
|
2007-06-16 01:56:05 +00:00
|
|
|
{
|
|
|
|
int error;
|
|
|
|
|
2017-01-19 14:27:37 +00:00
|
|
|
error = tmpfs_alloc_vp(mp, VFS_TO_TMPFS(mp)->tm_root, flags, vpp);
|
|
|
|
if (error == 0)
|
2007-06-25 18:46:13 +00:00
|
|
|
(*vpp)->v_vflag |= VV_ROOT;
|
2017-01-19 14:27:37 +00:00
|
|
|
return (error);
|
2007-06-16 01:56:05 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
static int
|
2011-05-22 01:07:54 +00:00
|
|
|
tmpfs_fhtovp(struct mount *mp, struct fid *fhp, int flags,
|
|
|
|
struct vnode **vpp)
|
2007-06-16 01:56:05 +00:00
|
|
|
{
|
|
|
|
struct tmpfs_fid *tfhp;
|
|
|
|
struct tmpfs_mount *tmp;
|
|
|
|
struct tmpfs_node *node;
|
2017-01-19 19:15:21 +00:00
|
|
|
int error;
|
2007-06-16 01:56:05 +00:00
|
|
|
|
|
|
|
tmp = VFS_TO_TMPFS(mp);
|
|
|
|
|
|
|
|
tfhp = (struct tmpfs_fid *)fhp;
|
|
|
|
if (tfhp->tf_len != sizeof(struct tmpfs_fid))
|
2017-01-19 19:15:21 +00:00
|
|
|
return (EINVAL);
|
2007-06-16 01:56:05 +00:00
|
|
|
|
|
|
|
if (tfhp->tf_id >= tmp->tm_nodes_max)
|
2017-01-19 19:15:21 +00:00
|
|
|
return (EINVAL);
|
2007-06-16 01:56:05 +00:00
|
|
|
|
|
|
|
TMPFS_LOCK(tmp);
|
|
|
|
LIST_FOREACH(node, &tmp->tm_nodes_used, tn_entries) {
|
|
|
|
if (node->tn_id == tfhp->tf_id &&
|
|
|
|
node->tn_gen == tfhp->tf_gen) {
|
2017-01-19 19:15:21 +00:00
|
|
|
tmpfs_ref_node(node);
|
2007-06-16 01:56:05 +00:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
TMPFS_UNLOCK(tmp);
|
|
|
|
|
2017-01-19 19:15:21 +00:00
|
|
|
if (node != NULL) {
|
|
|
|
error = tmpfs_alloc_vp(mp, node, LK_EXCLUSIVE, vpp);
|
|
|
|
tmpfs_free_node(tmp, node);
|
|
|
|
} else
|
|
|
|
error = EINVAL;
|
|
|
|
return (error);
|
2007-06-16 01:56:05 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/* ARGSUSED2 */
|
|
|
|
static int
|
2009-05-11 15:33:26 +00:00
|
|
|
tmpfs_statfs(struct mount *mp, struct statfs *sbp)
|
2007-06-16 01:56:05 +00:00
|
|
|
{
|
|
|
|
struct tmpfs_mount *tmp;
|
2012-04-07 15:23:51 +00:00
|
|
|
size_t used;
|
2007-06-16 01:56:05 +00:00
|
|
|
|
|
|
|
tmp = VFS_TO_TMPFS(mp);
|
|
|
|
|
2007-06-28 02:39:31 +00:00
|
|
|
sbp->f_iosize = PAGE_SIZE;
|
2007-06-16 01:56:05 +00:00
|
|
|
sbp->f_bsize = PAGE_SIZE;
|
|
|
|
|
2012-04-07 15:23:51 +00:00
|
|
|
used = tmpfs_pages_used(tmp);
|
2017-01-14 06:20:36 +00:00
|
|
|
if (tmp->tm_pages_max != ULONG_MAX)
|
2012-04-07 15:23:51 +00:00
|
|
|
sbp->f_blocks = tmp->tm_pages_max;
|
|
|
|
else
|
|
|
|
sbp->f_blocks = used + tmpfs_mem_avail();
|
|
|
|
if (sbp->f_blocks <= used)
|
|
|
|
sbp->f_bavail = 0;
|
|
|
|
else
|
|
|
|
sbp->f_bavail = sbp->f_blocks - used;
|
|
|
|
sbp->f_bfree = sbp->f_bavail;
|
|
|
|
used = tmp->tm_nodes_inuse;
|
|
|
|
sbp->f_files = tmp->tm_nodes_max;
|
|
|
|
if (sbp->f_files <= used)
|
|
|
|
sbp->f_ffree = 0;
|
|
|
|
else
|
|
|
|
sbp->f_ffree = sbp->f_files - used;
|
2007-06-16 01:56:05 +00:00
|
|
|
/* sbp->f_owner = tmp->tn_uid; */
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2014-07-14 09:52:33 +00:00
|
|
|
static int
|
|
|
|
tmpfs_sync(struct mount *mp, int waitfor)
|
|
|
|
{
|
2015-01-28 10:37:23 +00:00
|
|
|
struct vnode *vp, *mvp;
|
|
|
|
struct vm_object *obj;
|
2014-07-14 09:52:33 +00:00
|
|
|
|
|
|
|
if (waitfor == MNT_SUSPEND) {
|
|
|
|
MNT_ILOCK(mp);
|
|
|
|
mp->mnt_kern_flag |= MNTK_SUSPEND2 | MNTK_SUSPENDED;
|
|
|
|
MNT_IUNLOCK(mp);
|
2015-01-28 10:37:23 +00:00
|
|
|
} else if (waitfor == MNT_LAZY) {
|
|
|
|
/*
|
|
|
|
* Handle lazy updates of mtime from writes to mmaped
|
|
|
|
* regions. Use MNT_VNODE_FOREACH_ALL instead of
|
|
|
|
* MNT_VNODE_FOREACH_ACTIVE, since unmap of the
|
|
|
|
* tmpfs-backed vnode does not call vinactive(), due
|
|
|
|
* to vm object type is OBJT_SWAP.
|
|
|
|
*/
|
|
|
|
MNT_VNODE_FOREACH_ALL(vp, mp, mvp) {
|
|
|
|
if (vp->v_type != VREG) {
|
|
|
|
VI_UNLOCK(vp);
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
obj = vp->v_object;
|
|
|
|
KASSERT((obj->flags & (OBJ_TMPFS_NODE | OBJ_TMPFS)) ==
|
|
|
|
(OBJ_TMPFS_NODE | OBJ_TMPFS), ("non-tmpfs obj"));
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Unlocked read, avoid taking vnode lock if
|
|
|
|
* not needed. Lost update will be handled on
|
|
|
|
* the next call.
|
|
|
|
*/
|
|
|
|
if ((obj->flags & OBJ_TMPFS_DIRTY) == 0) {
|
|
|
|
VI_UNLOCK(vp);
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
if (vget(vp, LK_EXCLUSIVE | LK_RETRY | LK_INTERLOCK,
|
|
|
|
curthread) != 0)
|
|
|
|
continue;
|
|
|
|
tmpfs_check_mtime(vp);
|
|
|
|
vput(vp);
|
|
|
|
}
|
2014-07-14 09:52:33 +00:00
|
|
|
}
|
|
|
|
return (0);
|
|
|
|
}
|
|
|
|
|
2015-07-05 22:37:33 +00:00
|
|
|
/*
|
|
|
|
* The presence of a susp_clean method tells the VFS to track writes.
|
|
|
|
*/
|
|
|
|
static void
|
|
|
|
tmpfs_susp_clean(struct mount *mp __unused)
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
2007-06-16 01:56:05 +00:00
|
|
|
/*
|
|
|
|
* tmpfs vfs operations.
|
|
|
|
*/
|
|
|
|
|
|
|
|
struct vfsops tmpfs_vfsops = {
|
|
|
|
.vfs_mount = tmpfs_mount,
|
|
|
|
.vfs_unmount = tmpfs_unmount,
|
|
|
|
.vfs_root = tmpfs_root,
|
|
|
|
.vfs_statfs = tmpfs_statfs,
|
|
|
|
.vfs_fhtovp = tmpfs_fhtovp,
|
2014-07-14 09:52:33 +00:00
|
|
|
.vfs_sync = tmpfs_sync,
|
2015-07-05 22:37:33 +00:00
|
|
|
.vfs_susp_clean = tmpfs_susp_clean,
|
2007-06-16 01:56:05 +00:00
|
|
|
};
|
2013-08-23 22:52:20 +00:00
|
|
|
VFS_SET(tmpfs_vfsops, tmpfs, VFCF_JAIL);
|