Home | History | Annotate | Line # | Download | only in tmpfs
tmpfs_vfsops.c revision 1.30
      1 /*	$NetBSD: tmpfs_vfsops.c,v 1.30 2007/11/06 19:50:55 ad 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  * 3. All advertising materials mentioning features or use of this software
     20  *    must display the following acknowledgement:
     21  *        This product includes software developed by the NetBSD
     22  *        Foundation, Inc. and its contributors.
     23  * 4. Neither the name of The NetBSD Foundation nor the names of its
     24  *    contributors may be used to endorse or promote products derived
     25  *    from this software without specific prior written permission.
     26  *
     27  * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
     28  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
     29  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
     30  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
     31  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
     32  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
     33  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
     34  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
     35  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
     36  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
     37  * POSSIBILITY OF SUCH DAMAGE.
     38  */
     39 
     40 /*
     41  * Efficient memory file system.
     42  *
     43  * tmpfs is a file system that uses NetBSD's virtual memory sub-system
     44  * (the well-known UVM) to store file data and metadata in an efficient
     45  * way.  This means that it does not follow the structure of an on-disk
     46  * file system because it simply does not need to.  Instead, it uses
     47  * memory-specific data structures and algorithms to automatically
     48  * allocate and release resources.
     49  */
     50 
     51 #include <sys/cdefs.h>
     52 __KERNEL_RCSID(0, "$NetBSD: tmpfs_vfsops.c,v 1.30 2007/11/06 19:50:55 ad Exp $");
     53 
     54 #include <sys/param.h>
     55 #include <sys/types.h>
     56 #include <sys/kmem.h>
     57 #include <sys/mount.h>
     58 #include <sys/stat.h>
     59 #include <sys/systm.h>
     60 #include <sys/vnode.h>
     61 #include <sys/proc.h>
     62 
     63 #include <fs/tmpfs/tmpfs.h>
     64 
     65 /* --------------------------------------------------------------------- */
     66 
     67 static int	tmpfs_mount(struct mount *, const char *, void *, size_t *,
     68 		    struct lwp *);
     69 static int	tmpfs_start(struct mount *, int, struct lwp *);
     70 static int	tmpfs_unmount(struct mount *, int, struct lwp *);
     71 static int	tmpfs_root(struct mount *, struct vnode **);
     72 static int	tmpfs_quotactl(struct mount *, int, uid_t, void *,
     73 		    struct lwp *);
     74 static int	tmpfs_vget(struct mount *, ino_t, struct vnode **);
     75 static int	tmpfs_fhtovp(struct mount *, struct fid *, struct vnode **);
     76 static int	tmpfs_vptofh(struct vnode *, struct fid *, size_t *);
     77 static int	tmpfs_statvfs(struct mount *, struct statvfs *, struct lwp *);
     78 static int	tmpfs_sync(struct mount *, int, kauth_cred_t, struct lwp *);
     79 static void	tmpfs_init(void);
     80 static void	tmpfs_done(void);
     81 static int	tmpfs_snapshot(struct mount *, struct vnode *,
     82 		    struct timespec *);
     83 
     84 /* --------------------------------------------------------------------- */
     85 
     86 static int
     87 tmpfs_mount(struct mount *mp, const char *path, void *data, size_t *data_len,
     88     struct lwp *l)
     89 {
     90 	int error;
     91 	ino_t nodes;
     92 	size_t pages;
     93 	struct tmpfs_mount *tmp;
     94 	struct tmpfs_node *root;
     95 	struct tmpfs_args *args = data;
     96 
     97 	if (*data_len < sizeof *args)
     98 		return EINVAL;
     99 
    100 	/* Handle retrieval of mount point arguments. */
    101 	if (mp->mnt_flag & MNT_GETARGS) {
    102 		if (mp->mnt_data == NULL)
    103 			return EIO;
    104 		tmp = VFS_TO_TMPFS(mp);
    105 
    106 		args->ta_version = TMPFS_ARGS_VERSION;
    107 		mutex_enter(&tmp->tm_lock);
    108 		args->ta_nodes_max = tmp->tm_nodes_max;
    109 		args->ta_size_max = tmp->tm_pages_max * PAGE_SIZE;
    110 		root = tmp->tm_root;
    111 		mutex_exit(&tmp->tm_lock);
    112 
    113 		args->ta_root_uid = root->tn_uid;
    114 		args->ta_root_gid = root->tn_gid;
    115 		args->ta_root_mode = root->tn_mode;
    116 
    117 		*data_len = sizeof *args;
    118 		return 0;
    119 	}
    120 
    121 	if (mp->mnt_flag & MNT_UPDATE) {
    122 		/* XXX: There is no support yet to update file system
    123 		 * settings.  Should be added. */
    124 
    125 		return EOPNOTSUPP;
    126 	}
    127 
    128 	if (args->ta_version != TMPFS_ARGS_VERSION)
    129 		return EINVAL;
    130 
    131 	/* Do not allow mounts if we do not have enough memory to preserve
    132 	 * the minimum reserved pages. */
    133 	if (tmpfs_mem_info(true) < TMPFS_PAGES_RESERVED)
    134 		return EINVAL;
    135 
    136 	/* Get the maximum number of memory pages this file system is
    137 	 * allowed to use, based on the maximum size the user passed in
    138 	 * the mount structure.  A value of zero is treated as if the
    139 	 * maximum available space was requested. */
    140 	if (args->ta_size_max < PAGE_SIZE || args->ta_size_max >= SIZE_MAX)
    141 		pages = SIZE_MAX;
    142 	else
    143 		pages = args->ta_size_max / PAGE_SIZE +
    144 		    (args->ta_size_max % PAGE_SIZE == 0 ? 0 : 1);
    145 	KASSERT(pages > 0);
    146 
    147 	if (args->ta_nodes_max <= 3)
    148 		nodes = 3 + pages * PAGE_SIZE / 1024;
    149 	else
    150 		nodes = args->ta_nodes_max;
    151 	KASSERT(nodes >= 3);
    152 
    153 	/* Allocate the tmpfs mount structure and fill it. */
    154 	tmp = kmem_alloc(sizeof(struct tmpfs_mount), KM_SLEEP);
    155 	if (tmp == NULL)
    156 		return ENOMEM;
    157 
    158 	tmp->tm_nodes_max = nodes;
    159 	tmp->tm_nodes_cnt = 0;
    160 	LIST_INIT(&tmp->tm_nodes);
    161 
    162 	mutex_init(&tmp->tm_lock, MUTEX_DEFAULT, IPL_NONE);
    163 
    164 	tmp->tm_pages_max = pages;
    165 	tmp->tm_pages_used = 0;
    166 	tmpfs_pool_init(&tmp->tm_dirent_pool, sizeof(struct tmpfs_dirent),
    167 	    "dirent", tmp);
    168 	tmpfs_pool_init(&tmp->tm_node_pool, sizeof(struct tmpfs_node),
    169 	    "node", tmp);
    170 	tmpfs_str_pool_init(&tmp->tm_str_pool, tmp);
    171 
    172 	/* Allocate the root node. */
    173 	error = tmpfs_alloc_node(tmp, VDIR, args->ta_root_uid,
    174 	    args->ta_root_gid, args->ta_root_mode & ALLPERMS, NULL, NULL,
    175 	    VNOVAL, l->l_proc, &root);
    176 	KASSERT(error == 0 && root != NULL);
    177 	tmp->tm_root = root;
    178 
    179 	mp->mnt_data = tmp;
    180 	mp->mnt_flag |= MNT_LOCAL;
    181 	mp->mnt_stat.f_namemax = MAXNAMLEN;
    182 	mp->mnt_fs_bshift = PAGE_SHIFT;
    183 	mp->mnt_dev_bshift = DEV_BSHIFT;
    184 	mp->mnt_iflag |= IMNT_MPSAFE;
    185 	vfs_getnewfsid(mp);
    186 
    187 	return set_statvfs_info(path, UIO_USERSPACE, "tmpfs", UIO_SYSSPACE,
    188 	    mp->mnt_op->vfs_name, mp, l);
    189 }
    190 
    191 /* --------------------------------------------------------------------- */
    192 
    193 static int
    194 tmpfs_start(struct mount *mp, int flags,
    195     struct lwp *l)
    196 {
    197 
    198 	return 0;
    199 }
    200 
    201 /* --------------------------------------------------------------------- */
    202 
    203 /* ARGSUSED2 */
    204 static int
    205 tmpfs_unmount(struct mount *mp, int mntflags, struct lwp *l)
    206 {
    207 	int error;
    208 	int flags = 0;
    209 	struct tmpfs_mount *tmp;
    210 	struct tmpfs_node *node;
    211 
    212 	/* Handle forced unmounts. */
    213 	if (mntflags & MNT_FORCE)
    214 		flags |= FORCECLOSE;
    215 
    216 	/* Finalize all pending I/O. */
    217 	error = vflush(mp, NULL, flags);
    218 	if (error != 0)
    219 		return error;
    220 
    221 	tmp = VFS_TO_TMPFS(mp);
    222 
    223 	/* Free all associated data.  The loop iterates over the linked list
    224 	 * we have containing all used nodes.  For each of them that is
    225 	 * a directory, we free all its directory entries.  Note that after
    226 	 * freeing a node, it will automatically go to the available list,
    227 	 * so we will later have to iterate over it to release its items. */
    228 	while ((node = LIST_FIRST(&tmp->tm_nodes)) != NULL) {
    229 		if (node->tn_type == VDIR) {
    230 			struct tmpfs_dirent *de;
    231 
    232 			de = TAILQ_FIRST(&node->tn_spec.tn_dir.tn_dir);
    233 			while (de != NULL) {
    234 				struct tmpfs_dirent *nde;
    235 
    236 				nde = TAILQ_NEXT(de, td_entries);
    237 				KASSERT(de->td_node->tn_vnode == NULL);
    238 				tmpfs_free_dirent(tmp, de, false);
    239 				de = nde;
    240 				node->tn_size -= sizeof(struct tmpfs_dirent);
    241 			}
    242 		}
    243 
    244 		tmpfs_free_node(tmp, node);
    245 	}
    246 
    247 	tmpfs_pool_destroy(&tmp->tm_dirent_pool);
    248 	tmpfs_pool_destroy(&tmp->tm_node_pool);
    249 	tmpfs_str_pool_destroy(&tmp->tm_str_pool);
    250 
    251 	KASSERT(tmp->tm_pages_used == 0);
    252 
    253 	/* Throw away the tmpfs_mount structure. */
    254 	mutex_destroy(&tmp->tm_lock);
    255 	kmem_free(tmp, sizeof(*tmp));
    256 	mp->mnt_data = NULL;
    257 
    258 	return 0;
    259 }
    260 
    261 /* --------------------------------------------------------------------- */
    262 
    263 static int
    264 tmpfs_root(struct mount *mp, struct vnode **vpp)
    265 {
    266 
    267 	return tmpfs_alloc_vp(mp, VFS_TO_TMPFS(mp)->tm_root, vpp);
    268 }
    269 
    270 /* --------------------------------------------------------------------- */
    271 
    272 static int
    273 tmpfs_quotactl(struct mount *mp, int cmd, uid_t uid,
    274     void *arg, struct lwp *l)
    275 {
    276 
    277 	printf("tmpfs_quotactl called; need for it unknown yet\n");
    278 	return EOPNOTSUPP;
    279 }
    280 
    281 /* --------------------------------------------------------------------- */
    282 
    283 static int
    284 tmpfs_vget(struct mount *mp, ino_t ino,
    285     struct vnode **vpp)
    286 {
    287 
    288 	printf("tmpfs_vget called; need for it unknown yet\n");
    289 	return EOPNOTSUPP;
    290 }
    291 
    292 /* --------------------------------------------------------------------- */
    293 
    294 static int
    295 tmpfs_fhtovp(struct mount *mp, struct fid *fhp, struct vnode **vpp)
    296 {
    297 	bool found;
    298 	struct tmpfs_fid tfh;
    299 	struct tmpfs_mount *tmp;
    300 	struct tmpfs_node *node;
    301 
    302 	tmp = VFS_TO_TMPFS(mp);
    303 
    304 	if (fhp->fid_len != sizeof(struct tmpfs_fid))
    305 		return EINVAL;
    306 
    307 	memcpy(&tfh, fhp, sizeof(struct tmpfs_fid));
    308 
    309 	mutex_enter(&tmp->tm_lock);
    310 	if (tfh.tf_id >= tmp->tm_nodes_max) {
    311 		mutex_exit(&tmp->tm_lock);
    312 		return EINVAL;
    313 	}
    314 
    315 	found = false;
    316 	LIST_FOREACH(node, &tmp->tm_nodes, tn_entries) {
    317 		if (node->tn_id == tfh.tf_id &&
    318 		    node->tn_gen == tfh.tf_gen) {
    319 			found = true;
    320 			break;
    321 		}
    322 	}
    323 	mutex_exit(&tmp->tm_lock);
    324 
    325 	/* XXXAD nothing to prevent 'node' from being removed. */
    326 	return found ? tmpfs_alloc_vp(mp, node, vpp) : EINVAL;
    327 }
    328 
    329 /* --------------------------------------------------------------------- */
    330 
    331 static int
    332 tmpfs_vptofh(struct vnode *vp, struct fid *fhp, size_t *fh_size)
    333 {
    334 	struct tmpfs_fid tfh;
    335 	struct tmpfs_node *node;
    336 
    337 	if (*fh_size < sizeof(struct tmpfs_fid)) {
    338 		*fh_size = sizeof(struct tmpfs_fid);
    339 		return E2BIG;
    340 	}
    341 	*fh_size = sizeof(struct tmpfs_fid);
    342 	node = VP_TO_TMPFS_NODE(vp);
    343 
    344 	memset(&tfh, 0, sizeof(tfh));
    345 	tfh.tf_len = sizeof(struct tmpfs_fid);
    346 	tfh.tf_gen = node->tn_gen;
    347 	tfh.tf_id = node->tn_id;
    348 	memcpy(fhp, &tfh, sizeof(tfh));
    349 
    350 	return 0;
    351 }
    352 
    353 /* --------------------------------------------------------------------- */
    354 
    355 /* ARGSUSED2 */
    356 static int
    357 tmpfs_statvfs(struct mount *mp, struct statvfs *sbp, struct lwp *l)
    358 {
    359 	fsfilcnt_t freenodes;
    360 	struct tmpfs_mount *tmp;
    361 
    362 	tmp = VFS_TO_TMPFS(mp);
    363 
    364 	mutex_enter(&tmp->tm_lock);
    365 
    366 	sbp->f_iosize = sbp->f_frsize = sbp->f_bsize = PAGE_SIZE;
    367 
    368 	sbp->f_blocks = TMPFS_PAGES_MAX(tmp);
    369 	sbp->f_bavail = sbp->f_bfree = TMPFS_PAGES_AVAIL(tmp);
    370 	sbp->f_bresvd = 0;
    371 
    372 	freenodes = MIN(tmp->tm_nodes_max - tmp->tm_nodes_cnt,
    373 	    TMPFS_PAGES_AVAIL(tmp) * PAGE_SIZE / sizeof(struct tmpfs_node));
    374 
    375 	sbp->f_files = tmp->tm_nodes_cnt + freenodes;
    376 	sbp->f_favail = sbp->f_ffree = freenodes;
    377 	sbp->f_fresvd = 0;
    378 
    379 	mutex_exit(&tmp->tm_lock);
    380 
    381 	copy_statvfs_info(sbp, mp);
    382 
    383 	return 0;
    384 }
    385 
    386 /* --------------------------------------------------------------------- */
    387 
    388 /* ARGSUSED0 */
    389 static int
    390 tmpfs_sync(struct mount *mp, int waitfor,
    391     kauth_cred_t uc, struct lwp *l)
    392 {
    393 
    394 	return 0;
    395 }
    396 
    397 /* --------------------------------------------------------------------- */
    398 
    399 static void
    400 tmpfs_init(void)
    401 {
    402 
    403 }
    404 
    405 /* --------------------------------------------------------------------- */
    406 
    407 static void
    408 tmpfs_done(void)
    409 {
    410 
    411 }
    412 
    413 /* --------------------------------------------------------------------- */
    414 
    415 static int
    416 tmpfs_snapshot(struct mount *mp, struct vnode *vp,
    417     struct timespec *ctime)
    418 {
    419 
    420 	return EOPNOTSUPP;
    421 }
    422 
    423 /* --------------------------------------------------------------------- */
    424 
    425 /*
    426  * tmpfs vfs operations.
    427  */
    428 
    429 extern const struct vnodeopv_desc tmpfs_fifoop_opv_desc;
    430 extern const struct vnodeopv_desc tmpfs_specop_opv_desc;
    431 extern const struct vnodeopv_desc tmpfs_vnodeop_opv_desc;
    432 
    433 const struct vnodeopv_desc * const tmpfs_vnodeopv_descs[] = {
    434 	&tmpfs_fifoop_opv_desc,
    435 	&tmpfs_specop_opv_desc,
    436 	&tmpfs_vnodeop_opv_desc,
    437 	NULL,
    438 };
    439 
    440 struct vfsops tmpfs_vfsops = {
    441 	MOUNT_TMPFS,			/* vfs_name */
    442 	sizeof (struct tmpfs_args),
    443 	tmpfs_mount,			/* vfs_mount */
    444 	tmpfs_start,			/* vfs_start */
    445 	tmpfs_unmount,			/* vfs_unmount */
    446 	tmpfs_root,			/* vfs_root */
    447 	tmpfs_quotactl,			/* vfs_quotactl */
    448 	tmpfs_statvfs,			/* vfs_statvfs */
    449 	tmpfs_sync,			/* vfs_sync */
    450 	tmpfs_vget,			/* vfs_vget */
    451 	tmpfs_fhtovp,			/* vfs_fhtovp */
    452 	tmpfs_vptofh,			/* vfs_vptofh */
    453 	tmpfs_init,			/* vfs_init */
    454 	NULL,				/* vfs_reinit */
    455 	tmpfs_done,			/* vfs_done */
    456 	NULL,				/* vfs_mountroot */
    457 	tmpfs_snapshot,			/* vfs_snapshot */
    458 	vfs_stdextattrctl,		/* vfs_extattrctl */
    459 	(void *)eopnotsupp,		/* vfs_suspendctl */
    460 	tmpfs_vnodeopv_descs,
    461 	0,				/* vfs_refcount */
    462 	{ NULL, NULL },
    463 };
    464 VFS_ATTACH(tmpfs_vfsops);
    465