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