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