Home | History | Annotate | Line # | Download | only in tmpfs
tmpfs_vfsops.c revision 1.6
      1 /*	$NetBSD: tmpfs_vfsops.c,v 1.6 2005/09/23 15:36:15 jmmv Exp $	*/
      2 
      3 /*
      4  * Copyright (c) 2005 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.6 2005/09/23 15:36:15 jmmv Exp $");
     53 
     54 #include <sys/param.h>
     55 #include <sys/types.h>
     56 #include <sys/malloc.h>
     57 #include <sys/mount.h>
     58 #include <sys/systm.h>
     59 #include <sys/vnode.h>
     60 
     61 #include <fs/tmpfs/tmpfs.h>
     62 
     63 MALLOC_DEFINE(M_TMPFSMNT, "tmpfs mount", "tmpfs mount structures");
     64 
     65 /* --------------------------------------------------------------------- */
     66 
     67 static int	tmpfs_mount(struct mount *, const char *, void *,
     68 		    struct nameidata *, struct proc *);
     69 static int	tmpfs_start(struct mount *, int, struct proc *);
     70 static int	tmpfs_unmount(struct mount *, int, struct proc *);
     71 static int	tmpfs_root(struct mount *, struct vnode **);
     72 static int	tmpfs_quotactl(struct mount *, int, uid_t, void *,
     73 		    struct proc *);
     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 *);
     77 static int	tmpfs_statvfs(struct mount *, struct statvfs *, struct proc *);
     78 static int	tmpfs_sync(struct mount *, int, struct ucred *, struct proc *);
     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,
     88     struct nameidata *ndp, struct proc *p)
     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;
     96 
     97 	/* Handle retrieval of mount point arguments. */
     98 	if (mp->mnt_flag & MNT_GETARGS) {
     99 		if (mp->mnt_data == NULL)
    100 			return EIO;
    101 		tmp = VFS_TO_TMPFS(mp);
    102 
    103 		args.ta_version = TMPFS_ARGS_VERSION;
    104 		args.ta_nodes_max = tmp->tm_nodes_max;
    105 		args.ta_size_max = tmp->tm_pages_max * PAGE_SIZE;
    106 
    107 		root = tmp->tm_root;
    108 		args.ta_root_uid = root->tn_uid;
    109 		args.ta_root_gid = root->tn_gid;
    110 		args.ta_root_mode = root->tn_mode;
    111 
    112 		return copyout(&args, data, sizeof(args));
    113 	}
    114 
    115 	/* Verify that we have parameters, as they are required. */
    116 	if (data == NULL)
    117 		return EINVAL;
    118 
    119 	/* Get the mount parameters. */
    120 	error = copyin(data, &args, sizeof(args));
    121 	if (error != 0)
    122 		return error;
    123 
    124 	if (mp->mnt_flag & MNT_UPDATE) {
    125 		/* XXX: There is no support yet to update file system
    126 		 * settings.  Should be added. */
    127 
    128 		return EOPNOTSUPP;
    129 	}
    130 
    131 	if (args.ta_version != TMPFS_ARGS_VERSION)
    132 		return EINVAL;
    133 
    134 	/* Do not allow mounts if we do not have enough memory to preserve
    135 	 * the minimum reserved pages. */
    136 	if (tmpfs_mem_info(TRUE) < TMPFS_PAGES_RESERVED)
    137 		return EINVAL;
    138 
    139 	/* Get the maximum number of memory pages this file system is
    140 	 * allowed to use, based on the maximum size the user passed in
    141 	 * the mount structure.  A value of zero is treated as if the
    142 	 * maximum available space was requested. */
    143 	if (args.ta_size_max == 0)
    144 		pages = SIZE_MAX;
    145 	else
    146 		pages = args.ta_size_max / PAGE_SIZE +
    147 		    (args.ta_size_max % PAGE_SIZE == 0 ? 0 : 1);
    148 
    149 	if (args.ta_nodes_max == 0)
    150 		nodes = pages * PAGE_SIZE / 1024;
    151 	else
    152 		nodes = args.ta_nodes_max;
    153 
    154 	/* Allocate the tmpfs mount structure and fill it. */
    155 	tmp = (struct tmpfs_mount *)malloc(sizeof(struct tmpfs_mount),
    156 	    M_TMPFSMNT, M_WAITOK);
    157 	KASSERT(tmp != NULL);
    158 
    159 	tmp->tm_nodes_max = nodes;
    160 	tmp->tm_nodes_last = 2;
    161 	LIST_INIT(&tmp->tm_nodes_used);
    162 	LIST_INIT(&tmp->tm_nodes_avail);
    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, NULL, NULL, VNOVAL, p,
    175 	    &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 	vfs_getnewfsid(mp);
    183 
    184 	return set_statvfs_info(path, UIO_USERSPACE, "tmpfs", UIO_SYSSPACE,
    185 	    mp, p);
    186 }
    187 
    188 /* --------------------------------------------------------------------- */
    189 
    190 static int
    191 tmpfs_start(struct mount *mp, int flags, struct proc *p)
    192 {
    193 
    194 	return 0;
    195 }
    196 
    197 /* --------------------------------------------------------------------- */
    198 
    199 /* ARGSUSED2 */
    200 static int
    201 tmpfs_unmount(struct mount *mp, int mntflags, struct proc *p)
    202 {
    203 	int error;
    204 	int flags = 0;
    205 	struct tmpfs_mount *tmp;
    206 	struct tmpfs_node *node;
    207 
    208 	/* Handle forced unmounts. */
    209 	if (mntflags & MNT_FORCE)
    210 		flags |= FORCECLOSE;
    211 
    212 	/* Finalize all pending I/O. */
    213 	error = vflush(mp, NULL, flags);
    214 	if (error != 0)
    215 		return error;
    216 
    217 	tmp = VFS_TO_TMPFS(mp);
    218 
    219 	/* Free all associated data.  The loop iterates over the linked list
    220 	 * we have containing all used nodes.  For each of them that is
    221 	 * a directory, we free all its directory entries.  Note that after
    222 	 * freeing a node, it will automatically go to the available list,
    223 	 * so we will later have to iterate over it to release its items. */
    224 	node = LIST_FIRST(&tmp->tm_nodes_used);
    225 	while (node != NULL) {
    226 		struct tmpfs_node *next;
    227 
    228 		if (node->tn_type == VDIR) {
    229 			struct tmpfs_dirent *de;
    230 
    231 			de = TAILQ_FIRST(&node->tn_dir);
    232 			while (de != NULL) {
    233 				struct tmpfs_dirent *nde;
    234 
    235 				nde = TAILQ_NEXT(de, td_entries);
    236 				tmpfs_free_dirent(tmp, de, FALSE);
    237 				de = nde;
    238 				node->tn_size -= sizeof(struct tmpfs_dirent);
    239 			}
    240 		}
    241 
    242 		next = LIST_NEXT(node, tn_entries);
    243 		tmpfs_free_node(tmp, node);
    244 		node = next;
    245 	}
    246 	node = LIST_FIRST(&tmp->tm_nodes_avail);
    247 	while (node != NULL) {
    248 		struct tmpfs_node *next;
    249 
    250 		next = LIST_NEXT(node, tn_entries);
    251 		LIST_REMOVE(node, tn_entries);
    252 		TMPFS_POOL_PUT(&tmp->tm_node_pool, node);
    253 		node = next;
    254 	}
    255 
    256 	tmpfs_pool_destroy(&tmp->tm_dirent_pool);
    257 	tmpfs_pool_destroy(&tmp->tm_node_pool);
    258 	tmpfs_str_pool_destroy(&tmp->tm_str_pool);
    259 
    260 	KASSERT(tmp->tm_pages_used == 0);
    261 
    262 	/* Throw away the tmpfs_mount structure. */
    263 	free(mp->mnt_data, M_TMPFSMNT);
    264 	mp->mnt_data = NULL;
    265 
    266 	return 0;
    267 }
    268 
    269 /* --------------------------------------------------------------------- */
    270 
    271 static int
    272 tmpfs_root(struct mount *mp, struct vnode **vpp)
    273 {
    274 
    275 	return tmpfs_alloc_vp(mp, VFS_TO_TMPFS(mp)->tm_root, vpp);
    276 }
    277 
    278 /* --------------------------------------------------------------------- */
    279 
    280 static int
    281 tmpfs_quotactl(struct mount *mp, int cmd, uid_t uid, void *arg,
    282     struct proc *p)
    283 {
    284 
    285 	printf("tmpfs_quotactl called; need for it unknown yet\n");
    286 	return EOPNOTSUPP;
    287 }
    288 
    289 /* --------------------------------------------------------------------- */
    290 
    291 static int
    292 tmpfs_vget(struct mount *mp, ino_t ino, struct vnode **vpp)
    293 {
    294 
    295 	printf("tmpfs_vget called; need for it unknown yet\n");
    296 	return EOPNOTSUPP;
    297 }
    298 
    299 /* --------------------------------------------------------------------- */
    300 
    301 static int
    302 tmpfs_fhtovp(struct mount *mp, struct fid *fhp, struct vnode **vpp)
    303 {
    304 	boolean_t found;
    305 	struct tmpfs_fid *tfhp;
    306 	struct tmpfs_mount *tmp;
    307 	struct tmpfs_node *node;
    308 
    309 	tmp = VFS_TO_TMPFS(mp);
    310 
    311 	tfhp = (struct tmpfs_fid *)fhp;
    312 	if (tfhp->tf_len != sizeof(struct tmpfs_fid))
    313 		return EINVAL;
    314 
    315 	if (tfhp->tf_id >= tmp->tm_nodes_max)
    316 		return EINVAL;
    317 
    318 	found = FALSE;
    319 	LIST_FOREACH(node, &tmp->tm_nodes_used, tn_entries) {
    320 		if (node->tn_id == tfhp->tf_id &&
    321 		    node->tn_gen == tfhp->tf_gen) {
    322 			found = TRUE;
    323 			break;
    324 		}
    325 	}
    326 
    327 	return found ? tmpfs_alloc_vp(mp, node, vpp) : EINVAL;
    328 }
    329 
    330 /* --------------------------------------------------------------------- */
    331 
    332 static int
    333 tmpfs_vptofh(struct vnode *vp, struct fid *fhp)
    334 {
    335 	struct tmpfs_fid *tfhp;
    336 	struct tmpfs_node *node;
    337 
    338 	tfhp = (struct tmpfs_fid *)fhp;
    339 	node = VP_TO_TMPFS_NODE(vp);
    340 
    341 	tfhp->tf_len = sizeof(struct tmpfs_fid);
    342 	tfhp->tf_id = node->tn_id;
    343 	tfhp->tf_gen = node->tn_gen;
    344 
    345 	return 0;
    346 }
    347 
    348 /* --------------------------------------------------------------------- */
    349 
    350 /* ARGSUSED2 */
    351 static int
    352 tmpfs_statvfs(struct mount *mp, struct statvfs *sbp, struct proc *p)
    353 {
    354 	fsfilcnt_t freenodes, usednodes;
    355 	struct tmpfs_mount *tmp;
    356 	struct tmpfs_node *dummy;
    357 
    358 	tmp = VFS_TO_TMPFS(mp);
    359 
    360 	sbp->f_iosize = sbp->f_frsize = sbp->f_bsize = PAGE_SIZE;
    361 
    362 	sbp->f_blocks = TMPFS_PAGES_MAX(tmp);
    363 	sbp->f_bavail = sbp->f_bfree = TMPFS_PAGES_AVAIL(tmp);
    364 	sbp->f_bresvd = 0;
    365 
    366 	freenodes = MIN(tmp->tm_nodes_max - tmp->tm_nodes_last,
    367 	    TMPFS_PAGES_AVAIL(tmp) * PAGE_SIZE / sizeof(struct tmpfs_node));
    368 	LIST_FOREACH(dummy, &tmp->tm_nodes_avail, tn_entries)
    369 		freenodes++;
    370 
    371 	usednodes = 0;
    372 	LIST_FOREACH(dummy, &tmp->tm_nodes_used, tn_entries)
    373 		usednodes++;
    374 
    375 	sbp->f_files = freenodes + usednodes;
    376 	sbp->f_favail = sbp->f_ffree = freenodes;
    377 	sbp->f_fresvd = 0;
    378 
    379 	copy_statvfs_info(sbp, mp);
    380 
    381 	return 0;
    382 }
    383 
    384 /* --------------------------------------------------------------------- */
    385 
    386 /* ARGSUSED0 */
    387 static int
    388 tmpfs_sync(struct mount *mp, int waitfor, struct ucred *uc, struct proc *p)
    389 {
    390 
    391 	return 0;
    392 }
    393 
    394 /* --------------------------------------------------------------------- */
    395 
    396 static void
    397 tmpfs_init(void)
    398 {
    399 
    400 #ifdef _LKM
    401 	malloc_type_attach(M_TMPFSMNT);
    402 #endif
    403 }
    404 
    405 /* --------------------------------------------------------------------- */
    406 
    407 static void
    408 tmpfs_done(void)
    409 {
    410 
    411 #ifdef _LKM
    412 	malloc_type_detach(M_TMPFSMNT);
    413 #endif
    414 }
    415 
    416 /* --------------------------------------------------------------------- */
    417 
    418 static int
    419 tmpfs_snapshot(struct mount *mp, struct vnode *vp, struct timespec *ctime)
    420 {
    421 
    422 	return EOPNOTSUPP;
    423 }
    424 
    425 /* --------------------------------------------------------------------- */
    426 
    427 /*
    428  * tmpfs vfs operations.
    429  */
    430 
    431 extern const struct vnodeopv_desc tmpfs_fifoop_opv_desc;
    432 extern const struct vnodeopv_desc tmpfs_specop_opv_desc;
    433 extern const struct vnodeopv_desc tmpfs_vnodeop_opv_desc;
    434 
    435 const struct vnodeopv_desc * const tmpfs_vnodeopv_descs[] = {
    436 	&tmpfs_fifoop_opv_desc,
    437 	&tmpfs_specop_opv_desc,
    438 	&tmpfs_vnodeop_opv_desc,
    439 	NULL,
    440 };
    441 
    442 struct vfsops tmpfs_vfsops = {
    443 	MOUNT_TMPFS,			/* vfs_name */
    444 	tmpfs_mount,			/* vfs_mount */
    445 	tmpfs_start,			/* vfs_start */
    446 	tmpfs_unmount,			/* vfs_unmount */
    447 	tmpfs_root,			/* vfs_root */
    448 	tmpfs_quotactl,			/* vfs_quotactl */
    449 	tmpfs_statvfs,			/* vfs_statvfs */
    450 	tmpfs_sync,			/* vfs_sync */
    451 	tmpfs_vget,			/* vfs_vget */
    452 	tmpfs_fhtovp,			/* vfs_fhtovp */
    453 	tmpfs_vptofh,			/* vfs_vptofh */
    454 	tmpfs_init,			/* vfs_init */
    455 	NULL,				/* vfs_reinit */
    456 	tmpfs_done,			/* vfs_done */
    457 	NULL,				/* vfs_mountroot */
    458 	tmpfs_snapshot,			/* vfs_snapshot */
    459 	vfs_stdextattrctl,		/* vfs_extattrctl */
    460 	tmpfs_vnodeopv_descs,
    461 };
    462 VFS_ATTACH(tmpfs_vfsops);
    463