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