xref: /freebsd/sys/fs/devfs/devfs_vfsops.c (revision d056fa046c6a91b90cd98165face0e42a33a5173)
1 /*-
2  * Copyright (c) 1992, 1993, 1995
3  *	The Regents of the University of California.  All rights reserved.
4  * Copyright (c) 2000
5  *	Poul-Henning Kamp.  All rights reserved.
6  *
7  * This code is derived from software donated to Berkeley by
8  * Jan-Simon Pendry.
9  *
10  * Redistribution and use in source and binary forms, with or without
11  * modification, are permitted provided that the following conditions
12  * are met:
13  * 1. Redistributions of source code must retain the above copyright
14  *    notice, this list of conditions and the following disclaimer.
15  * 2. Neither the name of the University nor the names of its contributors
16  *    may be used to endorse or promote products derived from this software
17  *    without specific prior written permission.
18  *
19  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
20  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
21  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
22  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
23  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
24  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
25  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
26  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
27  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
28  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
29  * SUCH DAMAGE.
30  *
31  *	@(#)kernfs_vfsops.c	8.10 (Berkeley) 5/14/95
32  * From: FreeBSD: src/sys/miscfs/kernfs/kernfs_vfsops.c 1.36
33  *
34  * $FreeBSD$
35  */
36 
37 #include "opt_devfs.h"
38 
39 #include <sys/param.h>
40 #include <sys/systm.h>
41 #include <sys/kernel.h>
42 #include <sys/lock.h>
43 #include <sys/malloc.h>
44 #include <sys/mount.h>
45 #include <sys/proc.h>
46 #include <sys/sx.h>
47 #include <sys/vnode.h>
48 #include <sys/limits.h>
49 
50 #include <fs/devfs/devfs.h>
51 
52 static struct unrhdr	*devfs_unr;
53 
54 MALLOC_DEFINE(M_DEVFS, "DEVFS", "DEVFS data");
55 
56 static vfs_mount_t	devfs_mount;
57 static vfs_unmount_t	devfs_unmount;
58 static vfs_root_t	devfs_root;
59 static vfs_statfs_t	devfs_statfs;
60 
61 /*
62  * Mount the filesystem
63  */
64 static int
65 devfs_mount(struct mount *mp, struct thread *td)
66 {
67 	int error;
68 	struct devfs_mount *fmp;
69 	struct vnode *rvp;
70 
71 	if (devfs_unr == NULL)
72 		devfs_unr = new_unrhdr(0, INT_MAX, NULL);
73 
74 	error = 0;
75 
76 	if (mp->mnt_flag & (MNT_UPDATE | MNT_ROOTFS))
77 		return (EOPNOTSUPP);
78 
79 	fmp = malloc(sizeof *fmp, M_DEVFS, M_WAITOK | M_ZERO);
80 	fmp->dm_idx = alloc_unr(devfs_unr);
81 	sx_init(&fmp->dm_lock, "devfsmount");
82 
83 	mp->mnt_flag |= MNT_LOCAL;
84 	mp->mnt_kern_flag |= MNTK_MPSAFE;
85 #ifdef MAC
86 	mp->mnt_flag |= MNT_MULTILABEL;
87 #endif
88 	fmp->dm_mount = mp;
89 	mp->mnt_data = (void *) fmp;
90 	vfs_getnewfsid(mp);
91 
92 	fmp->dm_rootdir = devfs_vmkdir(fmp, NULL, 0, NULL, DEVFS_ROOTINO);
93 
94 	error = devfs_root(mp, LK_EXCLUSIVE, &rvp, td);
95 	if (error) {
96 		sx_destroy(&fmp->dm_lock);
97 		free_unr(devfs_unr, fmp->dm_idx);
98 		free(fmp, M_DEVFS);
99 		return (error);
100 	}
101 
102 	VOP_UNLOCK(rvp, 0, td);
103 
104 	vfs_mountedfrom(mp, "devfs");
105 
106 	return (0);
107 }
108 
109 static int
110 devfs_unmount(struct mount *mp, int mntflags, struct thread *td)
111 {
112 	int error;
113 	int flags = 0;
114 	struct devfs_mount *fmp;
115 
116 	fmp = VFSTODEVFS(mp);
117 	/* There is 1 extra root vnode reference from devfs_mount(). */
118 	error = vflush(mp, 1, flags, td);
119 	if (error)
120 		return (error);
121 	sx_xlock(&fmp->dm_lock);
122 	devfs_cleanup(fmp);
123 	devfs_rules_cleanup(fmp);
124 	sx_xunlock(&fmp->dm_lock);
125 	mp->mnt_data = NULL;
126 	sx_destroy(&fmp->dm_lock);
127 	free_unr(devfs_unr, fmp->dm_idx);
128 	free(fmp, M_DEVFS);
129 	return 0;
130 }
131 
132 /* Return locked reference to root.  */
133 
134 static int
135 devfs_root(struct mount *mp, int flags, struct vnode **vpp, struct thread *td)
136 {
137 	int error;
138 	struct vnode *vp;
139 	struct devfs_mount *dmp;
140 
141 	dmp = VFSTODEVFS(mp);
142 	error = devfs_allocv(dmp->dm_rootdir, mp, &vp, td);
143 	if (error)
144 		return (error);
145 	vp->v_vflag |= VV_ROOT;
146 	*vpp = vp;
147 	return (0);
148 }
149 
150 static int
151 devfs_statfs(struct mount *mp, struct statfs *sbp, struct thread *td)
152 {
153 
154 	sbp->f_flags = 0;
155 	sbp->f_bsize = DEV_BSIZE;
156 	sbp->f_iosize = DEV_BSIZE;
157 	sbp->f_blocks = 2;		/* 1K to keep df happy */
158 	sbp->f_bfree = 0;
159 	sbp->f_bavail = 0;
160 	sbp->f_files = 0;
161 	sbp->f_ffree = 0;
162 	return (0);
163 }
164 
165 static struct vfsops devfs_vfsops = {
166 	.vfs_mount =		devfs_mount,
167 	.vfs_root =		devfs_root,
168 	.vfs_statfs =		devfs_statfs,
169 	.vfs_unmount =		devfs_unmount,
170 };
171 
172 VFS_SET(devfs_vfsops, devfs, VFCF_SYNTHETIC);
173