rawfs.c revision 1.2 1 /* $NetBSD: rawfs.c,v 1.2 2001/07/07 09:06:43 scw 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 #include <rawfs.h>
44
45 extern int debug;
46
47 #define RAWFS_BSIZE 512
48
49 /*
50 * In-core open file.
51 */
52 struct file {
53 daddr_t fs_nextblk; /* block number to read next */
54 int fs_len; /* amount left in f_buf */
55 char * fs_ptr; /* read pointer into f_buf */
56 char fs_buf[RAWFS_BSIZE];
57 };
58
59 static int
60 rawfs_get_block __P((struct open_file *));
61
62 int rawfs_open(path, f)
63 char *path;
64 struct open_file *f;
65 {
66 struct file *fs;
67
68 /*
69 * The actual PROM driver has already been opened.
70 * Just allocate the I/O buffer, etc.
71 */
72 fs = alloc(sizeof(struct file));
73 fs->fs_nextblk = 0;
74 fs->fs_len = 0;
75 fs->fs_ptr = fs->fs_buf;
76
77 f->f_fsdata = fs;
78 return (0);
79 }
80
81 int rawfs_close(f)
82 struct open_file *f;
83 {
84 struct file *fs;
85
86 fs = (struct file *) f->f_fsdata;
87 f->f_fsdata = (void *)0;
88
89 if (fs != (struct file *)0)
90 free(fs, sizeof(*fs));
91
92 return (0);
93 }
94
95 int rawfs_read(f, start, size, resid)
96 struct open_file *f;
97 void *start;
98 u_int size;
99 u_int *resid;
100 {
101 struct file *fs = (struct file *)f->f_fsdata;
102 char *addr = start;
103 int error = 0;
104 size_t csize;
105
106 while (size != 0) {
107
108 if (fs->fs_len == 0)
109 if ((error = rawfs_get_block(f)) != 0)
110 break;
111
112 if (fs->fs_len <= 0)
113 break; /* EOF */
114
115 csize = size;
116 if (csize > fs->fs_len)
117 csize = fs->fs_len;
118
119 memcpy(addr, fs->fs_ptr, csize);
120 fs->fs_ptr += csize;
121 fs->fs_len -= csize;
122 addr += csize;
123 size -= csize;
124 }
125 if (resid)
126 *resid = size;
127 return (error);
128 }
129
130 int rawfs_write(f, start, size, resid)
131 struct open_file *f;
132 void *start;
133 size_t size;
134 size_t *resid; /* out */
135 {
136 return (EROFS);
137 }
138
139 off_t rawfs_seek(f, offset, where)
140 struct open_file *f;
141 off_t offset;
142 int where;
143 {
144 return (EFTYPE);
145 }
146
147 int rawfs_stat(f, sb)
148 struct open_file *f;
149 struct stat *sb;
150 {
151 return (EFTYPE);
152 }
153
154
155 /*
156 * Read a block from the underlying stream device
157 * (In our case, a tape drive.)
158 */
159 static int
160 rawfs_get_block(f)
161 struct open_file *f;
162 {
163 struct file *fs;
164 int error, len;
165
166 fs = (struct file *)f->f_fsdata;
167 fs->fs_ptr = fs->fs_buf;
168
169 twiddle();
170 error = f->f_dev->dv_strategy(f->f_devdata, F_READ,
171 fs->fs_nextblk, RAWFS_BSIZE, fs->fs_buf, &len);
172
173 if (!error) {
174 fs->fs_len = len;
175 fs->fs_nextblk += (RAWFS_BSIZE / DEV_BSIZE);
176 }
177
178 return (error);
179 }
180
181