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