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