rawfs.c revision 1.1.24.3 1 /* $NetBSD: rawfs.c,v 1.1.24.3 2004/09/21 13:23:40 skrll Exp $ */
2
3 /*
4 * Copyright (c) 1995 Gordon W. Ross
5 * 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. The name of the author may not be used to endorse or promote products
16 * derived from this software without specific prior written permission.
17 * 4. All advertising materials mentioning features or use of this software
18 * must display the following acknowledgement:
19 * This product includes software developed by Gordon W. Ross
20 *
21 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
22 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
23 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
24 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
25 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
26 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
27 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
28 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
29 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
30 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
31 */
32
33 /*
34 * Raw file system - for stream devices like tapes.
35 * No random access, only sequential read allowed.
36 * This exists only to allow upper level code to be
37 * shielded from the fact that the device must be
38 * read only with whole block position and size.
39 */
40
41 #include <sys/param.h>
42 #include <stand.h>
43
44 #include "rawfs.h"
45
46 extern int debug;
47
48 /* Our devices are generally willing to do 8K transfers. */
49 #define RAWFS_BSIZE 0x2000
50
51 /*
52 * In-core open file.
53 */
54 struct file {
55 daddr_t fs_nextblk; /* block number to read next */
56 off_t fs_off; /* seek offset in file */
57 int fs_len; /* amount left in f_buf */
58 char * fs_ptr; /* read pointer into f_buf */
59 char fs_buf[RAWFS_BSIZE];
60 };
61
62 static int rawfs_get_block __P((struct open_file *));
63
64 int
65 rawfs_open(path, f)
66 const char *path;
67 struct open_file *f;
68 {
69 struct file *fs;
70
71 /*
72 * The actual PROM driver has already been opened.
73 * Just allocate the I/O buffer, etc.
74 */
75 fs = alloc(sizeof(struct file));
76 fs->fs_nextblk = 0;
77 fs->fs_off = 0;
78 fs->fs_len = 0;
79 fs->fs_ptr = fs->fs_buf;
80
81 #ifdef DEBUG_RAWFS
82 printf("rawfs_open: fs=0x%x\n", fs);
83 #endif
84
85 f->f_fsdata = fs;
86 return (0);
87 }
88
89 int
90 rawfs_close(f)
91 struct open_file *f;
92 {
93 struct file *fs;
94
95 fs = (struct file *) f->f_fsdata;
96 f->f_fsdata = NULL;
97
98 #ifdef DEBUG_RAWFS
99 if (debug) {
100 printf("rawfs_close: breakpoint...", fs->fs_buf);
101 __asm (" trap #0");
102 }
103 #endif
104
105 if (fs != NULL)
106 free(fs, sizeof(*fs));
107
108 return (0);
109 }
110
111 int
112 rawfs_read(f, start, size, resid)
113 struct open_file *f;
114 void *start;
115 u_int size;
116 u_int *resid;
117 {
118 struct file *fs = (struct file *)f->f_fsdata;
119 char *addr = start;
120 int error = 0;
121 size_t csize;
122
123 while (size != 0) {
124
125 if (fs->fs_len == 0)
126 if ((error = rawfs_get_block(f)) != 0)
127 break;
128
129 if (fs->fs_len <= 0)
130 break; /* EOF */
131
132 csize = size;
133 if (csize > fs->fs_len)
134 csize = fs->fs_len;
135
136 bcopy(fs->fs_ptr, addr, csize);
137 fs->fs_off += csize;
138 fs->fs_ptr += csize;
139 fs->fs_len -= csize;
140 addr += csize;
141 size -= csize;
142 }
143 if (resid)
144 *resid = size;
145 return (error);
146 }
147
148 int
149 rawfs_write(f, start, size, resid)
150 struct open_file *f;
151 void *start;
152 size_t size;
153 size_t *resid; /* out */
154 {
155 #ifdef DEBUG_RAWFS
156 panic("rawfs_write");
157 #endif
158 return (EROFS);
159 }
160
161 off_t
162 rawfs_seek(f, offset, where)
163 struct open_file *f;
164 off_t offset;
165 int where;
166 {
167 struct file *fs = (struct file *)f->f_fsdata;
168 off_t csize;
169
170 switch (where) {
171 case SEEK_SET:
172 offset -= fs->fs_off;
173 /* FALLTHROUGH */
174 case SEEK_CUR:
175 if (offset >= 0)
176 break;
177 /* FALLTHROUGH */
178 case SEEK_END:
179 default:
180 return (-1);
181 }
182
183 while (offset != 0) {
184
185 if (fs->fs_len == 0)
186 if (rawfs_get_block(f) != 0)
187 return (-1);
188
189 if (fs->fs_len <= 0)
190 break; /* EOF */
191
192 csize = offset;
193 if (csize > fs->fs_len)
194 csize = fs->fs_len;
195
196 fs->fs_off += csize;
197 fs->fs_ptr += csize;
198 fs->fs_len -= csize;
199 offset -= csize;
200 }
201 return (fs->fs_off);
202 }
203
204 int
205 rawfs_stat(f, sb)
206 struct open_file *f;
207 struct stat *sb;
208 {
209 #ifdef DEBUG_RAWFS
210 panic("rawfs_stat");
211 #endif
212 return (EFTYPE);
213 }
214
215
216 /*
217 * Read a block from the underlying stream device
218 * (In our case, a tape drive.)
219 */
220 static int
221 rawfs_get_block(f)
222 struct open_file *f;
223 {
224 struct file *fs;
225 int error, len;
226
227 fs = (struct file *)f->f_fsdata;
228 fs->fs_ptr = fs->fs_buf;
229
230 twiddle();
231 error = f->f_dev->dv_strategy(f->f_devdata, F_READ,
232 fs->fs_nextblk, RAWFS_BSIZE, fs->fs_buf, &len);
233
234 if (!error) {
235 fs->fs_len = len;
236 fs->fs_nextblk += (RAWFS_BSIZE / DEV_BSIZE);
237 }
238
239 return (error);
240 }
241
242