xref: /freebsd/sys/fs/devfs/devfs_vfsops.c (revision 7aa383846770374466b1dcb2cefd71bde9acf463)
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 <sys/param.h>
38 #include <sys/systm.h>
39 #include <sys/kernel.h>
40 #include <sys/lock.h>
41 #include <sys/malloc.h>
42 #include <sys/mount.h>
43 #include <sys/proc.h>
44 #include <sys/sx.h>
45 #include <sys/vnode.h>
46 #include <sys/limits.h>
47 
48 #include <fs/devfs/devfs.h>
49 
50 static struct unrhdr	*devfs_unr;
51 
52 MALLOC_DEFINE(M_DEVFS, "DEVFS", "DEVFS data");
53 
54 static vfs_mount_t	devfs_mount;
55 static vfs_unmount_t	devfs_unmount;
56 static vfs_root_t	devfs_root;
57 static vfs_statfs_t	devfs_statfs;
58 
59 /*
60  * Mount the filesystem
61  */
62 static int
63 devfs_mount(struct mount *mp)
64 {
65 	int error;
66 	struct devfs_mount *fmp;
67 	struct vnode *rvp;
68 
69 	if (devfs_unr == NULL)
70 		devfs_unr = new_unrhdr(0, INT_MAX, NULL);
71 
72 	error = 0;
73 
74 	if (mp->mnt_flag & (MNT_UPDATE | MNT_ROOTFS))
75 		return (EOPNOTSUPP);
76 
77 	fmp = malloc(sizeof *fmp, M_DEVFS, M_WAITOK | M_ZERO);
78 	fmp->dm_idx = alloc_unr(devfs_unr);
79 	sx_init(&fmp->dm_lock, "devfsmount");
80 	fmp->dm_holdcnt = 1;
81 
82 	MNT_ILOCK(mp);
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 	MNT_IUNLOCK(mp);
89 	fmp->dm_mount = mp;
90 	mp->mnt_data = (void *) fmp;
91 	vfs_getnewfsid(mp);
92 
93 	fmp->dm_rootdir = devfs_vmkdir(fmp, NULL, 0, NULL, DEVFS_ROOTINO);
94 
95 	error = devfs_root(mp, LK_EXCLUSIVE, &rvp);
96 	if (error) {
97 		sx_destroy(&fmp->dm_lock);
98 		free_unr(devfs_unr, fmp->dm_idx);
99 		free(fmp, M_DEVFS);
100 		return (error);
101 	}
102 
103 	VOP_UNLOCK(rvp, 0);
104 
105 	vfs_mountedfrom(mp, "devfs");
106 
107 	return (0);
108 }
109 
110 void
111 devfs_unmount_final(struct devfs_mount *fmp)
112 {
113 	sx_destroy(&fmp->dm_lock);
114 	free(fmp, M_DEVFS);
115 }
116 
117 static int
118 devfs_unmount(struct mount *mp, int mntflags)
119 {
120 	int error;
121 	int flags = 0;
122 	struct devfs_mount *fmp;
123 	int hold;
124 	u_int idx;
125 
126 	fmp = VFSTODEVFS(mp);
127 	KASSERT(fmp->dm_mount != NULL,
128 		("devfs_unmount unmounted devfs_mount"));
129 	/* There is 1 extra root vnode reference from devfs_mount(). */
130 	error = vflush(mp, 1, flags, curthread);
131 	if (error)
132 		return (error);
133 	sx_xlock(&fmp->dm_lock);
134 	devfs_cleanup(fmp);
135 	devfs_rules_cleanup(fmp);
136 	fmp->dm_mount = NULL;
137 	hold = --fmp->dm_holdcnt;
138 	mp->mnt_data = NULL;
139 	idx = fmp->dm_idx;
140 	sx_xunlock(&fmp->dm_lock);
141 	free_unr(devfs_unr, idx);
142 	if (hold == 0)
143 		devfs_unmount_final(fmp);
144 	return 0;
145 }
146 
147 /* Return locked reference to root.  */
148 
149 static int
150 devfs_root(struct mount *mp, int flags, struct vnode **vpp)
151 {
152 	int error;
153 	struct vnode *vp;
154 	struct devfs_mount *dmp;
155 
156 	dmp = VFSTODEVFS(mp);
157 	sx_xlock(&dmp->dm_lock);
158 	error = devfs_allocv(dmp->dm_rootdir, mp, &vp);
159 	if (error)
160 		return (error);
161 	vp->v_vflag |= VV_ROOT;
162 	*vpp = vp;
163 	return (0);
164 }
165 
166 static int
167 devfs_statfs(struct mount *mp, struct statfs *sbp)
168 {
169 
170 	sbp->f_flags = 0;
171 	sbp->f_bsize = DEV_BSIZE;
172 	sbp->f_iosize = DEV_BSIZE;
173 	sbp->f_blocks = 2;		/* 1K to keep df happy */
174 	sbp->f_bfree = 0;
175 	sbp->f_bavail = 0;
176 	sbp->f_files = 0;
177 	sbp->f_ffree = 0;
178 	return (0);
179 }
180 
181 static struct vfsops devfs_vfsops = {
182 	.vfs_mount =		devfs_mount,
183 	.vfs_root =		devfs_root,
184 	.vfs_statfs =		devfs_statfs,
185 	.vfs_unmount =		devfs_unmount,
186 };
187 
188 VFS_SET(devfs_vfsops, devfs, VFCF_SYNTHETIC);
189