xref: /freebsd-12.1/sys/fs/tmpfs/tmpfs_vfsops.c (revision 4a9f095f)
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/systm.h>
50 #include <sys/dirent.h>
51 #include <sys/limits.h>
52 #include <sys/lock.h>
53 #include <sys/mount.h>
54 #include <sys/mutex.h>
55 #include <sys/proc.h>
56 #include <sys/jail.h>
57 #include <sys/kernel.h>
58 #include <sys/rwlock.h>
59 #include <sys/stat.h>
60 #include <sys/sx.h>
61 #include <sys/sysctl.h>
62 #include <sys/vnode.h>
63 
64 #include <vm/vm.h>
65 #include <vm/vm_param.h>
66 #include <vm/pmap.h>
67 #include <vm/vm_extern.h>
68 #include <vm/vm_map.h>
69 #include <vm/vm_object.h>
70 #include <vm/vm_param.h>
71 
72 #include <fs/tmpfs/tmpfs.h>
73 
74 /*
75  * Default permission for root node
76  */
77 #define TMPFS_DEFAULT_ROOT_MODE	(S_IRWXU|S_IRGRP|S_IXGRP|S_IROTH|S_IXOTH)
78 
79 MALLOC_DEFINE(M_TMPFSMNT, "tmpfs mount", "tmpfs mount structures");
80 MALLOC_DEFINE(M_TMPFSNAME, "tmpfs name", "tmpfs file names");
81 
82 static int	tmpfs_mount(struct mount *);
83 static int	tmpfs_unmount(struct mount *, int);
84 static int	tmpfs_root(struct mount *, int flags, struct vnode **);
85 static int	tmpfs_fhtovp(struct mount *, struct fid *, int,
86 		    struct vnode **);
87 static int	tmpfs_statfs(struct mount *, struct statfs *);
88 static void	tmpfs_susp_clean(struct mount *);
89 
90 static const char *tmpfs_opts[] = {
91 	"from", "size", "maxfilesize", "inodes", "uid", "gid", "mode", "export",
92 	"union", "nonc", NULL
93 };
94 
95 static const char *tmpfs_updateopts[] = {
96 	"from", "export", "size", NULL
97 };
98 
99 static int
tmpfs_node_ctor(void * mem,int size,void * arg,int flags)100 tmpfs_node_ctor(void *mem, int size, void *arg, int flags)
101 {
102 	struct tmpfs_node *node = (struct tmpfs_node *)mem;
103 
104 	node->tn_gen++;
105 	node->tn_size = 0;
106 	node->tn_status = 0;
107 	node->tn_flags = 0;
108 	node->tn_links = 0;
109 	node->tn_vnode = NULL;
110 	node->tn_vpstate = 0;
111 
112 	return (0);
113 }
114 
115 static void
tmpfs_node_dtor(void * mem,int size,void * arg)116 tmpfs_node_dtor(void *mem, int size, void *arg)
117 {
118 	struct tmpfs_node *node = (struct tmpfs_node *)mem;
119 	node->tn_type = VNON;
120 }
121 
122 static int
tmpfs_node_init(void * mem,int size,int flags)123 tmpfs_node_init(void *mem, int size, int flags)
124 {
125 	struct tmpfs_node *node = (struct tmpfs_node *)mem;
126 	node->tn_id = 0;
127 
128 	mtx_init(&node->tn_interlock, "tmpfs node interlock", NULL, MTX_DEF);
129 	node->tn_gen = arc4random();
130 
131 	return (0);
132 }
133 
134 static void
tmpfs_node_fini(void * mem,int size)135 tmpfs_node_fini(void *mem, int size)
136 {
137 	struct tmpfs_node *node = (struct tmpfs_node *)mem;
138 
139 	mtx_destroy(&node->tn_interlock);
140 }
141 
142 /*
143  * Handle updates of time from writes to mmaped regions.  Use
144  * MNT_VNODE_FOREACH_ALL instead of MNT_VNODE_FOREACH_ACTIVE, since
145  * unmap of the tmpfs-backed vnode does not call vinactive(), due to
146  * vm object type is OBJT_SWAP.
147  * If lazy, only handle delayed update of mtime due to the writes to
148  * mapped files.
149  */
150 static void
tmpfs_update_mtime(struct mount * mp,bool lazy)151 tmpfs_update_mtime(struct mount *mp, bool lazy)
152 {
153 	struct vnode *vp, *mvp;
154 	struct vm_object *obj;
155 
156 	MNT_VNODE_FOREACH_ALL(vp, mp, mvp) {
157 		if (vp->v_type != VREG) {
158 			VI_UNLOCK(vp);
159 			continue;
160 		}
161 		obj = vp->v_object;
162 		KASSERT((obj->flags & (OBJ_TMPFS_NODE | OBJ_TMPFS)) ==
163 		    (OBJ_TMPFS_NODE | OBJ_TMPFS), ("non-tmpfs obj"));
164 
165 		/*
166 		 * In lazy case, do unlocked read, avoid taking vnode
167 		 * lock if not needed.  Lost update will be handled on
168 		 * the next call.
169 		 * For non-lazy case, we must flush all pending
170 		 * metadata changes now.
171 		 */
172 		if (!lazy || (obj->flags & OBJ_TMPFS_DIRTY) != 0) {
173 			if (vget(vp, LK_EXCLUSIVE | LK_INTERLOCK,
174 			    curthread) != 0)
175 				continue;
176 			tmpfs_check_mtime(vp);
177 			if (!lazy)
178 				tmpfs_update(vp);
179 			vput(vp);
180 		} else {
181 			VI_UNLOCK(vp);
182 			continue;
183 		}
184 	}
185 }
186 
187 struct tmpfs_check_rw_maps_arg {
188 	bool found;
189 };
190 
191 static bool
tmpfs_check_rw_maps_cb(struct mount * mp __unused,vm_map_t map __unused,vm_map_entry_t entry __unused,void * arg)192 tmpfs_check_rw_maps_cb(struct mount *mp __unused, vm_map_t map __unused,
193     vm_map_entry_t entry __unused, void *arg)
194 {
195 	struct tmpfs_check_rw_maps_arg *a;
196 
197 	a = arg;
198 	a->found = true;
199 	return (true);
200 }
201 
202 /*
203  * Revoke write permissions from all mappings of regular files
204  * belonging to the specified tmpfs mount.
205  */
206 static bool
tmpfs_revoke_rw_maps_cb(struct mount * mp __unused,vm_map_t map,vm_map_entry_t entry,void * arg __unused)207 tmpfs_revoke_rw_maps_cb(struct mount *mp __unused, vm_map_t map,
208     vm_map_entry_t entry, void *arg __unused)
209 {
210 
211 	/*
212 	 * XXXKIB: might be invalidate the mapping
213 	 * instead ?  The process is not going to be
214 	 * happy in any case.
215 	 */
216 	entry->max_protection &= ~VM_PROT_WRITE;
217 	if ((entry->protection & VM_PROT_WRITE) != 0) {
218 		entry->protection &= ~VM_PROT_WRITE;
219 		pmap_protect(map->pmap, entry->start, entry->end,
220 		    entry->protection);
221 	}
222 	return (false);
223 }
224 
225 static void
tmpfs_all_rw_maps(struct mount * mp,bool (* cb)(struct mount * mp,vm_map_t,vm_map_entry_t,void *),void * cb_arg)226 tmpfs_all_rw_maps(struct mount *mp, bool (*cb)(struct mount *mp, vm_map_t,
227     vm_map_entry_t, void *), void *cb_arg)
228 {
229 	struct proc *p;
230 	struct vmspace *vm;
231 	vm_map_t map;
232 	vm_map_entry_t entry;
233 	vm_object_t object;
234 	struct vnode *vp;
235 	int gen;
236 	bool terminate;
237 
238 	terminate = false;
239 	sx_slock(&allproc_lock);
240 again:
241 	gen = allproc_gen;
242 	FOREACH_PROC_IN_SYSTEM(p) {
243 		PROC_LOCK(p);
244 		if (p->p_state != PRS_NORMAL || (p->p_flag & (P_INEXEC |
245 		    P_SYSTEM | P_WEXIT)) != 0) {
246 			PROC_UNLOCK(p);
247 			continue;
248 		}
249 		vm = vmspace_acquire_ref(p);
250 		_PHOLD_LITE(p);
251 		PROC_UNLOCK(p);
252 		if (vm == NULL) {
253 			PRELE(p);
254 			continue;
255 		}
256 		sx_sunlock(&allproc_lock);
257 		map = &vm->vm_map;
258 
259 		vm_map_lock(map);
260 		if (map->busy)
261 			vm_map_wait_busy(map);
262 		for (entry = map->header.next; entry != &map->header;
263 		    entry = entry->next) {
264 			if ((entry->eflags & (MAP_ENTRY_GUARD |
265 			    MAP_ENTRY_IS_SUB_MAP | MAP_ENTRY_COW)) != 0 ||
266 			    (entry->max_protection & VM_PROT_WRITE) == 0)
267 				continue;
268 			object = entry->object.vm_object;
269 			if (object == NULL || object->type != OBJT_SWAP ||
270 			    (object->flags & OBJ_TMPFS_NODE) == 0)
271 				continue;
272 			/*
273 			 * No need to dig into shadow chain, mapping
274 			 * of the object not at top is readonly.
275 			 */
276 
277 			VM_OBJECT_RLOCK(object);
278 			if (object->type == OBJT_DEAD) {
279 				VM_OBJECT_RUNLOCK(object);
280 				continue;
281 			}
282 			MPASS(object->ref_count > 1);
283 			if ((object->flags & (OBJ_TMPFS_NODE | OBJ_TMPFS)) !=
284 			    (OBJ_TMPFS_NODE | OBJ_TMPFS)) {
285 				VM_OBJECT_RUNLOCK(object);
286 				continue;
287 			}
288 			vp = object->un_pager.swp.swp_tmpfs;
289 			if (vp->v_mount != mp) {
290 				VM_OBJECT_RUNLOCK(object);
291 				continue;
292 			}
293 
294 			terminate = cb(mp, map, entry, cb_arg);
295 			VM_OBJECT_RUNLOCK(object);
296 			if (terminate)
297 				break;
298 		}
299 		vm_map_unlock(map);
300 
301 		vmspace_free(vm);
302 		sx_slock(&allproc_lock);
303 		PRELE(p);
304 		if (terminate)
305 			break;
306 	}
307 	if (!terminate && gen != allproc_gen)
308 		goto again;
309 	sx_sunlock(&allproc_lock);
310 }
311 
312 static bool
tmpfs_check_rw_maps(struct mount * mp)313 tmpfs_check_rw_maps(struct mount *mp)
314 {
315 	struct tmpfs_check_rw_maps_arg ca;
316 
317 	ca.found = false;
318 	tmpfs_all_rw_maps(mp, tmpfs_check_rw_maps_cb, &ca);
319 	return (ca.found);
320 }
321 
322 static int
tmpfs_rw_to_ro(struct mount * mp)323 tmpfs_rw_to_ro(struct mount *mp)
324 {
325 	int error, flags;
326 	bool forced;
327 
328 	forced = (mp->mnt_flag & MNT_FORCE) != 0;
329 	flags = WRITECLOSE | (forced ? FORCECLOSE : 0);
330 
331 	if ((error = vn_start_write(NULL, &mp, V_WAIT)) != 0)
332 		return (error);
333 	error = vfs_write_suspend_umnt(mp);
334 	if (error != 0)
335 		return (error);
336 	if (!forced && tmpfs_check_rw_maps(mp)) {
337 		error = EBUSY;
338 		goto out;
339 	}
340 	VFS_TO_TMPFS(mp)->tm_ronly = 1;
341 	MNT_ILOCK(mp);
342 	mp->mnt_flag |= MNT_RDONLY;
343 	MNT_IUNLOCK(mp);
344 	for (;;) {
345 		tmpfs_all_rw_maps(mp, tmpfs_revoke_rw_maps_cb, NULL);
346 		tmpfs_update_mtime(mp, false);
347 		error = vflush(mp, 0, flags, curthread);
348 		if (error != 0) {
349 			VFS_TO_TMPFS(mp)->tm_ronly = 0;
350 			MNT_ILOCK(mp);
351 			mp->mnt_flag &= ~MNT_RDONLY;
352 			MNT_IUNLOCK(mp);
353 			goto out;
354 		}
355 		if (!tmpfs_check_rw_maps(mp))
356 			break;
357 	}
358 out:
359 	vfs_write_resume(mp, 0);
360 	return (error);
361 }
362 
363 static int
tmpfs_mount(struct mount * mp)364 tmpfs_mount(struct mount *mp)
365 {
366 	const size_t nodes_per_page = howmany(PAGE_SIZE,
367 	    sizeof(struct tmpfs_dirent) + sizeof(struct tmpfs_node));
368 	struct tmpfs_mount *tmp;
369 	struct tmpfs_node *root;
370 	int error;
371 	bool nonc;
372 	/* Size counters. */
373 	u_quad_t pages;
374 	off_t nodes_max, size_max, maxfilesize;
375 
376 	/* Root node attributes. */
377 	uid_t root_uid;
378 	gid_t root_gid;
379 	mode_t root_mode;
380 
381 	struct vattr va;
382 
383 	if (vfs_filteropt(mp->mnt_optnew, tmpfs_opts))
384 		return (EINVAL);
385 
386 	if (mp->mnt_flag & MNT_UPDATE) {
387 		/* Only support update mounts for certain options. */
388 		if (vfs_filteropt(mp->mnt_optnew, tmpfs_updateopts) != 0)
389 			return (EOPNOTSUPP);
390 		if (vfs_getopt_size(mp->mnt_optnew, "size", &size_max) == 0) {
391 			/*
392 			 * On-the-fly resizing is not supported (yet). We still
393 			 * need to have "size" listed as "supported", otherwise
394 			 * trying to update fs that is listed in fstab with size
395 			 * parameter, say trying to change rw to ro or vice
396 			 * versa, would cause vfs_filteropt() to bail.
397 			 */
398 			if (size_max != VFS_TO_TMPFS(mp)->tm_size_max)
399 				return (EOPNOTSUPP);
400 		}
401 		if (vfs_flagopt(mp->mnt_optnew, "ro", NULL, 0) &&
402 		    !(VFS_TO_TMPFS(mp)->tm_ronly)) {
403 			/* RW -> RO */
404 			return (tmpfs_rw_to_ro(mp));
405 		} else if (!vfs_flagopt(mp->mnt_optnew, "ro", NULL, 0) &&
406 		    VFS_TO_TMPFS(mp)->tm_ronly) {
407 			/* RO -> RW */
408 			VFS_TO_TMPFS(mp)->tm_ronly = 0;
409 			MNT_ILOCK(mp);
410 			mp->mnt_flag &= ~MNT_RDONLY;
411 			MNT_IUNLOCK(mp);
412 		}
413 		return (0);
414 	}
415 
416 	vn_lock(mp->mnt_vnodecovered, LK_SHARED | LK_RETRY);
417 	error = VOP_GETATTR(mp->mnt_vnodecovered, &va, mp->mnt_cred);
418 	VOP_UNLOCK(mp->mnt_vnodecovered, 0);
419 	if (error)
420 		return (error);
421 
422 	if (mp->mnt_cred->cr_ruid != 0 ||
423 	    vfs_scanopt(mp->mnt_optnew, "gid", "%d", &root_gid) != 1)
424 		root_gid = va.va_gid;
425 	if (mp->mnt_cred->cr_ruid != 0 ||
426 	    vfs_scanopt(mp->mnt_optnew, "uid", "%d", &root_uid) != 1)
427 		root_uid = va.va_uid;
428 	if (mp->mnt_cred->cr_ruid != 0 ||
429 	    vfs_scanopt(mp->mnt_optnew, "mode", "%ho", &root_mode) != 1)
430 		root_mode = va.va_mode;
431 	if (vfs_getopt_size(mp->mnt_optnew, "inodes", &nodes_max) != 0)
432 		nodes_max = 0;
433 	if (vfs_getopt_size(mp->mnt_optnew, "size", &size_max) != 0)
434 		size_max = 0;
435 	if (vfs_getopt_size(mp->mnt_optnew, "maxfilesize", &maxfilesize) != 0)
436 		maxfilesize = 0;
437 	nonc = vfs_getopt(mp->mnt_optnew, "nonc", NULL, NULL) == 0;
438 
439 	/* Do not allow mounts if we do not have enough memory to preserve
440 	 * the minimum reserved pages. */
441 	if (tmpfs_mem_avail() < TMPFS_PAGES_MINRESERVED)
442 		return (ENOSPC);
443 
444 	/* Get the maximum number of memory pages this file system is
445 	 * allowed to use, based on the maximum size the user passed in
446 	 * the mount structure.  A value of zero is treated as if the
447 	 * maximum available space was requested. */
448 	if (size_max == 0 || size_max > OFF_MAX - PAGE_SIZE ||
449 	    (SIZE_MAX < OFF_MAX && size_max / PAGE_SIZE >= SIZE_MAX))
450 		pages = SIZE_MAX;
451 	else {
452 		size_max = roundup(size_max, PAGE_SIZE);
453 		pages = howmany(size_max, PAGE_SIZE);
454 	}
455 	MPASS(pages > 0);
456 
457 	if (nodes_max <= 3) {
458 		if (pages < INT_MAX / nodes_per_page)
459 			nodes_max = pages * nodes_per_page;
460 		else
461 			nodes_max = INT_MAX;
462 	}
463 	if (nodes_max > INT_MAX)
464 		nodes_max = INT_MAX;
465 	MPASS(nodes_max >= 3);
466 
467 	/* Allocate the tmpfs mount structure and fill it. */
468 	tmp = (struct tmpfs_mount *)malloc(sizeof(struct tmpfs_mount),
469 	    M_TMPFSMNT, M_WAITOK | M_ZERO);
470 
471 	mtx_init(&tmp->tm_allnode_lock, "tmpfs allnode lock", NULL, MTX_DEF);
472 	tmp->tm_nodes_max = nodes_max;
473 	tmp->tm_nodes_inuse = 0;
474 	tmp->tm_refcount = 1;
475 	tmp->tm_maxfilesize = maxfilesize > 0 ? maxfilesize : OFF_MAX;
476 	LIST_INIT(&tmp->tm_nodes_used);
477 
478 	tmp->tm_size_max = size_max;
479 	tmp->tm_pages_max = pages;
480 	tmp->tm_pages_used = 0;
481 	new_unrhdr64(&tmp->tm_ino_unr, 2);
482 	tmp->tm_dirent_pool = uma_zcreate("TMPFS dirent",
483 	    sizeof(struct tmpfs_dirent), NULL, NULL, NULL, NULL,
484 	    UMA_ALIGN_PTR, 0);
485 	tmp->tm_node_pool = uma_zcreate("TMPFS node",
486 	    sizeof(struct tmpfs_node), tmpfs_node_ctor, tmpfs_node_dtor,
487 	    tmpfs_node_init, tmpfs_node_fini, UMA_ALIGN_PTR, 0);
488 	tmp->tm_ronly = (mp->mnt_flag & MNT_RDONLY) != 0;
489 	tmp->tm_nonc = nonc;
490 
491 	/* Allocate the root node. */
492 	error = tmpfs_alloc_node(mp, tmp, VDIR, root_uid, root_gid,
493 	    root_mode & ALLPERMS, NULL, NULL, VNOVAL, &root);
494 
495 	if (error != 0 || root == NULL) {
496 		uma_zdestroy(tmp->tm_node_pool);
497 		uma_zdestroy(tmp->tm_dirent_pool);
498 		free(tmp, M_TMPFSMNT);
499 		return (error);
500 	}
501 	KASSERT(root->tn_id == 2,
502 	    ("tmpfs root with invalid ino: %ju", (uintmax_t)root->tn_id));
503 	tmp->tm_root = root;
504 
505 	MNT_ILOCK(mp);
506 	mp->mnt_flag |= MNT_LOCAL;
507 	mp->mnt_kern_flag |= MNTK_LOOKUP_SHARED | MNTK_EXTENDED_SHARED |
508 	    MNTK_TEXT_REFS;
509 	MNT_IUNLOCK(mp);
510 
511 	mp->mnt_data = tmp;
512 	mp->mnt_stat.f_namemax = MAXNAMLEN;
513 	vfs_getnewfsid(mp);
514 	vfs_mountedfrom(mp, "tmpfs");
515 
516 	return 0;
517 }
518 
519 /* ARGSUSED2 */
520 static int
tmpfs_unmount(struct mount * mp,int mntflags)521 tmpfs_unmount(struct mount *mp, int mntflags)
522 {
523 	struct tmpfs_mount *tmp;
524 	struct tmpfs_node *node;
525 	int error, flags;
526 
527 	flags = (mntflags & MNT_FORCE) != 0 ? FORCECLOSE : 0;
528 	tmp = VFS_TO_TMPFS(mp);
529 
530 	/* Stop writers */
531 	error = vfs_write_suspend_umnt(mp);
532 	if (error != 0)
533 		return (error);
534 	/*
535 	 * At this point, nodes cannot be destroyed by any other
536 	 * thread because write suspension is started.
537 	 */
538 
539 	for (;;) {
540 		error = vflush(mp, 0, flags, curthread);
541 		if (error != 0) {
542 			vfs_write_resume(mp, VR_START_WRITE);
543 			return (error);
544 		}
545 		MNT_ILOCK(mp);
546 		if (mp->mnt_nvnodelistsize == 0) {
547 			MNT_IUNLOCK(mp);
548 			break;
549 		}
550 		MNT_IUNLOCK(mp);
551 		if ((mntflags & MNT_FORCE) == 0) {
552 			vfs_write_resume(mp, VR_START_WRITE);
553 			return (EBUSY);
554 		}
555 	}
556 
557 	TMPFS_LOCK(tmp);
558 	while ((node = LIST_FIRST(&tmp->tm_nodes_used)) != NULL) {
559 		TMPFS_NODE_LOCK(node);
560 		if (node->tn_type == VDIR)
561 			tmpfs_dir_destroy(tmp, node);
562 		if (tmpfs_free_node_locked(tmp, node, true))
563 			TMPFS_LOCK(tmp);
564 		else
565 			TMPFS_NODE_UNLOCK(node);
566 	}
567 
568 	mp->mnt_data = NULL;
569 	tmpfs_free_tmp(tmp);
570 	vfs_write_resume(mp, VR_START_WRITE);
571 
572 	MNT_ILOCK(mp);
573 	mp->mnt_flag &= ~MNT_LOCAL;
574 	MNT_IUNLOCK(mp);
575 
576 	return (0);
577 }
578 
579 void
tmpfs_free_tmp(struct tmpfs_mount * tmp)580 tmpfs_free_tmp(struct tmpfs_mount *tmp)
581 {
582 
583 	MPASS(tmp->tm_refcount > 0);
584 	tmp->tm_refcount--;
585 	if (tmp->tm_refcount > 0) {
586 		TMPFS_UNLOCK(tmp);
587 		return;
588 	}
589 	TMPFS_UNLOCK(tmp);
590 
591 	uma_zdestroy(tmp->tm_dirent_pool);
592 	uma_zdestroy(tmp->tm_node_pool);
593 
594 	mtx_destroy(&tmp->tm_allnode_lock);
595 	MPASS(tmp->tm_pages_used == 0);
596 	MPASS(tmp->tm_nodes_inuse == 0);
597 
598 	free(tmp, M_TMPFSMNT);
599 }
600 
601 static int
tmpfs_root(struct mount * mp,int flags,struct vnode ** vpp)602 tmpfs_root(struct mount *mp, int flags, struct vnode **vpp)
603 {
604 	int error;
605 
606 	error = tmpfs_alloc_vp(mp, VFS_TO_TMPFS(mp)->tm_root, flags, vpp);
607 	if (error == 0)
608 		(*vpp)->v_vflag |= VV_ROOT;
609 	return (error);
610 }
611 
612 static int
tmpfs_fhtovp(struct mount * mp,struct fid * fhp,int flags,struct vnode ** vpp)613 tmpfs_fhtovp(struct mount *mp, struct fid *fhp, int flags,
614     struct vnode **vpp)
615 {
616 	struct tmpfs_fid *tfhp;
617 	struct tmpfs_mount *tmp;
618 	struct tmpfs_node *node;
619 	int error;
620 
621 	tmp = VFS_TO_TMPFS(mp);
622 
623 	tfhp = (struct tmpfs_fid *)fhp;
624 	if (tfhp->tf_len != sizeof(struct tmpfs_fid))
625 		return (EINVAL);
626 
627 	if (tfhp->tf_id >= tmp->tm_nodes_max)
628 		return (EINVAL);
629 
630 	TMPFS_LOCK(tmp);
631 	LIST_FOREACH(node, &tmp->tm_nodes_used, tn_entries) {
632 		if (node->tn_id == tfhp->tf_id &&
633 		    node->tn_gen == tfhp->tf_gen) {
634 			tmpfs_ref_node(node);
635 			break;
636 		}
637 	}
638 	TMPFS_UNLOCK(tmp);
639 
640 	if (node != NULL) {
641 		error = tmpfs_alloc_vp(mp, node, LK_EXCLUSIVE, vpp);
642 		tmpfs_free_node(tmp, node);
643 	} else
644 		error = EINVAL;
645 	return (error);
646 }
647 
648 /* ARGSUSED2 */
649 static int
tmpfs_statfs(struct mount * mp,struct statfs * sbp)650 tmpfs_statfs(struct mount *mp, struct statfs *sbp)
651 {
652 	struct tmpfs_mount *tmp;
653 	size_t used;
654 
655 	tmp = VFS_TO_TMPFS(mp);
656 
657 	sbp->f_iosize = PAGE_SIZE;
658 	sbp->f_bsize = PAGE_SIZE;
659 
660 	used = tmpfs_pages_used(tmp);
661 	if (tmp->tm_pages_max != ULONG_MAX)
662 		 sbp->f_blocks = tmp->tm_pages_max;
663 	else
664 		 sbp->f_blocks = used + tmpfs_mem_avail();
665 	if (sbp->f_blocks <= used)
666 		sbp->f_bavail = 0;
667 	else
668 		sbp->f_bavail = sbp->f_blocks - used;
669 	sbp->f_bfree = sbp->f_bavail;
670 	used = tmp->tm_nodes_inuse;
671 	sbp->f_files = tmp->tm_nodes_max;
672 	if (sbp->f_files <= used)
673 		sbp->f_ffree = 0;
674 	else
675 		sbp->f_ffree = sbp->f_files - used;
676 	/* sbp->f_owner = tmp->tn_uid; */
677 
678 	return 0;
679 }
680 
681 static int
tmpfs_sync(struct mount * mp,int waitfor)682 tmpfs_sync(struct mount *mp, int waitfor)
683 {
684 
685 	if (waitfor == MNT_SUSPEND) {
686 		MNT_ILOCK(mp);
687 		mp->mnt_kern_flag |= MNTK_SUSPEND2 | MNTK_SUSPENDED;
688 		MNT_IUNLOCK(mp);
689 	} else if (waitfor == MNT_LAZY) {
690 		tmpfs_update_mtime(mp, true);
691 	}
692 	return (0);
693 }
694 
695 /*
696  * The presence of a susp_clean method tells the VFS to track writes.
697  */
698 static void
tmpfs_susp_clean(struct mount * mp __unused)699 tmpfs_susp_clean(struct mount *mp __unused)
700 {
701 }
702 
703 /*
704  * tmpfs vfs operations.
705  */
706 
707 struct vfsops tmpfs_vfsops = {
708 	.vfs_mount =			tmpfs_mount,
709 	.vfs_unmount =			tmpfs_unmount,
710 	.vfs_root =			tmpfs_root,
711 	.vfs_statfs =			tmpfs_statfs,
712 	.vfs_fhtovp =			tmpfs_fhtovp,
713 	.vfs_sync =			tmpfs_sync,
714 	.vfs_susp_clean =		tmpfs_susp_clean,
715 };
716 VFS_SET(tmpfs_vfsops, tmpfs, VFCF_JAIL);
717