mfs_vfsops.c revision 1.35 1 /* $NetBSD: mfs_vfsops.c,v 1.35 2001/09/15 16:13:06 chs 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/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 <miscfs/syncfs/syncfs.h>
54
55 #include <ufs/ufs/quota.h>
56 #include <ufs/ufs/inode.h>
57 #include <ufs/ufs/ufsmount.h>
58 #include <ufs/ufs/ufs_extern.h>
59
60 #include <ufs/ffs/fs.h>
61 #include <ufs/ffs/ffs_extern.h>
62
63 #include <ufs/mfs/mfsnode.h>
64 #include <ufs/mfs/mfs_extern.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 const struct vnodeopv_desc mfs_vnodeop_opv_desc;
78
79 const struct vnodeopv_desc * const 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 mfs_reinit,
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_reinit()
120 {
121 ffs_reinit();
122 }
123
124 void
125 mfs_done()
126 {
127 /*
128 * ffs_done() ensures to free necessary resources
129 * only once, when it's no more needed.
130 */
131 ffs_done();
132 }
133
134 /*
135 * Called by main() when mfs is going to be mounted as root.
136 */
137
138 int
139 mfs_mountroot()
140 {
141 struct fs *fs;
142 struct mount *mp;
143 struct proc *p = curproc; /* XXX */
144 struct ufsmount *ump;
145 struct mfsnode *mfsp;
146 int error = 0;
147
148 /*
149 * Get vnodes for rootdev.
150 */
151 if (bdevvp(rootdev, &rootvp)) {
152 printf("mfs_mountroot: can't setup bdevvp's");
153 return (error);
154 }
155
156 if ((error = vfs_rootmountalloc(MOUNT_MFS, "mfs_root", &mp))) {
157 vrele(rootvp);
158 return (error);
159 }
160
161 mfsp = malloc(sizeof *mfsp, M_MFSNODE, M_WAITOK);
162 rootvp->v_data = mfsp;
163 rootvp->v_op = mfs_vnodeop_p;
164 rootvp->v_tag = VT_MFS;
165 mfsp->mfs_baseoff = mfs_rootbase;
166 mfsp->mfs_size = mfs_rootsize;
167 mfsp->mfs_vnode = rootvp;
168 mfsp->mfs_proc = NULL; /* indicate kernel space */
169 BUFQ_INIT(&mfsp->mfs_buflist);
170 if ((error = ffs_mountfs(rootvp, mp, p)) != 0) {
171 mp->mnt_op->vfs_refcount--;
172 vfs_unbusy(mp);
173 free(mp, M_MOUNT);
174 free(mfsp, M_MFSNODE);
175 vrele(rootvp);
176 return (error);
177 }
178 simple_lock(&mountlist_slock);
179 CIRCLEQ_INSERT_TAIL(&mountlist, mp, mnt_list);
180 simple_unlock(&mountlist_slock);
181 mp->mnt_vnodecovered = NULLVP;
182 ump = VFSTOUFS(mp);
183 fs = ump->um_fs;
184 (void) copystr(mp->mnt_stat.f_mntonname, fs->fs_fsmnt, MNAMELEN - 1, 0);
185 (void)ffs_statfs(mp, &mp->mnt_stat, p);
186 vfs_unbusy(mp);
187 inittodr((time_t)0);
188 return (0);
189 }
190
191 /*
192 * This is called early in boot to set the base address and size
193 * of the mini-root.
194 */
195 int
196 mfs_initminiroot(base)
197 caddr_t base;
198 {
199 struct fs *fs = (struct fs *)(base + SBOFF);
200 extern int (*mountroot) __P((void));
201
202 /* check for valid super block */
203 if (fs->fs_magic != FS_MAGIC || fs->fs_bsize > MAXBSIZE ||
204 fs->fs_bsize < sizeof(struct fs))
205 return (0);
206 mountroot = mfs_mountroot;
207 mfs_rootbase = base;
208 mfs_rootsize = fs->fs_fsize * fs->fs_size;
209 rootdev = makedev(255, mfs_minor);
210 mfs_minor++;
211 return (mfs_rootsize);
212 }
213
214 /*
215 * VFS Operations.
216 *
217 * mount system call
218 */
219 /* ARGSUSED */
220 int
221 mfs_mount(mp, path, data, ndp, p)
222 struct mount *mp;
223 const char *path;
224 void *data;
225 struct nameidata *ndp;
226 struct proc *p;
227 {
228 struct vnode *devvp;
229 struct mfs_args args;
230 struct ufsmount *ump;
231 struct fs *fs;
232 struct mfsnode *mfsp;
233 size_t size;
234 int flags, error;
235
236 error = copyin(data, (caddr_t)&args, sizeof (struct mfs_args));
237 if (error)
238 return (error);
239
240 /*
241 * If updating, check whether changing from read-only to
242 * read/write; if there is no device name, that's all we do.
243 */
244 if (mp->mnt_flag & MNT_UPDATE) {
245 ump = VFSTOUFS(mp);
246 fs = ump->um_fs;
247 if (fs->fs_ronly == 0 && (mp->mnt_flag & MNT_RDONLY)) {
248 flags = WRITECLOSE;
249 if (mp->mnt_flag & MNT_FORCE)
250 flags |= FORCECLOSE;
251 error = ffs_flushfiles(mp, flags, p);
252 if (error)
253 return (error);
254 }
255 if (fs->fs_ronly && (mp->mnt_flag & MNT_WANTRDWR))
256 fs->fs_ronly = 0;
257 if (args.fspec == 0)
258 return (vfs_export(mp, &ump->um_export, &args.export));
259 return (0);
260 }
261 error = getnewvnode(VT_MFS, (struct mount *)0, mfs_vnodeop_p, &devvp);
262 if (error)
263 return (error);
264 devvp->v_type = VBLK;
265 if (checkalias(devvp, makedev(255, mfs_minor), (struct mount *)0))
266 panic("mfs_mount: dup dev");
267 mfs_minor++;
268 mfsp = (struct mfsnode *)malloc(sizeof *mfsp, M_MFSNODE, M_WAITOK);
269 devvp->v_data = mfsp;
270 mfsp->mfs_baseoff = args.base;
271 mfsp->mfs_size = args.size;
272 mfsp->mfs_vnode = devvp;
273 mfsp->mfs_proc = p;
274 BUFQ_INIT(&mfsp->mfs_buflist);
275 if ((error = ffs_mountfs(devvp, mp, p)) != 0) {
276 BUFQ_FIRST(&mfsp->mfs_buflist) = (struct buf *) -1;
277 vrele(devvp);
278 return (error);
279 }
280 ump = VFSTOUFS(mp);
281 fs = ump->um_fs;
282 (void) copyinstr(path, fs->fs_fsmnt, sizeof(fs->fs_fsmnt) - 1, &size);
283 memset(fs->fs_fsmnt + size, 0, sizeof(fs->fs_fsmnt) - size);
284 memcpy(mp->mnt_stat.f_mntonname, fs->fs_fsmnt, MNAMELEN);
285 (void) copyinstr(args.fspec, mp->mnt_stat.f_mntfromname, MNAMELEN - 1,
286 &size);
287 memset(mp->mnt_stat.f_mntfromname + size, 0, MNAMELEN - size);
288 return (0);
289 }
290
291 int mfs_pri = PWAIT | PCATCH; /* XXX prob. temp */
292
293 /*
294 * Used to grab the process and keep it in the kernel to service
295 * memory filesystem I/O requests.
296 *
297 * Loop servicing I/O requests.
298 * Copy the requested data into or out of the memory filesystem
299 * address space.
300 */
301 /* ARGSUSED */
302 int
303 mfs_start(mp, flags, p)
304 struct mount *mp;
305 int flags;
306 struct proc *p;
307 {
308 struct vnode *vp = VFSTOUFS(mp)->um_devvp;
309 struct mfsnode *mfsp = VTOMFS(vp);
310 struct buf *bp;
311 caddr_t base;
312 int sleepreturn = 0;
313
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 */
328 if (sleepreturn != 0) {
329 /*
330 * XXX Freeze syncer. Must do this before locking
331 * the mount point. See dounmount() for details.
332 */
333 lockmgr(&syncer_lock, LK_EXCLUSIVE, NULL);
334 if (vfs_busy(mp, LK_NOWAIT, 0) != 0)
335 lockmgr(&syncer_lock, LK_RELEASE, NULL);
336 else if (dounmount(mp, 0, p) != 0)
337 CLRSIG(p, CURSIG(p));
338 sleepreturn = 0;
339 continue;
340 }
341
342 sleepreturn = tsleep(vp, mfs_pri, "mfsidl", 0);
343 }
344 return (sleepreturn);
345 }
346
347 /*
348 * Get file system statistics.
349 */
350 int
351 mfs_statfs(mp, sbp, p)
352 struct mount *mp;
353 struct statfs *sbp;
354 struct proc *p;
355 {
356 int error;
357
358 error = ffs_statfs(mp, sbp, p);
359 #ifdef COMPAT_09
360 sbp->f_type = 3;
361 #else
362 sbp->f_type = 0;
363 #endif
364 strncpy(&sbp->f_fstypename[0], mp->mnt_op->vfs_name, MFSNAMELEN);
365 return (error);
366 }
367