mfs_vfsops.c revision 1.9 1 /* $NetBSD: mfs_vfsops.c,v 1.9 1995/09/01 19:39:18 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 size_t 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(fs->fs_fsmnt, mp->mnt_stat.f_mntonname, MNAMELEN);
140 (void) copystr(ROOTNAME, mp->mnt_stat.f_mntfromname, MNAMELEN - 1,
141 &size);
142 bzero(mp->mnt_stat.f_mntfromname + size, MNAMELEN - size);
143 (void)ffs_statfs(mp, &mp->mnt_stat, p);
144 vfs_unlock(mp);
145 inittodr((time_t)0);
146 return (0);
147 }
148
149 /*
150 * This is called early in boot to set the base address and size
151 * of the mini-root.
152 */
153 mfs_initminiroot(base)
154 caddr_t base;
155 {
156 struct fs *fs = (struct fs *)(base + SBOFF);
157 extern int (*mountroot)();
158
159 /* check for valid super block */
160 if (fs->fs_magic != FS_MAGIC || fs->fs_bsize > MAXBSIZE ||
161 fs->fs_bsize < sizeof(struct fs))
162 return (0);
163 mountroot = mfs_mountroot;
164 mfs_rootbase = base;
165 mfs_rootsize = fs->fs_fsize * fs->fs_size;
166 rootdev = makedev(255, mfs_minor++);
167 return (mfs_rootsize);
168 }
169
170 /*
171 * VFS Operations.
172 *
173 * mount system call
174 */
175 /* ARGSUSED */
176 int
177 mfs_mount(mp, path, data, ndp, p)
178 register struct mount *mp;
179 char *path;
180 caddr_t data;
181 struct nameidata *ndp;
182 struct proc *p;
183 {
184 struct vnode *devvp;
185 struct mfs_args args;
186 struct ufsmount *ump;
187 register struct fs *fs;
188 register struct mfsnode *mfsp;
189 size_t size;
190 int flags, error;
191
192 if (error = copyin(data, (caddr_t)&args, sizeof (struct mfs_args)))
193 return (error);
194
195 /*
196 * If updating, check whether changing from read-only to
197 * read/write; if there is no device name, that's all we do.
198 */
199 if (mp->mnt_flag & MNT_UPDATE) {
200 ump = VFSTOUFS(mp);
201 fs = ump->um_fs;
202 if (fs->fs_ronly == 0 && (mp->mnt_flag & MNT_RDONLY)) {
203 flags = WRITECLOSE;
204 if (mp->mnt_flag & MNT_FORCE)
205 flags |= FORCECLOSE;
206 if (vfs_busy(mp))
207 return (EBUSY);
208 error = ffs_flushfiles(mp, flags, p);
209 vfs_unbusy(mp);
210 if (error)
211 return (error);
212 }
213 if (fs->fs_ronly && (mp->mnt_flag & MNT_WANTRDWR))
214 fs->fs_ronly = 0;
215 #ifdef EXPORTMFS
216 if (args.fspec == 0)
217 return (vfs_export(mp, &ump->um_export, &args.export));
218 #endif
219 return (0);
220 }
221 error = getnewvnode(VT_MFS, (struct mount *)0, mfs_vnodeop_p, &devvp);
222 if (error)
223 return (error);
224 devvp->v_type = VBLK;
225 if (checkalias(devvp, makedev(255, mfs_minor++), (struct mount *)0))
226 panic("mfs_mount: dup dev");
227 mfsp = (struct mfsnode *)malloc(sizeof *mfsp, M_MFSNODE, M_WAITOK);
228 devvp->v_data = mfsp;
229 mfsp->mfs_baseoff = args.base;
230 mfsp->mfs_size = args.size;
231 mfsp->mfs_vnode = devvp;
232 mfsp->mfs_pid = p->p_pid;
233 mfsp->mfs_buflist = (struct buf *)0;
234 if (error = ffs_mountfs(devvp, mp, p)) {
235 mfsp->mfs_buflist = (struct buf *)-1;
236 vrele(devvp);
237 return (error);
238 }
239 ump = VFSTOUFS(mp);
240 fs = ump->um_fs;
241 (void) copyinstr(path, fs->fs_fsmnt, sizeof(fs->fs_fsmnt) - 1, &size);
242 bzero(fs->fs_fsmnt + size, sizeof(fs->fs_fsmnt) - size);
243 bcopy(fs->fs_fsmnt, mp->mnt_stat.f_mntonname, 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 return (0);
248 }
249
250 int mfs_pri = PWAIT | PCATCH; /* XXX prob. temp */
251
252 /*
253 * Used to grab the process and keep it in the kernel to service
254 * memory filesystem I/O requests.
255 *
256 * Loop servicing I/O requests.
257 * Copy the requested data into or out of the memory filesystem
258 * address space.
259 */
260 /* ARGSUSED */
261 int
262 mfs_start(mp, flags, p)
263 struct mount *mp;
264 int flags;
265 struct proc *p;
266 {
267 register struct vnode *vp = VFSTOUFS(mp)->um_devvp;
268 register struct mfsnode *mfsp = VTOMFS(vp);
269 register struct buf *bp;
270 register caddr_t base;
271 int error = 0;
272
273 base = mfsp->mfs_baseoff;
274 while (mfsp->mfs_buflist != (struct buf *)-1) {
275 #define DOIO() \
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 DOIO();
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 DOIO();
290 if (dounmount(mp, 0, p) != 0)
291 CLRSIG(p, CURSIG(p));
292 }
293 }
294 return (error);
295 }
296
297 /*
298 * Get file system statistics.
299 */
300 mfs_statfs(mp, sbp, p)
301 struct mount *mp;
302 struct statfs *sbp;
303 struct proc *p;
304 {
305 int error;
306
307 error = ffs_statfs(mp, sbp, p);
308 #ifdef COMPAT_09
309 sbp->f_type = 3;
310 #else
311 sbp->f_type = 0;
312 #endif
313 strncpy(&sbp->f_fstypename[0], mp->mnt_op->vfs_name, MFSNAMELEN);
314 return (error);
315 }
316