mfs_vfsops.c revision 1.31
1/*	$NetBSD: mfs_vfsops.c,v 1.31 2001/01/22 12:17:43 jdolecek 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#if defined(_KERNEL) && !defined(_LKM)
39#include "opt_compat_netbsd.h"
40#endif
41
42#include <sys/param.h>
43#include <sys/systm.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 <ufs/ufs/quota.h>
54#include <ufs/ufs/inode.h>
55#include <ufs/ufs/ufsmount.h>
56#include <ufs/ufs/ufs_extern.h>
57
58#include <ufs/ffs/fs.h>
59#include <ufs/ffs/ffs_extern.h>
60
61#include <ufs/mfs/mfsnode.h>
62#include <ufs/mfs/mfs_extern.h>
63
64caddr_t	mfs_rootbase;	/* address of mini-root in kernel virtual memory */
65u_long	mfs_rootsize;	/* size of mini-root in bytes */
66
67static	int mfs_minor;	/* used for building internal dev_t */
68
69extern int (**mfs_vnodeop_p) __P((void *));
70
71/*
72 * mfs vfs operations.
73 */
74
75extern const struct vnodeopv_desc mfs_vnodeop_opv_desc;
76
77const struct vnodeopv_desc * const mfs_vnodeopv_descs[] = {
78	&mfs_vnodeop_opv_desc,
79	NULL,
80};
81
82struct vfsops mfs_vfsops = {
83	MOUNT_MFS,
84	mfs_mount,
85	mfs_start,
86	ffs_unmount,
87	ufs_root,
88	ufs_quotactl,
89	mfs_statfs,
90	ffs_sync,
91	ffs_vget,
92	ffs_fhtovp,
93	ffs_vptofh,
94	mfs_init,
95	mfs_done,
96	ffs_sysctl,
97	NULL,
98	ufs_check_export,
99	mfs_vnodeopv_descs,
100};
101
102/*
103 * Memory based filesystem initialization.
104 */
105void
106mfs_init()
107{
108	/*
109	 * ffs_init() ensures to initialize necessary resources
110	 * only once.
111	 */
112	ffs_init();
113}
114
115void
116mfs_done()
117{
118	/*
119	 * ffs_done() ensures to free necessary resources
120	 * only once, when it's no more needed.
121	 */
122	ffs_done();
123}
124
125/*
126 * Called by main() when mfs is going to be mounted as root.
127 */
128
129int
130mfs_mountroot()
131{
132	struct fs *fs;
133	struct mount *mp;
134	struct proc *p = curproc;	/* XXX */
135	struct ufsmount *ump;
136	struct mfsnode *mfsp;
137	int error = 0;
138
139	/*
140	 * Get vnodes for rootdev.
141	 */
142	if (bdevvp(rootdev, &rootvp)) {
143		printf("mfs_mountroot: can't setup bdevvp's");
144		return (error);
145	}
146
147	if ((error = vfs_rootmountalloc(MOUNT_MFS, "mfs_root", &mp))) {
148		vrele(rootvp);
149		return (error);
150	}
151
152	mfsp = malloc(sizeof *mfsp, M_MFSNODE, M_WAITOK);
153	rootvp->v_data = mfsp;
154	rootvp->v_op = mfs_vnodeop_p;
155	rootvp->v_tag = VT_MFS;
156	mfsp->mfs_baseoff = mfs_rootbase;
157	mfsp->mfs_size = mfs_rootsize;
158	mfsp->mfs_vnode = rootvp;
159	mfsp->mfs_proc = NULL;		/* indicate kernel space */
160	BUFQ_INIT(&mfsp->mfs_buflist);
161	if ((error = ffs_mountfs(rootvp, mp, p)) != 0) {
162		mp->mnt_op->vfs_refcount--;
163		vfs_unbusy(mp);
164		free(mp, M_MOUNT);
165		free(mfsp, M_MFSNODE);
166		vrele(rootvp);
167		return (error);
168	}
169	simple_lock(&mountlist_slock);
170	CIRCLEQ_INSERT_TAIL(&mountlist, mp, mnt_list);
171	simple_unlock(&mountlist_slock);
172	mp->mnt_vnodecovered = NULLVP;
173	ump = VFSTOUFS(mp);
174	fs = ump->um_fs;
175	(void) copystr(mp->mnt_stat.f_mntonname, fs->fs_fsmnt, MNAMELEN - 1, 0);
176	(void)ffs_statfs(mp, &mp->mnt_stat, p);
177	vfs_unbusy(mp);
178	inittodr((time_t)0);
179	return (0);
180}
181
182/*
183 * This is called early in boot to set the base address and size
184 * of the mini-root.
185 */
186int
187mfs_initminiroot(base)
188	caddr_t base;
189{
190	struct fs *fs = (struct fs *)(base + SBOFF);
191	extern int (*mountroot) __P((void));
192
193	/* check for valid super block */
194	if (fs->fs_magic != FS_MAGIC || fs->fs_bsize > MAXBSIZE ||
195	    fs->fs_bsize < sizeof(struct fs))
196		return (0);
197	mountroot = mfs_mountroot;
198	mfs_rootbase = base;
199	mfs_rootsize = fs->fs_fsize * fs->fs_size;
200	rootdev = makedev(255, mfs_minor++);
201	return (mfs_rootsize);
202}
203
204/*
205 * VFS Operations.
206 *
207 * mount system call
208 */
209/* ARGSUSED */
210int
211mfs_mount(mp, path, data, ndp, p)
212	struct mount *mp;
213	const char *path;
214	void *data;
215	struct nameidata *ndp;
216	struct proc *p;
217{
218	struct vnode *devvp;
219	struct mfs_args args;
220	struct ufsmount *ump;
221	struct fs *fs;
222	struct mfsnode *mfsp;
223	size_t size;
224	int flags, error;
225
226	error = copyin(data, (caddr_t)&args, sizeof (struct mfs_args));
227	if (error)
228		return (error);
229
230	/*
231	 * If updating, check whether changing from read-only to
232	 * read/write; if there is no device name, that's all we do.
233	 */
234	if (mp->mnt_flag & MNT_UPDATE) {
235		ump = VFSTOUFS(mp);
236		fs = ump->um_fs;
237		if (fs->fs_ronly == 0 && (mp->mnt_flag & MNT_RDONLY)) {
238			flags = WRITECLOSE;
239			if (mp->mnt_flag & MNT_FORCE)
240				flags |= FORCECLOSE;
241			error = ffs_flushfiles(mp, flags, p);
242			if (error)
243				return (error);
244		}
245		if (fs->fs_ronly && (mp->mnt_flag & MNT_WANTRDWR))
246			fs->fs_ronly = 0;
247		if (args.fspec == 0)
248			return (vfs_export(mp, &ump->um_export, &args.export));
249		return (0);
250	}
251	error = getnewvnode(VT_MFS, (struct mount *)0, mfs_vnodeop_p, &devvp);
252	if (error)
253		return (error);
254	devvp->v_type = VBLK;
255	if (checkalias(devvp, makedev(255, mfs_minor++), (struct mount *)0))
256		panic("mfs_mount: dup dev");
257	mfsp = (struct mfsnode *)malloc(sizeof *mfsp, M_MFSNODE, M_WAITOK);
258	devvp->v_data = mfsp;
259	mfsp->mfs_baseoff = args.base;
260	mfsp->mfs_size = args.size;
261	mfsp->mfs_vnode = devvp;
262	mfsp->mfs_proc = p;
263	BUFQ_INIT(&mfsp->mfs_buflist);
264	if ((error = ffs_mountfs(devvp, mp, p)) != 0) {
265		BUFQ_FIRST(&mfsp->mfs_buflist) = (struct buf *) -1;
266		vrele(devvp);
267		return (error);
268	}
269	ump = VFSTOUFS(mp);
270	fs = ump->um_fs;
271	(void) copyinstr(path, fs->fs_fsmnt, sizeof(fs->fs_fsmnt) - 1, &size);
272	memset(fs->fs_fsmnt + size, 0, sizeof(fs->fs_fsmnt) - size);
273	memcpy(mp->mnt_stat.f_mntonname, fs->fs_fsmnt, MNAMELEN);
274	(void) copyinstr(args.fspec, mp->mnt_stat.f_mntfromname, MNAMELEN - 1,
275	    &size);
276	memset(mp->mnt_stat.f_mntfromname + size, 0, MNAMELEN - size);
277	return (0);
278}
279
280int	mfs_pri = PWAIT | PCATCH;		/* XXX prob. temp */
281
282/*
283 * Used to grab the process and keep it in the kernel to service
284 * memory filesystem I/O requests.
285 *
286 * Loop servicing I/O requests.
287 * Copy the requested data into or out of the memory filesystem
288 * address space.
289 */
290/* ARGSUSED */
291int
292mfs_start(mp, flags, p)
293	struct mount *mp;
294	int flags;
295	struct proc *p;
296{
297	struct vnode *vp = VFSTOUFS(mp)->um_devvp;
298	struct mfsnode *mfsp = VTOMFS(vp);
299	struct buf *bp;
300	caddr_t base;
301	int sleepreturn = 0;
302
303	base = mfsp->mfs_baseoff;
304	while (BUFQ_FIRST(&mfsp->mfs_buflist) != (struct buf *) -1) {
305		while ((bp = BUFQ_FIRST(&mfsp->mfs_buflist)) != NULL) {
306			BUFQ_REMOVE(&mfsp->mfs_buflist, bp);
307			mfs_doio(bp, base);
308			wakeup((caddr_t)bp);
309		}
310		/*
311		 * If a non-ignored signal is received, try to unmount.
312		 * If that fails, or the filesystem is already in the
313		 * process of being unmounted, clear the signal (it has been
314		 * "processed"), otherwise we will loop here, as tsleep
315		 * will always return EINTR/ERESTART.
316		 */
317		if (sleepreturn != 0) {
318			if (vfs_busy(mp, LK_NOWAIT, 0) ||
319			    dounmount(mp, 0, p) != 0)
320				CLRSIG(p, CURSIG(p));
321			sleepreturn = 0;
322			continue;
323		}
324
325		sleepreturn = tsleep(vp, mfs_pri, "mfsidl", 0);
326	}
327	return (sleepreturn);
328}
329
330/*
331 * Get file system statistics.
332 */
333int
334mfs_statfs(mp, sbp, p)
335	struct mount *mp;
336	struct statfs *sbp;
337	struct proc *p;
338{
339	int error;
340
341	error = ffs_statfs(mp, sbp, p);
342#ifdef COMPAT_09
343	sbp->f_type = 3;
344#else
345	sbp->f_type = 0;
346#endif
347	strncpy(&sbp->f_fstypename[0], mp->mnt_op->vfs_name, MFSNAMELEN);
348	return (error);
349}
350