ustarfs.c revision 1.1 1 1.1 ross /* $NetBSD: ustarfs.c,v 1.1 1998/09/24 05:23:33 ross Exp $ */
2 1.1 ross
3 1.1 ross /* [Notice revision 2.2]
4 1.1 ross * Copyright (c) 1997, 1998 Avalon Computer Systems, Inc.
5 1.1 ross * All rights reserved.
6 1.1 ross *
7 1.1 ross * Author: Ross Harvey
8 1.1 ross *
9 1.1 ross * Redistribution and use in source and binary forms, with or without
10 1.1 ross * modification, are permitted provided that the following conditions
11 1.1 ross * are met:
12 1.1 ross * 1. Redistributions of source code must retain the above copyright and
13 1.1 ross * author notice, this list of conditions, and the following disclaimer.
14 1.1 ross * 2. Redistributions in binary form must reproduce the above copyright
15 1.1 ross * notice, this list of conditions and the following disclaimer in the
16 1.1 ross * documentation and/or other materials provided with the distribution.
17 1.1 ross * 3. Neither the name of Avalon Computer Systems, Inc. nor the names of
18 1.1 ross * its contributors may be used to endorse or promote products derived
19 1.1 ross * from this software without specific prior written permission.
20 1.1 ross * 4. This copyright will be assigned to The NetBSD Foundation on
21 1.1 ross * 1/1/2000 unless these terms (including possibly the assignment
22 1.1 ross * date) are updated in writing by Avalon prior to the latest specified
23 1.1 ross * assignment date.
24 1.1 ross *
25 1.1 ross * THIS SOFTWARE IS PROVIDED BY AVALON COMPUTER SYSTEMS, INC. AND CONTRIBUTORS
26 1.1 ross * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
27 1.1 ross * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
28 1.1 ross * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL AVALON OR THE CONTRIBUTORS
29 1.1 ross * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
30 1.1 ross * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
31 1.1 ross * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
32 1.1 ross * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
33 1.1 ross * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
34 1.1 ross * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
35 1.1 ross * POSSIBILITY OF SUCH DAMAGE.
36 1.1 ross */
37 1.1 ross
38 1.1 ross
39 1.1 ross /*
40 1.1 ross ******************************* USTAR FS *******************************
41 1.1 ross */
42 1.1 ross
43 1.1 ross /*
44 1.1 ross * Implement an ROFS with an 8K boot area followed by ustar-format data.
45 1.1 ross * The point: minimal FS overhead, and it's easy (well, `possible') to
46 1.1 ross * split files over multiple volumes.
47 1.1 ross *
48 1.1 ross * XXX - TODO LIST
49 1.1 ross * --- - ---- ----
50 1.1 ross * XXX - tag volume numbers and verify that the correct volume is
51 1.1 ross * inserted after volume swaps.
52 1.1 ross *
53 1.1 ross * XXX - stop hardwiring FS metadata for floppies...embed it in a file,
54 1.1 ross * file name, or something. (Remember __SYMDEF? :-)
55 1.1 ross *
56 1.1 ross */
57 1.1 ross
58 1.1 ross #include <lib/libkern/libkern.h>
59 1.1 ross #include "stand.h"
60 1.1 ross #include "ustarfs.h"
61 1.1 ross
62 1.1 ross #define USTAR_NAME_BLOCK 512
63 1.1 ross
64 1.1 ross typedef struct ustar_struct {
65 1.1 ross char ust_name[100],
66 1.1 ross ust_mode[8],
67 1.1 ross ust_uid[8],
68 1.1 ross ust_gid[8],
69 1.1 ross ust_size[12],
70 1.1 ross ust_misc[12 + 8 + 1 + 100],
71 1.1 ross ust_magic[6];
72 1.1 ross /* there is more, but we don't care */
73 1.1 ross } ustar_t;
74 1.1 ross
75 1.1 ross /*
76 1.1 ross * We buffer one even cylindar of data...it's actually only really one
77 1.1 ross * cyl on a 1.44M floppy, but on other devices it's fast enough with any
78 1.1 ross * kind of block buffering, so we optimize for the slowest device.
79 1.1 ross */
80 1.1 ross
81 1.1 ross typedef struct ust_active_struct {
82 1.1 ross ustar_t uas_active;
83 1.1 ross char uas_1cyl[18 * 2 * 512];
84 1.1 ross off_t uas_volsize; /* XXX this is hardwired now */
85 1.1 ross off_t uas_windowbase; /* relative to volume 0 */
86 1.1 ross off_t uas_filestart; /* relative to volume 0 */
87 1.1 ross off_t uas_fseek; /* relative to file */
88 1.1 ross int uas_init_window; /* data present in window */
89 1.1 ross int uas_init_fs; /* ust FS actually found */
90 1.1 ross off_t uas_filesize; /* relative to volume 0 */
91 1.1 ross } ust_active_t;
92 1.1 ross
93 1.1 ross #define BBSIZE 8192
94 1.1 ross
95 1.1 ross static int ustarfs_mode_offset = BBSIZE;
96 1.1 ross
97 1.1 ross static int ustarfs_cylinder_read __P((struct open_file *, off_t));
98 1.1 ross static int read512block __P((struct open_file *, off_t, char block[512]));
99 1.1 ross static void ustarfs_sscanf __P((const char *, const char *, int *));
100 1.1 ross static int convert __P((const char *, int, int));
101 1.1 ross
102 1.1 ross static int
103 1.1 ross convert(f, base, fw)
104 1.1 ross const char *f;
105 1.1 ross int base, fw;
106 1.1 ross {
107 1.1 ross int i, c, result = 0;
108 1.1 ross
109 1.1 ross while(fw > 0 && *f == ' ') {
110 1.1 ross --fw;
111 1.1 ross ++f;
112 1.1 ross }
113 1.1 ross for(i = 0; i < fw; ++i) {
114 1.1 ross c = f[i];
115 1.1 ross if ('0' <= c && c < '0' + base) {
116 1.1 ross c -= '0';
117 1.1 ross result = result * base + c;
118 1.1 ross } else break;
119 1.1 ross }
120 1.1 ross return result;
121 1.1 ross }
122 1.1 ross
123 1.1 ross static void
124 1.1 ross ustarfs_sscanf(s,f,xi)
125 1.1 ross const char *s,*f;
126 1.1 ross int *xi;
127 1.1 ross {
128 1.1 ross *xi = convert(s, 8, convert(f + 1, 10, 99));
129 1.1 ross }
130 1.1 ross
131 1.1 ross static int
132 1.1 ross ustarfs_cylinder_read(f, seek2)
133 1.1 ross struct open_file *f;
134 1.1 ross off_t seek2;
135 1.1 ross {
136 1.1 ross int e;
137 1.1 ross size_t xfercount;
138 1.1 ross ust_active_t *ustf;
139 1.1 ross
140 1.1 ross ustf = f->f_fsdata;
141 1.1 ross e = f->f_dev->dv_strategy(f->f_devdata, F_READ, seek2/512,
142 1.1 ross sizeof ustf->uas_1cyl, ustf->uas_1cyl, &xfercount);
143 1.1 ross if (e == 0 && xfercount != sizeof ustf->uas_1cyl)
144 1.1 ross printf("Warning, unexpected short transfer %d/%d\n",
145 1.1 ross (int)xfercount, (int) sizeof ustf->uas_1cyl);
146 1.1 ross return e;
147 1.1 ross }
148 1.1 ross
149 1.1 ross static int
150 1.1 ross read512block(f, offset, block)
151 1.1 ross struct open_file *f;
152 1.1 ross off_t offset;
153 1.1 ross char block[512];
154 1.1 ross {
155 1.1 ross ssize_t e;
156 1.1 ross int needvolume, havevolume, dienow = 0;
157 1.1 ross off_t lastbase, seek2;
158 1.1 ross ust_active_t *ustf;
159 1.1 ross
160 1.1 ross ustf = f->f_fsdata;
161 1.1 ross tryagain:
162 1.1 ross if(ustf->uas_init_window
163 1.1 ross && ustf->uas_windowbase <= offset
164 1.1 ross && offset - ustf->uas_windowbase < sizeof ustf->uas_1cyl) {
165 1.1 ross memcpy(block, ustf->uas_1cyl + offset - ustf->uas_windowbase,
166 1.1 ross 512);
167 1.1 ross return 0;
168 1.1 ross }
169 1.1 ross if (dienow++)
170 1.1 ross panic("ustarfs read512block");
171 1.1 ross lastbase = ustf->uas_windowbase;
172 1.1 ross seek2 = ustf->uas_windowbase = offset - offset % sizeof ustf->uas_1cyl;
173 1.1 ross if(ustf->uas_volsize) {
174 1.1 ross havevolume = lastbase / ustf->uas_volsize;
175 1.1 ross needvolume = ustf->uas_windowbase / ustf->uas_volsize;
176 1.1 ross if (havevolume != needvolume) {
177 1.1 ross if (needvolume != havevolume + 1)
178 1.1 ross printf("Caution: the disk required is not the"
179 1.1 ross " next disk in ascending sequence.\n");
180 1.1 ross printf("Please insert disk number %d"
181 1.1 ross " and type return...", needvolume + 1);
182 1.1 ross getchar();
183 1.1 ross }
184 1.1 ross seek2 %= ustf->uas_volsize;
185 1.1 ross }
186 1.1 ross e = ustarfs_cylinder_read(f, seek2);
187 1.1 ross if (e)
188 1.1 ross return e;
189 1.1 ross ustf->uas_init_window = 1;
190 1.1 ross goto tryagain;
191 1.1 ross }
192 1.1 ross
193 1.1 ross int
194 1.1 ross ustarfs_open(path, f)
195 1.1 ross char *path;
196 1.1 ross struct open_file *f;
197 1.1 ross
198 1.1 ross {
199 1.1 ross ust_active_t *ustf;
200 1.1 ross off_t offset;
201 1.1 ross char block[512];
202 1.1 ross int filesize;
203 1.1 ross int e, e2;
204 1.1 ross
205 1.1 ross if (*path == '/')
206 1.1 ross ++path;
207 1.1 ross e = EINVAL;
208 1.1 ross f->f_fsdata = ustf = alloc(sizeof *ustf);
209 1.1 ross memset(ustf, 0, sizeof *ustf);
210 1.1 ross offset = ustarfs_mode_offset;
211 1.1 ross ustf->uas_fseek = 0;
212 1.1 ross for(;;) {
213 1.1 ross ustf->uas_filestart = offset;
214 1.1 ross e2 = read512block(f, offset, block);
215 1.1 ross if (e2) {
216 1.1 ross e = e2;
217 1.1 ross break;
218 1.1 ross }
219 1.1 ross memcpy(&ustf->uas_active, block, sizeof ustf->uas_active);
220 1.1 ross if(strncmp(ustf->uas_active.ust_magic, "ustar", 5))
221 1.1 ross break;
222 1.1 ross e = ENOENT; /* it must be an actual ustarfs */
223 1.1 ross ustf->uas_init_fs = 1;
224 1.1 ross /*
225 1.1 ross * XXX - the right way to store FS metadata on ustarfs
226 1.1 ross * is to embed the data within a file. For now, we
227 1.1 ross * will avoid complexity by hardwiring metadata for
228 1.1 ross * a floppy.
229 1.1 ross */
230 1.1 ross ustf->uas_volsize = 80 * 2 * 18 * 512; /* XXX */
231 1.1 ross ustarfs_sscanf(ustf->uas_active.ust_size,"%12o",&filesize);
232 1.1 ross if(strncmp(ustf->uas_active.ust_name, path,
233 1.1 ross sizeof ustf->uas_active.ust_name) == 0) {
234 1.1 ross ustf->uas_filesize = filesize;
235 1.1 ross e = 0;
236 1.1 ross break;
237 1.1 ross }
238 1.1 ross offset += USTAR_NAME_BLOCK + filesize;
239 1.1 ross filesize %= 512;
240 1.1 ross if (filesize)
241 1.1 ross offset += 512 - filesize;
242 1.1 ross }
243 1.1 ross if (e) {
244 1.1 ross free(ustf, sizeof *ustf);
245 1.1 ross f->f_fsdata = 0;
246 1.1 ross }
247 1.1 ross return e;
248 1.1 ross }
249 1.1 ross
250 1.1 ross int
251 1.1 ross ustarfs_write(f, start, size, resid)
252 1.1 ross struct open_file *f;
253 1.1 ross void *start;
254 1.1 ross size_t size;
255 1.1 ross size_t *resid;
256 1.1 ross {
257 1.1 ross return (EROFS);
258 1.1 ross }
259 1.1 ross
260 1.1 ross off_t
261 1.1 ross ustarfs_seek(f, offs, whence)
262 1.1 ross struct open_file *f;
263 1.1 ross off_t offs;
264 1.1 ross int whence;
265 1.1 ross {
266 1.1 ross ust_active_t *ustf;
267 1.1 ross
268 1.1 ross ustf = f->f_fsdata;
269 1.1 ross switch (whence) {
270 1.1 ross case SEEK_SET:
271 1.1 ross ustf->uas_fseek = offs;
272 1.1 ross break;
273 1.1 ross case SEEK_CUR:
274 1.1 ross ustf->uas_fseek += offs;
275 1.1 ross break;
276 1.1 ross case SEEK_END:
277 1.1 ross ustf->uas_fseek = ustf->uas_filesize - offs;
278 1.1 ross break;
279 1.1 ross default:
280 1.1 ross return -1;
281 1.1 ross }
282 1.1 ross return ustf->uas_fseek;
283 1.1 ross }
284 1.1 ross
285 1.1 ross int
286 1.1 ross ustarfs_read(f, start, size, resid)
287 1.1 ross struct open_file *f;
288 1.1 ross void *start;
289 1.1 ross size_t size;
290 1.1 ross size_t *resid;
291 1.1 ross {
292 1.1 ross ust_active_t *ustf;
293 1.1 ross int e;
294 1.1 ross char *space512;
295 1.1 ross int blkoffs,
296 1.1 ross readoffs,
297 1.1 ross bufferoffset;
298 1.1 ross size_t seg;
299 1.1 ross int infile,
300 1.1 ross inbuffer;
301 1.1 ross
302 1.1 ross e = 0;
303 1.1 ross space512 = alloc(512);
304 1.1 ross ustf = f->f_fsdata;
305 1.1 ross while(size != 0) {
306 1.1 ross if (ustf->uas_fseek >= ustf->uas_filesize)
307 1.1 ross break;
308 1.1 ross bufferoffset = ustf->uas_fseek % 512;
309 1.1 ross blkoffs = ustf->uas_fseek - bufferoffset;
310 1.1 ross readoffs = ustf->uas_filestart + 512 + blkoffs;
311 1.1 ross e = read512block(f, readoffs, space512);
312 1.1 ross if (e)
313 1.1 ross break;
314 1.1 ross seg = size;
315 1.1 ross inbuffer = 512 - bufferoffset;
316 1.1 ross if (inbuffer < seg)
317 1.1 ross seg = inbuffer;
318 1.1 ross infile = ustf->uas_filesize - ustf->uas_fseek;
319 1.1 ross if (infile < seg)
320 1.1 ross seg = infile;
321 1.1 ross memcpy(start, space512 + bufferoffset, seg);
322 1.1 ross ustf->uas_fseek += seg;
323 1.1 ross start += seg;
324 1.1 ross size -= seg;
325 1.1 ross }
326 1.1 ross if (resid)
327 1.1 ross *resid = size;
328 1.1 ross free(space512, 512);
329 1.1 ross return e;
330 1.1 ross }
331 1.1 ross
332 1.1 ross int
333 1.1 ross ustarfs_stat(f, sb)
334 1.1 ross struct open_file *f;
335 1.1 ross struct stat *sb;
336 1.1 ross {
337 1.1 ross int mode, uid, gid;
338 1.1 ross ust_active_t *ustf;
339 1.1 ross
340 1.1 ross if (f == NULL)
341 1.1 ross return EINVAL;
342 1.1 ross ustf = f->f_fsdata;
343 1.1 ross memset(sb, 0, sizeof *sb);
344 1.1 ross ustarfs_sscanf(ustf->uas_active.ust_mode, "%8o", &mode);
345 1.1 ross ustarfs_sscanf(ustf->uas_active.ust_uid, "%8o", &uid);
346 1.1 ross ustarfs_sscanf(ustf->uas_active.ust_gid, "%8o", &gid);
347 1.1 ross sb->st_mode = mode;
348 1.1 ross sb->st_uid = uid;
349 1.1 ross sb->st_gid = gid;
350 1.1 ross sb->st_size = ustf->uas_filesize;
351 1.1 ross return 0;
352 1.1 ross }
353 1.1 ross
354 1.1 ross int
355 1.1 ross ustarfs_close(f)
356 1.1 ross struct open_file *f;
357 1.1 ross {
358 1.1 ross if (f == NULL || f->f_fsdata == NULL)
359 1.1 ross return EINVAL;
360 1.1 ross free(f->f_fsdata, sizeof(ust_active_t));
361 1.1 ross f->f_fsdata = 0;
362 1.1 ross return 0;
363 1.1 ross }
364