Home | History | Annotate | Line # | Download | only in tmpfs
tmpfs_vfsops.c revision 1.46
      1 /*	$NetBSD: tmpfs_vfsops.c,v 1.46 2010/06/26 03:38:14 rmind 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.46 2010/06/26 03:38:14 rmind 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/proc.h>
     55 #include <sys/module.h>
     56 
     57 #include <miscfs/genfs/genfs.h>
     58 #include <fs/tmpfs/tmpfs.h>
     59 #include <fs/tmpfs/tmpfs_args.h>
     60 
     61 MODULE(MODULE_CLASS_VFS, tmpfs, NULL);
     62 
     63 /* --------------------------------------------------------------------- */
     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 *, struct vnode **);
     69 static int	tmpfs_vget(struct mount *, ino_t, struct vnode **);
     70 static int	tmpfs_fhtovp(struct mount *, struct fid *, struct vnode **);
     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 *, struct vnode *,
     77 		    struct timespec *);
     78 
     79 /* --------------------------------------------------------------------- */
     80 
     81 static int
     82 tmpfs_mount(struct mount *mp, const char *path, void *data, size_t *data_len)
     83 {
     84 	struct tmpfs_mount *tmp;
     85 	struct tmpfs_node *root;
     86 	struct tmpfs_args *args = data;
     87 	uint64_t memlimit;
     88 	ino_t nodes;
     89 	int error;
     90 
     91 	if (*data_len < sizeof *args)
     92 		return EINVAL;
     93 
     94 	/* Handle retrieval of mount point arguments. */
     95 	if (mp->mnt_flag & MNT_GETARGS) {
     96 		if (mp->mnt_data == NULL)
     97 			return EIO;
     98 		tmp = VFS_TO_TMPFS(mp);
     99 
    100 		args->ta_version = TMPFS_ARGS_VERSION;
    101 		args->ta_nodes_max = tmp->tm_nodes_max;
    102 		args->ta_size_max = tmp->tm_mem_limit;
    103 
    104 		root = tmp->tm_root;
    105 		args->ta_root_uid = root->tn_uid;
    106 		args->ta_root_gid = root->tn_gid;
    107 		args->ta_root_mode = root->tn_mode;
    108 
    109 		*data_len = sizeof(*args);
    110 		return 0;
    111 	}
    112 
    113 	if (mp->mnt_flag & MNT_UPDATE) {
    114 		/* XXX: There is no support yet to update file system
    115 		 * settings.  Should be added. */
    116 
    117 		return EOPNOTSUPP;
    118 	}
    119 
    120 	if (args->ta_version != TMPFS_ARGS_VERSION)
    121 		return EINVAL;
    122 
    123 	/* Do not allow mounts if we do not have enough memory to preserve
    124 	 * the minimum reserved pages. */
    125 	if (tmpfs_mem_info(true) < TMPFS_PAGES_RESERVED)
    126 		return EINVAL;
    127 
    128 	/* Get the memory usage limit for this file-system. */
    129 	if (args->ta_size_max < PAGE_SIZE) {
    130 		memlimit = UINT64_MAX;
    131 	} else {
    132 		memlimit = args->ta_size_max;
    133 	}
    134 	KASSERT(memlimit > 0);
    135 
    136 	if (args->ta_nodes_max <= 3) {
    137 		nodes = 3 + (memlimit / 1024);
    138 	} else {
    139 		nodes = args->ta_nodes_max;
    140 	}
    141 	nodes = MIN(nodes, INT_MAX);
    142 	KASSERT(nodes >= 3);
    143 
    144 	/* Allocate the tmpfs mount structure and fill it. */
    145 	tmp = kmem_alloc(sizeof(struct tmpfs_mount), KM_SLEEP);
    146 	if (tmp == NULL)
    147 		return ENOMEM;
    148 
    149 	tmp->tm_nodes_max = nodes;
    150 	tmp->tm_nodes_cnt = 0;
    151 	LIST_INIT(&tmp->tm_nodes);
    152 
    153 	mutex_init(&tmp->tm_lock, MUTEX_DEFAULT, IPL_NONE);
    154 	tmpfs_mntmem_init(tmp, memlimit);
    155 
    156 	/* Allocate the root node. */
    157 	error = tmpfs_alloc_node(tmp, VDIR, args->ta_root_uid,
    158 	    args->ta_root_gid, args->ta_root_mode & ALLPERMS, NULL, NULL,
    159 	    VNOVAL, &root);
    160 	KASSERT(error == 0 && root != NULL);
    161 	root->tn_links++;
    162 	tmp->tm_root = root;
    163 
    164 	mp->mnt_data = tmp;
    165 	mp->mnt_flag |= MNT_LOCAL;
    166 	mp->mnt_stat.f_namemax = MAXNAMLEN;
    167 	mp->mnt_fs_bshift = PAGE_SHIFT;
    168 	mp->mnt_dev_bshift = DEV_BSHIFT;
    169 	mp->mnt_iflag |= IMNT_MPSAFE;
    170 	vfs_getnewfsid(mp);
    171 
    172 	return set_statvfs_info(path, UIO_USERSPACE, "tmpfs", UIO_SYSSPACE,
    173 	    mp->mnt_op->vfs_name, mp, curlwp);
    174 }
    175 
    176 /* --------------------------------------------------------------------- */
    177 
    178 static int
    179 tmpfs_start(struct mount *mp, int flags)
    180 {
    181 
    182 	return 0;
    183 }
    184 
    185 /* --------------------------------------------------------------------- */
    186 
    187 /* ARGSUSED2 */
    188 static int
    189 tmpfs_unmount(struct mount *mp, int mntflags)
    190 {
    191 	int error;
    192 	int flags = 0;
    193 	struct tmpfs_mount *tmp;
    194 	struct tmpfs_node *node;
    195 
    196 	/* Handle forced unmounts. */
    197 	if (mntflags & MNT_FORCE)
    198 		flags |= FORCECLOSE;
    199 
    200 	/* Finalize all pending I/O. */
    201 	error = vflush(mp, NULL, flags);
    202 	if (error != 0)
    203 		return error;
    204 
    205 	tmp = VFS_TO_TMPFS(mp);
    206 
    207 	/* Free all associated data.  The loop iterates over the linked list
    208 	 * we have containing all used nodes.  For each of them that is
    209 	 * a directory, we free all its directory entries.  Note that after
    210 	 * freeing a node, it will automatically go to the available list,
    211 	 * so we will later have to iterate over it to release its items. */
    212 	node = LIST_FIRST(&tmp->tm_nodes);
    213 	while (node != NULL) {
    214 		struct tmpfs_node *next;
    215 
    216 		if (node->tn_type == VDIR) {
    217 			struct tmpfs_dirent *de;
    218 
    219 			de = TAILQ_FIRST(&node->tn_spec.tn_dir.tn_dir);
    220 			while (de != NULL) {
    221 				struct tmpfs_dirent *nde;
    222 
    223 				nde = TAILQ_NEXT(de, td_entries);
    224 				tmpfs_free_dirent(tmp, de, false);
    225 				de = nde;
    226 				node->tn_size -= sizeof(struct tmpfs_dirent);
    227 			}
    228 		}
    229 
    230 		next = LIST_NEXT(node, tn_entries);
    231 		tmpfs_free_node(tmp, node);
    232 		node = next;
    233 	}
    234 
    235 	/* Throw away the tmpfs_mount structure. */
    236 	tmpfs_mntmem_destroy(tmp);
    237 	mutex_destroy(&tmp->tm_lock);
    238 	kmem_free(tmp, sizeof(*tmp));
    239 	mp->mnt_data = NULL;
    240 
    241 	return 0;
    242 }
    243 
    244 /* --------------------------------------------------------------------- */
    245 
    246 static int
    247 tmpfs_root(struct mount *mp, struct vnode **vpp)
    248 {
    249 
    250 	return tmpfs_alloc_vp(mp, VFS_TO_TMPFS(mp)->tm_root, vpp);
    251 }
    252 
    253 /* --------------------------------------------------------------------- */
    254 
    255 static int
    256 tmpfs_vget(struct mount *mp, ino_t ino,
    257     struct vnode **vpp)
    258 {
    259 
    260 	printf("tmpfs_vget called; need for it unknown yet\n");
    261 	return EOPNOTSUPP;
    262 }
    263 
    264 /* --------------------------------------------------------------------- */
    265 
    266 static int
    267 tmpfs_fhtovp(struct mount *mp, struct fid *fhp, struct vnode **vpp)
    268 {
    269 	bool found;
    270 	struct tmpfs_fid tfh;
    271 	struct tmpfs_mount *tmp;
    272 	struct tmpfs_node *node;
    273 
    274 	tmp = VFS_TO_TMPFS(mp);
    275 
    276 	if (fhp->fid_len != sizeof(struct tmpfs_fid))
    277 		return EINVAL;
    278 
    279 	memcpy(&tfh, fhp, sizeof(struct tmpfs_fid));
    280 
    281 	if (tfh.tf_id >= tmp->tm_nodes_max)
    282 		return EINVAL;
    283 
    284 	found = false;
    285 	mutex_enter(&tmp->tm_lock);
    286 	LIST_FOREACH(node, &tmp->tm_nodes, tn_entries) {
    287 		if (node->tn_id == tfh.tf_id &&
    288 		    node->tn_gen == tfh.tf_gen) {
    289 			found = true;
    290 			break;
    291 		}
    292 	}
    293 	mutex_exit(&tmp->tm_lock);
    294 
    295 	/* XXXAD nothing to prevent 'node' from being removed. */
    296 	return found ? tmpfs_alloc_vp(mp, node, vpp) : EINVAL;
    297 }
    298 
    299 /* --------------------------------------------------------------------- */
    300 
    301 static int
    302 tmpfs_vptofh(struct vnode *vp, struct fid *fhp, size_t *fh_size)
    303 {
    304 	struct tmpfs_fid tfh;
    305 	struct tmpfs_node *node;
    306 
    307 	if (*fh_size < sizeof(struct tmpfs_fid)) {
    308 		*fh_size = sizeof(struct tmpfs_fid);
    309 		return E2BIG;
    310 	}
    311 	*fh_size = sizeof(struct tmpfs_fid);
    312 	node = VP_TO_TMPFS_NODE(vp);
    313 
    314 	memset(&tfh, 0, sizeof(tfh));
    315 	tfh.tf_len = sizeof(struct tmpfs_fid);
    316 	tfh.tf_gen = node->tn_gen;
    317 	tfh.tf_id = node->tn_id;
    318 	memcpy(fhp, &tfh, sizeof(tfh));
    319 
    320 	return 0;
    321 }
    322 
    323 /* --------------------------------------------------------------------- */
    324 
    325 static int
    326 tmpfs_statvfs(struct mount *mp, struct statvfs *sbp)
    327 {
    328 	struct tmpfs_mount *tmp;
    329 	fsfilcnt_t freenodes;
    330 	size_t avail;
    331 
    332 	tmp = VFS_TO_TMPFS(mp);
    333 
    334 	sbp->f_iosize = sbp->f_frsize = sbp->f_bsize = PAGE_SIZE;
    335 
    336 	mutex_enter(&tmp->tm_acc_lock);
    337 	avail =  tmpfs_pages_avail(tmp);
    338 	sbp->f_blocks = (tmpfs_bytes_max(tmp) >> PAGE_SHIFT);
    339 	sbp->f_bavail = sbp->f_bfree = avail;
    340 	sbp->f_bresvd = 0;
    341 
    342 	freenodes = MIN(tmp->tm_nodes_max - tmp->tm_nodes_cnt,
    343 	    avail * PAGE_SIZE / sizeof(struct tmpfs_node));
    344 
    345 	sbp->f_files = tmp->tm_nodes_cnt + freenodes;
    346 	sbp->f_favail = sbp->f_ffree = freenodes;
    347 	sbp->f_fresvd = 0;
    348 	mutex_exit(&tmp->tm_acc_lock);
    349 
    350 	copy_statvfs_info(sbp, mp);
    351 
    352 	return 0;
    353 }
    354 
    355 /* --------------------------------------------------------------------- */
    356 
    357 /* ARGSUSED0 */
    358 static int
    359 tmpfs_sync(struct mount *mp, int waitfor,
    360     kauth_cred_t uc)
    361 {
    362 
    363 	return 0;
    364 }
    365 
    366 /* --------------------------------------------------------------------- */
    367 
    368 static void
    369 tmpfs_init(void)
    370 {
    371 
    372 }
    373 
    374 /* --------------------------------------------------------------------- */
    375 
    376 static void
    377 tmpfs_done(void)
    378 {
    379 
    380 }
    381 
    382 /* --------------------------------------------------------------------- */
    383 
    384 static int
    385 tmpfs_snapshot(struct mount *mp, struct vnode *vp,
    386     struct timespec *ctime)
    387 {
    388 
    389 	return EOPNOTSUPP;
    390 }
    391 
    392 /* --------------------------------------------------------------------- */
    393 
    394 /*
    395  * tmpfs vfs operations.
    396  */
    397 
    398 extern const struct vnodeopv_desc tmpfs_fifoop_opv_desc;
    399 extern const struct vnodeopv_desc tmpfs_specop_opv_desc;
    400 extern const struct vnodeopv_desc tmpfs_vnodeop_opv_desc;
    401 
    402 const struct vnodeopv_desc * const tmpfs_vnodeopv_descs[] = {
    403 	&tmpfs_fifoop_opv_desc,
    404 	&tmpfs_specop_opv_desc,
    405 	&tmpfs_vnodeop_opv_desc,
    406 	NULL,
    407 };
    408 
    409 struct vfsops tmpfs_vfsops = {
    410 	MOUNT_TMPFS,			/* vfs_name */
    411 	sizeof (struct tmpfs_args),
    412 	tmpfs_mount,			/* vfs_mount */
    413 	tmpfs_start,			/* vfs_start */
    414 	tmpfs_unmount,			/* vfs_unmount */
    415 	tmpfs_root,			/* vfs_root */
    416 	(void *)eopnotsupp,		/* vfs_quotactl */
    417 	tmpfs_statvfs,			/* vfs_statvfs */
    418 	tmpfs_sync,			/* vfs_sync */
    419 	tmpfs_vget,			/* vfs_vget */
    420 	tmpfs_fhtovp,			/* vfs_fhtovp */
    421 	tmpfs_vptofh,			/* vfs_vptofh */
    422 	tmpfs_init,			/* vfs_init */
    423 	NULL,				/* vfs_reinit */
    424 	tmpfs_done,			/* vfs_done */
    425 	NULL,				/* vfs_mountroot */
    426 	tmpfs_snapshot,			/* vfs_snapshot */
    427 	vfs_stdextattrctl,		/* vfs_extattrctl */
    428 	(void *)eopnotsupp,		/* vfs_suspendctl */
    429 	genfs_renamelock_enter,
    430 	genfs_renamelock_exit,
    431 	(void *)eopnotsupp,
    432 	tmpfs_vnodeopv_descs,
    433 	0,				/* vfs_refcount */
    434 	{ NULL, NULL },
    435 };
    436 
    437 static int
    438 tmpfs_modcmd(modcmd_t cmd, void *arg)
    439 {
    440 
    441 	switch (cmd) {
    442 	case MODULE_CMD_INIT:
    443 		return vfs_attach(&tmpfs_vfsops);
    444 	case MODULE_CMD_FINI:
    445 		return vfs_detach(&tmpfs_vfsops);
    446 	default:
    447 		return ENOTTY;
    448 	}
    449 }
    450