Home | History | Annotate | Line # | Download | only in tmpfs
tmpfs_vfsops.c revision 1.62
      1 /*	$NetBSD: tmpfs_vfsops.c,v 1.62 2014/06/07 09:54:34 martin Exp $	*/
      2 
      3 /*
      4  * Copyright (c) 2005, 2006, 2007 The NetBSD Foundation, Inc.
      5  * All rights reserved.
      6  *
      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
      9  * 2005 program.
     10  *
     11  * Redistribution and use in source and binary forms, with or without
     12  * modification, are permitted provided that the following conditions
     13  * are met:
     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.
     19  *
     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.
     31  */
     32 
     33 /*
     34  * Efficient memory file system.
     35  *
     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.
     42  */
     43 
     44 #include <sys/cdefs.h>
     45 __KERNEL_RCSID(0, "$NetBSD: tmpfs_vfsops.c,v 1.62 2014/06/07 09:54:34 martin Exp $");
     46 
     47 #include <sys/param.h>
     48 #include <sys/types.h>
     49 #include <sys/kmem.h>
     50 #include <sys/mount.h>
     51 #include <sys/stat.h>
     52 #include <sys/systm.h>
     53 #include <sys/vnode.h>
     54 #include <sys/module.h>
     55 
     56 #include <miscfs/genfs/genfs.h>
     57 #include <fs/tmpfs/tmpfs.h>
     58 #include <fs/tmpfs/tmpfs_args.h>
     59 
     60 MODULE(MODULE_CLASS_VFS, tmpfs, NULL);
     61 
     62 struct pool	tmpfs_dirent_pool;
     63 struct pool	tmpfs_node_pool;
     64 
     65 static int	tmpfs_mount(struct mount *, const char *, void *, size_t *);
     66 static int	tmpfs_start(struct mount *, int);
     67 static int	tmpfs_unmount(struct mount *, int);
     68 static int	tmpfs_root(struct mount *, vnode_t **);
     69 static int	tmpfs_vget(struct mount *, ino_t, vnode_t **);
     70 static int	tmpfs_fhtovp(struct mount *, struct fid *, vnode_t **);
     71 static int	tmpfs_vptofh(struct vnode *, struct fid *, size_t *);
     72 static int	tmpfs_statvfs(struct mount *, struct statvfs *);
     73 static int	tmpfs_sync(struct mount *, int, kauth_cred_t);
     74 static void	tmpfs_init(void);
     75 static void	tmpfs_done(void);
     76 static int	tmpfs_snapshot(struct mount *, vnode_t *, struct timespec *);
     77 
     78 static void
     79 tmpfs_init(void)
     80 {
     81 
     82 	pool_init(&tmpfs_dirent_pool, sizeof(tmpfs_dirent_t), 0, 0, 0,
     83 	    "tmpfs_dirent", &pool_allocator_nointr, IPL_NONE);
     84 	pool_init(&tmpfs_node_pool, sizeof(tmpfs_node_t), 0, 0, 0,
     85 	    "tmpfs_node", &pool_allocator_nointr, IPL_NONE);
     86 }
     87 
     88 static void
     89 tmpfs_done(void)
     90 {
     91 
     92 	pool_destroy(&tmpfs_dirent_pool);
     93 	pool_destroy(&tmpfs_node_pool);
     94 }
     95 
     96 static int
     97 tmpfs_mount(struct mount *mp, const char *path, void *data, size_t *data_len)
     98 {
     99 	struct tmpfs_args *args = data;
    100 	tmpfs_mount_t *tmp;
    101 	tmpfs_node_t *root;
    102 	uint64_t memlimit;
    103 	ino_t nodes;
    104 	int error;
    105 
    106 	if (args == NULL)
    107 		return EINVAL;
    108 
    109 	/* Validate the version. */
    110 	if (*data_len < sizeof(*args) ||
    111 	    args->ta_version != TMPFS_ARGS_VERSION)
    112 		return EINVAL;
    113 
    114 	/* Handle retrieval of mount point arguments. */
    115 	if (mp->mnt_flag & MNT_GETARGS) {
    116 		if (mp->mnt_data == NULL)
    117 			return EIO;
    118 		tmp = VFS_TO_TMPFS(mp);
    119 
    120 		args->ta_version = TMPFS_ARGS_VERSION;
    121 		args->ta_nodes_max = tmp->tm_nodes_max;
    122 		args->ta_size_max = tmp->tm_mem_limit;
    123 
    124 		root = tmp->tm_root;
    125 		args->ta_root_uid = root->tn_uid;
    126 		args->ta_root_gid = root->tn_gid;
    127 		args->ta_root_mode = root->tn_mode;
    128 
    129 		*data_len = sizeof(*args);
    130 		return 0;
    131 	}
    132 
    133 
    134 	/* Prohibit mounts if there is not enough memory. */
    135 	if (tmpfs_mem_info(true) < uvmexp.freetarg)
    136 		return EINVAL;
    137 
    138 	/* Get the memory usage limit for this file-system. */
    139 	if (args->ta_size_max < PAGE_SIZE) {
    140 		memlimit = UINT64_MAX;
    141 	} else {
    142 		memlimit = args->ta_size_max;
    143 	}
    144 	KASSERT(memlimit > 0);
    145 
    146 	if (args->ta_nodes_max <= 3) {
    147 		nodes = 3 + (memlimit / 1024);
    148 	} else {
    149 		nodes = args->ta_nodes_max;
    150 	}
    151 	nodes = MIN(nodes, INT_MAX);
    152 	KASSERT(nodes >= 3);
    153 
    154 	if (mp->mnt_flag & MNT_UPDATE) {
    155 		tmp = VFS_TO_TMPFS(mp);
    156 		if (nodes < tmp->tm_nodes_cnt)
    157 			return EBUSY;
    158 		if ((error = tmpfs_mntmem_set(tmp, memlimit)) != 0)
    159 			return error;
    160 		tmp->tm_nodes_max = nodes;
    161 		root = tmp->tm_root;
    162 		root->tn_uid = args->ta_root_uid;
    163 		root->tn_gid = args->ta_root_gid;
    164 		root->tn_mode = args->ta_root_mode;
    165 		return 0;
    166 	}
    167 
    168 	/* Allocate the tmpfs mount structure and fill it. */
    169 	tmp = kmem_zalloc(sizeof(tmpfs_mount_t), KM_SLEEP);
    170 	if (tmp == NULL)
    171 		return ENOMEM;
    172 
    173 	tmp->tm_nodes_max = nodes;
    174 	tmp->tm_nodes_cnt = 0;
    175 	LIST_INIT(&tmp->tm_nodes);
    176 
    177 	mutex_init(&tmp->tm_lock, MUTEX_DEFAULT, IPL_NONE);
    178 	tmpfs_mntmem_init(tmp, memlimit);
    179 
    180 	/* Allocate the root node. */
    181 	error = tmpfs_alloc_node(tmp, VDIR, args->ta_root_uid,
    182 	    args->ta_root_gid, args->ta_root_mode & ALLPERMS, NULL,
    183 	    VNOVAL, &root);
    184 	KASSERT(error == 0 && root != NULL);
    185 
    186 	/*
    187 	 * Parent of the root inode is itself.  Also, root inode has no
    188 	 * directory entry (i.e. is never attached), thus hold an extra
    189 	 * reference (link) for it.
    190 	 */
    191 	root->tn_links++;
    192 	root->tn_spec.tn_dir.tn_parent = root;
    193 	tmp->tm_root = root;
    194 
    195 	mp->mnt_data = tmp;
    196 	mp->mnt_flag |= MNT_LOCAL;
    197 	mp->mnt_stat.f_namemax = TMPFS_MAXNAMLEN;
    198 	mp->mnt_fs_bshift = PAGE_SHIFT;
    199 	mp->mnt_dev_bshift = DEV_BSHIFT;
    200 	mp->mnt_iflag |= IMNT_MPSAFE;
    201 	vfs_getnewfsid(mp);
    202 
    203 	error = set_statvfs_info(path, UIO_USERSPACE, "tmpfs", UIO_SYSSPACE,
    204 	    mp->mnt_op->vfs_name, mp, curlwp);
    205 	if (error) {
    206 		(void)tmpfs_unmount(mp, MNT_FORCE);
    207 	}
    208 	return error;
    209 }
    210 
    211 static int
    212 tmpfs_start(struct mount *mp, int flags)
    213 {
    214 
    215 	return 0;
    216 }
    217 
    218 static int
    219 tmpfs_unmount(struct mount *mp, int mntflags)
    220 {
    221 	tmpfs_mount_t *tmp = VFS_TO_TMPFS(mp);
    222 	tmpfs_node_t *node, *cnode;
    223 	int error, flags = 0;
    224 
    225 	/* Handle forced unmounts. */
    226 	if (mntflags & MNT_FORCE)
    227 		flags |= FORCECLOSE;
    228 
    229 	/* Finalize all pending I/O. */
    230 	error = vflush(mp, NULL, flags);
    231 	if (error != 0)
    232 		return error;
    233 
    234 	/*
    235 	 * First round, detach and destroy all directory entries.
    236 	 * Also, clear the pointers to the vnodes - they are gone.
    237 	 */
    238 	LIST_FOREACH(node, &tmp->tm_nodes, tn_entries) {
    239 		tmpfs_dirent_t *de;
    240 
    241 		node->tn_vnode = NULL;
    242 		if (node->tn_type != VDIR) {
    243 			continue;
    244 		}
    245 		while ((de = TAILQ_FIRST(&node->tn_spec.tn_dir.tn_dir)) != NULL) {
    246 			cnode = de->td_node;
    247 			if (cnode && cnode != TMPFS_NODE_WHITEOUT) {
    248 				cnode->tn_vnode = NULL;
    249 			}
    250 			tmpfs_dir_detach(node, de);
    251 			tmpfs_free_dirent(tmp, de);
    252 		}
    253 		/* Extra virtual entry (itself for the root). */
    254 		node->tn_links--;
    255 	}
    256 
    257 	/* Release the reference on root (diagnostic). */
    258 	node = tmp->tm_root;
    259 	node->tn_links--;
    260 
    261 	/* Second round, destroy all inodes. */
    262 	while ((node = LIST_FIRST(&tmp->tm_nodes)) != NULL) {
    263 		tmpfs_free_node(tmp, node);
    264 	}
    265 
    266 	/* Throw away the tmpfs_mount structure. */
    267 	tmpfs_mntmem_destroy(tmp);
    268 	mutex_destroy(&tmp->tm_lock);
    269 	kmem_free(tmp, sizeof(*tmp));
    270 	mp->mnt_data = NULL;
    271 
    272 	return 0;
    273 }
    274 
    275 static int
    276 tmpfs_root(struct mount *mp, vnode_t **vpp)
    277 {
    278 	tmpfs_node_t *node = VFS_TO_TMPFS(mp)->tm_root;
    279 
    280 	mutex_enter(&node->tn_vlock);
    281 	return tmpfs_vnode_get(mp, node, vpp);
    282 }
    283 
    284 static int
    285 tmpfs_vget(struct mount *mp, ino_t ino, vnode_t **vpp)
    286 {
    287 
    288 	return EOPNOTSUPP;
    289 }
    290 
    291 static int
    292 tmpfs_fhtovp(struct mount *mp, struct fid *fhp, vnode_t **vpp)
    293 {
    294 	tmpfs_mount_t *tmp = VFS_TO_TMPFS(mp);
    295 	tmpfs_node_t *node;
    296 	tmpfs_fid_t tfh;
    297 	int error;
    298 
    299 	if (fhp->fid_len != sizeof(tmpfs_fid_t)) {
    300 		return EINVAL;
    301 	}
    302 	memcpy(&tfh, fhp, sizeof(tmpfs_fid_t));
    303 
    304 	mutex_enter(&tmp->tm_lock);
    305 	LIST_FOREACH(node, &tmp->tm_nodes, tn_entries) {
    306 		if (node->tn_id == tfh.tf_id) {
    307 			mutex_enter(&node->tn_vlock);
    308 			break;
    309 		}
    310 	}
    311 	mutex_exit(&tmp->tm_lock);
    312 
    313 	if (node == NULL)
    314 		return ESTALE;
    315 	/* Will release the tn_vlock. */
    316 	if ((error = tmpfs_vnode_get(mp, node, vpp)) != 0)
    317 		return error;
    318 	if (TMPFS_NODE_GEN(node) != tfh.tf_gen) {
    319 		vput(*vpp);
    320 		*vpp = NULL;
    321 		return ESTALE;
    322 	}
    323 
    324 	return 0;
    325 }
    326 
    327 static int
    328 tmpfs_vptofh(vnode_t *vp, struct fid *fhp, size_t *fh_size)
    329 {
    330 	tmpfs_fid_t tfh;
    331 	tmpfs_node_t *node;
    332 
    333 	if (*fh_size < sizeof(tmpfs_fid_t)) {
    334 		*fh_size = sizeof(tmpfs_fid_t);
    335 		return E2BIG;
    336 	}
    337 	*fh_size = sizeof(tmpfs_fid_t);
    338 	node = VP_TO_TMPFS_NODE(vp);
    339 
    340 	memset(&tfh, 0, sizeof(tfh));
    341 	tfh.tf_len = sizeof(tmpfs_fid_t);
    342 	tfh.tf_gen = TMPFS_NODE_GEN(node);
    343 	tfh.tf_id = node->tn_id;
    344 	memcpy(fhp, &tfh, sizeof(tfh));
    345 
    346 	return 0;
    347 }
    348 
    349 static int
    350 tmpfs_statvfs(struct mount *mp, struct statvfs *sbp)
    351 {
    352 	tmpfs_mount_t *tmp;
    353 	fsfilcnt_t freenodes;
    354 	size_t avail;
    355 
    356 	tmp = VFS_TO_TMPFS(mp);
    357 
    358 	sbp->f_iosize = sbp->f_frsize = sbp->f_bsize = PAGE_SIZE;
    359 
    360 	mutex_enter(&tmp->tm_acc_lock);
    361 	avail =  tmpfs_pages_avail(tmp);
    362 	sbp->f_blocks = (tmpfs_bytes_max(tmp) >> PAGE_SHIFT);
    363 	sbp->f_bavail = sbp->f_bfree = avail;
    364 	sbp->f_bresvd = 0;
    365 
    366 	freenodes = MIN(tmp->tm_nodes_max - tmp->tm_nodes_cnt,
    367 	    avail * PAGE_SIZE / sizeof(tmpfs_node_t));
    368 
    369 	sbp->f_files = tmp->tm_nodes_cnt + freenodes;
    370 	sbp->f_favail = sbp->f_ffree = freenodes;
    371 	sbp->f_fresvd = 0;
    372 	mutex_exit(&tmp->tm_acc_lock);
    373 
    374 	copy_statvfs_info(sbp, mp);
    375 
    376 	return 0;
    377 }
    378 
    379 static int
    380 tmpfs_sync(struct mount *mp, int waitfor, kauth_cred_t uc)
    381 {
    382 
    383 	return 0;
    384 }
    385 
    386 static int
    387 tmpfs_snapshot(struct mount *mp, vnode_t *vp, struct timespec *ctime)
    388 {
    389 
    390 	return EOPNOTSUPP;
    391 }
    392 
    393 /*
    394  * tmpfs vfs operations.
    395  */
    396 
    397 extern const struct vnodeopv_desc tmpfs_fifoop_opv_desc;
    398 extern const struct vnodeopv_desc tmpfs_specop_opv_desc;
    399 extern const struct vnodeopv_desc tmpfs_vnodeop_opv_desc;
    400 
    401 const struct vnodeopv_desc * const tmpfs_vnodeopv_descs[] = {
    402 	&tmpfs_fifoop_opv_desc,
    403 	&tmpfs_specop_opv_desc,
    404 	&tmpfs_vnodeop_opv_desc,
    405 	NULL,
    406 };
    407 
    408 struct vfsops tmpfs_vfsops = {
    409 	.vfs_name = MOUNT_TMPFS,
    410 	.vfs_min_mount_data = sizeof (struct tmpfs_args),
    411 	.vfs_mount = tmpfs_mount,
    412 	.vfs_start = tmpfs_start,
    413 	.vfs_unmount = tmpfs_unmount,
    414 	.vfs_root = tmpfs_root,
    415 	.vfs_quotactl = (void *)eopnotsupp,
    416 	.vfs_statvfs = tmpfs_statvfs,
    417 	.vfs_sync = tmpfs_sync,
    418 	.vfs_vget = tmpfs_vget,
    419 	.vfs_fhtovp = tmpfs_fhtovp,
    420 	.vfs_vptofh = tmpfs_vptofh,
    421 	.vfs_init = tmpfs_init,
    422 	.vfs_done = tmpfs_done,
    423 	.vfs_snapshot = tmpfs_snapshot,
    424 	.vfs_extattrctl = vfs_stdextattrctl,
    425 	.vfs_suspendctl = (void *)eopnotsupp,
    426 	.vfs_renamelock_enter = genfs_renamelock_enter,
    427 	.vfs_renamelock_exit = genfs_renamelock_exit,
    428 	.vfs_fsync = (void *)eopnotsupp,
    429 	.vfs_opv_descs = tmpfs_vnodeopv_descs
    430 };
    431 
    432 static int
    433 tmpfs_modcmd(modcmd_t cmd, void *arg)
    434 {
    435 
    436 	switch (cmd) {
    437 	case MODULE_CMD_INIT:
    438 		return vfs_attach(&tmpfs_vfsops);
    439 	case MODULE_CMD_FINI:
    440 		return vfs_detach(&tmpfs_vfsops);
    441 	default:
    442 		return ENOTTY;
    443 	}
    444 }
    445