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