ustarfs.c revision 1.10 1 /* $NetBSD: ustarfs.c,v 1.10 1999/09/01 02:32:26 ross Exp $ */
2
3 /* [Notice revision 2.2]
4 * Copyright (c) 1997, 1998 Avalon Computer Systems, Inc.
5 * All rights reserved.
6 *
7 * Author: Ross Harvey
8 *
9 * Redistribution and use in source and binary forms, with or without
10 * modification, are permitted provided that the following conditions
11 * are met:
12 * 1. Redistributions of source code must retain the above copyright and
13 * author notice, this list of conditions, and the following disclaimer.
14 * 2. Redistributions in binary form must reproduce the above copyright
15 * notice, this list of conditions and the following disclaimer in the
16 * documentation and/or other materials provided with the distribution.
17 * 3. Neither the name of Avalon Computer Systems, Inc. nor the names of
18 * its contributors may be used to endorse or promote products derived
19 * from this software without specific prior written permission.
20 * 4. This copyright will be assigned to The NetBSD Foundation on
21 * 1/1/2000 unless these terms (including possibly the assignment
22 * date) are updated in writing by Avalon prior to the latest specified
23 * assignment date.
24 *
25 * THIS SOFTWARE IS PROVIDED BY AVALON COMPUTER SYSTEMS, INC. AND CONTRIBUTORS
26 * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
27 * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
28 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL AVALON OR THE CONTRIBUTORS
29 * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
30 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
31 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
32 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
33 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
34 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
35 * POSSIBILITY OF SUCH DAMAGE.
36 */
37
38
39 /*
40 ******************************* USTAR FS *******************************
41 */
42
43 /*
44 * Implement an ROFS with an 8K boot area followed by ustar-format data.
45 * The point: minimal FS overhead, and it's easy (well, `possible') to
46 * split files over multiple volumes.
47 *
48 * XXX - TODO LIST
49 * --- - ---- ----
50 * XXX - tag volume numbers and verify that the correct volume is
51 * inserted after volume swaps.
52 *
53 * XXX - stop hardwiring FS metadata for floppies...embed it in a file,
54 * file name, or something. (Remember __SYMDEF? :-)
55 *
56 * XXX Does not currently implement:
57 * XXX
58 * XXX LIBSA_NO_FS_CLOSE
59 * XXX LIBSA_NO_FS_SEEK
60 * XXX LIBSA_NO_FS_WRITE
61 * XXX LIBSA_NO_FS_SYMLINK (does this even make sense?)
62 * XXX LIBSA_FS_SINGLECOMPONENT
63 */
64
65 #ifdef _STANDALONE
66 #include <lib/libkern/libkern.h>
67 #else
68 #include <string.h>
69 #endif
70 #include "stand.h"
71 #include "ustarfs.h"
72
73 #define BBSIZE 8192
74 #define USTAR_NAME_BLOCK 512
75
76 /*
77 * Virtual offset: relative to start of ustar archive
78 * Logical offset: volume-relative
79 * Physical offset: the usual meaning
80 */
81
82 /* virtual offset to volume number */
83
84 #define vda2vn(_v,_volsize) ((_v) / (_volsize))
85
86 /* conversions between the three different levels of disk addresses */
87
88 #define vda2lda(_v,_volsize) ((_v) % (_volsize))
89 #define lda2vda(_v,_volsize,_volnumber) ((_v) + (_volsize) * (_volnumber))
90
91 #define lda2pda(_lda) ((_lda) + ustarfs_mode_offset)
92 #define pda2lda(_pda) ((_pda) - ustarfs_mode_offset)
93 /*
94 * Change this to off_t if you want to support big volumes. If we only use
95 * ustarfs on floppies it can stay int for libsa code density.
96 *
97 * It needs to be signed.
98 */
99 typedef int ustoffs;
100
101 typedef struct ustar_struct {
102 char ust_name[100],
103 ust_mode[8],
104 ust_uid[8],
105 ust_gid[8],
106 ust_size[12],
107 ust_misc[12 + 8 + 1 + 100],
108 ust_magic[6];
109 /* there is more, but we don't care */
110 } ustar_t;
111
112 /*
113 * We buffer one even cylindar of data...it's actually only really one
114 * cyl on a 1.44M floppy, but on other devices it's fast enough with any
115 * kind of block buffering, so we optimize for the slowest device.
116 */
117
118 typedef struct ust_active_struct {
119 ustar_t uas_active;
120 char uas_1cyl[18 * 2 * 512];
121 ustoffs uas_volsize; /* XXX this is hardwired now */
122 ustoffs uas_windowbase; /* relative to volume 0 */
123 ustoffs uas_filestart; /* relative to volume 0 */
124 ustoffs uas_fseek; /* relative to file */
125 ustoffs uas_filesize; /* relative to volume 0 */
126 int uas_init_window; /* data present in window */
127 int uas_init_fs; /* ust FS actually found */
128 int uas_volzerosig; /* ID volume 0 by signature */
129 int uas_offset; /* amount of cylinder below lba 0 */
130 } ust_active_t;
131
132 static const char formatid[] = "USTARFS",
133 metaname[] = "USTAR.volsize.";
134
135 static int ustarfs_mode_offset = BBSIZE;
136
137 static int checksig __P((ust_active_t *));
138 static int convert __P((const char *, int, int));
139 static int get_volume __P((struct open_file *, int));
140 static void setwindow(ust_active_t *, ustoffs, ustoffs);
141 static int ustarfs_cylinder_read __P((struct open_file *, ustoffs, int));
142 static void ustarfs_sscanf __P((const char *, const char *, int *));
143 static int read512block __P((struct open_file *, ustoffs, char block[512]));
144
145 static int
146 convert(f, base, fw)
147 const char *f;
148 int base, fw;
149 {
150 int i, c, result = 0;
151
152 while(fw > 0 && *f == ' ') {
153 --fw;
154 ++f;
155 }
156 for(i = 0; i < fw; ++i) {
157 c = f[i];
158 if ('0' <= c && c < '0' + base) {
159 c -= '0';
160 result = result * base + c;
161 } else break;
162 }
163 return result;
164 }
165
166 static void
167 ustarfs_sscanf(s,f,xi)
168 const char *s,*f;
169 int *xi;
170 {
171 *xi = convert(s, 8, convert(f + 1, 10, 99));
172 }
173
174 static int
175 ustarfs_cylinder_read(f, seek2, forcelabel)
176 struct open_file *f;
177 ustoffs seek2;
178 {
179 int i;
180 int e = 0; /* XXX work around gcc warning */
181 ustoffs lda;
182 char *xferbase;
183 ust_active_t *ustf;
184 size_t xferrqst, xfercount;
185
186 ustf = f->f_fsdata;
187 xferrqst = sizeof ustf->uas_1cyl;
188 xferbase = ustf->uas_1cyl;
189 lda = pda2lda(seek2);
190 if (lda < 0) {
191 lda = -lda;
192 ustf->uas_offset = lda;
193 /*
194 * don't read the label unless we have to. (Preserve
195 * sequential block access so tape boot works.)
196 */
197 if (!forcelabel) {
198 memset(xferbase, 0, lda);
199 xferrqst -= lda;
200 xferbase += lda;
201 seek2 += lda;
202 }
203 } else
204 ustf->uas_offset = 0;
205 while(xferrqst > 0) {
206 #if !defined(LIBSA_NO_TWIDDLE)
207 twiddle();
208 #endif
209 for (i = 0; i < 3; ++i) {
210 e = DEV_STRATEGY(f->f_dev)(f->f_devdata, F_READ,
211 seek2 / 512, xferrqst, xferbase, &xfercount);
212 if (e == 0)
213 break;
214 printf("@");
215 }
216 if (e)
217 break;
218 if (xfercount != xferrqst)
219 printf("Warning, unexpected short transfer %d/%d\n",
220 (int)xfercount, (int)xferrqst);
221 xferrqst -= xfercount;
222 xferbase += xfercount;
223 seek2 += xfercount;
224 }
225 return e;
226 }
227
228 static int
229 checksig(ustf)
230 ust_active_t *ustf;
231 {
232 int i, rcs;
233
234 for(i = rcs = 0; i < sizeof ustf->uas_1cyl; ++i)
235 rcs += ustf->uas_1cyl[i];
236 return rcs;
237 }
238
239 static int
240 get_volume(f, vn)
241 struct open_file *f;
242 int vn;
243 {
244 int e, needvolume, havevolume;
245 ust_active_t *ustf;
246
247 ustf = f->f_fsdata;
248 havevolume = vda2vn(ustf->uas_windowbase, ustf->uas_volsize);
249 needvolume = vn;
250 while(havevolume != needvolume) {
251 printf("\nPlease ");
252 if (havevolume >= 0)
253 printf("remove disk %d, ", havevolume + 1);
254 printf("insert disk %d, and type return...",
255 needvolume + 1);
256 getchar();
257 printf("\n");
258 e = ustarfs_cylinder_read(f, 0, 1);
259 if (e) /* Try again on error, needed on i386 */
260 e = ustarfs_cylinder_read(f, 0, 1);
261 if (e)
262 return e;
263 if(strncmp(formatid, ustf->uas_1cyl, strlen(formatid))) {
264 /* no magic, might be OK if we want volume 0 */
265 if (ustf->uas_volzerosig == checksig(ustf)) {
266 havevolume = 0;
267 continue;
268 }
269 printf("Disk is not from the volume set?!\n");
270 havevolume = -2;
271 continue;
272 }
273 ustarfs_sscanf(ustf->uas_1cyl + strlen(formatid), "%9o",
274 &havevolume);
275 --havevolume;
276 }
277 return 0;
278 }
279
280 static void
281 setwindow(ust_active_t *ustf, ustoffs pda, ustoffs vda)
282 {
283 ustf->uas_windowbase = lda2vda(pda2lda(pda), ustf->uas_volsize,
284 vda2vn(vda, ustf->uas_volsize))
285 + ustf->uas_offset;
286 ustf->uas_init_window = 1;
287 }
288
289 static int
290 read512block(f, vda, block)
291 struct open_file *f;
292 ustoffs vda;
293 char block[512];
294 {
295 ustoffs pda;
296 ssize_t e;
297 int dienow;
298 ust_active_t *ustf;
299
300 dienow = 0;
301 ustf = f->f_fsdata;
302
303 if (!ustf->uas_init_window
304 && ustf->uas_windowbase == 0) {
305 /*
306 * The algorithm doesn't require this, but without it we would
307 * need some trick to get the cylinder zero signature computed.
308 * That signature is used to identify volume zero, which we
309 * don't give a USTARFS label to. (It's platform-dependent.)
310 */
311 e = ustarfs_cylinder_read(f, 0, 0);
312 if (e)
313 return e;
314 ustf->uas_volzerosig = checksig(ustf);
315 setwindow(ustf, 0, 0);
316 }
317 /*
318 * if (vda in window)
319 * copy out and return data
320 * if (vda is on some other disk)
321 * do disk swap
322 * get physical disk address
323 * round down to cylinder boundary
324 * read cylindar
325 * set window (in vda space) and try again
326 * [ there is an implicit assumption that windowbase always identifies
327 * the current volume, even if initwindow == 0. This way, a
328 * windowbase of 0 causes the initial volume to be disk 0 ]
329 */
330 tryagain:
331 if(ustf->uas_init_window
332 && ustf->uas_windowbase <= vda && vda <
333 ustf->uas_windowbase + sizeof ustf->uas_1cyl - ustf->uas_offset) {
334 memcpy(block, ustf->uas_1cyl
335 + (vda - ustf->uas_windowbase)
336 + ustf->uas_offset, 512);
337 return 0;
338 }
339 if (dienow++)
340 panic("ustarfs read512block");
341 ustf->uas_init_window = 0;
342 e = get_volume(f, vda2vn(vda, ustf->uas_volsize));
343 if (e)
344 return e;
345 pda = lda2pda(vda2lda(vda, ustf->uas_volsize));
346 pda-= pda % sizeof ustf->uas_1cyl;
347 e = ustarfs_cylinder_read(f, pda, 0);
348 if (e)
349 return e;
350 setwindow(ustf, pda, vda);
351 goto tryagain;
352 }
353
354 int
355 ustarfs_open(path, f)
356 char *path;
357 struct open_file *f;
358
359 {
360 ust_active_t *ustf;
361 ustoffs offset;
362 char block[512];
363 int filesize;
364 int e, e2;
365 int newvolblocks;
366
367 if (*path == '/')
368 ++path;
369 e = EINVAL;
370 f->f_fsdata = ustf = alloc(sizeof *ustf);
371 memset(ustf, 0, sizeof *ustf);
372 offset = 0;
373 /* default to 2880 sector floppy */
374 ustf->uas_volsize = 80 * 2 * 18 * 512 - lda2pda(0);
375 ustf->uas_fseek = 0;
376 for(;;) {
377 ustf->uas_filestart = offset;
378 e2 = read512block(f, offset, block);
379 if (e2) {
380 e = e2;
381 break;
382 }
383 memcpy(&ustf->uas_active, block, sizeof ustf->uas_active);
384 if(strncmp(ustf->uas_active.ust_magic, "ustar", 5))
385 break;
386 e = ENOENT; /* it must be an actual ustarfs */
387 ustf->uas_init_fs = 1;
388 /* if volume metadata is found, use it */
389 if(strncmp(ustf->uas_active.ust_name, metaname,
390 strlen(metaname)) == 0) {
391 ustarfs_sscanf(ustf->uas_active.ust_name
392 + strlen(metaname), "%99o", &newvolblocks);
393 ustf->uas_volsize = newvolblocks * 512
394 - lda2pda(0);
395 }
396 ustarfs_sscanf(ustf->uas_active.ust_size,"%12o",&filesize);
397 if(strncmp(ustf->uas_active.ust_name, path,
398 sizeof ustf->uas_active.ust_name) == 0) {
399 ustf->uas_filesize = filesize;
400 e = 0;
401 break;
402 }
403 offset += USTAR_NAME_BLOCK + filesize;
404 filesize %= 512;
405 if (filesize)
406 offset += 512 - filesize;
407 }
408 if (e) {
409 free(ustf, sizeof *ustf);
410 f->f_fsdata = 0;
411 }
412 return e;
413 }
414
415 #ifndef LIBSA_NO_FS_WRITE
416 int
417 ustarfs_write(f, start, size, resid)
418 struct open_file *f;
419 void *start;
420 size_t size;
421 size_t *resid;
422 {
423 return (EROFS);
424 }
425 #endif /* !LIBSA_NO_FS_WRITE */
426
427 #ifndef LIBSA_NO_FS_SEEK
428 off_t
429 ustarfs_seek(f, offs, whence)
430 struct open_file *f;
431 off_t offs;
432 int whence;
433 {
434 ust_active_t *ustf;
435
436 ustf = f->f_fsdata;
437 switch (whence) {
438 case SEEK_SET:
439 ustf->uas_fseek = offs;
440 break;
441 case SEEK_CUR:
442 ustf->uas_fseek += offs;
443 break;
444 case SEEK_END:
445 ustf->uas_fseek = ustf->uas_filesize - offs;
446 break;
447 default:
448 return -1;
449 }
450 return ustf->uas_fseek;
451 }
452 #endif /* !LIBSA_NO_FS_CLOSE */
453
454 int
455 ustarfs_read(f, start, size, resid)
456 struct open_file *f;
457 void *start;
458 size_t size;
459 size_t *resid;
460 {
461 ust_active_t *ustf;
462 int e;
463 char *space512;
464 int blkoffs,
465 readoffs,
466 bufferoffset;
467 size_t seg;
468 int infile,
469 inbuffer;
470
471 e = 0;
472 space512 = alloc(512);
473 ustf = f->f_fsdata;
474 while(size != 0) {
475 if (ustf->uas_fseek >= ustf->uas_filesize)
476 break;
477 bufferoffset = ustf->uas_fseek % 512;
478 blkoffs = ustf->uas_fseek - bufferoffset;
479 readoffs = ustf->uas_filestart + 512 + blkoffs;
480 e = read512block(f, readoffs, space512);
481 if (e)
482 break;
483 seg = size;
484 inbuffer = 512 - bufferoffset;
485 if (inbuffer < seg)
486 seg = inbuffer;
487 infile = ustf->uas_filesize - ustf->uas_fseek;
488 if (infile < seg)
489 seg = infile;
490 memcpy(start, space512 + bufferoffset, seg);
491 ustf->uas_fseek += seg;
492 start = (caddr_t)start + seg;
493 size -= seg;
494 }
495 if (resid)
496 *resid = size;
497 free(space512, 512);
498 return e;
499 }
500
501 int
502 ustarfs_stat(f, sb)
503 struct open_file *f;
504 struct stat *sb;
505 {
506 int mode, uid, gid;
507 ust_active_t *ustf;
508
509 if (f == NULL)
510 return EINVAL;
511 ustf = f->f_fsdata;
512 memset(sb, 0, sizeof *sb);
513 ustarfs_sscanf(ustf->uas_active.ust_mode, "%8o", &mode);
514 ustarfs_sscanf(ustf->uas_active.ust_uid, "%8o", &uid);
515 ustarfs_sscanf(ustf->uas_active.ust_gid, "%8o", &gid);
516 sb->st_mode = mode;
517 sb->st_uid = uid;
518 sb->st_gid = gid;
519 sb->st_size = ustf->uas_filesize;
520 return 0;
521 }
522
523 #ifndef LIBSA_NO_FS_CLOSE
524 int
525 ustarfs_close(f)
526 struct open_file *f;
527 {
528 if (f == NULL || f->f_fsdata == NULL)
529 return EINVAL;
530 free(f->f_fsdata, sizeof(ust_active_t));
531 f->f_fsdata = 0;
532 return 0;
533 }
534 #endif /* !LIBSA_NO_FS_CLOSE */
535