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