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