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