xref: /freebsd/sys/fs/tmpfs/tmpfs_vfsops.c (revision 8f462da4f3cf6cabcbac87c971634bcdec12fe06)
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 
46 #include "opt_tmpfs.h"
47 
48 #include <sys/cdefs.h>
49 __FBSDID("$FreeBSD$");
50 
51 #include <sys/param.h>
52 #include <sys/systm.h>
53 #include <sys/dirent.h>
54 #include <sys/limits.h>
55 #include <sys/lock.h>
56 #include <sys/mount.h>
57 #include <sys/mutex.h>
58 #include <sys/proc.h>
59 #include <sys/jail.h>
60 #include <sys/kernel.h>
61 #include <sys/rwlock.h>
62 #include <sys/stat.h>
63 #include <sys/sysctl.h>
64 #include <sys/vnode.h>
65 
66 #include <vm/vm.h>
67 #include <vm/vm_object.h>
68 #include <vm/vm_param.h>
69 
70 #include <fs/tmpfs/tmpfs.h>
71 
72 /*
73  * Default permission for root node
74  */
75 #define TMPFS_DEFAULT_ROOT_MODE	(S_IRWXU|S_IRGRP|S_IXGRP|S_IROTH|S_IXOTH)
76 
77 MALLOC_DEFINE(M_TMPFSMNT, "tmpfs mount", "tmpfs mount structures");
78 MALLOC_DEFINE(M_TMPFSNAME, "tmpfs name", "tmpfs file names");
79 
80 static int	tmpfs_mount(struct mount *);
81 static int	tmpfs_unmount(struct mount *, int);
82 static int	tmpfs_root(struct mount *, int flags, struct vnode **);
83 static int	tmpfs_fhtovp(struct mount *, struct fid *, int,
84 		    struct vnode **);
85 static int	tmpfs_statfs(struct mount *, struct statfs *);
86 static void	tmpfs_susp_clean(struct mount *);
87 
88 static const char *tmpfs_opts[] = {
89 	"from", "size", "maxfilesize", "inodes", "uid", "gid", "mode", "export",
90 	"union", "nonc", NULL
91 };
92 
93 static const char *tmpfs_updateopts[] = {
94 	"from", "export", "size", NULL
95 };
96 
97 static int
98 tmpfs_node_ctor(void *mem, int size, void *arg, int flags)
99 {
100 	struct tmpfs_node *node = (struct tmpfs_node *)mem;
101 
102 	node->tn_gen++;
103 	node->tn_size = 0;
104 	node->tn_status = 0;
105 	node->tn_flags = 0;
106 	node->tn_links = 0;
107 	node->tn_vnode = NULL;
108 	node->tn_vpstate = 0;
109 
110 	return (0);
111 }
112 
113 static void
114 tmpfs_node_dtor(void *mem, int size, void *arg)
115 {
116 	struct tmpfs_node *node = (struct tmpfs_node *)mem;
117 	node->tn_type = VNON;
118 }
119 
120 static int
121 tmpfs_node_init(void *mem, int size, int flags)
122 {
123 	struct tmpfs_node *node = (struct tmpfs_node *)mem;
124 	node->tn_id = 0;
125 
126 	mtx_init(&node->tn_interlock, "tmpfs node interlock", NULL, MTX_DEF);
127 	node->tn_gen = arc4random();
128 
129 	return (0);
130 }
131 
132 static void
133 tmpfs_node_fini(void *mem, int size)
134 {
135 	struct tmpfs_node *node = (struct tmpfs_node *)mem;
136 
137 	mtx_destroy(&node->tn_interlock);
138 }
139 
140 static int
141 tmpfs_mount(struct mount *mp)
142 {
143 	const size_t nodes_per_page = howmany(PAGE_SIZE,
144 	    sizeof(struct tmpfs_dirent) + sizeof(struct tmpfs_node));
145 	struct tmpfs_mount *tmp;
146 	struct tmpfs_node *root;
147 	int error, flags;
148 	bool nonc;
149 	/* Size counters. */
150 	u_quad_t pages;
151 	off_t nodes_max, size_max, maxfilesize;
152 
153 	/* Root node attributes. */
154 	uid_t root_uid;
155 	gid_t root_gid;
156 	mode_t root_mode;
157 
158 	struct vattr va;
159 
160 	if (vfs_filteropt(mp->mnt_optnew, tmpfs_opts))
161 		return (EINVAL);
162 
163 	if (mp->mnt_flag & MNT_UPDATE) {
164 		/* Only support update mounts for certain options. */
165 		if (vfs_filteropt(mp->mnt_optnew, tmpfs_updateopts) != 0)
166 			return (EOPNOTSUPP);
167 		if (vfs_getopt_size(mp->mnt_optnew, "size", &size_max) == 0) {
168 			/*
169 			 * On-the-fly resizing is not supported (yet). We still
170 			 * need to have "size" listed as "supported", otherwise
171 			 * trying to update fs that is listed in fstab with size
172 			 * parameter, say trying to change rw to ro or vice
173 			 * versa, would cause vfs_filteropt() to bail.
174 			 */
175 			if (size_max != VFS_TO_TMPFS(mp)->tm_size_max)
176 				return (EOPNOTSUPP);
177 		}
178 		if (vfs_flagopt(mp->mnt_optnew, "ro", NULL, 0) &&
179 		    !(VFS_TO_TMPFS(mp)->tm_ronly)) {
180 			/* RW -> RO */
181 			error = VFS_SYNC(mp, MNT_WAIT);
182 			if (error)
183 				return (error);
184 			flags = WRITECLOSE;
185 			if (mp->mnt_flag & MNT_FORCE)
186 				flags |= FORCECLOSE;
187 			error = vflush(mp, 0, flags, curthread);
188 			if (error)
189 				return (error);
190 			VFS_TO_TMPFS(mp)->tm_ronly = 1;
191 			MNT_ILOCK(mp);
192 			mp->mnt_flag |= MNT_RDONLY;
193 			MNT_IUNLOCK(mp);
194 		} else if (!vfs_flagopt(mp->mnt_optnew, "ro", NULL, 0) &&
195 		    VFS_TO_TMPFS(mp)->tm_ronly) {
196 			/* RO -> RW */
197 			VFS_TO_TMPFS(mp)->tm_ronly = 0;
198 			MNT_ILOCK(mp);
199 			mp->mnt_flag &= ~MNT_RDONLY;
200 			MNT_IUNLOCK(mp);
201 		}
202 		return (0);
203 	}
204 
205 	vn_lock(mp->mnt_vnodecovered, LK_SHARED | LK_RETRY);
206 	error = VOP_GETATTR(mp->mnt_vnodecovered, &va, mp->mnt_cred);
207 	VOP_UNLOCK(mp->mnt_vnodecovered, 0);
208 	if (error)
209 		return (error);
210 
211 	if (mp->mnt_cred->cr_ruid != 0 ||
212 	    vfs_scanopt(mp->mnt_optnew, "gid", "%d", &root_gid) != 1)
213 		root_gid = va.va_gid;
214 	if (mp->mnt_cred->cr_ruid != 0 ||
215 	    vfs_scanopt(mp->mnt_optnew, "uid", "%d", &root_uid) != 1)
216 		root_uid = va.va_uid;
217 	if (mp->mnt_cred->cr_ruid != 0 ||
218 	    vfs_scanopt(mp->mnt_optnew, "mode", "%ho", &root_mode) != 1)
219 		root_mode = va.va_mode;
220 	if (vfs_getopt_size(mp->mnt_optnew, "inodes", &nodes_max) != 0)
221 		nodes_max = 0;
222 	if (vfs_getopt_size(mp->mnt_optnew, "size", &size_max) != 0)
223 		size_max = 0;
224 	if (vfs_getopt_size(mp->mnt_optnew, "maxfilesize", &maxfilesize) != 0)
225 		maxfilesize = 0;
226 	nonc = vfs_getopt(mp->mnt_optnew, "nonc", NULL, NULL) == 0;
227 
228 	/* Do not allow mounts if we do not have enough memory to preserve
229 	 * the minimum reserved pages. */
230 	if (tmpfs_mem_avail() < TMPFS_PAGES_MINRESERVED)
231 		return (ENOSPC);
232 
233 	/* Get the maximum number of memory pages this file system is
234 	 * allowed to use, based on the maximum size the user passed in
235 	 * the mount structure.  A value of zero is treated as if the
236 	 * maximum available space was requested. */
237 	if (size_max == 0 || size_max > OFF_MAX - PAGE_SIZE ||
238 	    (SIZE_MAX < OFF_MAX && size_max / PAGE_SIZE >= SIZE_MAX))
239 		pages = SIZE_MAX;
240 	else {
241 		size_max = roundup(size_max, PAGE_SIZE);
242 		pages = howmany(size_max, PAGE_SIZE);
243 	}
244 	MPASS(pages > 0);
245 
246 	if (nodes_max <= 3) {
247 		if (pages < INT_MAX / nodes_per_page)
248 			nodes_max = pages * nodes_per_page;
249 		else
250 			nodes_max = INT_MAX;
251 	}
252 	if (nodes_max > INT_MAX)
253 		nodes_max = INT_MAX;
254 	MPASS(nodes_max >= 3);
255 
256 	/* Allocate the tmpfs mount structure and fill it. */
257 	tmp = (struct tmpfs_mount *)malloc(sizeof(struct tmpfs_mount),
258 	    M_TMPFSMNT, M_WAITOK | M_ZERO);
259 
260 	mtx_init(&tmp->tm_allnode_lock, "tmpfs allnode lock", NULL, MTX_DEF);
261 	tmp->tm_nodes_max = nodes_max;
262 	tmp->tm_nodes_inuse = 0;
263 	tmp->tm_refcount = 1;
264 	tmp->tm_maxfilesize = maxfilesize > 0 ? maxfilesize : OFF_MAX;
265 	LIST_INIT(&tmp->tm_nodes_used);
266 
267 	tmp->tm_size_max = size_max;
268 	tmp->tm_pages_max = pages;
269 	tmp->tm_pages_used = 0;
270 	new_unrhdr64(&tmp->tm_ino_unr, 2);
271 	tmp->tm_dirent_pool = uma_zcreate("TMPFS dirent",
272 	    sizeof(struct tmpfs_dirent), NULL, NULL, NULL, NULL,
273 	    UMA_ALIGN_PTR, 0);
274 	tmp->tm_node_pool = uma_zcreate("TMPFS node",
275 	    sizeof(struct tmpfs_node), tmpfs_node_ctor, tmpfs_node_dtor,
276 	    tmpfs_node_init, tmpfs_node_fini, UMA_ALIGN_PTR, 0);
277 	tmp->tm_ronly = (mp->mnt_flag & MNT_RDONLY) != 0;
278 	tmp->tm_nonc = nonc;
279 
280 	/* Allocate the root node. */
281 	error = tmpfs_alloc_node(mp, tmp, VDIR, root_uid, root_gid,
282 	    root_mode & ALLPERMS, NULL, NULL, VNOVAL, &root);
283 
284 	if (error != 0 || root == NULL) {
285 		uma_zdestroy(tmp->tm_node_pool);
286 		uma_zdestroy(tmp->tm_dirent_pool);
287 		free(tmp, M_TMPFSMNT);
288 		return (error);
289 	}
290 	KASSERT(root->tn_id == 2,
291 	    ("tmpfs root with invalid ino: %ju", (uintmax_t)root->tn_id));
292 	tmp->tm_root = root;
293 
294 	MNT_ILOCK(mp);
295 	mp->mnt_flag |= MNT_LOCAL;
296 	mp->mnt_kern_flag |= MNTK_LOOKUP_SHARED | MNTK_EXTENDED_SHARED;
297 	MNT_IUNLOCK(mp);
298 
299 	mp->mnt_data = tmp;
300 	mp->mnt_stat.f_namemax = MAXNAMLEN;
301 	vfs_getnewfsid(mp);
302 	vfs_mountedfrom(mp, "tmpfs");
303 
304 	return 0;
305 }
306 
307 /* ARGSUSED2 */
308 static int
309 tmpfs_unmount(struct mount *mp, int mntflags)
310 {
311 	struct tmpfs_mount *tmp;
312 	struct tmpfs_node *node;
313 	int error, flags;
314 
315 	flags = (mntflags & MNT_FORCE) != 0 ? FORCECLOSE : 0;
316 	tmp = VFS_TO_TMPFS(mp);
317 
318 	/* Stop writers */
319 	error = vfs_write_suspend_umnt(mp);
320 	if (error != 0)
321 		return (error);
322 	/*
323 	 * At this point, nodes cannot be destroyed by any other
324 	 * thread because write suspension is started.
325 	 */
326 
327 	for (;;) {
328 		error = vflush(mp, 0, flags, curthread);
329 		if (error != 0) {
330 			vfs_write_resume(mp, VR_START_WRITE);
331 			return (error);
332 		}
333 		MNT_ILOCK(mp);
334 		if (mp->mnt_nvnodelistsize == 0) {
335 			MNT_IUNLOCK(mp);
336 			break;
337 		}
338 		MNT_IUNLOCK(mp);
339 		if ((mntflags & MNT_FORCE) == 0) {
340 			vfs_write_resume(mp, VR_START_WRITE);
341 			return (EBUSY);
342 		}
343 	}
344 
345 	TMPFS_LOCK(tmp);
346 	while ((node = LIST_FIRST(&tmp->tm_nodes_used)) != NULL) {
347 		TMPFS_NODE_LOCK(node);
348 		if (node->tn_type == VDIR)
349 			tmpfs_dir_destroy(tmp, node);
350 		if (tmpfs_free_node_locked(tmp, node, true))
351 			TMPFS_LOCK(tmp);
352 		else
353 			TMPFS_NODE_UNLOCK(node);
354 	}
355 
356 	mp->mnt_data = NULL;
357 	tmpfs_free_tmp(tmp);
358 	vfs_write_resume(mp, VR_START_WRITE);
359 
360 	MNT_ILOCK(mp);
361 	mp->mnt_flag &= ~MNT_LOCAL;
362 	MNT_IUNLOCK(mp);
363 
364 	return (0);
365 }
366 
367 void
368 tmpfs_free_tmp(struct tmpfs_mount *tmp)
369 {
370 
371 	MPASS(tmp->tm_refcount > 0);
372 	tmp->tm_refcount--;
373 	if (tmp->tm_refcount > 0) {
374 		TMPFS_UNLOCK(tmp);
375 		return;
376 	}
377 	TMPFS_UNLOCK(tmp);
378 
379 	uma_zdestroy(tmp->tm_dirent_pool);
380 	uma_zdestroy(tmp->tm_node_pool);
381 
382 	mtx_destroy(&tmp->tm_allnode_lock);
383 	MPASS(tmp->tm_pages_used == 0);
384 	MPASS(tmp->tm_nodes_inuse == 0);
385 
386 	free(tmp, M_TMPFSMNT);
387 }
388 
389 static int
390 tmpfs_root(struct mount *mp, int flags, struct vnode **vpp)
391 {
392 	int error;
393 
394 	error = tmpfs_alloc_vp(mp, VFS_TO_TMPFS(mp)->tm_root, flags, vpp);
395 	if (error == 0)
396 		(*vpp)->v_vflag |= VV_ROOT;
397 	return (error);
398 }
399 
400 static int
401 tmpfs_fhtovp(struct mount *mp, struct fid *fhp, int flags,
402     struct vnode **vpp)
403 {
404 	struct tmpfs_fid *tfhp;
405 	struct tmpfs_mount *tmp;
406 	struct tmpfs_node *node;
407 	int error;
408 
409 	tmp = VFS_TO_TMPFS(mp);
410 
411 	tfhp = (struct tmpfs_fid *)fhp;
412 	if (tfhp->tf_len != sizeof(struct tmpfs_fid))
413 		return (EINVAL);
414 
415 	if (tfhp->tf_id >= tmp->tm_nodes_max)
416 		return (EINVAL);
417 
418 	TMPFS_LOCK(tmp);
419 	LIST_FOREACH(node, &tmp->tm_nodes_used, tn_entries) {
420 		if (node->tn_id == tfhp->tf_id &&
421 		    node->tn_gen == tfhp->tf_gen) {
422 			tmpfs_ref_node(node);
423 			break;
424 		}
425 	}
426 	TMPFS_UNLOCK(tmp);
427 
428 	if (node != NULL) {
429 		error = tmpfs_alloc_vp(mp, node, LK_EXCLUSIVE, vpp);
430 		tmpfs_free_node(tmp, node);
431 	} else
432 		error = EINVAL;
433 	return (error);
434 }
435 
436 /* ARGSUSED2 */
437 static int
438 tmpfs_statfs(struct mount *mp, struct statfs *sbp)
439 {
440 	struct tmpfs_mount *tmp;
441 	size_t used;
442 
443 	tmp = VFS_TO_TMPFS(mp);
444 
445 	sbp->f_iosize = PAGE_SIZE;
446 	sbp->f_bsize = PAGE_SIZE;
447 
448 	used = tmpfs_pages_used(tmp);
449 	if (tmp->tm_pages_max != ULONG_MAX)
450 		 sbp->f_blocks = tmp->tm_pages_max;
451 	else
452 		 sbp->f_blocks = used + tmpfs_mem_avail();
453 	if (sbp->f_blocks <= used)
454 		sbp->f_bavail = 0;
455 	else
456 		sbp->f_bavail = sbp->f_blocks - used;
457 	sbp->f_bfree = sbp->f_bavail;
458 	used = tmp->tm_nodes_inuse;
459 	sbp->f_files = tmp->tm_nodes_max;
460 	if (sbp->f_files <= used)
461 		sbp->f_ffree = 0;
462 	else
463 		sbp->f_ffree = sbp->f_files - used;
464 	/* sbp->f_owner = tmp->tn_uid; */
465 
466 	return 0;
467 }
468 
469 static int
470 tmpfs_sync(struct mount *mp, int waitfor)
471 {
472 	struct vnode *vp, *mvp;
473 	struct vm_object *obj;
474 
475 	if (waitfor == MNT_SUSPEND) {
476 		MNT_ILOCK(mp);
477 		mp->mnt_kern_flag |= MNTK_SUSPEND2 | MNTK_SUSPENDED;
478 		MNT_IUNLOCK(mp);
479 	} else if (waitfor == MNT_LAZY) {
480 		/*
481 		 * Handle lazy updates of mtime from writes to mmaped
482 		 * regions.  Use MNT_VNODE_FOREACH_ALL instead of
483 		 * MNT_VNODE_FOREACH_ACTIVE, since unmap of the
484 		 * tmpfs-backed vnode does not call vinactive(), due
485 		 * to vm object type is OBJT_SWAP.
486 		 */
487 		MNT_VNODE_FOREACH_ALL(vp, mp, mvp) {
488 			if (vp->v_type != VREG) {
489 				VI_UNLOCK(vp);
490 				continue;
491 			}
492 			obj = vp->v_object;
493 			KASSERT((obj->flags & (OBJ_TMPFS_NODE | OBJ_TMPFS)) ==
494 			    (OBJ_TMPFS_NODE | OBJ_TMPFS), ("non-tmpfs obj"));
495 
496 			/*
497 			 * Unlocked read, avoid taking vnode lock if
498 			 * not needed.  Lost update will be handled on
499 			 * the next call.
500 			 */
501 			if ((obj->flags & OBJ_TMPFS_DIRTY) == 0) {
502 				VI_UNLOCK(vp);
503 				continue;
504 			}
505 			if (vget(vp, LK_EXCLUSIVE | LK_RETRY | LK_INTERLOCK,
506 			    curthread) != 0)
507 				continue;
508 			tmpfs_check_mtime(vp);
509 			vput(vp);
510 		}
511 	}
512 	return (0);
513 }
514 
515 /*
516  * The presence of a susp_clean method tells the VFS to track writes.
517  */
518 static void
519 tmpfs_susp_clean(struct mount *mp __unused)
520 {
521 }
522 
523 /*
524  * tmpfs vfs operations.
525  */
526 
527 struct vfsops tmpfs_vfsops = {
528 	.vfs_mount =			tmpfs_mount,
529 	.vfs_unmount =			tmpfs_unmount,
530 	.vfs_root =			tmpfs_root,
531 	.vfs_statfs =			tmpfs_statfs,
532 	.vfs_fhtovp =			tmpfs_fhtovp,
533 	.vfs_sync =			tmpfs_sync,
534 	.vfs_susp_clean =		tmpfs_susp_clean,
535 };
536 VFS_SET(tmpfs_vfsops, tmpfs, VFCF_JAIL);
537