xref: /freebsd/sys/fs/tmpfs/tmpfs_vfsops.c (revision f5147e312f43a9050468de539aeafa072caa1a60)
1 /*	$NetBSD: tmpfs_vfsops.c,v 1.10 2005/12/11 12:24:29 christos Exp $	*/
2 
3 /*-
4  * SPDX-License-Identifier: BSD-2-Clause-NetBSD
5  *
6  * Copyright (c) 2005 The NetBSD Foundation, Inc.
7  * All rights reserved.
8  *
9  * This code is derived from software contributed to The NetBSD Foundation
10  * by Julio M. Merino Vidal, developed as part of Google's Summer of Code
11  * 2005 program.
12  *
13  * Redistribution and use in source and binary forms, with or without
14  * modification, are permitted provided that the following conditions
15  * are met:
16  * 1. Redistributions of source code must retain the above copyright
17  *    notice, this list of conditions and the following disclaimer.
18  * 2. Redistributions in binary form must reproduce the above copyright
19  *    notice, this list of conditions and the following disclaimer in the
20  *    documentation and/or other materials provided with the distribution.
21  *
22  * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
23  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
24  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
25  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
26  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
27  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
28  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
29  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
30  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
31  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
32  * POSSIBILITY OF SUCH DAMAGE.
33  */
34 
35 /*
36  * Efficient memory file system.
37  *
38  * tmpfs is a file system that uses FreeBSD's virtual memory
39  * sub-system to store file data and metadata in an efficient way.
40  * This means that it does not follow the structure of an on-disk file
41  * system because it simply does not need to.  Instead, it uses
42  * memory-specific data structures and algorithms to automatically
43  * allocate and release resources.
44  */
45 #include <sys/cdefs.h>
46 __FBSDID("$FreeBSD$");
47 
48 #include <sys/param.h>
49 #include <sys/dirent.h>
50 #include <sys/limits.h>
51 #include <sys/lock.h>
52 #include <sys/mount.h>
53 #include <sys/mutex.h>
54 #include <sys/proc.h>
55 #include <sys/jail.h>
56 #include <sys/kernel.h>
57 #include <sys/rwlock.h>
58 #include <sys/stat.h>
59 #include <sys/systm.h>
60 #include <sys/sysctl.h>
61 #include <sys/vnode.h>
62 
63 #include <vm/vm.h>
64 #include <vm/vm_object.h>
65 #include <vm/vm_param.h>
66 
67 #include <fs/tmpfs/tmpfs.h>
68 
69 /*
70  * Default permission for root node
71  */
72 #define TMPFS_DEFAULT_ROOT_MODE	(S_IRWXU|S_IRGRP|S_IXGRP|S_IROTH|S_IXOTH)
73 
74 MALLOC_DEFINE(M_TMPFSMNT, "tmpfs mount", "tmpfs mount structures");
75 MALLOC_DEFINE(M_TMPFSNAME, "tmpfs name", "tmpfs file names");
76 
77 static int	tmpfs_mount(struct mount *);
78 static int	tmpfs_unmount(struct mount *, int);
79 static int	tmpfs_root(struct mount *, int flags, struct vnode **);
80 static int	tmpfs_fhtovp(struct mount *, struct fid *, int,
81 		    struct vnode **);
82 static int	tmpfs_statfs(struct mount *, struct statfs *);
83 static void	tmpfs_susp_clean(struct mount *);
84 
85 static const char *tmpfs_opts[] = {
86 	"from", "size", "maxfilesize", "inodes", "uid", "gid", "mode", "export",
87 	"union", "nonc", NULL
88 };
89 
90 static const char *tmpfs_updateopts[] = {
91 	"from", "export", NULL
92 };
93 
94 static int
95 tmpfs_node_ctor(void *mem, int size, void *arg, int flags)
96 {
97 	struct tmpfs_node *node = (struct tmpfs_node *)mem;
98 
99 	node->tn_gen++;
100 	node->tn_size = 0;
101 	node->tn_status = 0;
102 	node->tn_flags = 0;
103 	node->tn_links = 0;
104 	node->tn_vnode = NULL;
105 	node->tn_vpstate = 0;
106 
107 	return (0);
108 }
109 
110 static void
111 tmpfs_node_dtor(void *mem, int size, void *arg)
112 {
113 	struct tmpfs_node *node = (struct tmpfs_node *)mem;
114 	node->tn_type = VNON;
115 }
116 
117 static int
118 tmpfs_node_init(void *mem, int size, int flags)
119 {
120 	struct tmpfs_node *node = (struct tmpfs_node *)mem;
121 	node->tn_id = 0;
122 
123 	mtx_init(&node->tn_interlock, "tmpfs node interlock", NULL, MTX_DEF);
124 	node->tn_gen = arc4random();
125 
126 	return (0);
127 }
128 
129 static void
130 tmpfs_node_fini(void *mem, int size)
131 {
132 	struct tmpfs_node *node = (struct tmpfs_node *)mem;
133 
134 	mtx_destroy(&node->tn_interlock);
135 }
136 
137 static int
138 tmpfs_mount(struct mount *mp)
139 {
140 	const size_t nodes_per_page = howmany(PAGE_SIZE,
141 	    sizeof(struct tmpfs_dirent) + sizeof(struct tmpfs_node));
142 	struct tmpfs_mount *tmp;
143 	struct tmpfs_node *root;
144 	struct thread *td = curthread;
145 	int error;
146 	bool nonc;
147 	/* Size counters. */
148 	u_quad_t pages;
149 	off_t nodes_max, size_max, maxfilesize;
150 
151 	/* Root node attributes. */
152 	uid_t root_uid;
153 	gid_t root_gid;
154 	mode_t root_mode;
155 
156 	struct vattr va;
157 
158 	if (!prison_allow(td->td_ucred, PR_ALLOW_MOUNT_TMPFS))
159 		return (EPERM);
160 
161 	if (vfs_filteropt(mp->mnt_optnew, tmpfs_opts))
162 		return (EINVAL);
163 
164 	if (mp->mnt_flag & MNT_UPDATE) {
165 		/* Only support update mounts for certain options. */
166 		if (vfs_filteropt(mp->mnt_optnew, tmpfs_updateopts) != 0)
167 			return (EOPNOTSUPP);
168 		if (vfs_flagopt(mp->mnt_optnew, "ro", NULL, 0) !=
169 		    ((struct tmpfs_mount *)mp->mnt_data)->tm_ronly)
170 			return (EOPNOTSUPP);
171 		return (0);
172 	}
173 
174 	vn_lock(mp->mnt_vnodecovered, LK_SHARED | LK_RETRY);
175 	error = VOP_GETATTR(mp->mnt_vnodecovered, &va, mp->mnt_cred);
176 	VOP_UNLOCK(mp->mnt_vnodecovered, 0);
177 	if (error)
178 		return (error);
179 
180 	if (mp->mnt_cred->cr_ruid != 0 ||
181 	    vfs_scanopt(mp->mnt_optnew, "gid", "%d", &root_gid) != 1)
182 		root_gid = va.va_gid;
183 	if (mp->mnt_cred->cr_ruid != 0 ||
184 	    vfs_scanopt(mp->mnt_optnew, "uid", "%d", &root_uid) != 1)
185 		root_uid = va.va_uid;
186 	if (mp->mnt_cred->cr_ruid != 0 ||
187 	    vfs_scanopt(mp->mnt_optnew, "mode", "%ho", &root_mode) != 1)
188 		root_mode = va.va_mode;
189 	if (vfs_getopt_size(mp->mnt_optnew, "inodes", &nodes_max) != 0)
190 		nodes_max = 0;
191 	if (vfs_getopt_size(mp->mnt_optnew, "size", &size_max) != 0)
192 		size_max = 0;
193 	if (vfs_getopt_size(mp->mnt_optnew, "maxfilesize", &maxfilesize) != 0)
194 		maxfilesize = 0;
195 	nonc = vfs_getopt(mp->mnt_optnew, "nonc", NULL, NULL) == 0;
196 
197 	/* Do not allow mounts if we do not have enough memory to preserve
198 	 * the minimum reserved pages. */
199 	if (tmpfs_mem_avail() < TMPFS_PAGES_MINRESERVED)
200 		return (ENOSPC);
201 
202 	/* Get the maximum number of memory pages this file system is
203 	 * allowed to use, based on the maximum size the user passed in
204 	 * the mount structure.  A value of zero is treated as if the
205 	 * maximum available space was requested. */
206 	if (size_max == 0 || size_max > OFF_MAX - PAGE_SIZE ||
207 	    (SIZE_MAX < OFF_MAX && size_max / PAGE_SIZE >= SIZE_MAX))
208 		pages = SIZE_MAX;
209 	else {
210 		size_max = roundup(size_max, PAGE_SIZE);
211 		pages = howmany(size_max, PAGE_SIZE);
212 	}
213 	MPASS(pages > 0);
214 
215 	if (nodes_max <= 3) {
216 		if (pages < INT_MAX / nodes_per_page)
217 			nodes_max = pages * nodes_per_page;
218 		else
219 			nodes_max = INT_MAX;
220 	}
221 	if (nodes_max > INT_MAX)
222 		nodes_max = INT_MAX;
223 	MPASS(nodes_max >= 3);
224 
225 	/* Allocate the tmpfs mount structure and fill it. */
226 	tmp = (struct tmpfs_mount *)malloc(sizeof(struct tmpfs_mount),
227 	    M_TMPFSMNT, M_WAITOK | M_ZERO);
228 
229 	mtx_init(&tmp->tm_allnode_lock, "tmpfs allnode lock", NULL, MTX_DEF);
230 	tmp->tm_nodes_max = nodes_max;
231 	tmp->tm_nodes_inuse = 0;
232 	tmp->tm_refcount = 1;
233 	tmp->tm_maxfilesize = maxfilesize > 0 ? maxfilesize : OFF_MAX;
234 	LIST_INIT(&tmp->tm_nodes_used);
235 
236 	tmp->tm_pages_max = pages;
237 	tmp->tm_pages_used = 0;
238 	tmp->tm_ino_unr = new_unrhdr(2, INT_MAX, &tmp->tm_allnode_lock);
239 	tmp->tm_dirent_pool = uma_zcreate("TMPFS dirent",
240 	    sizeof(struct tmpfs_dirent), NULL, NULL, NULL, NULL,
241 	    UMA_ALIGN_PTR, 0);
242 	tmp->tm_node_pool = uma_zcreate("TMPFS node",
243 	    sizeof(struct tmpfs_node), tmpfs_node_ctor, tmpfs_node_dtor,
244 	    tmpfs_node_init, tmpfs_node_fini, UMA_ALIGN_PTR, 0);
245 	tmp->tm_ronly = (mp->mnt_flag & MNT_RDONLY) != 0;
246 	tmp->tm_nonc = nonc;
247 
248 	/* Allocate the root node. */
249 	error = tmpfs_alloc_node(mp, tmp, VDIR, root_uid, root_gid,
250 	    root_mode & ALLPERMS, NULL, NULL, VNOVAL, &root);
251 
252 	if (error != 0 || root == NULL) {
253 		uma_zdestroy(tmp->tm_node_pool);
254 		uma_zdestroy(tmp->tm_dirent_pool);
255 		delete_unrhdr(tmp->tm_ino_unr);
256 		free(tmp, M_TMPFSMNT);
257 		return (error);
258 	}
259 	KASSERT(root->tn_id == 2,
260 	    ("tmpfs root with invalid ino: %ju", (uintmax_t)root->tn_id));
261 	tmp->tm_root = root;
262 
263 	MNT_ILOCK(mp);
264 	mp->mnt_flag |= MNT_LOCAL;
265 	mp->mnt_kern_flag |= MNTK_LOOKUP_SHARED | MNTK_EXTENDED_SHARED;
266 	MNT_IUNLOCK(mp);
267 
268 	mp->mnt_data = tmp;
269 	mp->mnt_stat.f_namemax = MAXNAMLEN;
270 	vfs_getnewfsid(mp);
271 	vfs_mountedfrom(mp, "tmpfs");
272 
273 	return 0;
274 }
275 
276 /* ARGSUSED2 */
277 static int
278 tmpfs_unmount(struct mount *mp, int mntflags)
279 {
280 	struct tmpfs_mount *tmp;
281 	struct tmpfs_node *node;
282 	int error, flags;
283 
284 	flags = (mntflags & MNT_FORCE) != 0 ? FORCECLOSE : 0;
285 	tmp = VFS_TO_TMPFS(mp);
286 
287 	/* Stop writers */
288 	error = vfs_write_suspend_umnt(mp);
289 	if (error != 0)
290 		return (error);
291 	/*
292 	 * At this point, nodes cannot be destroyed by any other
293 	 * thread because write suspension is started.
294 	 */
295 
296 	for (;;) {
297 		error = vflush(mp, 0, flags, curthread);
298 		if (error != 0) {
299 			vfs_write_resume(mp, VR_START_WRITE);
300 			return (error);
301 		}
302 		MNT_ILOCK(mp);
303 		if (mp->mnt_nvnodelistsize == 0) {
304 			MNT_IUNLOCK(mp);
305 			break;
306 		}
307 		MNT_IUNLOCK(mp);
308 		if ((mntflags & MNT_FORCE) == 0) {
309 			vfs_write_resume(mp, VR_START_WRITE);
310 			return (EBUSY);
311 		}
312 	}
313 
314 	TMPFS_LOCK(tmp);
315 	while ((node = LIST_FIRST(&tmp->tm_nodes_used)) != NULL) {
316 		TMPFS_NODE_LOCK(node);
317 		if (node->tn_type == VDIR)
318 			tmpfs_dir_destroy(tmp, node);
319 		if (tmpfs_free_node_locked(tmp, node, true))
320 			TMPFS_LOCK(tmp);
321 		else
322 			TMPFS_NODE_UNLOCK(node);
323 	}
324 
325 	mp->mnt_data = NULL;
326 	tmpfs_free_tmp(tmp);
327 	vfs_write_resume(mp, VR_START_WRITE);
328 
329 	MNT_ILOCK(mp);
330 	mp->mnt_flag &= ~MNT_LOCAL;
331 	MNT_IUNLOCK(mp);
332 
333 	return (0);
334 }
335 
336 void
337 tmpfs_free_tmp(struct tmpfs_mount *tmp)
338 {
339 
340 	MPASS(tmp->tm_refcount > 0);
341 	tmp->tm_refcount--;
342 	if (tmp->tm_refcount > 0) {
343 		TMPFS_UNLOCK(tmp);
344 		return;
345 	}
346 	TMPFS_UNLOCK(tmp);
347 
348 	uma_zdestroy(tmp->tm_dirent_pool);
349 	uma_zdestroy(tmp->tm_node_pool);
350 	clear_unrhdr(tmp->tm_ino_unr);
351 	delete_unrhdr(tmp->tm_ino_unr);
352 
353 	mtx_destroy(&tmp->tm_allnode_lock);
354 	MPASS(tmp->tm_pages_used == 0);
355 	MPASS(tmp->tm_nodes_inuse == 0);
356 
357 	free(tmp, M_TMPFSMNT);
358 }
359 
360 static int
361 tmpfs_root(struct mount *mp, int flags, struct vnode **vpp)
362 {
363 	int error;
364 
365 	error = tmpfs_alloc_vp(mp, VFS_TO_TMPFS(mp)->tm_root, flags, vpp);
366 	if (error == 0)
367 		(*vpp)->v_vflag |= VV_ROOT;
368 	return (error);
369 }
370 
371 static int
372 tmpfs_fhtovp(struct mount *mp, struct fid *fhp, int flags,
373     struct vnode **vpp)
374 {
375 	struct tmpfs_fid *tfhp;
376 	struct tmpfs_mount *tmp;
377 	struct tmpfs_node *node;
378 	int error;
379 
380 	tmp = VFS_TO_TMPFS(mp);
381 
382 	tfhp = (struct tmpfs_fid *)fhp;
383 	if (tfhp->tf_len != sizeof(struct tmpfs_fid))
384 		return (EINVAL);
385 
386 	if (tfhp->tf_id >= tmp->tm_nodes_max)
387 		return (EINVAL);
388 
389 	TMPFS_LOCK(tmp);
390 	LIST_FOREACH(node, &tmp->tm_nodes_used, tn_entries) {
391 		if (node->tn_id == tfhp->tf_id &&
392 		    node->tn_gen == tfhp->tf_gen) {
393 			tmpfs_ref_node(node);
394 			break;
395 		}
396 	}
397 	TMPFS_UNLOCK(tmp);
398 
399 	if (node != NULL) {
400 		error = tmpfs_alloc_vp(mp, node, LK_EXCLUSIVE, vpp);
401 		tmpfs_free_node(tmp, node);
402 	} else
403 		error = EINVAL;
404 	return (error);
405 }
406 
407 /* ARGSUSED2 */
408 static int
409 tmpfs_statfs(struct mount *mp, struct statfs *sbp)
410 {
411 	struct tmpfs_mount *tmp;
412 	size_t used;
413 
414 	tmp = VFS_TO_TMPFS(mp);
415 
416 	sbp->f_iosize = PAGE_SIZE;
417 	sbp->f_bsize = PAGE_SIZE;
418 
419 	used = tmpfs_pages_used(tmp);
420 	if (tmp->tm_pages_max != ULONG_MAX)
421 		 sbp->f_blocks = tmp->tm_pages_max;
422 	else
423 		 sbp->f_blocks = used + tmpfs_mem_avail();
424 	if (sbp->f_blocks <= used)
425 		sbp->f_bavail = 0;
426 	else
427 		sbp->f_bavail = sbp->f_blocks - used;
428 	sbp->f_bfree = sbp->f_bavail;
429 	used = tmp->tm_nodes_inuse;
430 	sbp->f_files = tmp->tm_nodes_max;
431 	if (sbp->f_files <= used)
432 		sbp->f_ffree = 0;
433 	else
434 		sbp->f_ffree = sbp->f_files - used;
435 	/* sbp->f_owner = tmp->tn_uid; */
436 
437 	return 0;
438 }
439 
440 static int
441 tmpfs_sync(struct mount *mp, int waitfor)
442 {
443 	struct vnode *vp, *mvp;
444 	struct vm_object *obj;
445 
446 	if (waitfor == MNT_SUSPEND) {
447 		MNT_ILOCK(mp);
448 		mp->mnt_kern_flag |= MNTK_SUSPEND2 | MNTK_SUSPENDED;
449 		MNT_IUNLOCK(mp);
450 	} else if (waitfor == MNT_LAZY) {
451 		/*
452 		 * Handle lazy updates of mtime from writes to mmaped
453 		 * regions.  Use MNT_VNODE_FOREACH_ALL instead of
454 		 * MNT_VNODE_FOREACH_ACTIVE, since unmap of the
455 		 * tmpfs-backed vnode does not call vinactive(), due
456 		 * to vm object type is OBJT_SWAP.
457 		 */
458 		MNT_VNODE_FOREACH_ALL(vp, mp, mvp) {
459 			if (vp->v_type != VREG) {
460 				VI_UNLOCK(vp);
461 				continue;
462 			}
463 			obj = vp->v_object;
464 			KASSERT((obj->flags & (OBJ_TMPFS_NODE | OBJ_TMPFS)) ==
465 			    (OBJ_TMPFS_NODE | OBJ_TMPFS), ("non-tmpfs obj"));
466 
467 			/*
468 			 * Unlocked read, avoid taking vnode lock if
469 			 * not needed.  Lost update will be handled on
470 			 * the next call.
471 			 */
472 			if ((obj->flags & OBJ_TMPFS_DIRTY) == 0) {
473 				VI_UNLOCK(vp);
474 				continue;
475 			}
476 			if (vget(vp, LK_EXCLUSIVE | LK_RETRY | LK_INTERLOCK,
477 			    curthread) != 0)
478 				continue;
479 			tmpfs_check_mtime(vp);
480 			vput(vp);
481 		}
482 	}
483 	return (0);
484 }
485 
486 /*
487  * The presence of a susp_clean method tells the VFS to track writes.
488  */
489 static void
490 tmpfs_susp_clean(struct mount *mp __unused)
491 {
492 }
493 
494 /*
495  * tmpfs vfs operations.
496  */
497 
498 struct vfsops tmpfs_vfsops = {
499 	.vfs_mount =			tmpfs_mount,
500 	.vfs_unmount =			tmpfs_unmount,
501 	.vfs_root =			tmpfs_root,
502 	.vfs_statfs =			tmpfs_statfs,
503 	.vfs_fhtovp =			tmpfs_fhtovp,
504 	.vfs_sync =			tmpfs_sync,
505 	.vfs_susp_clean =		tmpfs_susp_clean,
506 };
507 VFS_SET(tmpfs_vfsops, tmpfs, VFCF_JAIL);
508