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: releng/8.4/sys/fs/devfs/devfs_vfsops.c 233867 2012-04-04 09:14:16Z mm $
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 static const char *devfs_opts[] = {
60 "from", "export", "ruleset", NULL
61 };
62
63 /*
64 * Mount the filesystem
65 */
66 static int
67 devfs_mount(struct mount *mp)
68 {
69 int error;
70 struct devfs_mount *fmp;
71 struct vnode *rvp;
72 int rsnum;
73
74 if (devfs_unr == NULL)
75 devfs_unr = new_unrhdr(0, INT_MAX, NULL);
76
77 error = 0;
78
79 if (mp->mnt_flag & MNT_ROOTFS)
80 return (EOPNOTSUPP);
81
82 rsnum = 0;
83
84 if (mp->mnt_optnew != NULL) {
85 if (vfs_filteropt(mp->mnt_optnew, devfs_opts))
86 return (EINVAL);
87
88 if (vfs_flagopt(mp->mnt_optnew, "export", NULL, 0))
89 return (EOPNOTSUPP);
90
91 if (vfs_getopt(mp->mnt_optnew, "ruleset", NULL, NULL) == 0 &&
92 (vfs_scanopt(mp->mnt_optnew, "ruleset", "%d",
93 &rsnum) != 1 || rsnum < 0 || rsnum > 65535))
94 error = EINVAL;
95 }
96
97 if (error) {
98 vfs_mount_error(mp, "%s", "invalid ruleset specification");
99 return (error);
100 }
101
102 if (mp->mnt_flag & MNT_UPDATE) {
103 if (rsnum != 0) {
104 fmp = mp->mnt_data;
105 if (fmp != NULL) {
106 sx_xlock(&fmp->dm_lock);
107 devfs_ruleset_set((devfs_rsnum)rsnum, fmp);
108 devfs_ruleset_apply(fmp);
109 sx_xunlock(&fmp->dm_lock);
110 }
111 }
112 return (0);
113 }
114
115 fmp = malloc(sizeof *fmp, M_DEVFS, M_WAITOK | M_ZERO);
116 fmp->dm_idx = alloc_unr(devfs_unr);
117 sx_init(&fmp->dm_lock, "devfsmount");
118 fmp->dm_holdcnt = 1;
119
120 MNT_ILOCK(mp);
121 mp->mnt_flag |= MNT_LOCAL;
122 mp->mnt_kern_flag |= MNTK_MPSAFE | MNTK_LOOKUP_SHARED |
123 MNTK_EXTENDED_SHARED;
124 #ifdef MAC
125 mp->mnt_flag |= MNT_MULTILABEL;
126 #endif
127 MNT_IUNLOCK(mp);
128 fmp->dm_mount = mp;
129 mp->mnt_data = (void *) fmp;
130 vfs_getnewfsid(mp);
131
132 fmp->dm_rootdir = devfs_vmkdir(fmp, NULL, 0, NULL, DEVFS_ROOTINO);
133
134 error = devfs_root(mp, LK_EXCLUSIVE, &rvp);
135 if (error) {
136 sx_destroy(&fmp->dm_lock);
137 free_unr(devfs_unr, fmp->dm_idx);
138 free(fmp, M_DEVFS);
139 return (error);
140 }
141
142 if (rsnum != 0) {
143 sx_xlock(&fmp->dm_lock);
144 devfs_ruleset_set((devfs_rsnum)rsnum, fmp);
145 sx_xunlock(&fmp->dm_lock);
146 }
147
148 VOP_UNLOCK(rvp, 0);
149
150 vfs_mountedfrom(mp, "devfs");
151
152 return (0);
153 }
154
155 void
156 devfs_unmount_final(struct devfs_mount *fmp)
157 {
158 sx_destroy(&fmp->dm_lock);
159 free(fmp, M_DEVFS);
160 }
161
162 static int
163 devfs_unmount(struct mount *mp, int mntflags)
164 {
165 int error;
166 int flags = 0;
167 struct devfs_mount *fmp;
168 int hold;
169 u_int idx;
170
171 fmp = VFSTODEVFS(mp);
172 KASSERT(fmp->dm_mount != NULL,
173 ("devfs_unmount unmounted devfs_mount"));
174 /* There is 1 extra root vnode reference from devfs_mount(). */
175 error = vflush(mp, 1, flags, curthread);
176 if (error)
177 return (error);
178 sx_xlock(&fmp->dm_lock);
179 devfs_cleanup(fmp);
180 devfs_rules_cleanup(fmp);
181 fmp->dm_mount = NULL;
182 hold = --fmp->dm_holdcnt;
183 mp->mnt_data = NULL;
184 idx = fmp->dm_idx;
185 sx_xunlock(&fmp->dm_lock);
186 free_unr(devfs_unr, idx);
187 if (hold == 0)
188 devfs_unmount_final(fmp);
189 return 0;
190 }
191
192 /* Return locked reference to root. */
193
194 static int
195 devfs_root(struct mount *mp, int flags, struct vnode **vpp)
196 {
197 int error;
198 struct vnode *vp;
199 struct devfs_mount *dmp;
200
201 dmp = VFSTODEVFS(mp);
202 sx_xlock(&dmp->dm_lock);
203 error = devfs_allocv(dmp->dm_rootdir, mp, LK_EXCLUSIVE, &vp);
204 if (error)
205 return (error);
206 vp->v_vflag |= VV_ROOT;
207 *vpp = vp;
208 return (0);
209 }
210
211 static int
212 devfs_statfs(struct mount *mp, struct statfs *sbp)
213 {
214
215 sbp->f_flags = 0;
216 sbp->f_bsize = DEV_BSIZE;
217 sbp->f_iosize = DEV_BSIZE;
218 sbp->f_blocks = 2; /* 1K to keep df happy */
219 sbp->f_bfree = 0;
220 sbp->f_bavail = 0;
221 sbp->f_files = 0;
222 sbp->f_ffree = 0;
223 return (0);
224 }
225
226 static struct vfsops devfs_vfsops = {
227 .vfs_mount = devfs_mount,
228 .vfs_root = devfs_root,
229 .vfs_statfs = devfs_statfs,
230 .vfs_unmount = devfs_unmount,
231 };
232
233 VFS_SET(devfs_vfsops, devfs, VFCF_SYNTHETIC);
Cache object: 36e7032df1d3e16840cfb3056f257687
|