1/*	$NetBSD: tmpfs_vfsops.c,v 1.52 2011/09/27 01:10:43 christos 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.52 2011/09/27 01:10:43 christos 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/module.h>
55
56#include <miscfs/genfs/genfs.h>
57#include <fs/tmpfs/tmpfs.h>
58#include <fs/tmpfs/tmpfs_args.h>
59
60MODULE(MODULE_CLASS_VFS, tmpfs, NULL);
61
62struct pool	tmpfs_dirent_pool;
63struct pool	tmpfs_node_pool;
64
65static int	tmpfs_mount(struct mount *, const char *, void *, size_t *);
66static int	tmpfs_start(struct mount *, int);
67static int	tmpfs_unmount(struct mount *, int);
68static int	tmpfs_root(struct mount *, vnode_t **);
69static int	tmpfs_vget(struct mount *, ino_t, vnode_t **);
70static int	tmpfs_fhtovp(struct mount *, struct fid *, vnode_t **);
71static int	tmpfs_vptofh(struct vnode *, struct fid *, size_t *);
72static int	tmpfs_statvfs(struct mount *, struct statvfs *);
73static int	tmpfs_sync(struct mount *, int, kauth_cred_t);
74static void	tmpfs_init(void);
75static void	tmpfs_done(void);
76static int	tmpfs_snapshot(struct mount *, vnode_t *, struct timespec *);
77
78static void
79tmpfs_init(void)
80{
81
82	pool_init(&tmpfs_dirent_pool, sizeof(tmpfs_dirent_t), 0, 0, 0,
83	    "tmpfs_dirent", &pool_allocator_nointr, IPL_NONE);
84	pool_init(&tmpfs_node_pool, sizeof(tmpfs_node_t), 0, 0, 0,
85	    "tmpfs_node", &pool_allocator_nointr, IPL_NONE);
86}
87
88static void
89tmpfs_done(void)
90{
91
92	pool_destroy(&tmpfs_dirent_pool);
93	pool_destroy(&tmpfs_node_pool);
94}
95
96static int
97tmpfs_mount(struct mount *mp, const char *path, void *data, size_t *data_len)
98{
99	struct tmpfs_args *args = data;
100	tmpfs_mount_t *tmp;
101	tmpfs_node_t *root;
102	uint64_t memlimit;
103	ino_t nodes;
104	int error;
105
106	if (args == NULL)
107		return EINVAL;
108
109	/* Validate the version. */
110	if (*data_len < sizeof(*args) ||
111	    args->ta_version != TMPFS_ARGS_VERSION)
112		return EINVAL;
113
114	/* Handle retrieval of mount point arguments. */
115	if (mp->mnt_flag & MNT_GETARGS) {
116		if (mp->mnt_data == NULL)
117			return EIO;
118		tmp = VFS_TO_TMPFS(mp);
119
120		args->ta_version = TMPFS_ARGS_VERSION;
121		args->ta_nodes_max = tmp->tm_nodes_max;
122		args->ta_size_max = tmp->tm_mem_limit;
123
124		root = tmp->tm_root;
125		args->ta_root_uid = root->tn_uid;
126		args->ta_root_gid = root->tn_gid;
127		args->ta_root_mode = root->tn_mode;
128
129		*data_len = sizeof(*args);
130		return 0;
131	}
132
133	if (mp->mnt_flag & MNT_UPDATE) {
134		/* TODO */
135		return EOPNOTSUPP;
136	}
137
138	/* Prohibit mounts if there is not enough memory. */
139	if (tmpfs_mem_info(true) < TMPFS_PAGES_RESERVED)
140		return EINVAL;
141
142	/* Get the memory usage limit for this file-system. */
143	if (args->ta_size_max < PAGE_SIZE) {
144		memlimit = UINT64_MAX;
145	} else {
146		memlimit = args->ta_size_max;
147	}
148	KASSERT(memlimit > 0);
149
150	if (args->ta_nodes_max <= 3) {
151		nodes = 3 + (memlimit / 1024);
152	} else {
153		nodes = args->ta_nodes_max;
154	}
155	nodes = MIN(nodes, INT_MAX);
156	KASSERT(nodes >= 3);
157
158	/* Allocate the tmpfs mount structure and fill it. */
159	tmp = kmem_zalloc(sizeof(tmpfs_mount_t), KM_SLEEP);
160	if (tmp == NULL)
161		return ENOMEM;
162
163	tmp->tm_nodes_max = nodes;
164	tmp->tm_nodes_cnt = 0;
165	LIST_INIT(&tmp->tm_nodes);
166
167	mutex_init(&tmp->tm_lock, MUTEX_DEFAULT, IPL_NONE);
168	tmpfs_mntmem_init(tmp, memlimit);
169
170	/* Allocate the root node. */
171	error = tmpfs_alloc_node(tmp, VDIR, args->ta_root_uid,
172	    args->ta_root_gid, args->ta_root_mode & ALLPERMS, NULL,
173	    VNOVAL, &root);
174	KASSERT(error == 0 && root != NULL);
175
176	/*
177	 * Parent of the root inode is itself.  Also, root inode has no
178	 * directory entry (i.e. is never attached), thus hold an extra
179	 * reference (link) for it.
180	 */
181	root->tn_links++;
182	root->tn_spec.tn_dir.tn_parent = root;
183	tmp->tm_root = root;
184
185	mp->mnt_data = tmp;
186	mp->mnt_flag |= MNT_LOCAL;
187	mp->mnt_stat.f_namemax = TMPFS_MAXNAMLEN;
188	mp->mnt_fs_bshift = PAGE_SHIFT;
189	mp->mnt_dev_bshift = DEV_BSHIFT;
190	mp->mnt_iflag |= IMNT_MPSAFE;
191	vfs_getnewfsid(mp);
192
193	error = set_statvfs_info(path, UIO_USERSPACE, "tmpfs", UIO_SYSSPACE,
194	    mp->mnt_op->vfs_name, mp, curlwp);
195	if (error) {
196		(void)tmpfs_unmount(mp, MNT_FORCE);
197	}
198	return error;
199}
200
201static int
202tmpfs_start(struct mount *mp, int flags)
203{
204
205	return 0;
206}
207
208static int
209tmpfs_unmount(struct mount *mp, int mntflags)
210{
211	tmpfs_mount_t *tmp;
212	tmpfs_node_t *node;
213	int error, flags = 0;
214
215	/* Handle forced unmounts. */
216	if (mntflags & MNT_FORCE)
217		flags |= FORCECLOSE;
218
219	/* Finalize all pending I/O. */
220	error = vflush(mp, NULL, flags);
221	if (error != 0)
222		return error;
223
224	tmp = VFS_TO_TMPFS(mp);
225
226	/* Destroy any existing inodes. */
227	while ((node = LIST_FIRST(&tmp->tm_nodes)) != NULL) {
228		if (node->tn_type == VDIR) {
229			tmpfs_dirent_t *de;
230
231			/* Destroy any directory entries. */
232			de = TAILQ_FIRST(&node->tn_spec.tn_dir.tn_dir);
233			while (de != NULL) {
234				tmpfs_dirent_t *nde;
235
236				nde = TAILQ_NEXT(de, td_entries);
237				tmpfs_free_dirent(tmp, de);
238				node->tn_size -= sizeof(tmpfs_dirent_t);
239				de = nde;
240			}
241		}
242		/* Removes inode from the list. */
243		tmpfs_free_node(tmp, node);
244	}
245
246	/* Throw away the tmpfs_mount structure. */
247	tmpfs_mntmem_destroy(tmp);
248	mutex_destroy(&tmp->tm_lock);
249	kmem_free(tmp, sizeof(*tmp));
250	mp->mnt_data = NULL;
251
252	return 0;
253}
254
255static int
256tmpfs_root(struct mount *mp, vnode_t **vpp)
257{
258	tmpfs_node_t *node = VFS_TO_TMPFS(mp)->tm_root;
259
260	mutex_enter(&node->tn_vlock);
261	return tmpfs_vnode_get(mp, node, vpp);
262}
263
264static int
265tmpfs_vget(struct mount *mp, ino_t ino, vnode_t **vpp)
266{
267
268	printf("tmpfs_vget called; need for it unknown yet\n");
269	return EOPNOTSUPP;
270}
271
272static int
273tmpfs_fhtovp(struct mount *mp, struct fid *fhp, vnode_t **vpp)
274{
275	tmpfs_mount_t *tmp = VFS_TO_TMPFS(mp);
276	tmpfs_node_t *node;
277	tmpfs_fid_t tfh;
278
279	if (fhp->fid_len != sizeof(tmpfs_fid_t)) {
280		return EINVAL;
281	}
282	memcpy(&tfh, fhp, sizeof(tmpfs_fid_t));
283
284	mutex_enter(&tmp->tm_lock);
285	LIST_FOREACH(node, &tmp->tm_nodes, tn_entries) {
286		if (node->tn_id != tfh.tf_id) {
287			continue;
288		}
289		if (TMPFS_NODE_GEN(node) != tfh.tf_gen) {
290			continue;
291		}
292		mutex_enter(&node->tn_vlock);
293		break;
294	}
295	mutex_exit(&tmp->tm_lock);
296
297	/* Will release the tn_vlock. */
298	return node ? tmpfs_vnode_get(mp, node, vpp) : ESTALE;
299}
300
301static int
302tmpfs_vptofh(vnode_t *vp, struct fid *fhp, size_t *fh_size)
303{
304	tmpfs_fid_t tfh;
305	tmpfs_node_t *node;
306
307	if (*fh_size < sizeof(tmpfs_fid_t)) {
308		*fh_size = sizeof(tmpfs_fid_t);
309		return E2BIG;
310	}
311	*fh_size = sizeof(tmpfs_fid_t);
312	node = VP_TO_TMPFS_NODE(vp);
313
314	memset(&tfh, 0, sizeof(tfh));
315	tfh.tf_len = sizeof(tmpfs_fid_t);
316	tfh.tf_gen = TMPFS_NODE_GEN(node);
317	tfh.tf_id = node->tn_id;
318	memcpy(fhp, &tfh, sizeof(tfh));
319
320	return 0;
321}
322
323static int
324tmpfs_statvfs(struct mount *mp, struct statvfs *sbp)
325{
326	tmpfs_mount_t *tmp;
327	fsfilcnt_t freenodes;
328	size_t avail;
329
330	tmp = VFS_TO_TMPFS(mp);
331
332	sbp->f_iosize = sbp->f_frsize = sbp->f_bsize = PAGE_SIZE;
333
334	mutex_enter(&tmp->tm_acc_lock);
335	avail =  tmpfs_pages_avail(tmp);
336	sbp->f_blocks = (tmpfs_bytes_max(tmp) >> PAGE_SHIFT);
337	sbp->f_bavail = sbp->f_bfree = avail;
338	sbp->f_bresvd = 0;
339
340	freenodes = MIN(tmp->tm_nodes_max - tmp->tm_nodes_cnt,
341	    avail * PAGE_SIZE / sizeof(tmpfs_node_t));
342
343	sbp->f_files = tmp->tm_nodes_cnt + freenodes;
344	sbp->f_favail = sbp->f_ffree = freenodes;
345	sbp->f_fresvd = 0;
346	mutex_exit(&tmp->tm_acc_lock);
347
348	copy_statvfs_info(sbp, mp);
349
350	return 0;
351}
352
353static int
354tmpfs_sync(struct mount *mp, int waitfor, kauth_cred_t uc)
355{
356
357	return 0;
358}
359
360static int
361tmpfs_snapshot(struct mount *mp, vnode_t *vp, struct timespec *ctime)
362{
363
364	return EOPNOTSUPP;
365}
366
367/*
368 * tmpfs vfs operations.
369 */
370
371extern const struct vnodeopv_desc tmpfs_fifoop_opv_desc;
372extern const struct vnodeopv_desc tmpfs_specop_opv_desc;
373extern const struct vnodeopv_desc tmpfs_vnodeop_opv_desc;
374
375const struct vnodeopv_desc * const tmpfs_vnodeopv_descs[] = {
376	&tmpfs_fifoop_opv_desc,
377	&tmpfs_specop_opv_desc,
378	&tmpfs_vnodeop_opv_desc,
379	NULL,
380};
381
382struct vfsops tmpfs_vfsops = {
383	MOUNT_TMPFS,			/* vfs_name */
384	sizeof (struct tmpfs_args),
385	tmpfs_mount,			/* vfs_mount */
386	tmpfs_start,			/* vfs_start */
387	tmpfs_unmount,			/* vfs_unmount */
388	tmpfs_root,			/* vfs_root */
389	(void *)eopnotsupp,		/* vfs_quotactl */
390	tmpfs_statvfs,			/* vfs_statvfs */
391	tmpfs_sync,			/* vfs_sync */
392	tmpfs_vget,			/* vfs_vget */
393	tmpfs_fhtovp,			/* vfs_fhtovp */
394	tmpfs_vptofh,			/* vfs_vptofh */
395	tmpfs_init,			/* vfs_init */
396	NULL,				/* vfs_reinit */
397	tmpfs_done,			/* vfs_done */
398	NULL,				/* vfs_mountroot */
399	tmpfs_snapshot,			/* vfs_snapshot */
400	vfs_stdextattrctl,		/* vfs_extattrctl */
401	(void *)eopnotsupp,		/* vfs_suspendctl */
402	genfs_renamelock_enter,
403	genfs_renamelock_exit,
404	(void *)eopnotsupp,
405	tmpfs_vnodeopv_descs,
406	0,				/* vfs_refcount */
407	{ NULL, NULL },
408};
409
410static int
411tmpfs_modcmd(modcmd_t cmd, void *arg)
412{
413
414	switch (cmd) {
415	case MODULE_CMD_INIT:
416		return vfs_attach(&tmpfs_vfsops);
417	case MODULE_CMD_FINI:
418		return vfs_detach(&tmpfs_vfsops);
419	default:
420		return ENOTTY;
421	}
422}
423