xref: /openbsd-src/sys/tmpfs/tmpfs_vfsops.c (revision ae3cb403620ab940fbaabb3055fac045a63d56b7)
1 /*	$OpenBSD: tmpfs_vfsops.c,v 1.14 2017/12/11 05:27:40 deraadt 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/mount.h>
47 #include <sys/stat.h>
48 #include <sys/systm.h>
49 #include <sys/vnode.h>
50 #include <sys/malloc.h>
51 
52 #include <tmpfs/tmpfs.h>
53 
54 /* MODULE(MODULE_CLASS_VFS, tmpfs, NULL); */
55 
56 struct pool	tmpfs_dirent_pool;
57 struct pool	tmpfs_node_pool;
58 
59 int	tmpfs_mount(struct mount *, const char *, void *, struct nameidata *,
60 	    struct proc *);
61 int	tmpfs_start(struct mount *, int, struct proc *);
62 int	tmpfs_unmount(struct mount *, int, struct proc *);
63 int	tmpfs_root(struct mount *, struct vnode **);
64 int	tmpfs_vget(struct mount *, ino_t, struct vnode **);
65 int	tmpfs_fhtovp(struct mount *, struct fid *, struct vnode **);
66 int	tmpfs_vptofh(struct vnode *, struct fid *);
67 int	tmpfs_statfs(struct mount *, struct statfs *, struct proc *);
68 int	tmpfs_sync(struct mount *, int, struct ucred *, struct proc *);
69 int	tmpfs_init(struct vfsconf *);
70 
71 int
72 tmpfs_init(struct vfsconf *vfsp)
73 {
74 
75 	pool_init(&tmpfs_dirent_pool, sizeof(tmpfs_dirent_t), 0, IPL_NONE,
76 	    PR_WAITOK, "tmpfs_dirent", NULL);
77 	pool_init(&tmpfs_node_pool, sizeof(tmpfs_node_t), 0, IPL_NONE,
78 	    PR_WAITOK, "tmpfs_node", NULL);
79 
80 	return 0;
81 }
82 
83 int
84 tmpfs_mount(struct mount *mp, const char *path, void *data,
85     struct nameidata *ndp, struct proc *p)
86 {
87 	struct tmpfs_args *args = data;
88 	tmpfs_mount_t *tmp;
89 	tmpfs_node_t *root;
90 	uint64_t memlimit;
91 	uint64_t nodes;
92 	int error;
93 
94 #if 0
95 	/* Handle retrieval of mount point arguments. */
96 	if (mp->mnt_flag & MNT_GETARGS) {
97 		if (mp->mnt_data == NULL)
98 			return EIO;
99 		tmp = VFS_TO_TMPFS(mp);
100 
101 		args->ta_version = TMPFS_ARGS_VERSION;
102 		args->ta_nodes_max = tmp->tm_nodes_max;
103 		args->ta_size_max = tmp->tm_mem_limit;
104 
105 		root = tmp->tm_root;
106 		args->ta_root_uid = root->tn_uid;
107 		args->ta_root_gid = root->tn_gid;
108 		args->ta_root_mode = root->tn_mode;
109 
110 		*data_len = sizeof(*args);
111 		return 0;
112 	}
113 #endif
114 
115 	if (mp->mnt_flag & MNT_UPDATE) {
116 		/* TODO */
117 		return EOPNOTSUPP;
118 	}
119 
120 	/* Prohibit mounts if there is not enough memory. */
121 	if (tmpfs_mem_info(1) < TMPFS_PAGES_RESERVED)
122 		return EINVAL;
123 
124 	error = copyin(data, args, sizeof(struct tmpfs_args));
125 	if (error)
126 		return error;
127 	if (args->ta_root_uid == VNOVAL || args->ta_root_gid == VNOVAL ||
128 	    args->ta_root_mode == VNOVAL)
129 		return EINVAL;
130 
131 	/* Get the memory usage limit for this file-system. */
132 	if (args->ta_size_max < PAGE_SIZE) {
133 		memlimit = UINT64_MAX;
134 	} else {
135 		memlimit = args->ta_size_max;
136 	}
137 	KASSERT(memlimit > 0);
138 
139 	if (args->ta_nodes_max <= 3) {
140 		nodes = 3 + (memlimit / 1024);
141 	} else {
142 		nodes = args->ta_nodes_max;
143 	}
144 	nodes = MIN(nodes, INT_MAX);
145 	KASSERT(nodes >= 3);
146 
147 	/* Allocate the tmpfs mount structure and fill it. */
148 	tmp = malloc(sizeof(tmpfs_mount_t), M_MISCFSMNT, M_WAITOK);
149 
150 	tmp->tm_nodes_max = (ino_t)nodes;
151 	tmp->tm_nodes_cnt = 0;
152 	tmp->tm_highest_inode = 1;
153 	LIST_INIT(&tmp->tm_nodes);
154 
155 	rw_init(&tmp->tm_lock, "tmplk");
156 	tmpfs_mntmem_init(tmp, memlimit);
157 
158 	/* Allocate the root node. */
159 	error = tmpfs_alloc_node(tmp, VDIR, args->ta_root_uid,
160 	    args->ta_root_gid, args->ta_root_mode & ALLPERMS, NULL,
161 	    VNOVAL, &root);
162 	KASSERT(error == 0 && root != NULL);
163 
164 	/*
165 	 * Parent of the root inode is itself.  Also, root inode has no
166 	 * directory entry (i.e. is never attached), thus hold an extra
167 	 * reference (link) for it.
168 	 */
169 	root->tn_links++;
170 	root->tn_spec.tn_dir.tn_parent = root;
171 	tmp->tm_root = root;
172 
173 	mp->mnt_data = tmp;
174 	mp->mnt_flag |= MNT_LOCAL;
175 	mp->mnt_stat.f_namemax = TMPFS_MAXNAMLEN;
176 #if 0
177 	mp->mnt_fs_bshift = PAGE_SHIFT;
178 	mp->mnt_dev_bshift = DEV_BSHIFT;
179 	mp->mnt_iflag |= IMNT_MPSAFE;
180 #endif
181 	vfs_getnewfsid(mp);
182 
183 	mp->mnt_stat.mount_info.tmpfs_args = *args;
184 
185 	bzero(&mp->mnt_stat.f_mntonname, sizeof(mp->mnt_stat.f_mntonname));
186 	bzero(&mp->mnt_stat.f_mntfromname, sizeof(mp->mnt_stat.f_mntfromname));
187 	bzero(&mp->mnt_stat.f_mntfromspec, sizeof(mp->mnt_stat.f_mntfromspec));
188 
189 	strlcpy(mp->mnt_stat.f_mntonname, path,
190 	    sizeof(mp->mnt_stat.f_mntonname) - 1);
191 	strlcpy(mp->mnt_stat.f_mntfromname, "tmpfs",
192 	    sizeof(mp->mnt_stat.f_mntfromname) - 1);
193 	strlcpy(mp->mnt_stat.f_mntfromspec, "tmpfs",
194 	    sizeof(mp->mnt_stat.f_mntfromspec) - 1);
195 
196 	return error;
197 }
198 
199 int
200 tmpfs_start(struct mount *mp, int flags, struct proc *p)
201 {
202 
203 	return 0;
204 }
205 
206 int
207 tmpfs_unmount(struct mount *mp, int mntflags, struct proc *p)
208 {
209 	tmpfs_mount_t *tmp = VFS_TO_TMPFS(mp);
210 	tmpfs_node_t *node, *cnode;
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 
223 	/*
224 	 * First round, detach and destroy all directory entries.
225 	 * Also, clear the pointers to the vnodes - they are gone.
226 	 */
227 	LIST_FOREACH(node, &tmp->tm_nodes, tn_entries) {
228 		tmpfs_dirent_t *de;
229 
230 		node->tn_vnode = NULL;
231 		if (node->tn_type != VDIR) {
232 			continue;
233 		}
234 		while ((de = TAILQ_FIRST(&node->tn_spec.tn_dir.tn_dir)) != NULL) {
235 			cnode = de->td_node;
236 			if (cnode) {
237 				cnode->tn_vnode = NULL;
238 			}
239 			tmpfs_dir_detach(node, de);
240 			tmpfs_free_dirent(tmp, de);
241 		}
242 	}
243 
244 	/* Second round, destroy all inodes. */
245 	while ((node = LIST_FIRST(&tmp->tm_nodes)) != NULL) {
246 		tmpfs_free_node(tmp, node);
247 	}
248 
249 	/* Throw away the tmpfs_mount structure. */
250 	tmpfs_mntmem_destroy(tmp);
251 	/* mutex_destroy(&tmp->tm_lock); */
252 	/* kmem_free(tmp, sizeof(*tmp)); */
253 	free(tmp, M_MISCFSMNT, sizeof(tmpfs_mount_t));
254 	mp->mnt_data = NULL;
255 
256 	return 0;
257 }
258 
259 int
260 tmpfs_root(struct mount *mp, struct vnode **vpp)
261 {
262 	tmpfs_node_t *node = VFS_TO_TMPFS(mp)->tm_root;
263 
264 	rw_enter_write(&node->tn_nlock);
265 	return tmpfs_vnode_get(mp, node, vpp);
266 }
267 
268 int
269 tmpfs_vget(struct mount *mp, ino_t ino, struct vnode **vpp)
270 {
271 
272 	printf("tmpfs_vget called; need for it unknown yet\n");
273 	return EOPNOTSUPP;
274 }
275 
276 int
277 tmpfs_fhtovp(struct mount *mp, struct fid *fhp, struct vnode **vpp)
278 {
279 	tmpfs_mount_t *tmp = VFS_TO_TMPFS(mp);
280 	tmpfs_node_t *node;
281 	tmpfs_fid_t tfh;
282 
283 	if (fhp->fid_len != sizeof(tmpfs_fid_t)) {
284 		return EINVAL;
285 	}
286 	memcpy(&tfh, fhp, sizeof(tmpfs_fid_t));
287 
288 	rw_enter_write(&tmp->tm_lock);
289 	LIST_FOREACH(node, &tmp->tm_nodes, tn_entries) {
290 		if (node->tn_id != tfh.tf_id) {
291 			continue;
292 		}
293 		if (TMPFS_NODE_GEN(node) != tfh.tf_gen) {
294 			continue;
295 		}
296 		rw_enter_write(&node->tn_nlock);
297 		break;
298 	}
299 	rw_exit_write(&tmp->tm_lock);
300 
301 	/* Will release the tn_nlock. */
302 	return node ? tmpfs_vnode_get(mp, node, vpp) : ESTALE;
303 }
304 
305 int
306 tmpfs_vptofh(struct vnode *vp, struct fid *fhp)
307 {
308 	tmpfs_fid_t tfh;
309 	tmpfs_node_t *node;
310 
311 	node = VP_TO_TMPFS_NODE(vp);
312 
313 	memset(&tfh, 0, sizeof(tfh));
314 	tfh.tf_len = sizeof(tmpfs_fid_t);
315 	tfh.tf_gen = TMPFS_NODE_GEN(node);
316 	tfh.tf_id = node->tn_id;
317 	memcpy(fhp, &tfh, sizeof(tfh));
318 
319 	return 0;
320 }
321 
322 int
323 tmpfs_statfs(struct mount *mp, struct statfs *sbp, struct proc *p)
324 {
325 	tmpfs_mount_t *tmp;
326 	fsfilcnt_t freenodes;
327 	uint64_t avail;
328 
329 	tmp = VFS_TO_TMPFS(mp);
330 
331 	sbp->f_iosize = sbp->f_bsize = PAGE_SIZE;
332 
333 	rw_enter_write(&tmp->tm_acc_lock);
334 	avail =  tmpfs_pages_avail(tmp);
335 	sbp->f_blocks = (tmpfs_bytes_max(tmp) >> PAGE_SHIFT);
336 	sbp->f_bfree = avail;
337 	sbp->f_bavail = avail & INT64_MAX; /* f_bavail is int64_t */
338 
339 	freenodes = MIN(tmp->tm_nodes_max - tmp->tm_nodes_cnt,
340 	    avail * PAGE_SIZE / sizeof(tmpfs_node_t));
341 
342 	sbp->f_files = tmp->tm_nodes_cnt + freenodes;
343 	sbp->f_ffree = freenodes;
344 	sbp->f_favail = freenodes & INT64_MAX; /* f_favail is int64_t */
345 	rw_exit_write(&tmp->tm_acc_lock);
346 
347 	copy_statfs_info(sbp, mp);
348 
349 	return 0;
350 }
351 
352 int
353 tmpfs_sync(struct mount *mp, int waitfor, struct ucred *cred, struct proc *p)
354 {
355 
356 	return 0;
357 }
358 
359 /*
360  * tmpfs vfs operations.
361  */
362 
363 struct vfsops tmpfs_vfsops = {
364 	tmpfs_mount,			/* vfs_mount */
365 	tmpfs_start,			/* vfs_start */
366 	tmpfs_unmount,			/* vfs_unmount */
367 	tmpfs_root,			/* vfs_root */
368 	(void *)eopnotsupp,		/* vfs_quotactl */
369 	tmpfs_statfs,			/* vfs_statfs */
370 	tmpfs_sync,			/* vfs_sync */
371 	tmpfs_vget,			/* vfs_vget */
372 	tmpfs_fhtovp,			/* vfs_fhtovp */
373 	tmpfs_vptofh,			/* vfs_vptofh */
374 	tmpfs_init,			/* vfs_init */
375 	(void *)eopnotsupp,		/* vfs_sysctl */
376 	(void *)eopnotsupp,
377 };
378