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