tmpfs_vfsops.c revision 1.41
1/*	$NetBSD: tmpfs_vfsops.c,v 1.41 2008/05/10 02:26:09 rumble 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.41 2008/05/10 02:26:09 rumble 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
60MODULE(MODULE_CLASS_VFS, tmpfs, NULL);
61
62/* --------------------------------------------------------------------- */
63
64static int	tmpfs_mount(struct mount *, const char *, void *, size_t *);
65static int	tmpfs_start(struct mount *, int);
66static int	tmpfs_unmount(struct mount *, int);
67static int	tmpfs_root(struct mount *, struct vnode **);
68static int	tmpfs_vget(struct mount *, ino_t, struct vnode **);
69static int	tmpfs_fhtovp(struct mount *, struct fid *, struct vnode **);
70static int	tmpfs_vptofh(struct vnode *, struct fid *, size_t *);
71static int	tmpfs_statvfs(struct mount *, struct statvfs *);
72static int	tmpfs_sync(struct mount *, int, kauth_cred_t);
73static void	tmpfs_init(void);
74static void	tmpfs_done(void);
75static int	tmpfs_snapshot(struct mount *, struct vnode *,
76		    struct timespec *);
77
78/* --------------------------------------------------------------------- */
79
80static int
81tmpfs_mount(struct mount *mp, const char *path, void *data, size_t *data_len)
82{
83	struct lwp *l = curlwp;
84	int error;
85	ino_t nodes;
86	size_t pages;
87	struct tmpfs_mount *tmp;
88	struct tmpfs_node *root;
89	struct tmpfs_args *args = data;
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_pages_max * PAGE_SIZE;
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 maximum number of memory pages this file system is
129	 * allowed to use, based on the maximum size the user passed in
130	 * the mount structure.  A value of zero is treated as if the
131	 * maximum available space was requested. */
132	if (args->ta_size_max < PAGE_SIZE || args->ta_size_max >= SIZE_MAX)
133		pages = SIZE_MAX;
134	else
135		pages = args->ta_size_max / PAGE_SIZE +
136		    (args->ta_size_max % PAGE_SIZE == 0 ? 0 : 1);
137	if (pages > INT_MAX)
138		pages = INT_MAX;
139	KASSERT(pages > 0);
140
141	if (args->ta_nodes_max <= 3)
142		nodes = 3 + pages * PAGE_SIZE / 1024;
143	else
144		nodes = args->ta_nodes_max;
145	if (nodes > INT_MAX)
146		nodes = INT_MAX;
147	KASSERT(nodes >= 3);
148
149	/* Allocate the tmpfs mount structure and fill it. */
150	tmp = kmem_alloc(sizeof(struct tmpfs_mount), KM_SLEEP);
151	if (tmp == NULL)
152		return ENOMEM;
153
154	tmp->tm_nodes_max = nodes;
155	tmp->tm_nodes_cnt = 0;
156	LIST_INIT(&tmp->tm_nodes);
157
158	mutex_init(&tmp->tm_lock, MUTEX_DEFAULT, IPL_NONE);
159
160	tmp->tm_pages_max = pages;
161	tmp->tm_pages_used = 0;
162	tmpfs_pool_init(&tmp->tm_dirent_pool, sizeof(struct tmpfs_dirent),
163	    "dirent", tmp);
164	tmpfs_pool_init(&tmp->tm_node_pool, sizeof(struct tmpfs_node),
165	    "node", tmp);
166	tmpfs_str_pool_init(&tmp->tm_str_pool, tmp);
167
168	/* Allocate the root node. */
169	error = tmpfs_alloc_node(tmp, VDIR, args->ta_root_uid,
170	    args->ta_root_gid, args->ta_root_mode & ALLPERMS, NULL, NULL,
171	    VNOVAL, &root);
172	KASSERT(error == 0 && root != NULL);
173	root->tn_links++;
174	tmp->tm_root = root;
175
176	mp->mnt_data = tmp;
177	mp->mnt_flag |= MNT_LOCAL;
178	mp->mnt_stat.f_namemax = MAXNAMLEN;
179	mp->mnt_fs_bshift = PAGE_SHIFT;
180	mp->mnt_dev_bshift = DEV_BSHIFT;
181	mp->mnt_iflag |= IMNT_MPSAFE;
182	vfs_getnewfsid(mp);
183
184	return set_statvfs_info(path, UIO_USERSPACE, "tmpfs", UIO_SYSSPACE,
185	    mp->mnt_op->vfs_name, mp, l);
186}
187
188/* --------------------------------------------------------------------- */
189
190static int
191tmpfs_start(struct mount *mp, int flags)
192{
193
194	return 0;
195}
196
197/* --------------------------------------------------------------------- */
198
199/* ARGSUSED2 */
200static int
201tmpfs_unmount(struct mount *mp, int mntflags)
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);
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_spec.tn_dir.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
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
263static int
264tmpfs_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
272static int
273tmpfs_vget(struct mount *mp, ino_t ino,
274    struct vnode **vpp)
275{
276
277	printf("tmpfs_vget called; need for it unknown yet\n");
278	return EOPNOTSUPP;
279}
280
281/* --------------------------------------------------------------------- */
282
283static int
284tmpfs_fhtovp(struct mount *mp, struct fid *fhp, struct vnode **vpp)
285{
286	bool found;
287	struct tmpfs_fid tfh;
288	struct tmpfs_mount *tmp;
289	struct tmpfs_node *node;
290
291	tmp = VFS_TO_TMPFS(mp);
292
293	if (fhp->fid_len != sizeof(struct tmpfs_fid))
294		return EINVAL;
295
296	memcpy(&tfh, fhp, sizeof(struct tmpfs_fid));
297
298	if (tfh.tf_id >= tmp->tm_nodes_max)
299		return EINVAL;
300
301	found = false;
302	mutex_enter(&tmp->tm_lock);
303	LIST_FOREACH(node, &tmp->tm_nodes, tn_entries) {
304		if (node->tn_id == tfh.tf_id &&
305		    node->tn_gen == tfh.tf_gen) {
306			found = true;
307			break;
308		}
309	}
310	mutex_exit(&tmp->tm_lock);
311
312	/* XXXAD nothing to prevent 'node' from being removed. */
313	return found ? tmpfs_alloc_vp(mp, node, vpp) : EINVAL;
314}
315
316/* --------------------------------------------------------------------- */
317
318static int
319tmpfs_vptofh(struct vnode *vp, struct fid *fhp, size_t *fh_size)
320{
321	struct tmpfs_fid tfh;
322	struct tmpfs_node *node;
323
324	if (*fh_size < sizeof(struct tmpfs_fid)) {
325		*fh_size = sizeof(struct tmpfs_fid);
326		return E2BIG;
327	}
328	*fh_size = sizeof(struct tmpfs_fid);
329	node = VP_TO_TMPFS_NODE(vp);
330
331	memset(&tfh, 0, sizeof(tfh));
332	tfh.tf_len = sizeof(struct tmpfs_fid);
333	tfh.tf_gen = node->tn_gen;
334	tfh.tf_id = node->tn_id;
335	memcpy(fhp, &tfh, sizeof(tfh));
336
337	return 0;
338}
339
340/* --------------------------------------------------------------------- */
341
342/* ARGSUSED2 */
343static int
344tmpfs_statvfs(struct mount *mp, struct statvfs *sbp)
345{
346	fsfilcnt_t freenodes;
347	struct tmpfs_mount *tmp;
348
349	tmp = VFS_TO_TMPFS(mp);
350
351	sbp->f_iosize = sbp->f_frsize = sbp->f_bsize = PAGE_SIZE;
352
353	sbp->f_blocks = TMPFS_PAGES_MAX(tmp);
354	sbp->f_bavail = sbp->f_bfree = TMPFS_PAGES_AVAIL(tmp);
355	sbp->f_bresvd = 0;
356
357	freenodes = MIN(tmp->tm_nodes_max - tmp->tm_nodes_cnt,
358	    TMPFS_PAGES_AVAIL(tmp) * PAGE_SIZE / sizeof(struct tmpfs_node));
359
360	sbp->f_files = tmp->tm_nodes_cnt + freenodes;
361	sbp->f_favail = sbp->f_ffree = freenodes;
362	sbp->f_fresvd = 0;
363
364	copy_statvfs_info(sbp, mp);
365
366	return 0;
367}
368
369/* --------------------------------------------------------------------- */
370
371/* ARGSUSED0 */
372static int
373tmpfs_sync(struct mount *mp, int waitfor,
374    kauth_cred_t uc)
375{
376
377	return 0;
378}
379
380/* --------------------------------------------------------------------- */
381
382static void
383tmpfs_init(void)
384{
385
386}
387
388/* --------------------------------------------------------------------- */
389
390static void
391tmpfs_done(void)
392{
393
394}
395
396/* --------------------------------------------------------------------- */
397
398static int
399tmpfs_snapshot(struct mount *mp, struct vnode *vp,
400    struct timespec *ctime)
401{
402
403	return EOPNOTSUPP;
404}
405
406/* --------------------------------------------------------------------- */
407
408/*
409 * tmpfs vfs operations.
410 */
411
412extern const struct vnodeopv_desc tmpfs_fifoop_opv_desc;
413extern const struct vnodeopv_desc tmpfs_specop_opv_desc;
414extern const struct vnodeopv_desc tmpfs_vnodeop_opv_desc;
415
416const struct vnodeopv_desc * const tmpfs_vnodeopv_descs[] = {
417	&tmpfs_fifoop_opv_desc,
418	&tmpfs_specop_opv_desc,
419	&tmpfs_vnodeop_opv_desc,
420	NULL,
421};
422
423struct vfsops tmpfs_vfsops = {
424	MOUNT_TMPFS,			/* vfs_name */
425	sizeof (struct tmpfs_args),
426	tmpfs_mount,			/* vfs_mount */
427	tmpfs_start,			/* vfs_start */
428	tmpfs_unmount,			/* vfs_unmount */
429	tmpfs_root,			/* vfs_root */
430	(void *)eopnotsupp,		/* vfs_quotactl */
431	tmpfs_statvfs,			/* vfs_statvfs */
432	tmpfs_sync,			/* vfs_sync */
433	tmpfs_vget,			/* vfs_vget */
434	tmpfs_fhtovp,			/* vfs_fhtovp */
435	tmpfs_vptofh,			/* vfs_vptofh */
436	tmpfs_init,			/* vfs_init */
437	NULL,				/* vfs_reinit */
438	tmpfs_done,			/* vfs_done */
439	NULL,				/* vfs_mountroot */
440	tmpfs_snapshot,			/* vfs_snapshot */
441	vfs_stdextattrctl,		/* vfs_extattrctl */
442	(void *)eopnotsupp,		/* vfs_suspendctl */
443	genfs_renamelock_enter,
444	genfs_renamelock_exit,
445	(void *)eopnotsupp,
446	tmpfs_vnodeopv_descs,
447	0,				/* vfs_refcount */
448	{ NULL, NULL },
449};
450
451static int
452tmpfs_modcmd(modcmd_t cmd, void *arg)
453{
454
455	switch (cmd) {
456	case MODULE_CMD_INIT:
457		return vfs_attach(&tmpfs_vfsops);
458	case MODULE_CMD_FINI:
459		return vfs_detach(&tmpfs_vfsops);
460	default:
461		return ENOTTY;
462	}
463}
464