Fix memory barrier in a debug function
[netbsd-mini2440.git] / sys / ufs / mfs / mfs_vfsops.c
blob79a35ce78b500d3c7e39f15a7b955980a07bd234
1 /* $NetBSD: mfs_vfsops.c,v 1.100 2008/12/19 17:11:57 pgoyette Exp $ */
3 /*
4 * Copyright (c) 1989, 1990, 1993, 1994
5 * The Regents of the University of California. All rights reserved.
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.
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.
31 * @(#)mfs_vfsops.c 8.11 (Berkeley) 6/19/95
34 #include <sys/cdefs.h>
35 __KERNEL_RCSID(0, "$NetBSD: mfs_vfsops.c,v 1.100 2008/12/19 17:11:57 pgoyette Exp $");
37 #if defined(_KERNEL_OPT)
38 #include "opt_compat_netbsd.h"
39 #endif
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/kmem.h>
53 #include <sys/module.h>
55 #include <miscfs/genfs/genfs.h>
56 #include <miscfs/specfs/specdev.h>
58 #include <ufs/ufs/quota.h>
59 #include <ufs/ufs/inode.h>
60 #include <ufs/ufs/ufsmount.h>
61 #include <ufs/ufs/ufs_extern.h>
63 #include <ufs/ffs/fs.h>
64 #include <ufs/ffs/ffs_extern.h>
66 #include <ufs/mfs/mfsnode.h>
67 #include <ufs/mfs/mfs_extern.h>
69 MODULE(MODULE_CLASS_VFS, mfs, "ffs");
71 void * mfs_rootbase; /* address of mini-root in kernel virtual memory */
72 u_long mfs_rootsize; /* size of mini-root in bytes */
73 kmutex_t mfs_lock; /* global lock */
75 static int mfs_minor; /* used for building internal dev_t */
76 static int mfs_initcnt;
78 extern int (**mfs_vnodeop_p)(void *);
80 static struct sysctllog *mfs_sysctl_log;
83 * mfs vfs operations.
86 extern const struct vnodeopv_desc mfs_vnodeop_opv_desc;
88 const struct vnodeopv_desc * const mfs_vnodeopv_descs[] = {
89 &mfs_vnodeop_opv_desc,
90 NULL,
93 struct vfsops mfs_vfsops = {
94 MOUNT_MFS,
95 sizeof (struct mfs_args),
96 mfs_mount,
97 mfs_start,
98 ffs_unmount,
99 ufs_root,
100 ufs_quotactl,
101 mfs_statvfs,
102 ffs_sync,
103 ffs_vget,
104 ffs_fhtovp,
105 ffs_vptofh,
106 mfs_init,
107 mfs_reinit,
108 mfs_done,
109 NULL,
110 (int (*)(struct mount *, struct vnode *, struct timespec *)) eopnotsupp,
111 vfs_stdextattrctl,
112 (void *)eopnotsupp, /* vfs_suspendctl */
113 genfs_renamelock_enter,
114 genfs_renamelock_exit,
115 (void *)eopnotsupp,
116 mfs_vnodeopv_descs,
118 { NULL, NULL },
121 static int
122 mfs_modcmd(modcmd_t cmd, void *arg)
124 int error;
126 switch (cmd) {
127 case MODULE_CMD_INIT:
128 error = vfs_attach(&mfs_vfsops);
129 if (error != 0)
130 break;
131 sysctl_createv(&mfs_sysctl_log, 0, NULL, NULL,
132 CTLFLAG_PERMANENT,
133 CTLTYPE_NODE, "vfs", NULL,
134 NULL, 0, NULL, 0,
135 CTL_VFS, CTL_EOL);
136 sysctl_createv(&mfs_sysctl_log, 0, NULL, NULL,
137 CTLFLAG_PERMANENT|CTLFLAG_ALIAS,
138 CTLTYPE_NODE, "mfs",
139 SYSCTL_DESCR("Memory based file system"),
140 NULL, 1, NULL, 0,
141 CTL_VFS, 3, CTL_EOL);
143 * XXX the "1" and the "3" above could be dynamic, thereby
144 * eliminating one more instance of the "number to vfs"
145 * mapping problem, but they are in order as taken from
146 * sys/mount.h
148 break;
149 case MODULE_CMD_FINI:
150 error = vfs_detach(&mfs_vfsops);
151 if (error != 0)
152 break;
153 sysctl_teardown(&mfs_sysctl_log);
154 break;
155 default:
156 error = ENOTTY;
157 break;
160 return (error);
164 * Memory based filesystem initialization.
166 void
167 mfs_init(void)
170 if (mfs_initcnt++ == 0) {
171 mutex_init(&mfs_lock, MUTEX_DEFAULT, IPL_NONE);
172 ffs_init();
176 void
177 mfs_reinit(void)
180 ffs_reinit();
183 void
184 mfs_done(void)
187 if (--mfs_initcnt == 0) {
188 ffs_done();
189 mutex_destroy(&mfs_lock);
194 * Called by main() when mfs is going to be mounted as root.
198 mfs_mountroot(void)
200 struct fs *fs;
201 struct mount *mp;
202 struct lwp *l = curlwp; /* XXX */
203 struct ufsmount *ump;
204 struct mfsnode *mfsp;
205 int error = 0;
207 if ((error = vfs_rootmountalloc(MOUNT_MFS, "mfs_root", &mp))) {
208 vrele(rootvp);
209 return (error);
212 mfsp = kmem_alloc(sizeof(*mfsp), KM_SLEEP);
213 rootvp->v_data = mfsp;
214 rootvp->v_op = mfs_vnodeop_p;
215 rootvp->v_tag = VT_MFS;
216 mfsp->mfs_baseoff = mfs_rootbase;
217 mfsp->mfs_size = mfs_rootsize;
218 mfsp->mfs_vnode = rootvp;
219 mfsp->mfs_proc = NULL; /* indicate kernel space */
220 mfsp->mfs_shutdown = 0;
221 cv_init(&mfsp->mfs_cv, "mfs");
222 mfsp->mfs_refcnt = 1;
223 bufq_alloc(&mfsp->mfs_buflist, "fcfs", 0);
224 if ((error = ffs_mountfs(rootvp, mp, l)) != 0) {
225 vfs_unbusy(mp, false, NULL);
226 bufq_free(mfsp->mfs_buflist);
227 vfs_destroy(mp);
228 kmem_free(mfsp, sizeof(*mfsp));
229 return (error);
231 mutex_enter(&mountlist_lock);
232 CIRCLEQ_INSERT_TAIL(&mountlist, mp, mnt_list);
233 mutex_exit(&mountlist_lock);
234 mp->mnt_vnodecovered = NULLVP;
235 ump = VFSTOUFS(mp);
236 fs = ump->um_fs;
237 (void) copystr(mp->mnt_stat.f_mntonname, fs->fs_fsmnt, MNAMELEN - 1, 0);
238 (void)ffs_statvfs(mp, &mp->mnt_stat);
239 vfs_unbusy(mp, false, NULL);
240 return (0);
244 * This is called early in boot to set the base address and size
245 * of the mini-root.
248 mfs_initminiroot(void *base)
250 struct fs *fs = (struct fs *)((char *)base + SBLOCK_UFS1);
252 /* check for valid super block */
253 if (fs->fs_magic != FS_UFS1_MAGIC || fs->fs_bsize > MAXBSIZE ||
254 fs->fs_bsize < sizeof(struct fs))
255 return (0);
256 rootfstype = MOUNT_MFS;
257 mfs_rootbase = base;
258 mfs_rootsize = fs->fs_fsize * fs->fs_size;
259 rootdev = makedev(255, mfs_minor);
260 mfs_minor++;
261 return (mfs_rootsize);
265 * VFS Operations.
267 * mount system call
269 /* ARGSUSED */
271 mfs_mount(struct mount *mp, const char *path, void *data, size_t *data_len)
273 struct lwp *l = curlwp;
274 struct vnode *devvp;
275 struct mfs_args *args = data;
276 struct ufsmount *ump;
277 struct fs *fs;
278 struct mfsnode *mfsp;
279 struct proc *p;
280 int flags, error = 0;
282 if (*data_len < sizeof *args)
283 return EINVAL;
285 p = l->l_proc;
286 if (mp->mnt_flag & MNT_GETARGS) {
287 struct vnode *vp;
289 ump = VFSTOUFS(mp);
290 if (ump == NULL)
291 return EIO;
293 vp = ump->um_devvp;
294 if (vp == NULL)
295 return EIO;
297 mfsp = VTOMFS(vp);
298 if (mfsp == NULL)
299 return EIO;
301 args->fspec = NULL;
302 args->base = mfsp->mfs_baseoff;
303 args->size = mfsp->mfs_size;
304 *data_len = sizeof *args;
305 return 0;
308 * XXX turn off async to avoid hangs when writing lots of data.
309 * the problem is that MFS needs to allocate pages to clean pages,
310 * so if we wait until the last minute to clean pages then there
311 * may not be any pages available to do the cleaning.
312 * ... and since the default partially-synchronous mode turns out
313 * to not be sufficient under heavy load, make it full synchronous.
315 mp->mnt_flag &= ~MNT_ASYNC;
316 mp->mnt_flag |= MNT_SYNCHRONOUS;
319 * If updating, check whether changing from read-only to
320 * read/write; if there is no device name, that's all we do.
322 if (mp->mnt_flag & MNT_UPDATE) {
323 ump = VFSTOUFS(mp);
324 fs = ump->um_fs;
325 if (fs->fs_ronly == 0 && (mp->mnt_flag & MNT_RDONLY)) {
326 flags = WRITECLOSE;
327 if (mp->mnt_flag & MNT_FORCE)
328 flags |= FORCECLOSE;
329 error = ffs_flushfiles(mp, flags, l);
330 if (error)
331 return (error);
333 if (fs->fs_ronly && (mp->mnt_iflag & IMNT_WANTRDWR))
334 fs->fs_ronly = 0;
335 if (args->fspec == NULL)
336 return EINVAL;
337 return (0);
339 error = getnewvnode(VT_MFS, (struct mount *)0, mfs_vnodeop_p, &devvp);
340 if (error)
341 return (error);
342 devvp->v_vflag |= VV_MPSAFE;
343 devvp->v_type = VBLK;
344 spec_node_init(devvp, makedev(255, mfs_minor));
345 mfs_minor++;
346 mfsp = kmem_alloc(sizeof(*mfsp), KM_SLEEP);
347 devvp->v_data = mfsp;
348 mfsp->mfs_baseoff = args->base;
349 mfsp->mfs_size = args->size;
350 mfsp->mfs_vnode = devvp;
351 mfsp->mfs_proc = p;
352 mfsp->mfs_shutdown = 0;
353 cv_init(&mfsp->mfs_cv, "mfsidl");
354 mfsp->mfs_refcnt = 1;
355 bufq_alloc(&mfsp->mfs_buflist, "fcfs", 0);
356 if ((error = ffs_mountfs(devvp, mp, l)) != 0) {
357 mfsp->mfs_shutdown = 1;
358 vrele(devvp);
359 return (error);
361 ump = VFSTOUFS(mp);
362 fs = ump->um_fs;
363 error = set_statvfs_info(path, UIO_USERSPACE, args->fspec,
364 UIO_USERSPACE, mp->mnt_op->vfs_name, mp, l);
365 if (error)
366 return error;
367 (void)strncpy(fs->fs_fsmnt, mp->mnt_stat.f_mntonname,
368 sizeof(fs->fs_fsmnt));
369 fs->fs_fsmnt[sizeof(fs->fs_fsmnt) - 1] = '\0';
370 /* XXX: cleanup on error */
371 return 0;
375 * Used to grab the process and keep it in the kernel to service
376 * memory filesystem I/O requests.
378 * Loop servicing I/O requests.
379 * Copy the requested data into or out of the memory filesystem
380 * address space.
382 /* ARGSUSED */
384 mfs_start(struct mount *mp, int flags)
386 struct vnode *vp;
387 struct mfsnode *mfsp;
388 struct proc *p;
389 struct buf *bp;
390 void *base;
391 int sleepreturn = 0, refcnt, error;
392 ksiginfoq_t kq;
395 * Ensure that file system is still mounted when getting mfsnode.
396 * Add a reference to the mfsnode to prevent it disappearing in
397 * this routine.
399 if ((error = vfs_busy(mp, NULL)) != 0)
400 return error;
401 vp = VFSTOUFS(mp)->um_devvp;
402 mfsp = VTOMFS(vp);
403 mutex_enter(&mfs_lock);
404 mfsp->mfs_refcnt++;
405 mutex_exit(&mfs_lock);
406 vfs_unbusy(mp, false, NULL);
408 base = mfsp->mfs_baseoff;
409 mutex_enter(&mfs_lock);
410 while (mfsp->mfs_shutdown != 1) {
411 while ((bp = bufq_get(mfsp->mfs_buflist)) != NULL) {
412 mutex_exit(&mfs_lock);
413 mfs_doio(bp, base);
414 mutex_enter(&mfs_lock);
417 * If a non-ignored signal is received, try to unmount.
418 * If that fails, or the filesystem is already in the
419 * process of being unmounted, clear the signal (it has been
420 * "processed"), otherwise we will loop here, as tsleep
421 * will always return EINTR/ERESTART.
423 if (sleepreturn != 0) {
424 mutex_exit(&mfs_lock);
425 if (dounmount(mp, 0, curlwp) != 0) {
426 p = curproc;
427 ksiginfo_queue_init(&kq);
428 mutex_enter(p->p_lock);
429 sigclearall(p, NULL, &kq);
430 mutex_exit(p->p_lock);
431 ksiginfo_queue_drain(&kq);
433 sleepreturn = 0;
434 mutex_enter(&mfs_lock);
435 continue;
438 sleepreturn = cv_wait_sig(&mfsp->mfs_cv, &mfs_lock);
440 KASSERT(bufq_peek(mfsp->mfs_buflist) == NULL);
441 refcnt = --mfsp->mfs_refcnt;
442 mutex_exit(&mfs_lock);
443 if (refcnt == 0) {
444 bufq_free(mfsp->mfs_buflist);
445 cv_destroy(&mfsp->mfs_cv);
446 kmem_free(mfsp, sizeof(*mfsp));
448 return (sleepreturn);
452 * Get file system statistics.
455 mfs_statvfs(struct mount *mp, struct statvfs *sbp)
457 int error;
459 error = ffs_statvfs(mp, sbp);
460 if (error)
461 return error;
462 (void)strncpy(sbp->f_fstypename, mp->mnt_op->vfs_name,
463 sizeof(sbp->f_fstypename));
464 sbp->f_fstypename[sizeof(sbp->f_fstypename) - 1] = '\0';
465 return 0;