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