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