mfs_vfsops.c revision 1.32.2.8 1 1.32.2.8 nathanw /* $NetBSD: mfs_vfsops.c,v 1.32.2.8 2002/06/24 22:12:41 nathanw Exp $ */
2 1.2 cgd
3 1.1 mycroft /*
4 1.1 mycroft * Copyright (c) 1989, 1990, 1993, 1994
5 1.1 mycroft * The Regents of the University of California. All rights reserved.
6 1.1 mycroft *
7 1.1 mycroft * Redistribution and use in source and binary forms, with or without
8 1.1 mycroft * modification, are permitted provided that the following conditions
9 1.1 mycroft * are met:
10 1.1 mycroft * 1. Redistributions of source code must retain the above copyright
11 1.1 mycroft * notice, this list of conditions and the following disclaimer.
12 1.1 mycroft * 2. Redistributions in binary form must reproduce the above copyright
13 1.1 mycroft * notice, this list of conditions and the following disclaimer in the
14 1.1 mycroft * documentation and/or other materials provided with the distribution.
15 1.1 mycroft * 3. All advertising materials mentioning features or use of this software
16 1.1 mycroft * must display the following acknowledgement:
17 1.1 mycroft * This product includes software developed by the University of
18 1.1 mycroft * California, Berkeley and its contributors.
19 1.1 mycroft * 4. Neither the name of the University nor the names of its contributors
20 1.1 mycroft * may be used to endorse or promote products derived from this software
21 1.1 mycroft * without specific prior written permission.
22 1.1 mycroft *
23 1.1 mycroft * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
24 1.1 mycroft * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
25 1.1 mycroft * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
26 1.1 mycroft * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
27 1.1 mycroft * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
28 1.1 mycroft * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
29 1.1 mycroft * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
30 1.1 mycroft * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
31 1.1 mycroft * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
32 1.1 mycroft * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
33 1.1 mycroft * SUCH DAMAGE.
34 1.1 mycroft *
35 1.15 fvdl * @(#)mfs_vfsops.c 8.11 (Berkeley) 6/19/95
36 1.1 mycroft */
37 1.32.2.4 nathanw
38 1.32.2.4 nathanw #include <sys/cdefs.h>
39 1.32.2.8 nathanw __KERNEL_RCSID(0, "$NetBSD: mfs_vfsops.c,v 1.32.2.8 2002/06/24 22:12:41 nathanw Exp $");
40 1.17 jonathan
41 1.32.2.2 nathanw #if defined(_KERNEL_OPT)
42 1.17 jonathan #include "opt_compat_netbsd.h"
43 1.17 jonathan #endif
44 1.1 mycroft
45 1.1 mycroft #include <sys/param.h>
46 1.1 mycroft #include <sys/systm.h>
47 1.1 mycroft #include <sys/time.h>
48 1.1 mycroft #include <sys/kernel.h>
49 1.32.2.1 nathanw #include <sys/lwp.h>
50 1.1 mycroft #include <sys/proc.h>
51 1.1 mycroft #include <sys/buf.h>
52 1.1 mycroft #include <sys/mount.h>
53 1.1 mycroft #include <sys/signalvar.h>
54 1.1 mycroft #include <sys/vnode.h>
55 1.1 mycroft #include <sys/malloc.h>
56 1.1 mycroft
57 1.32.2.2 nathanw #include <miscfs/syncfs/syncfs.h>
58 1.32.2.2 nathanw
59 1.1 mycroft #include <ufs/ufs/quota.h>
60 1.1 mycroft #include <ufs/ufs/inode.h>
61 1.1 mycroft #include <ufs/ufs/ufsmount.h>
62 1.1 mycroft #include <ufs/ufs/ufs_extern.h>
63 1.1 mycroft
64 1.1 mycroft #include <ufs/ffs/fs.h>
65 1.1 mycroft #include <ufs/ffs/ffs_extern.h>
66 1.1 mycroft
67 1.1 mycroft #include <ufs/mfs/mfsnode.h>
68 1.1 mycroft #include <ufs/mfs/mfs_extern.h>
69 1.1 mycroft
70 1.1 mycroft caddr_t mfs_rootbase; /* address of mini-root in kernel virtual memory */
71 1.1 mycroft u_long mfs_rootsize; /* size of mini-root in bytes */
72 1.1 mycroft
73 1.1 mycroft static int mfs_minor; /* used for building internal dev_t */
74 1.1 mycroft
75 1.10 christos extern int (**mfs_vnodeop_p) __P((void *));
76 1.1 mycroft
77 1.1 mycroft /*
78 1.1 mycroft * mfs vfs operations.
79 1.1 mycroft */
80 1.14 thorpej
81 1.31 jdolecek extern const struct vnodeopv_desc mfs_vnodeop_opv_desc;
82 1.14 thorpej
83 1.31 jdolecek const struct vnodeopv_desc * const mfs_vnodeopv_descs[] = {
84 1.14 thorpej &mfs_vnodeop_opv_desc,
85 1.14 thorpej NULL,
86 1.14 thorpej };
87 1.14 thorpej
88 1.1 mycroft struct vfsops mfs_vfsops = {
89 1.1 mycroft MOUNT_MFS,
90 1.1 mycroft mfs_mount,
91 1.1 mycroft mfs_start,
92 1.1 mycroft ffs_unmount,
93 1.1 mycroft ufs_root,
94 1.1 mycroft ufs_quotactl,
95 1.1 mycroft mfs_statfs,
96 1.1 mycroft ffs_sync,
97 1.1 mycroft ffs_vget,
98 1.1 mycroft ffs_fhtovp,
99 1.1 mycroft ffs_vptofh,
100 1.1 mycroft mfs_init,
101 1.32.2.3 nathanw mfs_reinit,
102 1.23 jdolecek mfs_done,
103 1.15 fvdl ffs_sysctl,
104 1.16 fvdl NULL,
105 1.19 wrstuden ufs_check_export,
106 1.14 thorpej mfs_vnodeopv_descs,
107 1.1 mycroft };
108 1.1 mycroft
109 1.15 fvdl /*
110 1.15 fvdl * Memory based filesystem initialization.
111 1.15 fvdl */
112 1.15 fvdl void
113 1.15 fvdl mfs_init()
114 1.15 fvdl {
115 1.23 jdolecek /*
116 1.23 jdolecek * ffs_init() ensures to initialize necessary resources
117 1.23 jdolecek * only once.
118 1.23 jdolecek */
119 1.23 jdolecek ffs_init();
120 1.32.2.3 nathanw }
121 1.32.2.3 nathanw
122 1.32.2.3 nathanw void
123 1.32.2.3 nathanw mfs_reinit()
124 1.32.2.3 nathanw {
125 1.32.2.3 nathanw ffs_reinit();
126 1.15 fvdl }
127 1.15 fvdl
128 1.23 jdolecek void
129 1.23 jdolecek mfs_done()
130 1.23 jdolecek {
131 1.23 jdolecek /*
132 1.23 jdolecek * ffs_done() ensures to free necessary resources
133 1.23 jdolecek * only once, when it's no more needed.
134 1.23 jdolecek */
135 1.23 jdolecek ffs_done();
136 1.23 jdolecek }
137 1.15 fvdl
138 1.1 mycroft /*
139 1.1 mycroft * Called by main() when mfs is going to be mounted as root.
140 1.1 mycroft */
141 1.1 mycroft
142 1.10 christos int
143 1.1 mycroft mfs_mountroot()
144 1.1 mycroft {
145 1.15 fvdl struct fs *fs;
146 1.15 fvdl struct mount *mp;
147 1.32.2.8 nathanw struct proc *p = curproc; /* XXX */
148 1.1 mycroft struct ufsmount *ump;
149 1.1 mycroft struct mfsnode *mfsp;
150 1.15 fvdl int error = 0;
151 1.1 mycroft
152 1.1 mycroft /*
153 1.13 mrg * Get vnodes for rootdev.
154 1.1 mycroft */
155 1.15 fvdl if (bdevvp(rootdev, &rootvp)) {
156 1.15 fvdl printf("mfs_mountroot: can't setup bdevvp's");
157 1.15 fvdl return (error);
158 1.15 fvdl }
159 1.15 fvdl
160 1.21 wrstuden if ((error = vfs_rootmountalloc(MOUNT_MFS, "mfs_root", &mp))) {
161 1.21 wrstuden vrele(rootvp);
162 1.15 fvdl return (error);
163 1.21 wrstuden }
164 1.1 mycroft
165 1.1 mycroft mfsp = malloc(sizeof *mfsp, M_MFSNODE, M_WAITOK);
166 1.1 mycroft rootvp->v_data = mfsp;
167 1.1 mycroft rootvp->v_op = mfs_vnodeop_p;
168 1.1 mycroft rootvp->v_tag = VT_MFS;
169 1.1 mycroft mfsp->mfs_baseoff = mfs_rootbase;
170 1.1 mycroft mfsp->mfs_size = mfs_rootsize;
171 1.1 mycroft mfsp->mfs_vnode = rootvp;
172 1.26 thorpej mfsp->mfs_proc = NULL; /* indicate kernel space */
173 1.22 thorpej BUFQ_INIT(&mfsp->mfs_buflist);
174 1.10 christos if ((error = ffs_mountfs(rootvp, mp, p)) != 0) {
175 1.15 fvdl mp->mnt_op->vfs_refcount--;
176 1.15 fvdl vfs_unbusy(mp);
177 1.1 mycroft free(mp, M_MOUNT);
178 1.1 mycroft free(mfsp, M_MFSNODE);
179 1.21 wrstuden vrele(rootvp);
180 1.1 mycroft return (error);
181 1.15 fvdl }
182 1.15 fvdl simple_lock(&mountlist_slock);
183 1.4 mycroft CIRCLEQ_INSERT_TAIL(&mountlist, mp, mnt_list);
184 1.15 fvdl simple_unlock(&mountlist_slock);
185 1.1 mycroft mp->mnt_vnodecovered = NULLVP;
186 1.1 mycroft ump = VFSTOUFS(mp);
187 1.1 mycroft fs = ump->um_fs;
188 1.15 fvdl (void) copystr(mp->mnt_stat.f_mntonname, fs->fs_fsmnt, MNAMELEN - 1, 0);
189 1.1 mycroft (void)ffs_statfs(mp, &mp->mnt_stat, p);
190 1.15 fvdl vfs_unbusy(mp);
191 1.1 mycroft inittodr((time_t)0);
192 1.1 mycroft return (0);
193 1.1 mycroft }
194 1.1 mycroft
195 1.1 mycroft /*
196 1.1 mycroft * This is called early in boot to set the base address and size
197 1.1 mycroft * of the mini-root.
198 1.1 mycroft */
199 1.10 christos int
200 1.1 mycroft mfs_initminiroot(base)
201 1.1 mycroft caddr_t base;
202 1.1 mycroft {
203 1.1 mycroft struct fs *fs = (struct fs *)(base + SBOFF);
204 1.1 mycroft
205 1.1 mycroft /* check for valid super block */
206 1.1 mycroft if (fs->fs_magic != FS_MAGIC || fs->fs_bsize > MAXBSIZE ||
207 1.1 mycroft fs->fs_bsize < sizeof(struct fs))
208 1.1 mycroft return (0);
209 1.1 mycroft mountroot = mfs_mountroot;
210 1.1 mycroft mfs_rootbase = base;
211 1.1 mycroft mfs_rootsize = fs->fs_fsize * fs->fs_size;
212 1.32 cgd rootdev = makedev(255, mfs_minor);
213 1.32 cgd mfs_minor++;
214 1.1 mycroft return (mfs_rootsize);
215 1.1 mycroft }
216 1.1 mycroft
217 1.1 mycroft /*
218 1.1 mycroft * VFS Operations.
219 1.1 mycroft *
220 1.1 mycroft * mount system call
221 1.1 mycroft */
222 1.1 mycroft /* ARGSUSED */
223 1.1 mycroft int
224 1.1 mycroft mfs_mount(mp, path, data, ndp, p)
225 1.25 augustss struct mount *mp;
226 1.11 cgd const char *path;
227 1.11 cgd void *data;
228 1.1 mycroft struct nameidata *ndp;
229 1.1 mycroft struct proc *p;
230 1.1 mycroft {
231 1.1 mycroft struct vnode *devvp;
232 1.1 mycroft struct mfs_args args;
233 1.1 mycroft struct ufsmount *ump;
234 1.25 augustss struct fs *fs;
235 1.25 augustss struct mfsnode *mfsp;
236 1.7 mycroft size_t size;
237 1.1 mycroft int flags, error;
238 1.32.2.5 nathanw
239 1.32.2.5 nathanw /*
240 1.32.2.5 nathanw * XXX turn off async to avoid hangs when writing lots of data.
241 1.32.2.5 nathanw * the problem is that MFS needs to allocate pages to clean pages,
242 1.32.2.5 nathanw * so if we wait until the last minute to clean pages then there
243 1.32.2.5 nathanw * may not be any pages available to do the cleaning.
244 1.32.2.5 nathanw */
245 1.32.2.5 nathanw mp->mnt_flag &= ~MNT_ASYNC;
246 1.1 mycroft
247 1.10 christos error = copyin(data, (caddr_t)&args, sizeof (struct mfs_args));
248 1.10 christos if (error)
249 1.1 mycroft return (error);
250 1.1 mycroft
251 1.1 mycroft /*
252 1.1 mycroft * If updating, check whether changing from read-only to
253 1.1 mycroft * read/write; if there is no device name, that's all we do.
254 1.1 mycroft */
255 1.1 mycroft if (mp->mnt_flag & MNT_UPDATE) {
256 1.1 mycroft ump = VFSTOUFS(mp);
257 1.1 mycroft fs = ump->um_fs;
258 1.1 mycroft if (fs->fs_ronly == 0 && (mp->mnt_flag & MNT_RDONLY)) {
259 1.1 mycroft flags = WRITECLOSE;
260 1.1 mycroft if (mp->mnt_flag & MNT_FORCE)
261 1.1 mycroft flags |= FORCECLOSE;
262 1.1 mycroft error = ffs_flushfiles(mp, flags, p);
263 1.1 mycroft if (error)
264 1.1 mycroft return (error);
265 1.1 mycroft }
266 1.1 mycroft if (fs->fs_ronly && (mp->mnt_flag & MNT_WANTRDWR))
267 1.1 mycroft fs->fs_ronly = 0;
268 1.1 mycroft if (args.fspec == 0)
269 1.1 mycroft return (vfs_export(mp, &ump->um_export, &args.export));
270 1.1 mycroft return (0);
271 1.1 mycroft }
272 1.1 mycroft error = getnewvnode(VT_MFS, (struct mount *)0, mfs_vnodeop_p, &devvp);
273 1.1 mycroft if (error)
274 1.1 mycroft return (error);
275 1.1 mycroft devvp->v_type = VBLK;
276 1.32 cgd if (checkalias(devvp, makedev(255, mfs_minor), (struct mount *)0))
277 1.1 mycroft panic("mfs_mount: dup dev");
278 1.32 cgd mfs_minor++;
279 1.1 mycroft mfsp = (struct mfsnode *)malloc(sizeof *mfsp, M_MFSNODE, M_WAITOK);
280 1.1 mycroft devvp->v_data = mfsp;
281 1.1 mycroft mfsp->mfs_baseoff = args.base;
282 1.1 mycroft mfsp->mfs_size = args.size;
283 1.1 mycroft mfsp->mfs_vnode = devvp;
284 1.26 thorpej mfsp->mfs_proc = p;
285 1.22 thorpej BUFQ_INIT(&mfsp->mfs_buflist);
286 1.10 christos if ((error = ffs_mountfs(devvp, mp, p)) != 0) {
287 1.28 thorpej BUFQ_FIRST(&mfsp->mfs_buflist) = (struct buf *) -1;
288 1.1 mycroft vrele(devvp);
289 1.1 mycroft return (error);
290 1.1 mycroft }
291 1.1 mycroft ump = VFSTOUFS(mp);
292 1.1 mycroft fs = ump->um_fs;
293 1.1 mycroft (void) copyinstr(path, fs->fs_fsmnt, sizeof(fs->fs_fsmnt) - 1, &size);
294 1.18 perry memset(fs->fs_fsmnt + size, 0, sizeof(fs->fs_fsmnt) - size);
295 1.18 perry memcpy(mp->mnt_stat.f_mntonname, fs->fs_fsmnt, MNAMELEN);
296 1.1 mycroft (void) copyinstr(args.fspec, mp->mnt_stat.f_mntfromname, MNAMELEN - 1,
297 1.5 mycroft &size);
298 1.18 perry memset(mp->mnt_stat.f_mntfromname + size, 0, MNAMELEN - size);
299 1.1 mycroft return (0);
300 1.1 mycroft }
301 1.1 mycroft
302 1.1 mycroft int mfs_pri = PWAIT | PCATCH; /* XXX prob. temp */
303 1.1 mycroft
304 1.1 mycroft /*
305 1.1 mycroft * Used to grab the process and keep it in the kernel to service
306 1.1 mycroft * memory filesystem I/O requests.
307 1.1 mycroft *
308 1.28 thorpej * Loop servicing I/O requests.
309 1.28 thorpej * Copy the requested data into or out of the memory filesystem
310 1.28 thorpej * address space.
311 1.1 mycroft */
312 1.1 mycroft /* ARGSUSED */
313 1.1 mycroft int
314 1.1 mycroft mfs_start(mp, flags, p)
315 1.1 mycroft struct mount *mp;
316 1.1 mycroft int flags;
317 1.1 mycroft struct proc *p;
318 1.1 mycroft {
319 1.25 augustss struct vnode *vp = VFSTOUFS(mp)->um_devvp;
320 1.25 augustss struct mfsnode *mfsp = VTOMFS(vp);
321 1.28 thorpej struct buf *bp;
322 1.28 thorpej caddr_t base;
323 1.15 fvdl int sleepreturn = 0;
324 1.32.2.1 nathanw struct lwp *l; /* XXX NJWLWP */
325 1.1 mycroft
326 1.32.2.1 nathanw /* XXX NJWLWP the vnode interface again gives us a proc in a
327 1.32.2.1 nathanw * place where we want a execution context. Cheat.
328 1.32.2.1 nathanw */
329 1.32.2.8 nathanw KASSERT(curproc == p);
330 1.32.2.8 nathanw l = curlwp;
331 1.28 thorpej base = mfsp->mfs_baseoff;
332 1.28 thorpej while (BUFQ_FIRST(&mfsp->mfs_buflist) != (struct buf *) -1) {
333 1.30 simonb while ((bp = BUFQ_FIRST(&mfsp->mfs_buflist)) != NULL) {
334 1.30 simonb BUFQ_REMOVE(&mfsp->mfs_buflist, bp);
335 1.30 simonb mfs_doio(bp, base);
336 1.30 simonb wakeup((caddr_t)bp);
337 1.30 simonb }
338 1.1 mycroft /*
339 1.1 mycroft * If a non-ignored signal is received, try to unmount.
340 1.15 fvdl * If that fails, or the filesystem is already in the
341 1.15 fvdl * process of being unmounted, clear the signal (it has been
342 1.15 fvdl * "processed"), otherwise we will loop here, as tsleep
343 1.32.2.7 nathanw * will always return EINTR/ERESTART.
344 1.32.2.7 nathanw */
345 1.15 fvdl if (sleepreturn != 0) {
346 1.32.2.2 nathanw /*
347 1.32.2.2 nathanw * XXX Freeze syncer. Must do this before locking
348 1.32.2.2 nathanw * the mount point. See dounmount() for details.
349 1.32.2.2 nathanw */
350 1.32.2.2 nathanw lockmgr(&syncer_lock, LK_EXCLUSIVE, NULL);
351 1.32.2.2 nathanw if (vfs_busy(mp, LK_NOWAIT, 0) != 0)
352 1.32.2.2 nathanw lockmgr(&syncer_lock, LK_RELEASE, NULL);
353 1.32.2.2 nathanw else if (dounmount(mp, 0, p) != 0)
354 1.32.2.1 nathanw CLRSIG(p, CURSIG(l));
355 1.15 fvdl sleepreturn = 0;
356 1.12 fvdl continue;
357 1.9 mycroft }
358 1.28 thorpej
359 1.28 thorpej sleepreturn = tsleep(vp, mfs_pri, "mfsidl", 0);
360 1.1 mycroft }
361 1.15 fvdl return (sleepreturn);
362 1.1 mycroft }
363 1.1 mycroft
364 1.1 mycroft /*
365 1.1 mycroft * Get file system statistics.
366 1.1 mycroft */
367 1.10 christos int
368 1.1 mycroft mfs_statfs(mp, sbp, p)
369 1.1 mycroft struct mount *mp;
370 1.1 mycroft struct statfs *sbp;
371 1.1 mycroft struct proc *p;
372 1.1 mycroft {
373 1.1 mycroft int error;
374 1.1 mycroft
375 1.1 mycroft error = ffs_statfs(mp, sbp, p);
376 1.1 mycroft #ifdef COMPAT_09
377 1.1 mycroft sbp->f_type = 3;
378 1.1 mycroft #else
379 1.1 mycroft sbp->f_type = 0;
380 1.1 mycroft #endif
381 1.1 mycroft strncpy(&sbp->f_fstypename[0], mp->mnt_op->vfs_name, MFSNAMELEN);
382 1.1 mycroft return (error);
383 1.1 mycroft }
384