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