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