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