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