mfs_vfsops.c revision 1.60 1 /* $NetBSD: mfs_vfsops.c,v 1.60 2004/05/25 14:55:46 hannken 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. Neither the name of the University nor the names of its contributors
16 * may be used to endorse or promote products derived from this software
17 * without specific prior written permission.
18 *
19 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
20 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
21 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
22 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
23 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
24 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
25 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
26 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
27 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
28 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
29 * SUCH DAMAGE.
30 *
31 * @(#)mfs_vfsops.c 8.11 (Berkeley) 6/19/95
32 */
33
34 #include <sys/cdefs.h>
35 __KERNEL_RCSID(0, "$NetBSD: mfs_vfsops.c,v 1.60 2004/05/25 14:55:46 hannken Exp $");
36
37 #if defined(_KERNEL_OPT)
38 #include "opt_compat_netbsd.h"
39 #endif
40
41 #include <sys/param.h>
42 #include <sys/systm.h>
43 #include <sys/sysctl.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 MALLOC_DEFINE(M_MFSNODE, "MFS node", "MFS vnode private part");
74
75 /*
76 * mfs vfs operations.
77 */
78
79 extern const struct vnodeopv_desc mfs_vnodeop_opv_desc;
80
81 const struct vnodeopv_desc * const mfs_vnodeopv_descs[] = {
82 &mfs_vnodeop_opv_desc,
83 NULL,
84 };
85
86 struct vfsops mfs_vfsops = {
87 MOUNT_MFS,
88 mfs_mount,
89 mfs_start,
90 ffs_unmount,
91 ufs_root,
92 ufs_quotactl,
93 mfs_statvfs,
94 ffs_sync,
95 ffs_vget,
96 ffs_fhtovp,
97 ffs_vptofh,
98 mfs_init,
99 mfs_reinit,
100 mfs_done,
101 NULL,
102 NULL,
103 ufs_check_export,
104 (int (*)(struct mount *, struct vnode *, struct timespec *)) eopnotsupp,
105 mfs_vnodeopv_descs,
106 };
107
108 SYSCTL_SETUP(sysctl_vfs_mfs_setup, "sysctl vfs.mfs subtree setup")
109 {
110
111 sysctl_createv(clog, 0, NULL, NULL,
112 CTLFLAG_PERMANENT,
113 CTLTYPE_NODE, "vfs", NULL,
114 NULL, 0, NULL, 0,
115 CTL_VFS, CTL_EOL);
116 sysctl_createv(clog, 0, NULL, NULL,
117 CTLFLAG_PERMANENT|CTLFLAG_ALIAS,
118 CTLTYPE_NODE, "mfs",
119 SYSCTL_DESCR("Memory based file system"),
120 NULL, 1, NULL, 0,
121 CTL_VFS, 3, CTL_EOL);
122 /*
123 * XXX the "1" and the "3" above could be dynamic, thereby
124 * eliminating one more instance of the "number to vfs"
125 * mapping problem, but they are in order as taken from
126 * sys/mount.h
127 */
128 }
129
130 /*
131 * Memory based filesystem initialization.
132 */
133 void
134 mfs_init()
135 {
136 #ifdef _LKM
137 malloc_type_attach(M_MFSNODE);
138 #endif
139 /*
140 * ffs_init() ensures to initialize necessary resources
141 * only once.
142 */
143 ffs_init();
144 }
145
146 void
147 mfs_reinit()
148 {
149 ffs_reinit();
150 }
151
152 void
153 mfs_done()
154 {
155 /*
156 * ffs_done() ensures to free necessary resources
157 * only once, when it's no more needed.
158 */
159 ffs_done();
160 #ifdef _LKM
161 malloc_type_detach(M_MFSNODE);
162 #endif
163 }
164
165 /*
166 * Called by main() when mfs is going to be mounted as root.
167 */
168
169 int
170 mfs_mountroot()
171 {
172 struct fs *fs;
173 struct mount *mp;
174 struct proc *p = curproc; /* XXX */
175 struct ufsmount *ump;
176 struct mfsnode *mfsp;
177 int error = 0;
178
179 /*
180 * Get vnodes for rootdev.
181 */
182 if (bdevvp(rootdev, &rootvp)) {
183 printf("mfs_mountroot: can't setup bdevvp's");
184 return (error);
185 }
186
187 if ((error = vfs_rootmountalloc(MOUNT_MFS, "mfs_root", &mp))) {
188 vrele(rootvp);
189 return (error);
190 }
191
192 mfsp = malloc(sizeof *mfsp, M_MFSNODE, M_WAITOK);
193 rootvp->v_data = mfsp;
194 rootvp->v_op = mfs_vnodeop_p;
195 rootvp->v_tag = VT_MFS;
196 mfsp->mfs_baseoff = mfs_rootbase;
197 mfsp->mfs_size = mfs_rootsize;
198 mfsp->mfs_vnode = rootvp;
199 mfsp->mfs_proc = NULL; /* indicate kernel space */
200 mfsp->mfs_shutdown = 0;
201 bufq_alloc(&mfsp->mfs_buflist, BUFQ_FCFS);
202 if ((error = ffs_mountfs(rootvp, mp, p)) != 0) {
203 mp->mnt_op->vfs_refcount--;
204 vfs_unbusy(mp);
205 bufq_free(&mfsp->mfs_buflist);
206 free(mp, M_MOUNT);
207 free(mfsp, M_MFSNODE);
208 vrele(rootvp);
209 return (error);
210 }
211 simple_lock(&mountlist_slock);
212 CIRCLEQ_INSERT_TAIL(&mountlist, mp, mnt_list);
213 simple_unlock(&mountlist_slock);
214 mp->mnt_vnodecovered = NULLVP;
215 ump = VFSTOUFS(mp);
216 fs = ump->um_fs;
217 (void) copystr(mp->mnt_stat.f_mntonname, fs->fs_fsmnt, MNAMELEN - 1, 0);
218 (void)ffs_statvfs(mp, &mp->mnt_stat, p);
219 vfs_unbusy(mp);
220 inittodr((time_t)0);
221 return (0);
222 }
223
224 /*
225 * This is called early in boot to set the base address and size
226 * of the mini-root.
227 */
228 int
229 mfs_initminiroot(base)
230 caddr_t base;
231 {
232 struct fs *fs = (struct fs *)(base + SBLOCK_UFS1);
233
234 /* check for valid super block */
235 if (fs->fs_magic != FS_UFS1_MAGIC || fs->fs_bsize > MAXBSIZE ||
236 fs->fs_bsize < sizeof(struct fs))
237 return (0);
238 mountroot = mfs_mountroot;
239 mfs_rootbase = base;
240 mfs_rootsize = fs->fs_fsize * fs->fs_size;
241 rootdev = makedev(255, mfs_minor);
242 mfs_minor++;
243 return (mfs_rootsize);
244 }
245
246 /*
247 * VFS Operations.
248 *
249 * mount system call
250 */
251 /* ARGSUSED */
252 int
253 mfs_mount(mp, path, data, ndp, p)
254 struct mount *mp;
255 const char *path;
256 void *data;
257 struct nameidata *ndp;
258 struct proc *p;
259 {
260 struct vnode *devvp;
261 struct mfs_args args;
262 struct ufsmount *ump;
263 struct fs *fs;
264 struct mfsnode *mfsp;
265 int flags, error;
266
267 if (mp->mnt_flag & MNT_GETARGS) {
268 struct vnode *vp;
269 struct mfsnode *mfsp;
270
271 ump = VFSTOUFS(mp);
272 if (ump == NULL)
273 return EIO;
274
275 vp = ump->um_devvp;
276 if (vp == NULL)
277 return EIO;
278
279 mfsp = VTOMFS(vp);
280 if (mfsp == NULL)
281 return EIO;
282
283 args.fspec = NULL;
284 vfs_showexport(mp, &args.export, &ump->um_export);
285 args.base = mfsp->mfs_baseoff;
286 args.size = mfsp->mfs_size;
287 return copyout(&args, data, sizeof(args));
288 }
289 /*
290 * XXX turn off async to avoid hangs when writing lots of data.
291 * the problem is that MFS needs to allocate pages to clean pages,
292 * so if we wait until the last minute to clean pages then there
293 * may not be any pages available to do the cleaning.
294 * ... and since the default partially-synchronous mode turns out
295 * to not be sufficient under heavy load, make it full synchronous.
296 */
297 mp->mnt_flag &= ~MNT_ASYNC;
298 mp->mnt_flag |= MNT_SYNCHRONOUS;
299
300 error = copyin(data, (caddr_t)&args, sizeof (struct mfs_args));
301 if (error)
302 return (error);
303
304 /*
305 * If updating, check whether changing from read-only to
306 * read/write; if there is no device name, that's all we do.
307 */
308 if (mp->mnt_flag & MNT_UPDATE) {
309 ump = VFSTOUFS(mp);
310 fs = ump->um_fs;
311 if (fs->fs_ronly == 0 && (mp->mnt_flag & MNT_RDONLY)) {
312 flags = WRITECLOSE;
313 if (mp->mnt_flag & MNT_FORCE)
314 flags |= FORCECLOSE;
315 error = ffs_flushfiles(mp, flags, p);
316 if (error)
317 return (error);
318 }
319 if (fs->fs_ronly && (mp->mnt_iflag & IMNT_WANTRDWR))
320 fs->fs_ronly = 0;
321 if (args.fspec == 0)
322 return (vfs_export(mp, &ump->um_export, &args.export));
323 return (0);
324 }
325 error = getnewvnode(VT_MFS, (struct mount *)0, mfs_vnodeop_p, &devvp);
326 if (error)
327 return (error);
328 devvp->v_type = VBLK;
329 if (checkalias(devvp, makedev(255, mfs_minor), (struct mount *)0))
330 panic("mfs_mount: dup dev");
331 mfs_minor++;
332 mfsp = (struct mfsnode *)malloc(sizeof *mfsp, M_MFSNODE, M_WAITOK);
333 devvp->v_data = mfsp;
334 mfsp->mfs_baseoff = args.base;
335 mfsp->mfs_size = args.size;
336 mfsp->mfs_vnode = devvp;
337 mfsp->mfs_proc = p;
338 mfsp->mfs_shutdown = 0;
339 bufq_alloc(&mfsp->mfs_buflist, BUFQ_FCFS);
340 if ((error = ffs_mountfs(devvp, mp, p)) != 0) {
341 mfsp->mfs_shutdown = 1;
342 vrele(devvp);
343 return (error);
344 }
345 ump = VFSTOUFS(mp);
346 fs = ump->um_fs;
347 error = set_statvfs_info(path, UIO_USERSPACE, args.fspec,
348 UIO_USERSPACE, mp, p);
349 if (error)
350 return error;
351 (void)strncpy(fs->fs_fsmnt, mp->mnt_stat.f_mntonname,
352 sizeof(fs->fs_fsmnt));
353 fs->fs_fsmnt[sizeof(fs->fs_fsmnt) - 1] = '\0';
354 /* XXX: cleanup on error */
355 return 0;
356 }
357
358 int mfs_pri = PWAIT | PCATCH; /* XXX prob. temp */
359
360 /*
361 * Used to grab the process and keep it in the kernel to service
362 * memory filesystem I/O requests.
363 *
364 * Loop servicing I/O requests.
365 * Copy the requested data into or out of the memory filesystem
366 * address space.
367 */
368 /* ARGSUSED */
369 int
370 mfs_start(mp, flags, p)
371 struct mount *mp;
372 int flags;
373 struct proc *p;
374 {
375 struct vnode *vp = VFSTOUFS(mp)->um_devvp;
376 struct mfsnode *mfsp = VTOMFS(vp);
377 struct buf *bp;
378 caddr_t base;
379 int sleepreturn = 0;
380 struct lwp *l; /* XXX NJWLWP */
381
382 /* XXX NJWLWP the vnode interface again gives us a proc in a
383 * place where we want a execution context. Cheat.
384 */
385 KASSERT(curproc == p);
386 l = curlwp;
387 base = mfsp->mfs_baseoff;
388 while (mfsp->mfs_shutdown != 1) {
389 while ((bp = BUFQ_GET(&mfsp->mfs_buflist)) != NULL) {
390 mfs_doio(bp, base);
391 wakeup((caddr_t)bp);
392 }
393 /*
394 * If a non-ignored signal is received, try to unmount.
395 * If that fails, or the filesystem is already in the
396 * process of being unmounted, clear the signal (it has been
397 * "processed"), otherwise we will loop here, as tsleep
398 * will always return EINTR/ERESTART.
399 */
400 if (sleepreturn != 0) {
401 /*
402 * XXX Freeze syncer. Must do this before locking
403 * the mount point. See dounmount() for details.
404 */
405 lockmgr(&syncer_lock, LK_EXCLUSIVE, NULL);
406 if (vfs_busy(mp, LK_NOWAIT, 0) != 0)
407 lockmgr(&syncer_lock, LK_RELEASE, NULL);
408 else if (dounmount(mp, 0, p) != 0)
409 CLRSIG(p, CURSIG(l));
410 sleepreturn = 0;
411 continue;
412 }
413
414 sleepreturn = tsleep(vp, mfs_pri, "mfsidl", 0);
415 }
416 KASSERT(BUFQ_PEEK(&mfsp->mfs_buflist) == NULL);
417 bufq_free(&mfsp->mfs_buflist);
418 return (sleepreturn);
419 }
420
421 /*
422 * Get file system statistics.
423 */
424 int
425 mfs_statvfs(mp, sbp, p)
426 struct mount *mp;
427 struct statvfs *sbp;
428 struct proc *p;
429 {
430 int error;
431
432 error = ffs_statvfs(mp, sbp, p);
433 if (error)
434 return error;
435 (void)strncpy(sbp->f_fstypename, mp->mnt_op->vfs_name,
436 sizeof(sbp->f_fstypename));
437 sbp->f_fstypename[sizeof(sbp->f_fstypename) - 1] = '\0';
438 return 0;
439 }
440