ustarfs.c revision 1.2 1 1.2 ross /* $NetBSD: ustarfs.c,v 1.2 1998/10/05 04:56:36 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.2 ross #define BBSIZE 8192
63 1.1 ross #define USTAR_NAME_BLOCK 512
64 1.1 ross
65 1.2 ross /*
66 1.2 ross * Virtual offset: relative to start of ustar archive
67 1.2 ross * Logical offset: volume-relative
68 1.2 ross * Physical offset: the usual meaning
69 1.2 ross */
70 1.2 ross
71 1.2 ross /* virtual offset to volume number */
72 1.2 ross
73 1.2 ross #define vda2vn(_v,_volsize) ((_v) / (_volsize))
74 1.2 ross
75 1.2 ross /* conversions between the three different levels of disk addresses */
76 1.2 ross
77 1.2 ross #define vda2lda(_v,_volsize) ((_v) % (_volsize))
78 1.2 ross #define lda2vda(_v,_volsize,_volnumber) ((_v) + (_volsize) * (_volnumber))
79 1.2 ross
80 1.2 ross #define lda2pda(_lda) ((_lda) + ustarfs_mode_offset)
81 1.2 ross #define pda2lda(_pda) ((_pda) - ustarfs_mode_offset)
82 1.2 ross /*
83 1.2 ross * Change this to off_t if you want to support big volumes. If we only use
84 1.2 ross * ustarfs on floppies it can stay int for libsa code density.
85 1.2 ross *
86 1.2 ross * It needs to be signed.
87 1.2 ross */
88 1.2 ross typedef int ustoffs;
89 1.2 ross
90 1.1 ross typedef struct ustar_struct {
91 1.1 ross char ust_name[100],
92 1.1 ross ust_mode[8],
93 1.1 ross ust_uid[8],
94 1.1 ross ust_gid[8],
95 1.1 ross ust_size[12],
96 1.1 ross ust_misc[12 + 8 + 1 + 100],
97 1.1 ross ust_magic[6];
98 1.1 ross /* there is more, but we don't care */
99 1.1 ross } ustar_t;
100 1.1 ross
101 1.1 ross /*
102 1.1 ross * We buffer one even cylindar of data...it's actually only really one
103 1.1 ross * cyl on a 1.44M floppy, but on other devices it's fast enough with any
104 1.1 ross * kind of block buffering, so we optimize for the slowest device.
105 1.1 ross */
106 1.1 ross
107 1.1 ross typedef struct ust_active_struct {
108 1.1 ross ustar_t uas_active;
109 1.1 ross char uas_1cyl[18 * 2 * 512];
110 1.2 ross ustoffs uas_volsize; /* XXX this is hardwired now */
111 1.2 ross ustoffs uas_windowbase; /* relative to volume 0 */
112 1.2 ross ustoffs uas_filestart; /* relative to volume 0 */
113 1.2 ross ustoffs uas_fseek; /* relative to file */
114 1.2 ross ustoffs uas_filesize; /* relative to volume 0 */
115 1.1 ross int uas_init_window; /* data present in window */
116 1.1 ross int uas_init_fs; /* ust FS actually found */
117 1.2 ross int uas_volzerosig; /* ID volume 0 by signature */
118 1.2 ross int uas_offset; /* amount of cylinder below lba 0 */
119 1.1 ross } ust_active_t;
120 1.1 ross
121 1.2 ross static const char formatid[] = "USTARFS";
122 1.1 ross
123 1.1 ross static int ustarfs_mode_offset = BBSIZE;
124 1.1 ross
125 1.2 ross static int checksig __P((ust_active_t *));
126 1.2 ross static int get_volume __P((struct open_file *, int));
127 1.2 ross static int convert __P((const char *, int, int));
128 1.2 ross static int ustarfs_cylinder_read __P((struct open_file *, ustoffs));
129 1.1 ross static void ustarfs_sscanf __P((const char *, const char *, int *));
130 1.2 ross static int read512block __P((struct open_file *, ustoffs, char block[512]));
131 1.1 ross
132 1.1 ross static int
133 1.1 ross convert(f, base, fw)
134 1.1 ross const char *f;
135 1.1 ross int base, fw;
136 1.1 ross {
137 1.1 ross int i, c, result = 0;
138 1.1 ross
139 1.1 ross while(fw > 0 && *f == ' ') {
140 1.1 ross --fw;
141 1.1 ross ++f;
142 1.1 ross }
143 1.1 ross for(i = 0; i < fw; ++i) {
144 1.1 ross c = f[i];
145 1.1 ross if ('0' <= c && c < '0' + base) {
146 1.1 ross c -= '0';
147 1.1 ross result = result * base + c;
148 1.1 ross } else break;
149 1.1 ross }
150 1.1 ross return result;
151 1.1 ross }
152 1.1 ross
153 1.1 ross static void
154 1.1 ross ustarfs_sscanf(s,f,xi)
155 1.1 ross const char *s,*f;
156 1.1 ross int *xi;
157 1.1 ross {
158 1.1 ross *xi = convert(s, 8, convert(f + 1, 10, 99));
159 1.1 ross }
160 1.1 ross
161 1.1 ross static int
162 1.1 ross ustarfs_cylinder_read(f, seek2)
163 1.1 ross struct open_file *f;
164 1.2 ross ustoffs seek2;
165 1.1 ross {
166 1.1 ross int e;
167 1.1 ross size_t xfercount;
168 1.1 ross ust_active_t *ustf;
169 1.1 ross
170 1.1 ross ustf = f->f_fsdata;
171 1.2 ross e = f->f_dev->dv_strategy(f->f_devdata, F_READ, seek2 / 512,
172 1.1 ross sizeof ustf->uas_1cyl, ustf->uas_1cyl, &xfercount);
173 1.2 ross if (e == 0 && xfercount != sizeof ustf->uas_1cyl) {
174 1.1 ross printf("Warning, unexpected short transfer %d/%d\n",
175 1.1 ross (int)xfercount, (int) sizeof ustf->uas_1cyl);
176 1.2 ross return EIO;
177 1.2 ross }
178 1.1 ross return e;
179 1.1 ross }
180 1.1 ross
181 1.1 ross static int
182 1.2 ross checksig(ustf)
183 1.2 ross ust_active_t *ustf;
184 1.2 ross {
185 1.2 ross int i, rcs;
186 1.2 ross
187 1.2 ross for(i = rcs = 0; i < sizeof ustf->uas_1cyl; ++i)
188 1.2 ross rcs += ustf->uas_1cyl[i];
189 1.2 ross return rcs;
190 1.2 ross }
191 1.2 ross
192 1.2 ross static int
193 1.2 ross get_volume (f, vn)
194 1.1 ross struct open_file *f;
195 1.2 ross int vn;
196 1.2 ross {
197 1.2 ross int e, needvolume, havevolume;
198 1.2 ross ust_active_t *ustf;
199 1.2 ross
200 1.2 ross ustf = f->f_fsdata;
201 1.2 ross havevolume = vda2vn(ustf->uas_windowbase, ustf->uas_volsize);
202 1.2 ross needvolume = vn;
203 1.2 ross while(havevolume != needvolume) {
204 1.2 ross printf("\nPlease ");
205 1.2 ross if (havevolume >= 0)
206 1.2 ross printf("remove disk %d, ", havevolume + 1);
207 1.2 ross printf("insert disk %d, and type return...",
208 1.2 ross needvolume + 1);
209 1.2 ross getchar();
210 1.2 ross printf("\n");
211 1.2 ross e = ustarfs_cylinder_read(f, 0);
212 1.2 ross if (e)
213 1.2 ross return e;
214 1.2 ross if(strncmp(formatid, ustf->uas_1cyl, strlen(formatid))) {
215 1.2 ross /* no magic, might be OK if we want volume 0 */
216 1.2 ross if (ustf->uas_volzerosig == checksig(ustf)) {
217 1.2 ross havevolume = 0;
218 1.2 ross continue;
219 1.2 ross }
220 1.2 ross printf("Disk is not from the volume set?!\n");
221 1.2 ross havevolume = -2;
222 1.2 ross continue;
223 1.2 ross }
224 1.2 ross ustarfs_sscanf(ustf->uas_1cyl + strlen(formatid), "%9o",
225 1.2 ross &havevolume);
226 1.2 ross --havevolume;
227 1.2 ross }
228 1.2 ross return 0;
229 1.2 ross }
230 1.2 ross
231 1.2 ross static int
232 1.2 ross read512block(f, vda, block)
233 1.2 ross struct open_file *f;
234 1.2 ross ustoffs vda;
235 1.1 ross char block[512];
236 1.1 ross {
237 1.2 ross ustoffs pda, lda;
238 1.1 ross ssize_t e;
239 1.2 ross int dienow;
240 1.1 ross ust_active_t *ustf;
241 1.1 ross
242 1.2 ross dienow = 0;
243 1.1 ross ustf = f->f_fsdata;
244 1.2 ross
245 1.2 ross if (!ustf->uas_init_window
246 1.2 ross && ustf->uas_windowbase == 0) {
247 1.2 ross /*
248 1.2 ross * The algorithm doesn't require this, but without it we would
249 1.2 ross * need some trick to get the cylinder zero signature computed.
250 1.2 ross * That signature is used to identify volume zero, which we
251 1.2 ross * don't give a USTARFS label to. (It's platform-dependent.)
252 1.2 ross */
253 1.2 ross e = ustarfs_cylinder_read(f, 0);
254 1.2 ross if (e)
255 1.2 ross return e;
256 1.2 ross ustf->uas_volzerosig = checksig(ustf);
257 1.2 ross ustf->uas_windowbase = lda2vda(pda2lda(0), ustf->uas_volsize, 0);
258 1.2 ross ustf->uas_init_window = 1;
259 1.2 ross }
260 1.2 ross /*
261 1.2 ross * if (vda in window)
262 1.2 ross * copy out and return data
263 1.2 ross * if (vda is on some other disk)
264 1.2 ross * do disk swap
265 1.2 ross * get physical disk address
266 1.2 ross * round down to cylinder boundary
267 1.2 ross * read cylindar
268 1.2 ross * set window (in vda space) and try again
269 1.2 ross * [ there is an implicit assumption that windowbase always identifies
270 1.2 ross * the current volume, even if initwindow == 0. This way, a
271 1.2 ross * windowbase of 0 causes the initial volume to be disk 0 ]
272 1.2 ross */
273 1.1 ross tryagain:
274 1.1 ross if(ustf->uas_init_window
275 1.2 ross && ustf->uas_windowbase <= vda && vda <
276 1.2 ross ustf->uas_windowbase + sizeof ustf->uas_1cyl - ustf->uas_offset) {
277 1.2 ross memcpy(block, ustf->uas_1cyl
278 1.2 ross + (vda - ustf->uas_windowbase)
279 1.2 ross + ustf->uas_offset, 512);
280 1.1 ross return 0;
281 1.1 ross }
282 1.1 ross if (dienow++)
283 1.1 ross panic("ustarfs read512block");
284 1.2 ross ustf->uas_init_window = 0;
285 1.2 ross e = get_volume(f, vda2vn(vda, ustf->uas_volsize));
286 1.2 ross if (e)
287 1.2 ross return e;
288 1.2 ross pda = lda2pda(vda2lda(vda, ustf->uas_volsize));
289 1.2 ross pda-= pda % sizeof ustf->uas_1cyl;
290 1.2 ross e = ustarfs_cylinder_read(f, pda);
291 1.1 ross if (e)
292 1.1 ross return e;
293 1.2 ross lda = pda2lda(pda);
294 1.2 ross if (lda < 0) {
295 1.2 ross ustf->uas_offset = -lda;
296 1.2 ross lda = 0;
297 1.2 ross } else
298 1.2 ross ustf->uas_offset = 0;
299 1.2 ross ustf->uas_windowbase = lda2vda(lda, ustf->uas_volsize,
300 1.2 ross vda2vn(vda, ustf->uas_volsize));
301 1.1 ross ustf->uas_init_window = 1;
302 1.1 ross goto tryagain;
303 1.1 ross }
304 1.1 ross
305 1.1 ross int
306 1.1 ross ustarfs_open(path, f)
307 1.1 ross char *path;
308 1.1 ross struct open_file *f;
309 1.1 ross
310 1.1 ross {
311 1.1 ross ust_active_t *ustf;
312 1.2 ross ustoffs offset;
313 1.1 ross char block[512];
314 1.1 ross int filesize;
315 1.1 ross int e, e2;
316 1.1 ross
317 1.1 ross if (*path == '/')
318 1.1 ross ++path;
319 1.1 ross e = EINVAL;
320 1.1 ross f->f_fsdata = ustf = alloc(sizeof *ustf);
321 1.1 ross memset(ustf, 0, sizeof *ustf);
322 1.2 ross offset = 0;
323 1.2 ross /* XXX -- hardwired for floppy */
324 1.2 ross ustf->uas_volsize = 80 * 2 * 18 * 512 - ustarfs_mode_offset;
325 1.1 ross ustf->uas_fseek = 0;
326 1.1 ross for(;;) {
327 1.1 ross ustf->uas_filestart = offset;
328 1.1 ross e2 = read512block(f, offset, block);
329 1.1 ross if (e2) {
330 1.1 ross e = e2;
331 1.1 ross break;
332 1.1 ross }
333 1.1 ross memcpy(&ustf->uas_active, block, sizeof ustf->uas_active);
334 1.1 ross if(strncmp(ustf->uas_active.ust_magic, "ustar", 5))
335 1.1 ross break;
336 1.1 ross e = ENOENT; /* it must be an actual ustarfs */
337 1.1 ross ustf->uas_init_fs = 1;
338 1.1 ross ustarfs_sscanf(ustf->uas_active.ust_size,"%12o",&filesize);
339 1.1 ross if(strncmp(ustf->uas_active.ust_name, path,
340 1.1 ross sizeof ustf->uas_active.ust_name) == 0) {
341 1.1 ross ustf->uas_filesize = filesize;
342 1.1 ross e = 0;
343 1.1 ross break;
344 1.1 ross }
345 1.1 ross offset += USTAR_NAME_BLOCK + filesize;
346 1.1 ross filesize %= 512;
347 1.1 ross if (filesize)
348 1.1 ross offset += 512 - filesize;
349 1.1 ross }
350 1.1 ross if (e) {
351 1.1 ross free(ustf, sizeof *ustf);
352 1.1 ross f->f_fsdata = 0;
353 1.1 ross }
354 1.1 ross return e;
355 1.1 ross }
356 1.1 ross
357 1.1 ross int
358 1.1 ross ustarfs_write(f, start, size, resid)
359 1.1 ross struct open_file *f;
360 1.1 ross void *start;
361 1.1 ross size_t size;
362 1.1 ross size_t *resid;
363 1.1 ross {
364 1.1 ross return (EROFS);
365 1.1 ross }
366 1.1 ross
367 1.1 ross off_t
368 1.1 ross ustarfs_seek(f, offs, whence)
369 1.1 ross struct open_file *f;
370 1.1 ross off_t offs;
371 1.1 ross int whence;
372 1.1 ross {
373 1.1 ross ust_active_t *ustf;
374 1.1 ross
375 1.1 ross ustf = f->f_fsdata;
376 1.1 ross switch (whence) {
377 1.1 ross case SEEK_SET:
378 1.1 ross ustf->uas_fseek = offs;
379 1.1 ross break;
380 1.1 ross case SEEK_CUR:
381 1.1 ross ustf->uas_fseek += offs;
382 1.1 ross break;
383 1.1 ross case SEEK_END:
384 1.1 ross ustf->uas_fseek = ustf->uas_filesize - offs;
385 1.1 ross break;
386 1.1 ross default:
387 1.1 ross return -1;
388 1.1 ross }
389 1.1 ross return ustf->uas_fseek;
390 1.1 ross }
391 1.1 ross
392 1.1 ross int
393 1.1 ross ustarfs_read(f, start, size, resid)
394 1.1 ross struct open_file *f;
395 1.1 ross void *start;
396 1.1 ross size_t size;
397 1.1 ross size_t *resid;
398 1.1 ross {
399 1.1 ross ust_active_t *ustf;
400 1.1 ross int e;
401 1.1 ross char *space512;
402 1.1 ross int blkoffs,
403 1.1 ross readoffs,
404 1.1 ross bufferoffset;
405 1.1 ross size_t seg;
406 1.1 ross int infile,
407 1.1 ross inbuffer;
408 1.1 ross
409 1.1 ross e = 0;
410 1.1 ross space512 = alloc(512);
411 1.1 ross ustf = f->f_fsdata;
412 1.1 ross while(size != 0) {
413 1.1 ross if (ustf->uas_fseek >= ustf->uas_filesize)
414 1.1 ross break;
415 1.1 ross bufferoffset = ustf->uas_fseek % 512;
416 1.1 ross blkoffs = ustf->uas_fseek - bufferoffset;
417 1.1 ross readoffs = ustf->uas_filestart + 512 + blkoffs;
418 1.1 ross e = read512block(f, readoffs, space512);
419 1.1 ross if (e)
420 1.1 ross break;
421 1.1 ross seg = size;
422 1.1 ross inbuffer = 512 - bufferoffset;
423 1.1 ross if (inbuffer < seg)
424 1.1 ross seg = inbuffer;
425 1.1 ross infile = ustf->uas_filesize - ustf->uas_fseek;
426 1.1 ross if (infile < seg)
427 1.1 ross seg = infile;
428 1.1 ross memcpy(start, space512 + bufferoffset, seg);
429 1.1 ross ustf->uas_fseek += seg;
430 1.1 ross start += seg;
431 1.1 ross size -= seg;
432 1.1 ross }
433 1.1 ross if (resid)
434 1.1 ross *resid = size;
435 1.1 ross free(space512, 512);
436 1.1 ross return e;
437 1.1 ross }
438 1.1 ross
439 1.1 ross int
440 1.1 ross ustarfs_stat(f, sb)
441 1.1 ross struct open_file *f;
442 1.1 ross struct stat *sb;
443 1.1 ross {
444 1.1 ross int mode, uid, gid;
445 1.1 ross ust_active_t *ustf;
446 1.1 ross
447 1.1 ross if (f == NULL)
448 1.1 ross return EINVAL;
449 1.1 ross ustf = f->f_fsdata;
450 1.1 ross memset(sb, 0, sizeof *sb);
451 1.1 ross ustarfs_sscanf(ustf->uas_active.ust_mode, "%8o", &mode);
452 1.1 ross ustarfs_sscanf(ustf->uas_active.ust_uid, "%8o", &uid);
453 1.1 ross ustarfs_sscanf(ustf->uas_active.ust_gid, "%8o", &gid);
454 1.1 ross sb->st_mode = mode;
455 1.1 ross sb->st_uid = uid;
456 1.1 ross sb->st_gid = gid;
457 1.1 ross sb->st_size = ustf->uas_filesize;
458 1.1 ross return 0;
459 1.1 ross }
460 1.1 ross
461 1.1 ross int
462 1.1 ross ustarfs_close(f)
463 1.1 ross struct open_file *f;
464 1.1 ross {
465 1.1 ross if (f == NULL || f->f_fsdata == NULL)
466 1.1 ross return EINVAL;
467 1.1 ross free(f->f_fsdata, sizeof(ust_active_t));
468 1.1 ross f->f_fsdata = 0;
469 1.1 ross return 0;
470 1.1 ross }
471