xref: /netbsd-src/sys/fs/tmpfs/tmpfs_vfsops.c (revision aac0938bfe9c8d92ec0783358c2a39d7de293ec1)
1*aac0938bShannken /*	$NetBSD: tmpfs_vfsops.c,v 1.78 2022/11/10 10:54:14 hannken Exp $	*/
2ec933656Sjmmv 
3ec933656Sjmmv /*
44a780c9aSad  * Copyright (c) 2005, 2006, 2007 The NetBSD Foundation, Inc.
5ec933656Sjmmv  * All rights reserved.
6ec933656Sjmmv  *
7ec933656Sjmmv  * This code is derived from software contributed to The NetBSD Foundation
8b0085cabSjmmv  * by Julio M. Merino Vidal, developed as part of Google's Summer of Code
9b0085cabSjmmv  * 2005 program.
10ec933656Sjmmv  *
11ec933656Sjmmv  * Redistribution and use in source and binary forms, with or without
12ec933656Sjmmv  * modification, are permitted provided that the following conditions
13ec933656Sjmmv  * are met:
14ec933656Sjmmv  * 1. Redistributions of source code must retain the above copyright
15ec933656Sjmmv  *    notice, this list of conditions and the following disclaimer.
16ec933656Sjmmv  * 2. Redistributions in binary form must reproduce the above copyright
17ec933656Sjmmv  *    notice, this list of conditions and the following disclaimer in the
18ec933656Sjmmv  *    documentation and/or other materials provided with the distribution.
19ec933656Sjmmv  *
20ec933656Sjmmv  * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
21ec933656Sjmmv  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
22ec933656Sjmmv  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
23ec933656Sjmmv  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
24ec933656Sjmmv  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
25ec933656Sjmmv  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
26ec933656Sjmmv  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
27ec933656Sjmmv  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
28ec933656Sjmmv  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
29ec933656Sjmmv  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
30ec933656Sjmmv  * POSSIBILITY OF SUCH DAMAGE.
31ec933656Sjmmv  */
32ec933656Sjmmv 
33ec933656Sjmmv /*
348e0a777aSjmmv  * Efficient memory file system.
35b0085cabSjmmv  *
36b0085cabSjmmv  * tmpfs is a file system that uses NetBSD's virtual memory sub-system
37b0085cabSjmmv  * (the well-known UVM) to store file data and metadata in an efficient
38b0085cabSjmmv  * way.  This means that it does not follow the structure of an on-disk
39b0085cabSjmmv  * file system because it simply does not need to.  Instead, it uses
40b0085cabSjmmv  * memory-specific data structures and algorithms to automatically
41b0085cabSjmmv  * allocate and release resources.
42ec933656Sjmmv  */
43ec933656Sjmmv 
44ec933656Sjmmv #include <sys/cdefs.h>
45*aac0938bShannken __KERNEL_RCSID(0, "$NetBSD: tmpfs_vfsops.c,v 1.78 2022/11/10 10:54:14 hannken Exp $");
46ec933656Sjmmv 
47ec933656Sjmmv #include <sys/param.h>
488c80da52Shannken #include <sys/atomic.h>
49ec933656Sjmmv #include <sys/types.h>
504a780c9aSad #include <sys/kmem.h>
51ec933656Sjmmv #include <sys/mount.h>
52c690718cSjmmv #include <sys/stat.h>
53ec933656Sjmmv #include <sys/systm.h>
54ec933656Sjmmv #include <sys/vnode.h>
558c80da52Shannken #include <sys/kauth.h>
56a1221b6dSrumble #include <sys/module.h>
57ec933656Sjmmv 
58717e1785Sdholland #include <miscfs/genfs/genfs.h>
59ec933656Sjmmv #include <fs/tmpfs/tmpfs.h>
60f00b7c9bSpooka #include <fs/tmpfs/tmpfs_args.h>
61ec933656Sjmmv 
62a1221b6dSrumble MODULE(MODULE_CLASS_VFS, tmpfs, NULL);
63a1221b6dSrumble 
647d4d81a3Srmind struct pool	tmpfs_dirent_pool;
657d4d81a3Srmind struct pool	tmpfs_node_pool;
66ec933656Sjmmv 
6712475e02Shannken void
tmpfs_init(void)687d4d81a3Srmind tmpfs_init(void)
697d4d81a3Srmind {
707d4d81a3Srmind 
719d8a0628Srmind 	pool_init(&tmpfs_dirent_pool, sizeof(tmpfs_dirent_t), 0, 0, 0,
727d4d81a3Srmind 	    "tmpfs_dirent", &pool_allocator_nointr, IPL_NONE);
739d8a0628Srmind 	pool_init(&tmpfs_node_pool, sizeof(tmpfs_node_t), 0, 0, 0,
747d4d81a3Srmind 	    "tmpfs_node", &pool_allocator_nointr, IPL_NONE);
757d4d81a3Srmind }
767d4d81a3Srmind 
7712475e02Shannken void
tmpfs_done(void)787d4d81a3Srmind tmpfs_done(void)
797d4d81a3Srmind {
807d4d81a3Srmind 
817d4d81a3Srmind 	pool_destroy(&tmpfs_dirent_pool);
827d4d81a3Srmind 	pool_destroy(&tmpfs_node_pool);
837d4d81a3Srmind }
84ec933656Sjmmv 
8512475e02Shannken int
tmpfs_mount(struct mount * mp,const char * path,void * data,size_t * data_len)8661e8303eSpooka tmpfs_mount(struct mount *mp, const char *path, void *data, size_t *data_len)
87ec933656Sjmmv {
882721ab6cSdsl 	struct tmpfs_args *args = data;
899d8a0628Srmind 	tmpfs_mount_t *tmp;
909d8a0628Srmind 	tmpfs_node_t *root;
918c80da52Shannken 	struct vattr va;
928c80da52Shannken 	struct vnode *vp;
93fc8b3b71Srmind 	uint64_t memlimit;
94fc8b3b71Srmind 	ino_t nodes;
95ac23b49fShannken 	int error, flags;
96d547094dSjoerg 	bool set_memlimit;
97d547094dSjoerg 	bool set_nodes;
982721ab6cSdsl 
9923f76b6dSmaxv 	if (args == NULL)
10023f76b6dSmaxv 		return EINVAL;
10123f76b6dSmaxv 
1027384069aSrmind 	/* Validate the version. */
1037384069aSrmind 	if (*data_len < sizeof(*args) ||
1047384069aSrmind 	    args->ta_version != TMPFS_ARGS_VERSION)
1052721ab6cSdsl 		return EINVAL;
106ec933656Sjmmv 
107ec933656Sjmmv 	/* Handle retrieval of mount point arguments. */
108ec933656Sjmmv 	if (mp->mnt_flag & MNT_GETARGS) {
109ec933656Sjmmv 		if (mp->mnt_data == NULL)
110ec933656Sjmmv 			return EIO;
111ec933656Sjmmv 		tmp = VFS_TO_TMPFS(mp);
112ec933656Sjmmv 
1132721ab6cSdsl 		args->ta_version = TMPFS_ARGS_VERSION;
1142721ab6cSdsl 		args->ta_nodes_max = tmp->tm_nodes_max;
115fc8b3b71Srmind 		args->ta_size_max = tmp->tm_mem_limit;
116dc73f992Sad 
11765b374fbSad 		root = tmp->tm_root;
1182721ab6cSdsl 		args->ta_root_uid = root->tn_uid;
1192721ab6cSdsl 		args->ta_root_gid = root->tn_gid;
1202721ab6cSdsl 		args->ta_root_mode = root->tn_mode;
121ec933656Sjmmv 
122fc8b3b71Srmind 		*data_len = sizeof(*args);
1232721ab6cSdsl 		return 0;
124ec933656Sjmmv 	}
125ec933656Sjmmv 
126ec933656Sjmmv 
1277384069aSrmind 	/* Prohibit mounts if there is not enough memory. */
128c7dd06b6Smartin 	if (tmpfs_mem_info(true) < uvmexp.freetarg)
129ec933656Sjmmv 		return EINVAL;
130ec933656Sjmmv 
131b7650b1aSmartin 	/* Check for invalid uid and gid arguments */
132b7650b1aSmartin 	if (args->ta_root_uid == VNOVAL || args->ta_root_gid == VNOVAL)
133b7650b1aSmartin 		return EINVAL;
134b7650b1aSmartin 
135fc8b3b71Srmind 	/* Get the memory usage limit for this file-system. */
136fc8b3b71Srmind 	if (args->ta_size_max < PAGE_SIZE) {
137fc8b3b71Srmind 		memlimit = UINT64_MAX;
138d547094dSjoerg 		set_memlimit = false;
139fc8b3b71Srmind 	} else {
140fc8b3b71Srmind 		memlimit = args->ta_size_max;
141d547094dSjoerg 		set_memlimit = true;
142fc8b3b71Srmind 	}
143fc8b3b71Srmind 	KASSERT(memlimit > 0);
144ec933656Sjmmv 
145fc8b3b71Srmind 	if (args->ta_nodes_max <= 3) {
146fc8b3b71Srmind 		nodes = 3 + (memlimit / 1024);
147d547094dSjoerg 		set_nodes = false;
148fc8b3b71Srmind 	} else {
1492721ab6cSdsl 		nodes = args->ta_nodes_max;
150d547094dSjoerg 		set_nodes = true;
151fc8b3b71Srmind 	}
152fc8b3b71Srmind 	nodes = MIN(nodes, INT_MAX);
153b35a89f1Sjmmv 	KASSERT(nodes >= 3);
154ec933656Sjmmv 
1550172bc97Schristos 	if (mp->mnt_flag & MNT_UPDATE) {
1560172bc97Schristos 		tmp = VFS_TO_TMPFS(mp);
157d547094dSjoerg 		if (set_nodes && nodes < tmp->tm_nodes_cnt)
1580172bc97Schristos 			return EBUSY;
15990ead62dShannken 		if ((mp->mnt_iflag & IMNT_WANTRDONLY)) {
160ac23b49fShannken 			/* Changing from read/write to read-only. */
161ac23b49fShannken 			flags = WRITECLOSE;
162ac23b49fShannken 			if ((mp->mnt_flag & MNT_FORCE))
163ac23b49fShannken 				flags |= FORCECLOSE;
164ac23b49fShannken 			error = vflush(mp, NULL, flags);
165ac23b49fShannken 			if (error)
166ac23b49fShannken 				return error;
167ac23b49fShannken 		}
168d547094dSjoerg 		if (set_memlimit) {
1690172bc97Schristos 			if ((error = tmpfs_mntmem_set(tmp, memlimit)) != 0)
1700172bc97Schristos 				return error;
171d547094dSjoerg 		}
172d547094dSjoerg 		if (set_nodes)
1730172bc97Schristos 			tmp->tm_nodes_max = nodes;
17471fd4168Schristos 		root = tmp->tm_root;
1750172bc97Schristos 		root->tn_uid = args->ta_root_uid;
1760172bc97Schristos 		root->tn_gid = args->ta_root_gid;
1770172bc97Schristos 		root->tn_mode = args->ta_root_mode;
1780172bc97Schristos 		return 0;
1790172bc97Schristos 	}
1800172bc97Schristos 
181ef24a844Schristos 	mp->mnt_flag |= MNT_LOCAL;
182ef24a844Schristos 	mp->mnt_stat.f_namemax = TMPFS_MAXNAMLEN;
183ef24a844Schristos 	mp->mnt_fs_bshift = PAGE_SHIFT;
184ef24a844Schristos 	mp->mnt_dev_bshift = DEV_BSHIFT;
185c90f9c8cSad 	mp->mnt_iflag |= IMNT_MPSAFE | IMNT_CAN_RWTORO | IMNT_SHRLOOKUP |
186c90f9c8cSad 	    IMNT_NCLOOKUP;
187ef24a844Schristos 	vfs_getnewfsid(mp);
188ef24a844Schristos 
189ec933656Sjmmv 	/* Allocate the tmpfs mount structure and fill it. */
1909d8a0628Srmind 	tmp = kmem_zalloc(sizeof(tmpfs_mount_t), KM_SLEEP);
191ec933656Sjmmv 	tmp->tm_nodes_max = nodes;
1924a780c9aSad 	tmp->tm_nodes_cnt = 0;
1934a780c9aSad 	LIST_INIT(&tmp->tm_nodes);
1944a780c9aSad 
1954a780c9aSad 	mutex_init(&tmp->tm_lock, MUTEX_DEFAULT, IPL_NONE);
196fc8b3b71Srmind 	tmpfs_mntmem_init(tmp, memlimit);
1978c80da52Shannken 	mp->mnt_data = tmp;
198ec933656Sjmmv 
199*aac0938bShannken 	error = set_statvfs_info(path, UIO_USERSPACE, "tmpfs", UIO_SYSSPACE,
200*aac0938bShannken 	    mp->mnt_op->vfs_name, mp, curlwp);
201*aac0938bShannken 	if (error)
202*aac0938bShannken 		goto errout;
203*aac0938bShannken 
204ec933656Sjmmv 	/* Allocate the root node. */
2058c80da52Shannken 	vattr_null(&va);
2068c80da52Shannken 	va.va_type = VDIR;
2078c80da52Shannken 	va.va_mode = args->ta_root_mode & ALLPERMS;
2088c80da52Shannken 	va.va_uid = args->ta_root_uid;
2098c80da52Shannken 	va.va_gid = args->ta_root_gid;
210b689ec0fShannken 	error = vcache_new(mp, NULL, &va, NOCRED, NULL, &vp);
211*aac0938bShannken 	if (error)
212*aac0938bShannken 		goto errout;
213b5981bcfSdholland 	KASSERT(vp != NULL);
2148c80da52Shannken 	root = VP_TO_TMPFS_NODE(vp);
215b5981bcfSdholland 	KASSERT(root != NULL);
2164781942cSrmind 
2174781942cSrmind 	/*
2184781942cSrmind 	 * Parent of the root inode is itself.  Also, root inode has no
2194781942cSrmind 	 * directory entry (i.e. is never attached), thus hold an extra
2204781942cSrmind 	 * reference (link) for it.
2214781942cSrmind 	 */
2224a780c9aSad 	root->tn_links++;
2234781942cSrmind 	root->tn_spec.tn_dir.tn_parent = root;
224ec933656Sjmmv 	tmp->tm_root = root;
2258c80da52Shannken 	vrele(vp);
226ec933656Sjmmv 
227*aac0938bShannken 	return 0;
228*aac0938bShannken 
229*aac0938bShannken errout:
230*aac0938bShannken 	mp->mnt_data = NULL;
231*aac0938bShannken 	tmpfs_mntmem_destroy(tmp);
232*aac0938bShannken 	mutex_destroy(&tmp->tm_lock);
233*aac0938bShannken 	kmem_free(tmp, sizeof(*tmp));
234*aac0938bShannken 
2357d4d81a3Srmind 	return error;
236ec933656Sjmmv }
237ec933656Sjmmv 
23812475e02Shannken int
tmpfs_start(struct mount * mp,int flags)23961e8303eSpooka tmpfs_start(struct mount *mp, int flags)
240ec933656Sjmmv {
241ec933656Sjmmv 
242ec933656Sjmmv 	return 0;
243ec933656Sjmmv }
244ec933656Sjmmv 
24512475e02Shannken int
tmpfs_unmount(struct mount * mp,int mntflags)24661e8303eSpooka tmpfs_unmount(struct mount *mp, int mntflags)
247ec933656Sjmmv {
2481f5dbc94Srmind 	tmpfs_mount_t *tmp = VFS_TO_TMPFS(mp);
2491f5dbc94Srmind 	tmpfs_node_t *node, *cnode;
2507d4d81a3Srmind 	int error, flags = 0;
251ec933656Sjmmv 
252ec933656Sjmmv 	/* Handle forced unmounts. */
253ec933656Sjmmv 	if (mntflags & MNT_FORCE)
254ec933656Sjmmv 		flags |= FORCECLOSE;
255ec933656Sjmmv 
256ec933656Sjmmv 	/* Finalize all pending I/O. */
257ec933656Sjmmv 	error = vflush(mp, NULL, flags);
258ec933656Sjmmv 	if (error != 0)
259ec933656Sjmmv 		return error;
260ec933656Sjmmv 
2611f5dbc94Srmind 	/*
2621f5dbc94Srmind 	 * First round, detach and destroy all directory entries.
2631f5dbc94Srmind 	 * Also, clear the pointers to the vnodes - they are gone.
2641f5dbc94Srmind 	 */
2651f5dbc94Srmind 	LIST_FOREACH(node, &tmp->tm_nodes, tn_entries) {
2669d8a0628Srmind 		tmpfs_dirent_t *de;
267ec933656Sjmmv 
2681f5dbc94Srmind 		node->tn_vnode = NULL;
2691f5dbc94Srmind 		if (node->tn_type != VDIR) {
2701f5dbc94Srmind 			continue;
2711f5dbc94Srmind 		}
2721f5dbc94Srmind 		while ((de = TAILQ_FIRST(&node->tn_spec.tn_dir.tn_dir)) != NULL) {
27389433ee6Srmind 			cnode = de->td_node;
27489433ee6Srmind 			if (cnode && cnode != TMPFS_NODE_WHITEOUT) {
2751f5dbc94Srmind 				cnode->tn_vnode = NULL;
2761f5dbc94Srmind 			}
2771f5dbc94Srmind 			tmpfs_dir_detach(node, de);
2784781942cSrmind 			tmpfs_free_dirent(tmp, de);
279ec933656Sjmmv 		}
280e63cf28eSrmind 		/* Extra virtual entry (itself for the root). */
281e63cf28eSrmind 		node->tn_links--;
282ec933656Sjmmv 	}
2831f5dbc94Srmind 
284e63cf28eSrmind 	/* Release the reference on root (diagnostic). */
285e63cf28eSrmind 	node = tmp->tm_root;
286e63cf28eSrmind 	node->tn_links--;
287e63cf28eSrmind 
2881f5dbc94Srmind 	/* Second round, destroy all inodes. */
2891f5dbc94Srmind 	while ((node = LIST_FIRST(&tmp->tm_nodes)) != NULL) {
290ec933656Sjmmv 		tmpfs_free_node(tmp, node);
29165b374fbSad 	}
292ec933656Sjmmv 
293ec933656Sjmmv 	/* Throw away the tmpfs_mount structure. */
294fc8b3b71Srmind 	tmpfs_mntmem_destroy(tmp);
2954a780c9aSad 	mutex_destroy(&tmp->tm_lock);
2964a780c9aSad 	kmem_free(tmp, sizeof(*tmp));
297ec933656Sjmmv 	mp->mnt_data = NULL;
298ec933656Sjmmv 
299ec933656Sjmmv 	return 0;
300ec933656Sjmmv }
301ec933656Sjmmv 
30212475e02Shannken int
tmpfs_root(struct mount * mp,int lktype,vnode_t ** vpp)303c2e9cb94Sad tmpfs_root(struct mount *mp, int lktype, vnode_t **vpp)
304ec933656Sjmmv {
3054781942cSrmind 	tmpfs_node_t *node = VFS_TO_TMPFS(mp)->tm_root;
3068c80da52Shannken 	int error;
307ec933656Sjmmv 
3088c80da52Shannken 	error = vcache_get(mp, &node, sizeof(node), vpp);
3098c80da52Shannken 	if (error)
3108c80da52Shannken 		return error;
311c2e9cb94Sad 	error = vn_lock(*vpp, lktype);
3128c80da52Shannken 	if (error) {
3138c80da52Shannken 		vrele(*vpp);
3148c80da52Shannken 		*vpp = NULL;
3158c80da52Shannken 		return error;
3168c80da52Shannken 	}
3178c80da52Shannken 
3188c80da52Shannken 	return 0;
319ec933656Sjmmv }
320ec933656Sjmmv 
32112475e02Shannken int
tmpfs_vget(struct mount * mp,ino_t ino,int lktype,vnode_t ** vpp)322c2e9cb94Sad tmpfs_vget(struct mount *mp, ino_t ino, int lktype, vnode_t **vpp)
323ec933656Sjmmv {
324ec933656Sjmmv 
325ec933656Sjmmv 	return EOPNOTSUPP;
326ec933656Sjmmv }
327ec933656Sjmmv 
32812475e02Shannken int
tmpfs_fhtovp(struct mount * mp,struct fid * fhp,int lktype,vnode_t ** vpp)329c2e9cb94Sad tmpfs_fhtovp(struct mount *mp, struct fid *fhp, int lktype, vnode_t **vpp)
330ec933656Sjmmv {
3314781942cSrmind 	tmpfs_mount_t *tmp = VFS_TO_TMPFS(mp);
3329d8a0628Srmind 	tmpfs_node_t *node;
3339d8a0628Srmind 	tmpfs_fid_t tfh;
334b04f8aa8Shannken 	int error;
335ec933656Sjmmv 
3364781942cSrmind 	if (fhp->fid_len != sizeof(tmpfs_fid_t)) {
337ec933656Sjmmv 		return EINVAL;
3384781942cSrmind 	}
3399d8a0628Srmind 	memcpy(&tfh, fhp, sizeof(tmpfs_fid_t));
340a3b5baedSmartin 
3414a780c9aSad 	mutex_enter(&tmp->tm_lock);
342c2e9cb94Sad 	/* XXX big oof .. use a better data structure */
3434a780c9aSad 	LIST_FOREACH(node, &tmp->tm_nodes, tn_entries) {
344b04f8aa8Shannken 		if (node->tn_id == tfh.tf_id) {
3458c80da52Shannken 			/* Prevent this node from disappearing. */
3468c80da52Shannken 			atomic_inc_32(&node->tn_holdcount);
3474781942cSrmind 			break;
348ec933656Sjmmv 		}
349b04f8aa8Shannken 	}
3504a780c9aSad 	mutex_exit(&tmp->tm_lock);
351b04f8aa8Shannken 	if (node == NULL)
352b04f8aa8Shannken 		return ESTALE;
3538c80da52Shannken 
3548c80da52Shannken 	error = vcache_get(mp, &node, sizeof(node), vpp);
3558c80da52Shannken 	/* If this node has been reclaimed free it now. */
3568c80da52Shannken 	if (atomic_dec_32_nv(&node->tn_holdcount) == TMPFS_NODE_RECLAIMED) {
3578c80da52Shannken 		KASSERT(error != 0);
3588c80da52Shannken 		tmpfs_free_node(tmp, node);
3598c80da52Shannken 	}
3608c80da52Shannken 	if (error)
3618c80da52Shannken 		return (error == ENOENT ? ESTALE : error);
362c2e9cb94Sad 	error = vn_lock(*vpp, lktype);
3638c80da52Shannken 	if (error) {
3648c80da52Shannken 		vrele(*vpp);
3658c80da52Shannken 		*vpp = NULL;
366b04f8aa8Shannken 		return error;
3678c80da52Shannken 	}
368b04f8aa8Shannken 	if (TMPFS_NODE_GEN(node) != tfh.tf_gen) {
369b04f8aa8Shannken 		vput(*vpp);
370b04f8aa8Shannken 		*vpp = NULL;
371b04f8aa8Shannken 		return ESTALE;
372b04f8aa8Shannken 	}
373b04f8aa8Shannken 
374b04f8aa8Shannken 	return 0;
375ec933656Sjmmv }
376ec933656Sjmmv 
37712475e02Shannken int
tmpfs_vptofh(vnode_t * vp,struct fid * fhp,size_t * fh_size)3789d8a0628Srmind tmpfs_vptofh(vnode_t *vp, struct fid *fhp, size_t *fh_size)
379ec933656Sjmmv {
3809d8a0628Srmind 	tmpfs_fid_t tfh;
3819d8a0628Srmind 	tmpfs_node_t *node;
382ec933656Sjmmv 
3839d8a0628Srmind 	if (*fh_size < sizeof(tmpfs_fid_t)) {
3849d8a0628Srmind 		*fh_size = sizeof(tmpfs_fid_t);
385a3b5baedSmartin 		return E2BIG;
386a3b5baedSmartin 	}
3879d8a0628Srmind 	*fh_size = sizeof(tmpfs_fid_t);
388ec933656Sjmmv 	node = VP_TO_TMPFS_NODE(vp);
389ec933656Sjmmv 
390a3b5baedSmartin 	memset(&tfh, 0, sizeof(tfh));
3919d8a0628Srmind 	tfh.tf_len = sizeof(tmpfs_fid_t);
3924781942cSrmind 	tfh.tf_gen = TMPFS_NODE_GEN(node);
393a3b5baedSmartin 	tfh.tf_id = node->tn_id;
394a3b5baedSmartin 	memcpy(fhp, &tfh, sizeof(tfh));
395ec933656Sjmmv 
396ec933656Sjmmv 	return 0;
397ec933656Sjmmv }
398ec933656Sjmmv 
39912475e02Shannken int
tmpfs_statvfs(struct mount * mp,struct statvfs * sbp)40061e8303eSpooka tmpfs_statvfs(struct mount *mp, struct statvfs *sbp)
401ec933656Sjmmv {
4029d8a0628Srmind 	tmpfs_mount_t *tmp;
403bf5767dcSrmind 	fsfilcnt_t freenodes;
404bf5767dcSrmind 	size_t avail;
405ec933656Sjmmv 
406ec933656Sjmmv 	tmp = VFS_TO_TMPFS(mp);
407ec933656Sjmmv 
408ec933656Sjmmv 	sbp->f_iosize = sbp->f_frsize = sbp->f_bsize = PAGE_SIZE;
409ec933656Sjmmv 
410bf5767dcSrmind 	mutex_enter(&tmp->tm_acc_lock);
411bf5767dcSrmind 	avail =  tmpfs_pages_avail(tmp);
412fc8b3b71Srmind 	sbp->f_blocks = (tmpfs_bytes_max(tmp) >> PAGE_SHIFT);
413bf5767dcSrmind 	sbp->f_bavail = sbp->f_bfree = avail;
414ec933656Sjmmv 	sbp->f_bresvd = 0;
415ec933656Sjmmv 
4164a780c9aSad 	freenodes = MIN(tmp->tm_nodes_max - tmp->tm_nodes_cnt,
4179d8a0628Srmind 	    avail * PAGE_SIZE / sizeof(tmpfs_node_t));
418ec933656Sjmmv 
4194a780c9aSad 	sbp->f_files = tmp->tm_nodes_cnt + freenodes;
420ec933656Sjmmv 	sbp->f_favail = sbp->f_ffree = freenodes;
421ec933656Sjmmv 	sbp->f_fresvd = 0;
422bf5767dcSrmind 	mutex_exit(&tmp->tm_acc_lock);
423ec933656Sjmmv 
424ec933656Sjmmv 	copy_statvfs_info(sbp, mp);
425ec933656Sjmmv 
426ec933656Sjmmv 	return 0;
427ec933656Sjmmv }
428ec933656Sjmmv 
42912475e02Shannken int
tmpfs_sync(struct mount * mp,int waitfor,kauth_cred_t uc)4309d8a0628Srmind tmpfs_sync(struct mount *mp, int waitfor, kauth_cred_t uc)
431ec933656Sjmmv {
432ec933656Sjmmv 
433ec933656Sjmmv 	return 0;
434ec933656Sjmmv }
435ec933656Sjmmv 
43612475e02Shannken int
tmpfs_snapshot(struct mount * mp,vnode_t * vp,struct timespec * ctime)4379d8a0628Srmind tmpfs_snapshot(struct mount *mp, vnode_t *vp, struct timespec *ctime)
438ec933656Sjmmv {
439ec933656Sjmmv 
440ec933656Sjmmv 	return EOPNOTSUPP;
441ec933656Sjmmv }
442ec933656Sjmmv 
443ec933656Sjmmv /*
444ec933656Sjmmv  * tmpfs vfs operations.
445ec933656Sjmmv  */
446ec933656Sjmmv 
447ec933656Sjmmv extern const struct vnodeopv_desc tmpfs_fifoop_opv_desc;
448ec933656Sjmmv extern const struct vnodeopv_desc tmpfs_specop_opv_desc;
449ec933656Sjmmv extern const struct vnodeopv_desc tmpfs_vnodeop_opv_desc;
450ec933656Sjmmv 
451ec933656Sjmmv const struct vnodeopv_desc * const tmpfs_vnodeopv_descs[] = {
452ec933656Sjmmv 	&tmpfs_fifoop_opv_desc,
453ec933656Sjmmv 	&tmpfs_specop_opv_desc,
454ec933656Sjmmv 	&tmpfs_vnodeop_opv_desc,
455ec933656Sjmmv 	NULL,
456ec933656Sjmmv };
457ec933656Sjmmv 
458ec933656Sjmmv struct vfsops tmpfs_vfsops = {
4596d285189Shannken 	.vfs_name = MOUNT_TMPFS,
4606d285189Shannken 	.vfs_min_mount_data = sizeof (struct tmpfs_args),
4616d285189Shannken 	.vfs_mount = tmpfs_mount,
4626d285189Shannken 	.vfs_start = tmpfs_start,
4636d285189Shannken 	.vfs_unmount = tmpfs_unmount,
4646d285189Shannken 	.vfs_root = tmpfs_root,
4656d285189Shannken 	.vfs_quotactl = (void *)eopnotsupp,
4666d285189Shannken 	.vfs_statvfs = tmpfs_statvfs,
4676d285189Shannken 	.vfs_sync = tmpfs_sync,
4686d285189Shannken 	.vfs_vget = tmpfs_vget,
4698c80da52Shannken 	.vfs_loadvnode = tmpfs_loadvnode,
4708c80da52Shannken 	.vfs_newvnode = tmpfs_newvnode,
4716d285189Shannken 	.vfs_fhtovp = tmpfs_fhtovp,
4726d285189Shannken 	.vfs_vptofh = tmpfs_vptofh,
4736d285189Shannken 	.vfs_init = tmpfs_init,
4746d285189Shannken 	.vfs_done = tmpfs_done,
4756d285189Shannken 	.vfs_snapshot = tmpfs_snapshot,
4766d285189Shannken 	.vfs_extattrctl = vfs_stdextattrctl,
477326db3aaShannken 	.vfs_suspendctl = genfs_suspendctl,
4786d285189Shannken 	.vfs_renamelock_enter = genfs_renamelock_enter,
4796d285189Shannken 	.vfs_renamelock_exit = genfs_renamelock_exit,
4806d285189Shannken 	.vfs_fsync = (void *)eopnotsupp,
4816d285189Shannken 	.vfs_opv_descs = tmpfs_vnodeopv_descs
482ec933656Sjmmv };
483a1221b6dSrumble 
484a1221b6dSrumble static int
tmpfs_modcmd(modcmd_t cmd,void * arg)485a1221b6dSrumble tmpfs_modcmd(modcmd_t cmd, void *arg)
486a1221b6dSrumble {
487a1221b6dSrumble 
488a1221b6dSrumble 	switch (cmd) {
489a1221b6dSrumble 	case MODULE_CMD_INIT:
490a1221b6dSrumble 		return vfs_attach(&tmpfs_vfsops);
491a1221b6dSrumble 	case MODULE_CMD_FINI:
492a1221b6dSrumble 		return vfs_detach(&tmpfs_vfsops);
493a1221b6dSrumble 	default:
494a1221b6dSrumble 		return ENOTTY;
495a1221b6dSrumble 	}
496a1221b6dSrumble }
497