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