mfs_vfsops.c revision 1.46 1 /* $NetBSD: mfs_vfsops.c,v 1.46 2003/04/16 21:44:28 christos 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.46 2003/04/16 21:44:28 christos 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 + SBLOCK_UFS1);
207
208 /* check for valid super block */
209 if (fs->fs_magic != FS_UFS1_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 int flags, error;
240
241 if (mp->mnt_flag & MNT_GETARGS) {
242 struct vnode *vp;
243 struct mfsnode *mfsp;
244
245 ump = VFSTOUFS(mp);
246 if (ump == NULL)
247 return EIO;
248
249 vp = ump->um_devvp;
250 if (vp == NULL)
251 return EIO;
252
253 mfsp = VTOMFS(vp);
254 if (mfsp == NULL)
255 return EIO;
256
257 args.fspec = NULL;
258 vfs_showexport(mp, &args.export, &ump->um_export);
259 args.base = mfsp->mfs_baseoff;
260 args.size = mfsp->mfs_size;
261 return copyout(&args, data, sizeof(args));
262 }
263 /*
264 * XXX turn off async to avoid hangs when writing lots of data.
265 * the problem is that MFS needs to allocate pages to clean pages,
266 * so if we wait until the last minute to clean pages then there
267 * may not be any pages available to do the cleaning.
268 * ... and since the default partially-synchronous mode turns out
269 * to not be sufficient under heavy load, make it full synchronous.
270 */
271 mp->mnt_flag &= ~MNT_ASYNC;
272 mp->mnt_flag |= MNT_SYNCHRONOUS;
273
274 error = copyin(data, (caddr_t)&args, sizeof (struct mfs_args));
275 if (error)
276 return (error);
277
278 /*
279 * If updating, check whether changing from read-only to
280 * read/write; if there is no device name, that's all we do.
281 */
282 if (mp->mnt_flag & MNT_UPDATE) {
283 ump = VFSTOUFS(mp);
284 fs = ump->um_fs;
285 if (fs->fs_ronly == 0 && (mp->mnt_flag & MNT_RDONLY)) {
286 flags = WRITECLOSE;
287 if (mp->mnt_flag & MNT_FORCE)
288 flags |= FORCECLOSE;
289 error = ffs_flushfiles(mp, flags, p);
290 if (error)
291 return (error);
292 }
293 if (fs->fs_ronly && (mp->mnt_flag & MNT_WANTRDWR))
294 fs->fs_ronly = 0;
295 if (args.fspec == 0)
296 return (vfs_export(mp, &ump->um_export, &args.export));
297 return (0);
298 }
299 error = getnewvnode(VT_MFS, (struct mount *)0, mfs_vnodeop_p, &devvp);
300 if (error)
301 return (error);
302 devvp->v_type = VBLK;
303 if (checkalias(devvp, makedev(255, mfs_minor), (struct mount *)0))
304 panic("mfs_mount: dup dev");
305 mfs_minor++;
306 mfsp = (struct mfsnode *)malloc(sizeof *mfsp, M_MFSNODE, M_WAITOK);
307 devvp->v_data = mfsp;
308 mfsp->mfs_baseoff = args.base;
309 mfsp->mfs_size = args.size;
310 mfsp->mfs_vnode = devvp;
311 mfsp->mfs_proc = p;
312 mfsp->mfs_shutdown = 0;
313 bufq_alloc(&mfsp->mfs_buflist, BUFQ_FCFS);
314 if ((error = ffs_mountfs(devvp, mp, p)) != 0) {
315 mfsp->mfs_shutdown = 1;
316 vrele(devvp);
317 return (error);
318 }
319 ump = VFSTOUFS(mp);
320 fs = ump->um_fs;
321 error = set_statfs_info(path, UIO_USERSPACE, args.fspec,
322 UIO_USERSPACE, mp, p);
323 (void)memcpy(fs->fs_fsmnt, mp->mnt_stat.f_mntonname,
324 sizeof(fs->fs_fsmnt));
325 return error;
326 }
327
328 int mfs_pri = PWAIT | PCATCH; /* XXX prob. temp */
329
330 /*
331 * Used to grab the process and keep it in the kernel to service
332 * memory filesystem I/O requests.
333 *
334 * Loop servicing I/O requests.
335 * Copy the requested data into or out of the memory filesystem
336 * address space.
337 */
338 /* ARGSUSED */
339 int
340 mfs_start(mp, flags, p)
341 struct mount *mp;
342 int flags;
343 struct proc *p;
344 {
345 struct vnode *vp = VFSTOUFS(mp)->um_devvp;
346 struct mfsnode *mfsp = VTOMFS(vp);
347 struct buf *bp;
348 caddr_t base;
349 int sleepreturn = 0;
350 struct lwp *l; /* XXX NJWLWP */
351
352 /* XXX NJWLWP the vnode interface again gives us a proc in a
353 * place where we want a execution context. Cheat.
354 */
355 KASSERT(curproc == p);
356 l = curlwp;
357 base = mfsp->mfs_baseoff;
358 while (mfsp->mfs_shutdown != 1) {
359 while ((bp = BUFQ_GET(&mfsp->mfs_buflist)) != NULL) {
360 mfs_doio(bp, base);
361 wakeup((caddr_t)bp);
362 }
363 /*
364 * If a non-ignored signal is received, try to unmount.
365 * If that fails, or the filesystem is already in the
366 * process of being unmounted, clear the signal (it has been
367 * "processed"), otherwise we will loop here, as tsleep
368 * will always return EINTR/ERESTART.
369 */
370 if (sleepreturn != 0) {
371 /*
372 * XXX Freeze syncer. Must do this before locking
373 * the mount point. See dounmount() for details.
374 */
375 lockmgr(&syncer_lock, LK_EXCLUSIVE, NULL);
376 if (vfs_busy(mp, LK_NOWAIT, 0) != 0)
377 lockmgr(&syncer_lock, LK_RELEASE, NULL);
378 else if (dounmount(mp, 0, p) != 0)
379 CLRSIG(p, CURSIG(l));
380 sleepreturn = 0;
381 continue;
382 }
383
384 sleepreturn = tsleep(vp, mfs_pri, "mfsidl", 0);
385 }
386 KASSERT(BUFQ_PEEK(&mfsp->mfs_buflist) == NULL);
387 bufq_free(&mfsp->mfs_buflist);
388 return (sleepreturn);
389 }
390
391 /*
392 * Get file system statistics.
393 */
394 int
395 mfs_statfs(mp, sbp, p)
396 struct mount *mp;
397 struct statfs *sbp;
398 struct proc *p;
399 {
400 int error;
401
402 error = ffs_statfs(mp, sbp, p);
403 #ifdef COMPAT_09
404 sbp->f_type = 3;
405 #else
406 sbp->f_type = 0;
407 #endif
408 strncpy(&sbp->f_fstypename[0], mp->mnt_op->vfs_name, MFSNAMELEN);
409 return (error);
410 }
411