xref: /openbsd-src/sys/tmpfs/tmpfs_vfsops.c (revision 2777ee89d0e541ec819d05abee114837837abbec)
1 /*	$OpenBSD: tmpfs_vfsops.c,v 1.8 2016/01/13 13:01:40 gsoares 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 
129 	/* Get the memory usage limit for this file-system. */
130 	if (args.ta_size_max < PAGE_SIZE) {
131 		memlimit = UINT64_MAX;
132 	} else {
133 		memlimit = args.ta_size_max;
134 	}
135 	KASSERT(memlimit > 0);
136 
137 	if (args.ta_nodes_max <= 3) {
138 		nodes = 3 + (memlimit / 1024);
139 	} else {
140 		nodes = args.ta_nodes_max;
141 	}
142 	nodes = MIN(nodes, INT_MAX);
143 	KASSERT(nodes >= 3);
144 
145 	/* Allocate the tmpfs mount structure and fill it. */
146 	tmp = malloc(sizeof(tmpfs_mount_t), M_MISCFSMNT, M_WAITOK);
147 
148 	tmp->tm_nodes_max = (ino_t)nodes;
149 	tmp->tm_nodes_cnt = 0;
150 	tmp->tm_highest_inode = 1;
151 	LIST_INIT(&tmp->tm_nodes);
152 
153 	rw_init(&tmp->tm_lock, "tmplk");
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,
159 	    VNOVAL, &root);
160 	KASSERT(error == 0 && root != NULL);
161 
162 	/*
163 	 * Parent of the root inode is itself.  Also, root inode has no
164 	 * directory entry (i.e. is never attached), thus hold an extra
165 	 * reference (link) for it.
166 	 */
167 	root->tn_links++;
168 	root->tn_spec.tn_dir.tn_parent = root;
169 	tmp->tm_root = root;
170 
171 	mp->mnt_data = tmp;
172 	mp->mnt_flag |= MNT_LOCAL;
173 	mp->mnt_stat.f_namemax = TMPFS_MAXNAMLEN;
174 #if 0
175 	mp->mnt_fs_bshift = PAGE_SHIFT;
176 	mp->mnt_dev_bshift = DEV_BSHIFT;
177 	mp->mnt_iflag |= IMNT_MPSAFE;
178 #endif
179 	vfs_getnewfsid(mp);
180 
181 	mp->mnt_stat.mount_info.tmpfs_args = args;
182 
183 	bzero(&mp->mnt_stat.f_mntonname, sizeof(mp->mnt_stat.f_mntonname));
184 	bzero(&mp->mnt_stat.f_mntfromname, sizeof(mp->mnt_stat.f_mntfromname));
185 	bzero(&mp->mnt_stat.f_mntfromspec, sizeof(mp->mnt_stat.f_mntfromspec));
186 
187 	strlcpy(mp->mnt_stat.f_mntonname, path,
188 	    sizeof(mp->mnt_stat.f_mntonname) - 1);
189 	strlcpy(mp->mnt_stat.f_mntfromname, "tmpfs",
190 	    sizeof(mp->mnt_stat.f_mntfromname) - 1);
191 	strlcpy(mp->mnt_stat.f_mntfromspec, "tmpfs",
192 	    sizeof(mp->mnt_stat.f_mntfromspec) - 1);
193 
194 	return error;
195 }
196 
197 int
198 tmpfs_start(struct mount *mp, int flags, struct proc *p)
199 {
200 
201 	return 0;
202 }
203 
204 int
205 tmpfs_unmount(struct mount *mp, int mntflags, struct proc *p)
206 {
207 	tmpfs_mount_t *tmp = VFS_TO_TMPFS(mp);
208 	tmpfs_node_t *node, *cnode;
209 	int error, flags = 0;
210 
211 	/* Handle forced unmounts. */
212 	if (mntflags & MNT_FORCE)
213 		flags |= FORCECLOSE;
214 
215 	/* Finalize all pending I/O. */
216 	error = vflush(mp, NULL, flags);
217 	if (error != 0)
218 		return error;
219 
220 
221 	/*
222 	 * First round, detach and destroy all directory entries.
223 	 * Also, clear the pointers to the vnodes - they are gone.
224 	 */
225 	LIST_FOREACH(node, &tmp->tm_nodes, tn_entries) {
226 		tmpfs_dirent_t *de;
227 
228 		node->tn_vnode = NULL;
229 		if (node->tn_type != VDIR) {
230 			continue;
231 		}
232 		while ((de = TAILQ_FIRST(&node->tn_spec.tn_dir.tn_dir)) != NULL) {
233 			cnode = de->td_node;
234 			if (cnode) {
235 				cnode->tn_vnode = NULL;
236 			}
237 			tmpfs_dir_detach(node, de);
238 			tmpfs_free_dirent(tmp, de);
239 		}
240 	}
241 
242 	/* Second round, destroy all inodes. */
243 	while ((node = LIST_FIRST(&tmp->tm_nodes)) != NULL) {
244 		tmpfs_free_node(tmp, node);
245 	}
246 
247 	/* Throw away the tmpfs_mount structure. */
248 	tmpfs_mntmem_destroy(tmp);
249 	/* mutex_destroy(&tmp->tm_lock); */
250 	/* kmem_free(tmp, sizeof(*tmp)); */
251 	free(tmp, M_MISCFSMNT, sizeof(tmpfs_mount_t));
252 	mp->mnt_data = NULL;
253 
254 	return 0;
255 }
256 
257 int
258 tmpfs_root(struct mount *mp, struct vnode **vpp)
259 {
260 	tmpfs_node_t *node = VFS_TO_TMPFS(mp)->tm_root;
261 
262 	rw_enter_write(&node->tn_nlock);
263 	return tmpfs_vnode_get(mp, node, vpp);
264 }
265 
266 int
267 tmpfs_vget(struct mount *mp, ino_t ino, struct vnode **vpp)
268 {
269 
270 	printf("tmpfs_vget called; need for it unknown yet\n");
271 	return EOPNOTSUPP;
272 }
273 
274 int
275 tmpfs_fhtovp(struct mount *mp, struct fid *fhp, struct vnode **vpp)
276 {
277 	tmpfs_mount_t *tmp = VFS_TO_TMPFS(mp);
278 	tmpfs_node_t *node;
279 	tmpfs_fid_t tfh;
280 
281 	if (fhp->fid_len != sizeof(tmpfs_fid_t)) {
282 		return EINVAL;
283 	}
284 	memcpy(&tfh, fhp, sizeof(tmpfs_fid_t));
285 
286 	rw_enter_write(&tmp->tm_lock);
287 	LIST_FOREACH(node, &tmp->tm_nodes, tn_entries) {
288 		if (node->tn_id != tfh.tf_id) {
289 			continue;
290 		}
291 		if (TMPFS_NODE_GEN(node) != tfh.tf_gen) {
292 			continue;
293 		}
294 		rw_enter_write(&node->tn_nlock);
295 		break;
296 	}
297 	rw_exit_write(&tmp->tm_lock);
298 
299 	/* Will release the tn_nlock. */
300 	return node ? tmpfs_vnode_get(mp, node, vpp) : ESTALE;
301 }
302 
303 int
304 tmpfs_vptofh(struct vnode *vp, struct fid *fhp)
305 {
306 	tmpfs_fid_t tfh;
307 	tmpfs_node_t *node;
308 
309 	node = VP_TO_TMPFS_NODE(vp);
310 
311 	memset(&tfh, 0, sizeof(tfh));
312 	tfh.tf_len = sizeof(tmpfs_fid_t);
313 	tfh.tf_gen = TMPFS_NODE_GEN(node);
314 	tfh.tf_id = node->tn_id;
315 	memcpy(fhp, &tfh, sizeof(tfh));
316 
317 	return 0;
318 }
319 
320 int
321 tmpfs_statfs(struct mount *mp, struct statfs *sbp, struct proc *p)
322 {
323 	tmpfs_mount_t *tmp;
324 	fsfilcnt_t freenodes;
325 	uint64_t avail;
326 
327 	tmp = VFS_TO_TMPFS(mp);
328 
329 	sbp->f_iosize = sbp->f_bsize = PAGE_SIZE;
330 
331 	rw_enter_write(&tmp->tm_acc_lock);
332 	avail =  tmpfs_pages_avail(tmp);
333 	sbp->f_blocks = (tmpfs_bytes_max(tmp) >> PAGE_SHIFT);
334 	sbp->f_bfree = avail;
335 	sbp->f_bavail = avail & INT64_MAX; /* f_bavail is int64_t */
336 
337 	freenodes = MIN(tmp->tm_nodes_max - tmp->tm_nodes_cnt,
338 	    avail * PAGE_SIZE / sizeof(tmpfs_node_t));
339 
340 	sbp->f_files = tmp->tm_nodes_cnt + freenodes;
341 	sbp->f_ffree = freenodes;
342 	sbp->f_favail = freenodes & INT64_MAX; /* f_favail is int64_t */
343 	rw_exit_write(&tmp->tm_acc_lock);
344 
345 	copy_statfs_info(sbp, mp);
346 
347 	return 0;
348 }
349 
350 int
351 tmpfs_sync(struct mount *mp, int waitfor, struct ucred *cred, struct proc *p)
352 {
353 
354 	return 0;
355 }
356 
357 /*
358  * tmpfs vfs operations.
359  */
360 
361 struct vfsops tmpfs_vfsops = {
362 	tmpfs_mount,			/* vfs_mount */
363 	tmpfs_start,			/* vfs_start */
364 	tmpfs_unmount,			/* vfs_unmount */
365 	tmpfs_root,			/* vfs_root */
366 	(void *)eopnotsupp,		/* vfs_quotactl */
367 	tmpfs_statfs,			/* vfs_statfs */
368 	tmpfs_sync,			/* vfs_sync */
369 	tmpfs_vget,			/* vfs_vget */
370 	tmpfs_fhtovp,			/* vfs_fhtovp */
371 	tmpfs_vptofh,			/* vfs_vptofh */
372 	tmpfs_init,			/* vfs_init */
373 	NULL,				/* vfs_sysctl */
374 	(void *)eopnotsupp,
375 };
376