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