mfs_vfsops.c revision 1.15 1 /* $NetBSD: mfs_vfsops.c,v 1.15 1998/03/01 02:23:29 fvdl 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.11 (Berkeley) 6/19/95
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) __P((void *));
66
67 /*
68 * mfs vfs operations.
69 */
70
71 extern struct vnodeopv_desc mfs_vnodeop_opv_desc;
72
73 struct vnodeopv_desc *mfs_vnodeopv_descs[] = {
74 &mfs_vnodeop_opv_desc,
75 NULL,
76 };
77
78 struct vfsops mfs_vfsops = {
79 MOUNT_MFS,
80 mfs_mount,
81 mfs_start,
82 ffs_unmount,
83 ufs_root,
84 ufs_quotactl,
85 mfs_statfs,
86 ffs_sync,
87 ffs_vget,
88 ffs_fhtovp,
89 ffs_vptofh,
90 mfs_init,
91 ffs_sysctl,
92 mfs_mountroot,
93 mfs_vnodeopv_descs,
94 };
95
96 /*
97 * Memory based filesystem initialization.
98 */
99 void
100 mfs_init()
101 {
102 }
103
104
105 /*
106 * Called by main() when mfs is going to be mounted as root.
107 */
108
109 int
110 mfs_mountroot()
111 {
112 extern struct vnode *rootvp;
113 struct fs *fs;
114 struct mount *mp;
115 struct proc *p = curproc; /* XXX */
116 struct ufsmount *ump;
117 struct mfsnode *mfsp;
118 int error = 0;
119
120 /*
121 * Get vnodes for rootdev.
122 */
123 if (bdevvp(rootdev, &rootvp)) {
124 printf("mfs_mountroot: can't setup bdevvp's");
125 return (error);
126 }
127
128 if ((error = vfs_rootmountalloc(MOUNT_MFS, "mfs_root", &mp)))
129 return (error);
130
131 mfsp = malloc(sizeof *mfsp, M_MFSNODE, M_WAITOK);
132 rootvp->v_data = mfsp;
133 rootvp->v_op = mfs_vnodeop_p;
134 rootvp->v_tag = VT_MFS;
135 mfsp->mfs_baseoff = mfs_rootbase;
136 mfsp->mfs_size = mfs_rootsize;
137 mfsp->mfs_vnode = rootvp;
138 mfsp->mfs_pid = p->p_pid;
139 mfsp->mfs_buflist = (struct buf *)0;
140 if ((error = ffs_mountfs(rootvp, mp, p)) != 0) {
141 mp->mnt_op->vfs_refcount--;
142 vfs_unbusy(mp);
143 free(mp, M_MOUNT);
144 free(mfsp, M_MFSNODE);
145 return (error);
146 }
147 simple_lock(&mountlist_slock);
148 CIRCLEQ_INSERT_TAIL(&mountlist, mp, mnt_list);
149 simple_unlock(&mountlist_slock);
150 mp->mnt_vnodecovered = NULLVP;
151 ump = VFSTOUFS(mp);
152 fs = ump->um_fs;
153 (void) copystr(mp->mnt_stat.f_mntonname, fs->fs_fsmnt, MNAMELEN - 1, 0);
154 (void)ffs_statfs(mp, &mp->mnt_stat, p);
155 vfs_unbusy(mp);
156 inittodr((time_t)0);
157 return (0);
158 }
159
160 /*
161 * This is called early in boot to set the base address and size
162 * of the mini-root.
163 */
164 int
165 mfs_initminiroot(base)
166 caddr_t base;
167 {
168 struct fs *fs = (struct fs *)(base + SBOFF);
169 extern int (*mountroot) __P((void));
170
171 /* check for valid super block */
172 if (fs->fs_magic != FS_MAGIC || fs->fs_bsize > MAXBSIZE ||
173 fs->fs_bsize < sizeof(struct fs))
174 return (0);
175 mountroot = mfs_mountroot;
176 mfs_rootbase = base;
177 mfs_rootsize = fs->fs_fsize * fs->fs_size;
178 rootdev = makedev(255, mfs_minor++);
179 return (mfs_rootsize);
180 }
181
182 /*
183 * VFS Operations.
184 *
185 * mount system call
186 */
187 /* ARGSUSED */
188 int
189 mfs_mount(mp, path, data, ndp, p)
190 register struct mount *mp;
191 const char *path;
192 void *data;
193 struct nameidata *ndp;
194 struct proc *p;
195 {
196 struct vnode *devvp;
197 struct mfs_args args;
198 struct ufsmount *ump;
199 register struct fs *fs;
200 register struct mfsnode *mfsp;
201 size_t size;
202 int flags, error;
203
204 error = copyin(data, (caddr_t)&args, sizeof (struct mfs_args));
205 if (error)
206 return (error);
207
208 /*
209 * If updating, check whether changing from read-only to
210 * read/write; if there is no device name, that's all we do.
211 */
212 if (mp->mnt_flag & MNT_UPDATE) {
213 ump = VFSTOUFS(mp);
214 fs = ump->um_fs;
215 if (fs->fs_ronly == 0 && (mp->mnt_flag & MNT_RDONLY)) {
216 flags = WRITECLOSE;
217 if (mp->mnt_flag & MNT_FORCE)
218 flags |= FORCECLOSE;
219 error = ffs_flushfiles(mp, flags, p);
220 if (error)
221 return (error);
222 }
223 if (fs->fs_ronly && (mp->mnt_flag & MNT_WANTRDWR))
224 fs->fs_ronly = 0;
225 #ifdef EXPORTMFS
226 if (args.fspec == 0)
227 return (vfs_export(mp, &ump->um_export, &args.export));
228 #endif
229 return (0);
230 }
231 error = getnewvnode(VT_MFS, (struct mount *)0, mfs_vnodeop_p, &devvp);
232 if (error)
233 return (error);
234 devvp->v_type = VBLK;
235 if (checkalias(devvp, makedev(255, mfs_minor++), (struct mount *)0))
236 panic("mfs_mount: dup dev");
237 mfsp = (struct mfsnode *)malloc(sizeof *mfsp, M_MFSNODE, M_WAITOK);
238 devvp->v_data = mfsp;
239 mfsp->mfs_baseoff = args.base;
240 mfsp->mfs_size = args.size;
241 mfsp->mfs_vnode = devvp;
242 mfsp->mfs_pid = p->p_pid;
243 mfsp->mfs_buflist = (struct buf *)0;
244 if ((error = ffs_mountfs(devvp, mp, p)) != 0) {
245 mfsp->mfs_buflist = (struct buf *)-1;
246 vrele(devvp);
247 return (error);
248 }
249 ump = VFSTOUFS(mp);
250 fs = ump->um_fs;
251 (void) copyinstr(path, fs->fs_fsmnt, sizeof(fs->fs_fsmnt) - 1, &size);
252 bzero(fs->fs_fsmnt + size, sizeof(fs->fs_fsmnt) - size);
253 bcopy(fs->fs_fsmnt, mp->mnt_stat.f_mntonname, MNAMELEN);
254 (void) copyinstr(args.fspec, mp->mnt_stat.f_mntfromname, MNAMELEN - 1,
255 &size);
256 bzero(mp->mnt_stat.f_mntfromname + size, MNAMELEN - size);
257 return (0);
258 }
259
260 int mfs_pri = PWAIT | PCATCH; /* XXX prob. temp */
261
262 /*
263 * Used to grab the process and keep it in the kernel to service
264 * memory filesystem I/O requests.
265 *
266 * Loop servicing I/O requests.
267 * Copy the requested data into or out of the memory filesystem
268 * address space.
269 */
270 /* ARGSUSED */
271 int
272 mfs_start(mp, flags, p)
273 struct mount *mp;
274 int flags;
275 struct proc *p;
276 {
277 register struct vnode *vp = VFSTOUFS(mp)->um_devvp;
278 register struct mfsnode *mfsp = VTOMFS(vp);
279 register struct buf *bp;
280 register caddr_t base;
281 int sleepreturn = 0;
282
283 base = mfsp->mfs_baseoff;
284 while (mfsp->mfs_buflist != (struct buf *)-1) {
285 /*
286 * If a non-ignored signal is received, try to unmount.
287 * If that fails, or the filesystem is already in the
288 * process of being unmounted, clear the signal (it has been
289 * "processed"), otherwise we will loop here, as tsleep
290 * will always return EINTR/ERESTART.
291 */
292 if (sleepreturn != 0) {
293 if (vfs_busy(mp, LK_NOWAIT, 0) ||
294 dounmount(mp, 0, p) != 0)
295 CLRSIG(p, CURSIG(p));
296 sleepreturn = 0;
297 continue;
298 }
299
300 while ((bp = mfsp->mfs_buflist) != NULL) {
301 mfsp->mfs_buflist = bp->b_actf;
302 mfs_doio(bp, base);
303 wakeup((caddr_t)bp);
304 }
305 sleepreturn = tsleep(vp, mfs_pri, "mfsidl", 0);
306 }
307 return (sleepreturn);
308 }
309
310 /*
311 * Get file system statistics.
312 */
313 int
314 mfs_statfs(mp, sbp, p)
315 struct mount *mp;
316 struct statfs *sbp;
317 struct proc *p;
318 {
319 int error;
320
321 error = ffs_statfs(mp, sbp, p);
322 #ifdef COMPAT_09
323 sbp->f_type = 3;
324 #else
325 sbp->f_type = 0;
326 #endif
327 strncpy(&sbp->f_fstypename[0], mp->mnt_op->vfs_name, MFSNAMELEN);
328 return (error);
329 }
330