xref: /openbsd-src/sys/tmpfs/tmpfs_vfsops.c (revision 0b7734b3d77bb9b21afec6f4621cae6c805dbd45)
1 /*	$OpenBSD: tmpfs_vfsops.c,v 1.10 2016/07/22 13:11:01 kettenis Exp $	*/
2 /*	$NetBSD: tmpfs_vfsops.c,v 1.52 2011/09/27 01:10:43 christos Exp $	*/
3 
4 /*
5  * Copyright (c) 2005, 2006, 2007 The NetBSD Foundation, Inc.
6  * All rights reserved.
7  *
8  * This code is derived from software contributed to The NetBSD Foundation
9  * by Julio M. Merino Vidal, developed as part of Google's Summer of Code
10  * 2005 program.
11  *
12  * Redistribution and use in source and binary forms, with or without
13  * modification, are permitted provided that the following conditions
14  * are met:
15  * 1. Redistributions of source code must retain the above copyright
16  *    notice, this list of conditions and the following disclaimer.
17  * 2. Redistributions in binary form must reproduce the above copyright
18  *    notice, this list of conditions and the following disclaimer in the
19  *    documentation and/or other materials provided with the distribution.
20  *
21  * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
22  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
23  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
24  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
25  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
26  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
27  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
28  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
29  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
30  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
31  * POSSIBILITY OF SUCH DAMAGE.
32  */
33 
34 /*
35  * Efficient memory file system.
36  *
37  * tmpfs is a file system that uses NetBSD's virtual memory sub-system
38  * (the well-known UVM) to store file data and metadata in an efficient
39  * way.  This means that it does not follow the structure of an on-disk
40  * file system because it simply does not need to.  Instead, it uses
41  * memory-specific data structures and algorithms to automatically
42  * allocate and release resources.
43  */
44 
45 #include <sys/param.h>
46 #include <sys/types.h>
47 #include <sys/mount.h>
48 #include <sys/stat.h>
49 #include <sys/systm.h>
50 #include <sys/vnode.h>
51 #include <sys/malloc.h>
52 
53 #include <tmpfs/tmpfs.h>
54 
55 /* MODULE(MODULE_CLASS_VFS, tmpfs, NULL); */
56 
57 struct pool	tmpfs_dirent_pool;
58 struct pool	tmpfs_node_pool;
59 
60 int	tmpfs_mount(struct mount *, const char *, void *, struct nameidata *,
61 	    struct proc *);
62 int	tmpfs_start(struct mount *, int, struct proc *);
63 int	tmpfs_unmount(struct mount *, int, struct proc *);
64 int	tmpfs_root(struct mount *, struct vnode **);
65 int	tmpfs_vget(struct mount *, ino_t, struct vnode **);
66 int	tmpfs_fhtovp(struct mount *, struct fid *, struct vnode **);
67 int	tmpfs_vptofh(struct vnode *, struct fid *);
68 int	tmpfs_statfs(struct mount *, struct statfs *, struct proc *);
69 int	tmpfs_sync(struct mount *, int, struct ucred *, struct proc *);
70 int	tmpfs_init(struct vfsconf *);
71 
72 int
73 tmpfs_init(struct vfsconf *vfsp)
74 {
75 
76 	pool_init(&tmpfs_dirent_pool, sizeof(tmpfs_dirent_t), 0, 0, PR_WAITOK,
77 	    "tmpfs_dirent", NULL);
78 	pool_init(&tmpfs_node_pool, sizeof(tmpfs_node_t), 0, 0, PR_WAITOK,
79 	    "tmpfs_node", NULL);
80 
81 	return 0;
82 }
83 
84 int
85 tmpfs_mount(struct mount *mp, const char *path, void *data,
86     struct nameidata *ndp, struct proc *p)
87 {
88 	struct tmpfs_args args;
89 	tmpfs_mount_t *tmp;
90 	tmpfs_node_t *root;
91 	uint64_t memlimit;
92 	uint64_t nodes;
93 	int error;
94 
95 #if 0
96 	/* Handle retrieval of mount point arguments. */
97 	if (mp->mnt_flag & MNT_GETARGS) {
98 		if (mp->mnt_data == NULL)
99 			return EIO;
100 		tmp = VFS_TO_TMPFS(mp);
101 
102 		args->ta_version = TMPFS_ARGS_VERSION;
103 		args->ta_nodes_max = tmp->tm_nodes_max;
104 		args->ta_size_max = tmp->tm_mem_limit;
105 
106 		root = tmp->tm_root;
107 		args->ta_root_uid = root->tn_uid;
108 		args->ta_root_gid = root->tn_gid;
109 		args->ta_root_mode = root->tn_mode;
110 
111 		*data_len = sizeof(*args);
112 		return 0;
113 	}
114 #endif
115 
116 	if (mp->mnt_flag & MNT_UPDATE) {
117 		/* TODO */
118 		return EOPNOTSUPP;
119 	}
120 
121 	/* Prohibit mounts if there is not enough memory. */
122 	if (tmpfs_mem_info(1) < TMPFS_PAGES_RESERVED)
123 		return EINVAL;
124 
125 	error = copyin(data, &args, sizeof(struct tmpfs_args));
126 	if (error)
127 		return error;
128 	if (args.ta_root_uid == VNOVAL || args.ta_root_gid == VNOVAL ||
129 	    args.ta_root_mode == VNOVAL)
130 		return EINVAL;
131 
132 	/* Get the memory usage limit for this file-system. */
133 	if (args.ta_size_max < PAGE_SIZE) {
134 		memlimit = UINT64_MAX;
135 	} else {
136 		memlimit = args.ta_size_max;
137 	}
138 	KASSERT(memlimit > 0);
139 
140 	if (args.ta_nodes_max <= 3) {
141 		nodes = 3 + (memlimit / 1024);
142 	} else {
143 		nodes = args.ta_nodes_max;
144 	}
145 	nodes = MIN(nodes, INT_MAX);
146 	KASSERT(nodes >= 3);
147 
148 	/* Allocate the tmpfs mount structure and fill it. */
149 	tmp = malloc(sizeof(tmpfs_mount_t), M_MISCFSMNT, M_WAITOK);
150 
151 	tmp->tm_nodes_max = (ino_t)nodes;
152 	tmp->tm_nodes_cnt = 0;
153 	tmp->tm_highest_inode = 1;
154 	LIST_INIT(&tmp->tm_nodes);
155 
156 	rw_init(&tmp->tm_lock, "tmplk");
157 	tmpfs_mntmem_init(tmp, memlimit);
158 
159 	/* Allocate the root node. */
160 	error = tmpfs_alloc_node(tmp, VDIR, args.ta_root_uid,
161 	    args.ta_root_gid, args.ta_root_mode & ALLPERMS, NULL,
162 	    VNOVAL, &root);
163 	KASSERT(error == 0 && root != NULL);
164 
165 	/*
166 	 * Parent of the root inode is itself.  Also, root inode has no
167 	 * directory entry (i.e. is never attached), thus hold an extra
168 	 * reference (link) for it.
169 	 */
170 	root->tn_links++;
171 	root->tn_spec.tn_dir.tn_parent = root;
172 	tmp->tm_root = root;
173 
174 	mp->mnt_data = tmp;
175 	mp->mnt_flag |= MNT_LOCAL;
176 	mp->mnt_stat.f_namemax = TMPFS_MAXNAMLEN;
177 #if 0
178 	mp->mnt_fs_bshift = PAGE_SHIFT;
179 	mp->mnt_dev_bshift = DEV_BSHIFT;
180 	mp->mnt_iflag |= IMNT_MPSAFE;
181 #endif
182 	vfs_getnewfsid(mp);
183 
184 	mp->mnt_stat.mount_info.tmpfs_args = args;
185 
186 	bzero(&mp->mnt_stat.f_mntonname, sizeof(mp->mnt_stat.f_mntonname));
187 	bzero(&mp->mnt_stat.f_mntfromname, sizeof(mp->mnt_stat.f_mntfromname));
188 	bzero(&mp->mnt_stat.f_mntfromspec, sizeof(mp->mnt_stat.f_mntfromspec));
189 
190 	strlcpy(mp->mnt_stat.f_mntonname, path,
191 	    sizeof(mp->mnt_stat.f_mntonname) - 1);
192 	strlcpy(mp->mnt_stat.f_mntfromname, "tmpfs",
193 	    sizeof(mp->mnt_stat.f_mntfromname) - 1);
194 	strlcpy(mp->mnt_stat.f_mntfromspec, "tmpfs",
195 	    sizeof(mp->mnt_stat.f_mntfromspec) - 1);
196 
197 	return error;
198 }
199 
200 int
201 tmpfs_start(struct mount *mp, int flags, struct proc *p)
202 {
203 
204 	return 0;
205 }
206 
207 int
208 tmpfs_unmount(struct mount *mp, int mntflags, struct proc *p)
209 {
210 	tmpfs_mount_t *tmp = VFS_TO_TMPFS(mp);
211 	tmpfs_node_t *node, *cnode;
212 	int error, flags = 0;
213 
214 	/* Handle forced unmounts. */
215 	if (mntflags & MNT_FORCE)
216 		flags |= FORCECLOSE;
217 
218 	/* Finalize all pending I/O. */
219 	error = vflush(mp, NULL, flags);
220 	if (error != 0)
221 		return error;
222 
223 
224 	/*
225 	 * First round, detach and destroy all directory entries.
226 	 * Also, clear the pointers to the vnodes - they are gone.
227 	 */
228 	LIST_FOREACH(node, &tmp->tm_nodes, tn_entries) {
229 		tmpfs_dirent_t *de;
230 
231 		node->tn_vnode = NULL;
232 		if (node->tn_type != VDIR) {
233 			continue;
234 		}
235 		while ((de = TAILQ_FIRST(&node->tn_spec.tn_dir.tn_dir)) != NULL) {
236 			cnode = de->td_node;
237 			if (cnode) {
238 				cnode->tn_vnode = NULL;
239 			}
240 			tmpfs_dir_detach(node, de);
241 			tmpfs_free_dirent(tmp, de);
242 		}
243 	}
244 
245 	/* Second round, destroy all inodes. */
246 	while ((node = LIST_FIRST(&tmp->tm_nodes)) != NULL) {
247 		tmpfs_free_node(tmp, node);
248 	}
249 
250 	/* Throw away the tmpfs_mount structure. */
251 	tmpfs_mntmem_destroy(tmp);
252 	/* mutex_destroy(&tmp->tm_lock); */
253 	/* kmem_free(tmp, sizeof(*tmp)); */
254 	free(tmp, M_MISCFSMNT, sizeof(tmpfs_mount_t));
255 	mp->mnt_data = NULL;
256 
257 	return 0;
258 }
259 
260 int
261 tmpfs_root(struct mount *mp, struct vnode **vpp)
262 {
263 	tmpfs_node_t *node = VFS_TO_TMPFS(mp)->tm_root;
264 
265 	rw_enter_write(&node->tn_nlock);
266 	return tmpfs_vnode_get(mp, node, vpp);
267 }
268 
269 int
270 tmpfs_vget(struct mount *mp, ino_t ino, struct vnode **vpp)
271 {
272 
273 	printf("tmpfs_vget called; need for it unknown yet\n");
274 	return EOPNOTSUPP;
275 }
276 
277 int
278 tmpfs_fhtovp(struct mount *mp, struct fid *fhp, struct vnode **vpp)
279 {
280 	tmpfs_mount_t *tmp = VFS_TO_TMPFS(mp);
281 	tmpfs_node_t *node;
282 	tmpfs_fid_t tfh;
283 
284 	if (fhp->fid_len != sizeof(tmpfs_fid_t)) {
285 		return EINVAL;
286 	}
287 	memcpy(&tfh, fhp, sizeof(tmpfs_fid_t));
288 
289 	rw_enter_write(&tmp->tm_lock);
290 	LIST_FOREACH(node, &tmp->tm_nodes, tn_entries) {
291 		if (node->tn_id != tfh.tf_id) {
292 			continue;
293 		}
294 		if (TMPFS_NODE_GEN(node) != tfh.tf_gen) {
295 			continue;
296 		}
297 		rw_enter_write(&node->tn_nlock);
298 		break;
299 	}
300 	rw_exit_write(&tmp->tm_lock);
301 
302 	/* Will release the tn_nlock. */
303 	return node ? tmpfs_vnode_get(mp, node, vpp) : ESTALE;
304 }
305 
306 int
307 tmpfs_vptofh(struct vnode *vp, struct fid *fhp)
308 {
309 	tmpfs_fid_t tfh;
310 	tmpfs_node_t *node;
311 
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 
323 int
324 tmpfs_statfs(struct mount *mp, struct statfs *sbp, struct proc *p)
325 {
326 	tmpfs_mount_t *tmp;
327 	fsfilcnt_t freenodes;
328 	uint64_t avail;
329 
330 	tmp = VFS_TO_TMPFS(mp);
331 
332 	sbp->f_iosize = sbp->f_bsize = PAGE_SIZE;
333 
334 	rw_enter_write(&tmp->tm_acc_lock);
335 	avail =  tmpfs_pages_avail(tmp);
336 	sbp->f_blocks = (tmpfs_bytes_max(tmp) >> PAGE_SHIFT);
337 	sbp->f_bfree = avail;
338 	sbp->f_bavail = avail & INT64_MAX; /* f_bavail is int64_t */
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_ffree = freenodes;
345 	sbp->f_favail = freenodes & INT64_MAX; /* f_favail is int64_t */
346 	rw_exit_write(&tmp->tm_acc_lock);
347 
348 	copy_statfs_info(sbp, mp);
349 
350 	return 0;
351 }
352 
353 int
354 tmpfs_sync(struct mount *mp, int waitfor, struct ucred *cred, struct proc *p)
355 {
356 
357 	return 0;
358 }
359 
360 /*
361  * tmpfs vfs operations.
362  */
363 
364 struct vfsops tmpfs_vfsops = {
365 	tmpfs_mount,			/* vfs_mount */
366 	tmpfs_start,			/* vfs_start */
367 	tmpfs_unmount,			/* vfs_unmount */
368 	tmpfs_root,			/* vfs_root */
369 	(void *)eopnotsupp,		/* vfs_quotactl */
370 	tmpfs_statfs,			/* vfs_statfs */
371 	tmpfs_sync,			/* vfs_sync */
372 	tmpfs_vget,			/* vfs_vget */
373 	tmpfs_fhtovp,			/* vfs_fhtovp */
374 	tmpfs_vptofh,			/* vfs_vptofh */
375 	tmpfs_init,			/* vfs_init */
376 	(void *)eopnotsupp,		/* vfs_sysctl */
377 	(void *)eopnotsupp,
378 };
379