xref: /openbsd/sys/ufs/mfs/mfs_vfsops.c (revision 1821443c)
1 /*	$OpenBSD: mfs_vfsops.c,v 1.26 2003/08/14 07:46:40 mickey Exp $	*/
2 /*	$NetBSD: mfs_vfsops.c,v 1.10 1996/02/09 22:31:28 christos Exp $	*/
3 
4 /*
5  * Copyright (c) 1989, 1990, 1993, 1994
6  *	The Regents of the University of California.  All rights reserved.
7  *
8  * Redistribution and use in source and binary forms, with or without
9  * modification, are permitted provided that the following conditions
10  * are met:
11  * 1. Redistributions of source code must retain the above copyright
12  *    notice, this list of conditions and the following disclaimer.
13  * 2. Redistributions in binary form must reproduce the above copyright
14  *    notice, this list of conditions and the following disclaimer in the
15  *    documentation and/or other materials provided with the distribution.
16  * 3. Neither the name of the University nor the names of its contributors
17  *    may be used to endorse or promote products derived from this software
18  *    without specific prior written permission.
19  *
20  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
21  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
22  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
23  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
24  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
25  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
26  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
27  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
28  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
29  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
30  * SUCH DAMAGE.
31  *
32  *	@(#)mfs_vfsops.c	8.4 (Berkeley) 4/16/94
33  */
34 
35 #include <sys/param.h>
36 #include <sys/systm.h>
37 #include <sys/time.h>
38 #include <sys/kernel.h>
39 #include <sys/proc.h>
40 #include <sys/buf.h>
41 #include <sys/mount.h>
42 #include <sys/signalvar.h>
43 #include <sys/vnode.h>
44 #include <sys/malloc.h>
45 #include <sys/kthread.h>
46 
47 #include <ufs/ufs/extattr.h>
48 #include <ufs/ufs/quota.h>
49 #include <ufs/ufs/inode.h>
50 #include <ufs/ufs/ufsmount.h>
51 #include <ufs/ufs/ufs_extern.h>
52 
53 #include <ufs/ffs/fs.h>
54 #include <ufs/ffs/ffs_extern.h>
55 
56 #include <ufs/mfs/mfsnode.h>
57 #include <ufs/mfs/mfs_extern.h>
58 
59 caddr_t	mfs_rootbase;	/* address of mini-root in kernel virtual memory */
60 u_long	mfs_rootsize;	/* size of mini-root in bytes */
61 
62 static	int mfs_minor;	/* used for building internal dev_t */
63 
64 extern int (**mfs_vnodeop_p)(void *);
65 
66 /*
67  * mfs vfs operations.
68  */
69 const struct vfsops mfs_vfsops = {
70 	mfs_mount,
71 	mfs_start,
72 	ffs_unmount,
73 	ufs_root,
74 	ufs_quotactl,
75 	mfs_statfs,
76 	ffs_sync,
77 	ffs_vget,
78 	ffs_fhtovp,
79 	ffs_vptofh,
80 	mfs_init,
81 	ffs_sysctl,
82 	mfs_checkexp,
83 	vfs_stdextattrctl
84 };
85 
86 /*
87  * Called by main() when mfs is going to be mounted as root.
88  */
89 
90 int
91 mfs_mountroot()
92 {
93 	extern struct vnode *rootvp;
94 	register struct fs *fs;
95 	struct mount *mp;
96 	struct proc *p = curproc;	/* XXX */
97 	struct ufsmount *ump;
98 	struct mfsnode *mfsp;
99 	int error;
100 
101 	if ((error = bdevvp(swapdev, &swapdev_vp)) ||
102 	    (error = bdevvp(rootdev, &rootvp))) {
103 		printf("mfs_mountroot: can't setup bdevvp's");
104 		return (error);
105 	}
106 	if ((error = vfs_rootmountalloc("mfs", "mfs_root", &mp)) != 0)
107 		return (error);
108 	mfsp = malloc(sizeof *mfsp, M_MFSNODE, M_WAITOK);
109 	rootvp->v_data = mfsp;
110 	rootvp->v_op = mfs_vnodeop_p;
111 	rootvp->v_tag = VT_MFS;
112 	mfsp->mfs_baseoff = mfs_rootbase;
113 	mfsp->mfs_size = mfs_rootsize;
114 	mfsp->mfs_vnode = rootvp;
115 	mfsp->mfs_pid = p->p_pid;
116 	mfsp->mfs_buflist = (struct buf *)0;
117 	if ((error = ffs_mountfs(rootvp, mp, p)) != 0) {
118 		mp->mnt_vfc->vfc_refcount--;
119 		vfs_unbusy(mp, p);
120 		free(mp, M_MOUNT);
121 		free(mfsp, M_MFSNODE);
122 		return (error);
123 	}
124 	simple_lock(&mountlist_slock);
125 	CIRCLEQ_INSERT_TAIL(&mountlist, mp, mnt_list);
126 	simple_unlock(&mountlist_slock);
127 	ump = VFSTOUFS(mp);
128 	fs = ump->um_fs;
129 	(void) copystr(mp->mnt_stat.f_mntonname, fs->fs_fsmnt, MNAMELEN - 1, 0);
130 	(void)ffs_statfs(mp, &mp->mnt_stat, p);
131 	vfs_unbusy(mp, p);
132 	inittodr((time_t)0);
133 	return (0);
134 }
135 
136 /*
137  * This is called early in boot to set the base address and size
138  * of the mini-root.
139  */
140 int
141 mfs_initminiroot(base)
142 	caddr_t base;
143 {
144 	struct fs *fs = (struct fs *)(base + SBOFF);
145 	extern int (*mountroot)(void);
146 
147 	/* check for valid super block */
148 	if (fs->fs_magic != FS_MAGIC || fs->fs_bsize > MAXBSIZE ||
149 	    fs->fs_bsize < sizeof(struct fs))
150 		return (0);
151 	mountroot = mfs_mountroot;
152 	mfs_rootbase = base;
153 	mfs_rootsize = fs->fs_fsize * fs->fs_size;
154 	rootdev = makedev(255, mfs_minor);
155 	mfs_minor++;
156 	return (mfs_rootsize);
157 }
158 
159 /*
160  * VFS Operations.
161  *
162  * mount system call
163  */
164 /* ARGSUSED */
165 int
166 mfs_mount(mp, path, data, ndp, p)
167 	register struct mount *mp;
168 	const char *path;
169 	void *data;
170 	struct nameidata *ndp;
171 	struct proc *p;
172 {
173 	struct vnode *devvp;
174 	struct mfs_args args;
175 	struct ufsmount *ump;
176 	register struct fs *fs;
177 	register struct mfsnode *mfsp;
178 	size_t size;
179 	int flags, error;
180 
181 	error = copyin(data, (caddr_t)&args, sizeof (struct mfs_args));
182 	if (error)
183 		return (error);
184 
185 	/*
186 	 * If updating, check whether changing from read-only to
187 	 * read/write; if there is no device name, that's all we do.
188 	 */
189 	if (mp->mnt_flag & MNT_UPDATE) {
190 		ump = VFSTOUFS(mp);
191 		fs = ump->um_fs;
192 		if (fs->fs_ronly == 0 && (mp->mnt_flag & MNT_RDONLY)) {
193 			flags = WRITECLOSE;
194 			if (mp->mnt_flag & MNT_FORCE)
195 				flags |= FORCECLOSE;
196 			error = ffs_flushfiles(mp, flags, p);
197 			if (error)
198 				return (error);
199 		}
200 		if (fs->fs_ronly && (mp->mnt_flag & MNT_WANTRDWR))
201 			fs->fs_ronly = 0;
202 #ifdef EXPORTMFS
203 		if (args.fspec == 0)
204 			return (vfs_export(mp, &ump->um_export,
205 			    &args.export_info));
206 #endif
207 		return (0);
208 	}
209 	error = getnewvnode(VT_MFS, (struct mount *)0, mfs_vnodeop_p, &devvp);
210 	if (error)
211 		return (error);
212 	devvp->v_type = VBLK;
213 	if (checkalias(devvp, makedev(255, mfs_minor), (struct mount *)0))
214 		panic("mfs_mount: dup dev");
215 	mfs_minor++;
216 	mfsp = malloc(sizeof *mfsp, M_MFSNODE, M_WAITOK);
217 	devvp->v_data = mfsp;
218 	mfsp->mfs_baseoff = args.base;
219 	mfsp->mfs_size = args.size;
220 	mfsp->mfs_vnode = devvp;
221 	mfsp->mfs_pid = p->p_pid;
222 	mfsp->mfs_buflist = (struct buf *)0;
223 	if ((error = ffs_mountfs(devvp, mp, p)) != 0) {
224 		mfsp->mfs_buflist = (struct buf *)-1;
225 		vrele(devvp);
226 		return (error);
227 	}
228 	ump = VFSTOUFS(mp);
229 	fs = ump->um_fs;
230 	(void) copyinstr(path, fs->fs_fsmnt, sizeof(fs->fs_fsmnt) - 1, &size);
231 	bzero(fs->fs_fsmnt + size, sizeof(fs->fs_fsmnt) - size);
232 	bcopy(fs->fs_fsmnt, mp->mnt_stat.f_mntonname, MNAMELEN);
233 	(void) copyinstr(args.fspec, mp->mnt_stat.f_mntfromname, MNAMELEN - 1,
234 	    &size);
235 	bzero(mp->mnt_stat.f_mntfromname + size, MNAMELEN - size);
236 	bcopy(&args, &mp->mnt_stat.mount_info.mfs_args, sizeof(args));
237 	return (0);
238 }
239 
240 int	mfs_pri = PWAIT | PCATCH;		/* XXX prob. temp */
241 
242 /*
243  * Used to grab the process and keep it in the kernel to service
244  * memory filesystem I/O requests.
245  *
246  * Loop servicing I/O requests.
247  * Copy the requested data into or out of the memory filesystem
248  * address space.
249  */
250 /* ARGSUSED */
251 int
252 mfs_start(mp, flags, p)
253 	struct mount *mp;
254 	int flags;
255 	struct proc *p;
256 {
257 	struct vnode *vp = VFSTOUFS(mp)->um_devvp;
258 	struct mfsnode *mfsp = VTOMFS(vp);
259 	struct buf *bp;
260 	caddr_t base;
261 	int sleepreturn = 0;
262 
263 	base = mfsp->mfs_baseoff;
264 	while (mfsp->mfs_buflist != (struct buf *)-1) {
265 		while ((bp = mfsp->mfs_buflist) != NULL) {
266 			mfsp->mfs_buflist = bp->b_actf;
267 			mfs_doio(bp, base);
268 			wakeup((caddr_t)bp);
269 		}
270 		/*
271 		 * If a non-ignored signal is received, try to unmount.
272 		 * If that fails, clear the signal (it has been "processed"),
273 		 * otherwise we will loop here, as tsleep will always return
274 		 * EINTR/ERESTART.
275 		 */
276 		if (sleepreturn != 0) {
277 			if (vfs_busy(mp, LK_EXCLUSIVE|LK_NOWAIT, NULL, p) ||
278 			    dounmount(mp, 0, p, NULL))
279 				CLRSIG(p, CURSIG(p));
280 			sleepreturn = 0;
281 			continue;
282 		}
283 		sleepreturn = tsleep((caddr_t)vp, mfs_pri, "mfsidl", 0);
284 	}
285 	return (0);
286 }
287 
288 /*
289  * Get file system statistics.
290  */
291 int
292 mfs_statfs(mp, sbp, p)
293 	struct mount *mp;
294 	struct statfs *sbp;
295 	struct proc *p;
296 {
297 	int error;
298 
299 	error = ffs_statfs(mp, sbp, p);
300 	strncpy(&sbp->f_fstypename[0], mp->mnt_vfc->vfc_name, MFSNAMELEN);
301 	if (sbp != &mp->mnt_stat)
302 		bcopy(&mp->mnt_stat.mount_info.mfs_args,
303 		    &sbp->mount_info.mfs_args, sizeof(struct mfs_args));
304 	return (error);
305 }
306 
307 /*
308  * check export permission, not supported
309  */
310 /* ARGUSED */
311 int
312 mfs_checkexp(mp, nam, exflagsp, credanonp)
313 	register struct mount *mp;
314 	struct mbuf *nam;
315 	int *exflagsp;
316 	struct ucred **credanonp;
317 {
318 	return (EOPNOTSUPP);
319 }
320 
321 /*
322  * Memory based filesystem initialization.
323  */
324 int
325 mfs_init(vfsp)
326 	struct vfsconf *vfsp;
327 {
328 	return (ffs_init(vfsp));
329 }
330