Initial import from FreeBSD RELENG_4:
[dragonfly.git] / share / doc / papers / memfs / A.t
1 .\" Copyright (c) 1990 The Regents of the University of California.
2 .\" All rights reserved.
3 .\"
4 .\" Redistribution and use in source and binary forms, with or without
5 .\" modification, are permitted provided that the following conditions
6 .\" are met:
7 .\" 1. Redistributions of source code must retain the above copyright
8 .\"    notice, this list of conditions and the following disclaimer.
9 .\" 2. Redistributions in binary form must reproduce the above copyright
10 .\"    notice, this list of conditions and the following disclaimer in the
11 .\"    documentation and/or other materials provided with the distribution.
12 .\" 3. All advertising materials mentioning features or use of this software
13 .\"    must display the following acknowledgement:
14 .\"     This product includes software developed by the University of
15 .\"     California, Berkeley and its contributors.
16 .\" 4. 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 .\"     @(#)A.t 5.1 (Berkeley) 4/16/91
33 .\"
34 .bp
35 .nr PS 10
36 .nr VS 12
37 .NH
38 Appendix A - Implementation Details
39 .LP
40 .nf
41 .vS
42 /*
43  * This structure defines the control data for the memory
44  * based file system.
45  */
46 struct mfsnode {
47         struct  vnode *mfs_vnode;       /* vnode associated with this mfsnode */
48         caddr_t mfs_baseoff;            /* base of file system in memory */
49         long    mfs_size;               /* size of memory file system */
50         pid_t   mfs_pid;                /* supporting process pid */
51         struct  buf *mfs_buflist;       /* list of I/O requests */
52 };
53
54 /*
55  * Convert between mfsnode pointers and vnode pointers
56  */
57 #define VTOMFS(vp)      ((struct mfsnode *)(vp)->v_data)
58 #define MFSTOV(mfsp)    ((mfsp)->mfs_vnode)
59 #define MFS_EXIT        (struct buf *)-1
60
61 /*
62  * Arguments to mount MFS
63  */
64 struct mfs_args {
65         char    *name;          /* name to export for statfs */
66         caddr_t base;           /* base address of file system in memory */
67         u_long  size;           /* size of file system */
68 };
69 .bp
70 /*
71  * Mount an MFS filesystem.
72  */
73 mfs_mount(mp, path, data)
74         struct mount *mp;
75         char *path;
76         caddr_t data;
77 {
78         struct vnode *devvp;
79         struct mfsnode *mfsp;
80         struct buf *bp;
81         struct mfs_args args;
82
83         /*
84          * Create a block device to represent the disk.
85          */
86         devvp = getnewvnode(VT_MFS, VBLK, &mfs_vnodeops);
87         mfsp = VTOMFS(devvp);
88         /*
89          * Save base address of the filesystem from the supporting process.
90          */
91         copyin(data, &args, (sizeof mfs_args));
92         mfsp->mfs_baseoff = args.base;
93         mfsp->mfs_size = args.size;
94         /*
95          * Record the process identifier of the supporting process.
96          */
97         mfsp->mfs_pid = u.u_procp->p_pid;
98         /*
99          * Mount the filesystem.
100          */
101         mfsp->mfs_buflist = NULL;
102         mountfs(devvp, mp);
103         /*
104          * Loop processing I/O requests.
105          */
106         while (mfsp->mfs_buflist != MFS_EXIT) {
107                 while (mfsp->mfs_buflist != NULL) {
108                         bp = mfsp->mfs_buflist;
109                         mfsp->mfs_buflist = bp->av_forw;
110                         offset = mfsp->mfs_baseoff + (bp->b_blkno * DEV_BSIZE);
111                         if (bp->b_flags & B_READ)
112                                 copyin(offset, bp->b_un.b_addr, bp->b_bcount);
113                         else /* write_request */
114                                 copyout(bp->b_un.b_addr, offset, bp->b_bcount);
115                         biodone(bp);
116                 }
117                 sleep((caddr_t)devvp, PWAIT);
118         }
119 }
120 .bp
121 /*
122  * If the MFS process requests the I/O then we must do it directly.
123  * Otherwise put the request on the list and request the MFS process
124  * to be run.
125  */
126 mfs_strategy(bp)
127         struct buf *bp;
128 {
129         struct vnode *devvp;
130         struct mfsnode *mfsp;
131         off_t offset;
132
133         devvp = bp->b_vp;
134         mfsp = VTOMFS(devvp);
135         if (mfsp->mfs_pid == u.u_procp->p_pid) {
136                 offset = mfsp->mfs_baseoff + (bp->b_blkno * DEV_BSIZE);
137                 if (bp->b_flags & B_READ)
138                         copyin(offset, bp->b_un.b_addr, bp->b_bcount);
139                 else /* write_request */
140                         copyout(bp->b_un.b_addr, offset, bp->b_bcount);
141                 biodone(bp);
142         } else {
143                 bp->av_forw = mfsp->mfs_buflist;
144                 mfsp->mfs_buflist = bp;
145                 wakeup((caddr_t)bp->b_vp);
146         }
147 }
148
149 /*
150  * The close routine is called by unmount after the filesystem
151  * has been successfully unmounted.
152  */
153 mfs_close(devvp)
154         struct vnode *devvp;
155 {
156         struct mfsnode *mfsp = VTOMFS(vp);
157         struct buf *bp;
158
159         /*
160          * Finish any pending I/O requests.
161          */
162         while (bp = mfsp->mfs_buflist) {
163                 mfsp->mfs_buflist = bp->av_forw;
164                 mfs_doio(bp, mfsp->mfs_baseoff);
165                 wakeup((caddr_t)bp);
166         }
167         /*
168          * Send a request to the filesystem server to exit.
169          */
170         mfsp->mfs_buflist = MFS_EXIT;
171         wakeup((caddr_t)vp);
172 }
173 .vE