rawfs.c revision 1.3 1 /* $NetBSD: rawfs.c,v 1.3 2003/11/14 16:52:40 tsutsui 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
43 #include <lib/libsa/stand.h>
44 #include <hp300/stand/common/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 rawfs_file {
55 daddr_t fs_nextblk; /* block number to read next */
56 int fs_len; /* amount left in f_buf */
57 char * fs_ptr; /* read pointer into f_buf */
58 char fs_buf[RAWFS_BSIZE];
59 };
60
61 static int rawfs_get_block(struct open_file *);
62
63 int
64 rawfs_open(path, f)
65 const char *path;
66 struct open_file *f;
67 {
68 struct rawfs_file *fs;
69
70 /*
71 * The actual tape driver has already been opened.
72 * Just allocate the I/O buffer, etc.
73 */
74 fs = alloc(sizeof(struct rawfs_file));
75 fs->fs_nextblk = 0;
76 fs->fs_len = 0;
77 fs->fs_ptr = fs->fs_buf;
78
79 #ifdef DEBUG_RAWFS
80 printf("rawfs_open: fs=0x%x\n", (u_long)fs);
81 #endif
82
83 f->f_fsdata = fs;
84 return 0;
85 }
86
87 int
88 rawfs_close(f)
89 struct open_file *f;
90 {
91 struct rawfs_file *fs;
92
93 fs = (struct rawfs_file *) f->f_fsdata;
94 f->f_fsdata = (void *)0;
95
96 #ifdef DEBUG_RAWFS
97 printf("rawfs_close: fs=0x%x\n", (u_long)fs);
98 #endif
99
100 if (fs != (struct rawfs_file *)0)
101 free(fs, sizeof(*fs));
102
103 return 0;
104 }
105
106 int
107 rawfs_read(f, start, size, resid)
108 struct open_file *f;
109 void *start;
110 u_int size;
111 u_int *resid;
112 {
113 struct rawfs_file *fs = (struct rawfs_file *)f->f_fsdata;
114 char *addr = start;
115 int error = 0;
116 size_t csize;
117
118 #ifdef DEBUG_RAWFS
119 printf("rawfs_read: file=0x%x, start=0x%x, size=%d, resid=0x%x\n",
120 (u_long)f, (u_long)start, size, resid);
121 printf(" fs=0x%x\n", (u_long)fs);
122 #endif
123
124 while (size != 0) {
125
126 if (fs->fs_len == 0)
127 if ((error = rawfs_get_block(f)) != 0)
128 break;
129
130 if (fs->fs_len <= 0)
131 break; /* EOF */
132
133 csize = size;
134 if (csize > fs->fs_len)
135 csize = fs->fs_len;
136
137 memcpy(addr, fs->fs_ptr, 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 printf("rawfs_write: YOU'RE NOT SUPPOSED TO GET HERE!\n");
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
168 #ifdef DEBUG_RAWFS
169 printf("rawfs_seek: YOU'RE NOT SUPPOSED TO GET HERE!\n");
170 #endif
171 return EFTYPE;
172 }
173
174 int
175 rawfs_stat(f, sb)
176 struct open_file *f;
177 struct stat *sb;
178 {
179
180 #ifdef DEBUG_RAWFS
181 printf("rawfs_stat: I'll let you live only because of exec.c\n");
182 #endif
183 /*
184 * Clear out the stat buffer so that the uid check
185 * won't fail. See sys/lib/libsa/exec.c
186 */
187 memset(sb, 0, sizeof(*sb));
188
189 return EFTYPE;
190 }
191
192 /*
193 * Read a block from the underlying stream device
194 * (In our case, a tape drive.)
195 */
196 static int
197 rawfs_get_block(f)
198 struct open_file *f;
199 {
200 struct rawfs_file *fs;
201 int error, len;
202
203 fs = (struct rawfs_file *)f->f_fsdata;
204 fs->fs_ptr = fs->fs_buf;
205
206 twiddle();
207 #ifdef DEBUG_RAWFS
208 printf("rawfs_get_block: calling strategy\n");
209 #endif
210 error = f->f_dev->dv_strategy(f->f_devdata, F_READ,
211 fs->fs_nextblk, RAWFS_BSIZE, fs->fs_buf, &len);
212 #ifdef DEBUG_RAWFS
213 printf("rawfs_get_block: strategy returned %d\n", error);
214 #endif
215
216 if (!error) {
217 fs->fs_len = len;
218 fs->fs_nextblk += (RAWFS_BSIZE / DEV_BSIZE);
219 }
220
221 return error;
222 }
223