rawfs.c revision 1.7 1 1.7 tsutsui /* $NetBSD: rawfs.c,v 1.7 2006/06/25 17:36:07 tsutsui 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.3 tsutsui static int rawfs_get_block(struct open_file *);
62 1.1 thorpej
63 1.1 thorpej int
64 1.4 tsutsui rawfs_open(const char *path, struct open_file *f)
65 1.1 thorpej {
66 1.1 thorpej struct rawfs_file *fs;
67 1.1 thorpej
68 1.1 thorpej /*
69 1.1 thorpej * The actual tape driver has already been opened.
70 1.1 thorpej * Just allocate the I/O buffer, etc.
71 1.1 thorpej */
72 1.1 thorpej fs = alloc(sizeof(struct rawfs_file));
73 1.1 thorpej fs->fs_nextblk = 0;
74 1.1 thorpej fs->fs_len = 0;
75 1.1 thorpej fs->fs_ptr = fs->fs_buf;
76 1.1 thorpej
77 1.1 thorpej #ifdef DEBUG_RAWFS
78 1.1 thorpej printf("rawfs_open: fs=0x%x\n", (u_long)fs);
79 1.1 thorpej #endif
80 1.1 thorpej
81 1.1 thorpej f->f_fsdata = fs;
82 1.3 tsutsui return 0;
83 1.1 thorpej }
84 1.1 thorpej
85 1.1 thorpej int
86 1.4 tsutsui rawfs_close(struct open_file *f)
87 1.1 thorpej {
88 1.1 thorpej struct rawfs_file *fs;
89 1.1 thorpej
90 1.1 thorpej fs = (struct rawfs_file *) f->f_fsdata;
91 1.1 thorpej f->f_fsdata = (void *)0;
92 1.1 thorpej
93 1.1 thorpej #ifdef DEBUG_RAWFS
94 1.1 thorpej printf("rawfs_close: fs=0x%x\n", (u_long)fs);
95 1.1 thorpej #endif
96 1.1 thorpej
97 1.1 thorpej if (fs != (struct rawfs_file *)0)
98 1.6 christos dealloc(fs, sizeof(*fs));
99 1.1 thorpej
100 1.3 tsutsui return 0;
101 1.1 thorpej }
102 1.1 thorpej
103 1.1 thorpej int
104 1.4 tsutsui rawfs_read(struct open_file *f, void *start, u_int size, u_int *resid)
105 1.1 thorpej {
106 1.1 thorpej struct rawfs_file *fs = (struct rawfs_file *)f->f_fsdata;
107 1.1 thorpej char *addr = start;
108 1.1 thorpej int error = 0;
109 1.1 thorpej size_t csize;
110 1.1 thorpej
111 1.1 thorpej #ifdef DEBUG_RAWFS
112 1.1 thorpej printf("rawfs_read: file=0x%x, start=0x%x, size=%d, resid=0x%x\n",
113 1.1 thorpej (u_long)f, (u_long)start, size, resid);
114 1.1 thorpej printf(" fs=0x%x\n", (u_long)fs);
115 1.1 thorpej #endif
116 1.1 thorpej
117 1.1 thorpej while (size != 0) {
118 1.1 thorpej
119 1.1 thorpej if (fs->fs_len == 0)
120 1.1 thorpej if ((error = rawfs_get_block(f)) != 0)
121 1.1 thorpej break;
122 1.1 thorpej
123 1.1 thorpej if (fs->fs_len <= 0)
124 1.1 thorpej break; /* EOF */
125 1.1 thorpej
126 1.1 thorpej csize = size;
127 1.1 thorpej if (csize > fs->fs_len)
128 1.1 thorpej csize = fs->fs_len;
129 1.1 thorpej
130 1.3 tsutsui memcpy(addr, fs->fs_ptr, csize);
131 1.1 thorpej fs->fs_ptr += csize;
132 1.1 thorpej fs->fs_len -= csize;
133 1.1 thorpej addr += csize;
134 1.1 thorpej size -= csize;
135 1.1 thorpej }
136 1.1 thorpej if (resid)
137 1.1 thorpej *resid = size;
138 1.3 tsutsui return error;
139 1.1 thorpej }
140 1.1 thorpej
141 1.1 thorpej int
142 1.4 tsutsui rawfs_write(struct open_file *f, void *start, size_t size, size_t *resid)
143 1.1 thorpej {
144 1.4 tsutsui
145 1.1 thorpej #ifdef DEBUG_RAWFS
146 1.1 thorpej printf("rawfs_write: YOU'RE NOT SUPPOSED TO GET HERE!\n");
147 1.1 thorpej #endif
148 1.3 tsutsui return EROFS;
149 1.1 thorpej }
150 1.1 thorpej
151 1.1 thorpej off_t
152 1.4 tsutsui rawfs_seek(struct open_file *f, off_t offset, int where)
153 1.1 thorpej {
154 1.3 tsutsui
155 1.1 thorpej #ifdef DEBUG_RAWFS
156 1.1 thorpej printf("rawfs_seek: YOU'RE NOT SUPPOSED TO GET HERE!\n");
157 1.1 thorpej #endif
158 1.3 tsutsui return EFTYPE;
159 1.1 thorpej }
160 1.1 thorpej
161 1.1 thorpej int
162 1.4 tsutsui rawfs_stat(struct open_file *f, struct stat *sb)
163 1.1 thorpej {
164 1.3 tsutsui
165 1.1 thorpej #ifdef DEBUG_RAWFS
166 1.1 thorpej printf("rawfs_stat: I'll let you live only because of exec.c\n");
167 1.1 thorpej #endif
168 1.1 thorpej /*
169 1.1 thorpej * Clear out the stat buffer so that the uid check
170 1.1 thorpej * won't fail. See sys/lib/libsa/exec.c
171 1.1 thorpej */
172 1.3 tsutsui memset(sb, 0, sizeof(*sb));
173 1.1 thorpej
174 1.3 tsutsui return EFTYPE;
175 1.1 thorpej }
176 1.1 thorpej
177 1.1 thorpej /*
178 1.1 thorpej * Read a block from the underlying stream device
179 1.1 thorpej * (In our case, a tape drive.)
180 1.1 thorpej */
181 1.1 thorpej static int
182 1.4 tsutsui rawfs_get_block(struct open_file *f)
183 1.1 thorpej {
184 1.1 thorpej struct rawfs_file *fs;
185 1.7 tsutsui int error;
186 1.7 tsutsui size_t len;
187 1.1 thorpej
188 1.1 thorpej fs = (struct rawfs_file *)f->f_fsdata;
189 1.1 thorpej fs->fs_ptr = fs->fs_buf;
190 1.1 thorpej
191 1.1 thorpej twiddle();
192 1.1 thorpej #ifdef DEBUG_RAWFS
193 1.1 thorpej printf("rawfs_get_block: calling strategy\n");
194 1.1 thorpej #endif
195 1.1 thorpej error = f->f_dev->dv_strategy(f->f_devdata, F_READ,
196 1.1 thorpej fs->fs_nextblk, RAWFS_BSIZE, fs->fs_buf, &len);
197 1.1 thorpej #ifdef DEBUG_RAWFS
198 1.1 thorpej printf("rawfs_get_block: strategy returned %d\n", error);
199 1.1 thorpej #endif
200 1.1 thorpej
201 1.1 thorpej if (!error) {
202 1.1 thorpej fs->fs_len = len;
203 1.1 thorpej fs->fs_nextblk += (RAWFS_BSIZE / DEV_BSIZE);
204 1.1 thorpej }
205 1.1 thorpej
206 1.3 tsutsui return error;
207 1.1 thorpej }
208