xref: /netbsd-src/sys/fs/tmpfs/tmpfs_vfsops.c (revision cb861154c176d3dcc8ff846f449e3c16a5f5edb5)
1 /*	$NetBSD: tmpfs_vfsops.c,v 1.47 2011/04/02 14:24:53 hannken 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.47 2011/04/02 14:24:53 hannken 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 	found = false;
282 	mutex_enter(&tmp->tm_lock);
283 	LIST_FOREACH(node, &tmp->tm_nodes, tn_entries) {
284 		if (node->tn_id == tfh.tf_id &&
285 		    node->tn_gen == tfh.tf_gen) {
286 			found = true;
287 			break;
288 		}
289 	}
290 	mutex_exit(&tmp->tm_lock);
291 
292 	/* XXXAD nothing to prevent 'node' from being removed. */
293 	return found ? tmpfs_alloc_vp(mp, node, vpp) : ESTALE;
294 }
295 
296 /* --------------------------------------------------------------------- */
297 
298 static int
299 tmpfs_vptofh(struct vnode *vp, struct fid *fhp, size_t *fh_size)
300 {
301 	struct tmpfs_fid tfh;
302 	struct tmpfs_node *node;
303 
304 	if (*fh_size < sizeof(struct tmpfs_fid)) {
305 		*fh_size = sizeof(struct tmpfs_fid);
306 		return E2BIG;
307 	}
308 	*fh_size = sizeof(struct tmpfs_fid);
309 	node = VP_TO_TMPFS_NODE(vp);
310 
311 	memset(&tfh, 0, sizeof(tfh));
312 	tfh.tf_len = sizeof(struct tmpfs_fid);
313 	tfh.tf_gen = node->tn_gen;
314 	tfh.tf_id = node->tn_id;
315 	memcpy(fhp, &tfh, sizeof(tfh));
316 
317 	return 0;
318 }
319 
320 /* --------------------------------------------------------------------- */
321 
322 static int
323 tmpfs_statvfs(struct mount *mp, struct statvfs *sbp)
324 {
325 	struct tmpfs_mount *tmp;
326 	fsfilcnt_t freenodes;
327 	size_t avail;
328 
329 	tmp = VFS_TO_TMPFS(mp);
330 
331 	sbp->f_iosize = sbp->f_frsize = sbp->f_bsize = PAGE_SIZE;
332 
333 	mutex_enter(&tmp->tm_acc_lock);
334 	avail =  tmpfs_pages_avail(tmp);
335 	sbp->f_blocks = (tmpfs_bytes_max(tmp) >> PAGE_SHIFT);
336 	sbp->f_bavail = sbp->f_bfree = avail;
337 	sbp->f_bresvd = 0;
338 
339 	freenodes = MIN(tmp->tm_nodes_max - tmp->tm_nodes_cnt,
340 	    avail * PAGE_SIZE / sizeof(struct tmpfs_node));
341 
342 	sbp->f_files = tmp->tm_nodes_cnt + freenodes;
343 	sbp->f_favail = sbp->f_ffree = freenodes;
344 	sbp->f_fresvd = 0;
345 	mutex_exit(&tmp->tm_acc_lock);
346 
347 	copy_statvfs_info(sbp, mp);
348 
349 	return 0;
350 }
351 
352 /* --------------------------------------------------------------------- */
353 
354 /* ARGSUSED0 */
355 static int
356 tmpfs_sync(struct mount *mp, int waitfor,
357     kauth_cred_t uc)
358 {
359 
360 	return 0;
361 }
362 
363 /* --------------------------------------------------------------------- */
364 
365 static void
366 tmpfs_init(void)
367 {
368 
369 }
370 
371 /* --------------------------------------------------------------------- */
372 
373 static void
374 tmpfs_done(void)
375 {
376 
377 }
378 
379 /* --------------------------------------------------------------------- */
380 
381 static int
382 tmpfs_snapshot(struct mount *mp, struct vnode *vp,
383     struct timespec *ctime)
384 {
385 
386 	return EOPNOTSUPP;
387 }
388 
389 /* --------------------------------------------------------------------- */
390 
391 /*
392  * tmpfs vfs operations.
393  */
394 
395 extern const struct vnodeopv_desc tmpfs_fifoop_opv_desc;
396 extern const struct vnodeopv_desc tmpfs_specop_opv_desc;
397 extern const struct vnodeopv_desc tmpfs_vnodeop_opv_desc;
398 
399 const struct vnodeopv_desc * const tmpfs_vnodeopv_descs[] = {
400 	&tmpfs_fifoop_opv_desc,
401 	&tmpfs_specop_opv_desc,
402 	&tmpfs_vnodeop_opv_desc,
403 	NULL,
404 };
405 
406 struct vfsops tmpfs_vfsops = {
407 	MOUNT_TMPFS,			/* vfs_name */
408 	sizeof (struct tmpfs_args),
409 	tmpfs_mount,			/* vfs_mount */
410 	tmpfs_start,			/* vfs_start */
411 	tmpfs_unmount,			/* vfs_unmount */
412 	tmpfs_root,			/* vfs_root */
413 	(void *)eopnotsupp,		/* vfs_quotactl */
414 	tmpfs_statvfs,			/* vfs_statvfs */
415 	tmpfs_sync,			/* vfs_sync */
416 	tmpfs_vget,			/* vfs_vget */
417 	tmpfs_fhtovp,			/* vfs_fhtovp */
418 	tmpfs_vptofh,			/* vfs_vptofh */
419 	tmpfs_init,			/* vfs_init */
420 	NULL,				/* vfs_reinit */
421 	tmpfs_done,			/* vfs_done */
422 	NULL,				/* vfs_mountroot */
423 	tmpfs_snapshot,			/* vfs_snapshot */
424 	vfs_stdextattrctl,		/* vfs_extattrctl */
425 	(void *)eopnotsupp,		/* vfs_suspendctl */
426 	genfs_renamelock_enter,
427 	genfs_renamelock_exit,
428 	(void *)eopnotsupp,
429 	tmpfs_vnodeopv_descs,
430 	0,				/* vfs_refcount */
431 	{ NULL, NULL },
432 };
433 
434 static int
435 tmpfs_modcmd(modcmd_t cmd, void *arg)
436 {
437 
438 	switch (cmd) {
439 	case MODULE_CMD_INIT:
440 		return vfs_attach(&tmpfs_vfsops);
441 	case MODULE_CMD_FINI:
442 		return vfs_detach(&tmpfs_vfsops);
443 	default:
444 		return ENOTTY;
445 	}
446 }
447