mfs_vfsops.c revision 1.1.1.1 1 /*
2 * Copyright (c) 1989, 1990, 1993, 1994
3 * The Regents of the University of California. All rights reserved.
4 *
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
7 * are met:
8 * 1. Redistributions of source code must retain the above copyright
9 * notice, this list of conditions and the following disclaimer.
10 * 2. Redistributions in binary form must reproduce the above copyright
11 * notice, this list of conditions and the following disclaimer in the
12 * documentation and/or other materials provided with the distribution.
13 * 3. All advertising materials mentioning features or use of this software
14 * must display the following acknowledgement:
15 * This product includes software developed by the University of
16 * California, Berkeley and its contributors.
17 * 4. Neither the name of the University nor the names of its contributors
18 * may be used to endorse or promote products derived from this software
19 * without specific prior written permission.
20 *
21 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
22 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
23 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
24 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
25 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
26 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
27 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
28 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
29 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
30 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
31 * SUCH DAMAGE.
32 *
33 * @(#)mfs_vfsops.c 8.4 (Berkeley) 4/16/94
34 */
35
36 #include <sys/param.h>
37 #include <sys/systm.h>
38 #include <sys/time.h>
39 #include <sys/kernel.h>
40 #include <sys/proc.h>
41 #include <sys/buf.h>
42 #include <sys/mount.h>
43 #include <sys/signalvar.h>
44 #include <sys/vnode.h>
45 #include <sys/malloc.h>
46
47 #include <ufs/ufs/quota.h>
48 #include <ufs/ufs/inode.h>
49 #include <ufs/ufs/ufsmount.h>
50 #include <ufs/ufs/ufs_extern.h>
51
52 #include <ufs/ffs/fs.h>
53 #include <ufs/ffs/ffs_extern.h>
54
55 #include <ufs/mfs/mfsnode.h>
56 #include <ufs/mfs/mfs_extern.h>
57
58 caddr_t mfs_rootbase; /* address of mini-root in kernel virtual memory */
59 u_long mfs_rootsize; /* size of mini-root in bytes */
60
61 static int mfs_minor; /* used for building internal dev_t */
62
63 extern int (**mfs_vnodeop_p)();
64
65 /*
66 * mfs vfs operations.
67 */
68 struct vfsops mfs_vfsops = {
69 mfs_mount,
70 mfs_start,
71 ffs_unmount,
72 ufs_root,
73 ufs_quotactl,
74 mfs_statfs,
75 ffs_sync,
76 ffs_vget,
77 ffs_fhtovp,
78 ffs_vptofh,
79 mfs_init,
80 };
81
82 /*
83 * Called by main() when mfs is going to be mounted as root.
84 *
85 * Name is updated by mount(8) after booting.
86 */
87 #define ROOTNAME "mfs_root"
88
89 mfs_mountroot()
90 {
91 extern struct vnode *rootvp;
92 register struct fs *fs;
93 register struct mount *mp;
94 struct proc *p = curproc; /* XXX */
95 struct ufsmount *ump;
96 struct mfsnode *mfsp;
97 u_int size;
98 int error;
99
100 /*
101 * Get vnodes for swapdev and rootdev.
102 */
103 if (bdevvp(swapdev, &swapdev_vp) || bdevvp(rootdev, &rootvp))
104 panic("mfs_mountroot: can't setup bdevvp's");
105
106 mp = malloc((u_long)sizeof(struct mount), M_MOUNT, M_WAITOK);
107 bzero((char *)mp, (u_long)sizeof(struct mount));
108 mp->mnt_op = &mfs_vfsops;
109 mp->mnt_flag = MNT_RDONLY;
110 mfsp = malloc(sizeof *mfsp, M_MFSNODE, M_WAITOK);
111 rootvp->v_data = mfsp;
112 rootvp->v_op = mfs_vnodeop_p;
113 rootvp->v_tag = VT_MFS;
114 mfsp->mfs_baseoff = mfs_rootbase;
115 mfsp->mfs_size = mfs_rootsize;
116 mfsp->mfs_vnode = rootvp;
117 mfsp->mfs_pid = p->p_pid;
118 mfsp->mfs_buflist = (struct buf *)0;
119 if (error = ffs_mountfs(rootvp, mp, p)) {
120 free(mp, M_MOUNT);
121 free(mfsp, M_MFSNODE);
122 return (error);
123 }
124 if (error = vfs_lock(mp)) {
125 (void)ffs_unmount(mp, 0, p);
126 free(mp, M_MOUNT);
127 free(mfsp, M_MFSNODE);
128 return (error);
129 }
130 TAILQ_INSERT_TAIL(&mountlist, mp, mnt_list);
131 mp->mnt_flag |= MNT_ROOTFS;
132 mp->mnt_vnodecovered = NULLVP;
133 ump = VFSTOUFS(mp);
134 fs = ump->um_fs;
135 bzero(fs->fs_fsmnt, sizeof(fs->fs_fsmnt));
136 fs->fs_fsmnt[0] = '/';
137 bcopy((caddr_t)fs->fs_fsmnt, (caddr_t)mp->mnt_stat.f_mntonname,
138 MNAMELEN);
139 (void) copystr(ROOTNAME, mp->mnt_stat.f_mntfromname, MNAMELEN - 1,
140 &size);
141 bzero(mp->mnt_stat.f_mntfromname + size, MNAMELEN - size);
142 (void)ffs_statfs(mp, &mp->mnt_stat, p);
143 vfs_unlock(mp);
144 inittodr((time_t)0);
145 return (0);
146 }
147
148 /*
149 * This is called early in boot to set the base address and size
150 * of the mini-root.
151 */
152 mfs_initminiroot(base)
153 caddr_t base;
154 {
155 struct fs *fs = (struct fs *)(base + SBOFF);
156 extern int (*mountroot)();
157
158 /* check for valid super block */
159 if (fs->fs_magic != FS_MAGIC || fs->fs_bsize > MAXBSIZE ||
160 fs->fs_bsize < sizeof(struct fs))
161 return (0);
162 mountroot = mfs_mountroot;
163 mfs_rootbase = base;
164 mfs_rootsize = fs->fs_fsize * fs->fs_size;
165 rootdev = makedev(255, mfs_minor++);
166 return (mfs_rootsize);
167 }
168
169 /*
170 * VFS Operations.
171 *
172 * mount system call
173 */
174 /* ARGSUSED */
175 int
176 mfs_mount(mp, path, data, ndp, p)
177 register struct mount *mp;
178 char *path;
179 caddr_t data;
180 struct nameidata *ndp;
181 struct proc *p;
182 {
183 struct vnode *devvp;
184 struct mfs_args args;
185 struct ufsmount *ump;
186 register struct fs *fs;
187 register struct mfsnode *mfsp;
188 u_int size;
189 int flags, error;
190
191 if (error = copyin(data, (caddr_t)&args, sizeof (struct mfs_args)))
192 return (error);
193
194 /*
195 * If updating, check whether changing from read-only to
196 * read/write; if there is no device name, that's all we do.
197 */
198 if (mp->mnt_flag & MNT_UPDATE) {
199 ump = VFSTOUFS(mp);
200 fs = ump->um_fs;
201 if (fs->fs_ronly == 0 && (mp->mnt_flag & MNT_RDONLY)) {
202 flags = WRITECLOSE;
203 if (mp->mnt_flag & MNT_FORCE)
204 flags |= FORCECLOSE;
205 if (vfs_busy(mp))
206 return (EBUSY);
207 error = ffs_flushfiles(mp, flags, p);
208 vfs_unbusy(mp);
209 if (error)
210 return (error);
211 }
212 if (fs->fs_ronly && (mp->mnt_flag & MNT_WANTRDWR))
213 fs->fs_ronly = 0;
214 #ifdef EXPORTMFS
215 if (args.fspec == 0)
216 return (vfs_export(mp, &ump->um_export, &args.export));
217 #endif
218 return (0);
219 }
220 error = getnewvnode(VT_MFS, (struct mount *)0, mfs_vnodeop_p, &devvp);
221 if (error)
222 return (error);
223 devvp->v_type = VBLK;
224 if (checkalias(devvp, makedev(255, mfs_minor++), (struct mount *)0))
225 panic("mfs_mount: dup dev");
226 mfsp = (struct mfsnode *)malloc(sizeof *mfsp, M_MFSNODE, M_WAITOK);
227 devvp->v_data = mfsp;
228 mfsp->mfs_baseoff = args.base;
229 mfsp->mfs_size = args.size;
230 mfsp->mfs_vnode = devvp;
231 mfsp->mfs_pid = p->p_pid;
232 mfsp->mfs_buflist = (struct buf *)0;
233 if (error = ffs_mountfs(devvp, mp, p)) {
234 mfsp->mfs_buflist = (struct buf *)-1;
235 vrele(devvp);
236 return (error);
237 }
238 ump = VFSTOUFS(mp);
239 fs = ump->um_fs;
240 (void) copyinstr(path, fs->fs_fsmnt, sizeof(fs->fs_fsmnt) - 1, &size);
241 bzero(fs->fs_fsmnt + size, sizeof(fs->fs_fsmnt) - size);
242 bcopy((caddr_t)fs->fs_fsmnt, (caddr_t)mp->mnt_stat.f_mntonname,
243 MNAMELEN);
244 (void) copyinstr(args.fspec, mp->mnt_stat.f_mntfromname, MNAMELEN - 1,
245 &size);
246 bzero(mp->mnt_stat.f_mntfromname + size, MNAMELEN - size);
247 (void) mfs_statfs(mp, &mp->mnt_stat, p);
248 return (0);
249 }
250
251 int mfs_pri = PWAIT | PCATCH; /* XXX prob. temp */
252
253 /*
254 * Used to grab the process and keep it in the kernel to service
255 * memory filesystem I/O requests.
256 *
257 * Loop servicing I/O requests.
258 * Copy the requested data into or out of the memory filesystem
259 * address space.
260 */
261 /* ARGSUSED */
262 int
263 mfs_start(mp, flags, p)
264 struct mount *mp;
265 int flags;
266 struct proc *p;
267 {
268 register struct vnode *vp = VFSTOUFS(mp)->um_devvp;
269 register struct mfsnode *mfsp = VTOMFS(vp);
270 register struct buf *bp;
271 register caddr_t base;
272 int error = 0;
273
274 base = mfsp->mfs_baseoff;
275 while (mfsp->mfs_buflist != (struct buf *)(-1)) {
276 while (bp = mfsp->mfs_buflist) {
277 mfsp->mfs_buflist = bp->b_actf;
278 mfs_doio(bp, base);
279 wakeup((caddr_t)bp);
280 }
281 /*
282 * If a non-ignored signal is received, try to unmount.
283 * If that fails, clear the signal (it has been "processed"),
284 * otherwise we will loop here, as tsleep will always return
285 * EINTR/ERESTART.
286 */
287 if (error = tsleep((caddr_t)vp, mfs_pri, "mfsidl", 0))
288 if (dounmount(mp, 0, p) != 0)
289 CLRSIG(p, CURSIG(p));
290 }
291 return (error);
292 }
293
294 /*
295 * Get file system statistics.
296 */
297 mfs_statfs(mp, sbp, p)
298 struct mount *mp;
299 struct statfs *sbp;
300 struct proc *p;
301 {
302 int error;
303
304 error = ffs_statfs(mp, sbp, p);
305 sbp->f_type = MOUNT_MFS;
306 return (error);
307 }
308