ustarfs.c revision 1.9 1 /* $NetBSD: ustarfs.c,v 1.9 1999/06/22 22:44:16 christos 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 e = 0; /* XXX work around gcc warning */
180 ustoffs lda;
181 char *xferbase;
182 ust_active_t *ustf;
183 size_t xferrqst, xfercount;
184
185 ustf = f->f_fsdata;
186 xferrqst = sizeof ustf->uas_1cyl;
187 xferbase = ustf->uas_1cyl;
188 lda = pda2lda(seek2);
189 if (lda < 0) {
190 lda = -lda;
191 ustf->uas_offset = lda;
192 /*
193 * don't read the label unless we have to. (Preserve
194 * sequential block access so tape boot works.)
195 */
196 if (!forcelabel) {
197 memset(xferbase, 0, lda);
198 xferrqst -= lda;
199 xferbase += lda;
200 seek2 += lda;
201 }
202 } else
203 ustf->uas_offset = 0;
204 while(xferrqst > 0) {
205 #if !defined(LIBSA_NO_TWIDDLE)
206 twiddle();
207 #endif
208 e = DEV_STRATEGY(f->f_dev)(f->f_devdata, F_READ, seek2 / 512,
209 xferrqst, xferbase, &xfercount);
210 if (e)
211 break;
212 if (xfercount != xferrqst)
213 printf("Warning, unexpected short transfer %d/%d\n",
214 (int)xfercount, (int)xferrqst);
215 xferrqst -= xfercount;
216 xferbase += xfercount;
217 }
218 return e;
219 }
220
221 static int
222 checksig(ustf)
223 ust_active_t *ustf;
224 {
225 int i, rcs;
226
227 for(i = rcs = 0; i < sizeof ustf->uas_1cyl; ++i)
228 rcs += ustf->uas_1cyl[i];
229 return rcs;
230 }
231
232 static int
233 get_volume(f, vn)
234 struct open_file *f;
235 int vn;
236 {
237 int e, needvolume, havevolume;
238 ust_active_t *ustf;
239
240 ustf = f->f_fsdata;
241 havevolume = vda2vn(ustf->uas_windowbase, ustf->uas_volsize);
242 needvolume = vn;
243 while(havevolume != needvolume) {
244 printf("\nPlease ");
245 if (havevolume >= 0)
246 printf("remove disk %d, ", havevolume + 1);
247 printf("insert disk %d, and type return...",
248 needvolume + 1);
249 getchar();
250 printf("\n");
251 e = ustarfs_cylinder_read(f, 0, 1);
252 if (e) /* Try again on error, needed on i386 */
253 e = ustarfs_cylinder_read(f, 0, 1);
254 if (e)
255 return e;
256 if(strncmp(formatid, ustf->uas_1cyl, strlen(formatid))) {
257 /* no magic, might be OK if we want volume 0 */
258 if (ustf->uas_volzerosig == checksig(ustf)) {
259 havevolume = 0;
260 continue;
261 }
262 printf("Disk is not from the volume set?!\n");
263 havevolume = -2;
264 continue;
265 }
266 ustarfs_sscanf(ustf->uas_1cyl + strlen(formatid), "%9o",
267 &havevolume);
268 --havevolume;
269 }
270 return 0;
271 }
272
273 static void
274 setwindow(ust_active_t *ustf, ustoffs pda, ustoffs vda)
275 {
276 ustf->uas_windowbase = lda2vda(pda2lda(pda), ustf->uas_volsize,
277 vda2vn(vda, ustf->uas_volsize))
278 + ustf->uas_offset;
279 ustf->uas_init_window = 1;
280 }
281
282 static int
283 read512block(f, vda, block)
284 struct open_file *f;
285 ustoffs vda;
286 char block[512];
287 {
288 ustoffs pda;
289 ssize_t e;
290 int dienow;
291 ust_active_t *ustf;
292
293 dienow = 0;
294 ustf = f->f_fsdata;
295
296 if (!ustf->uas_init_window
297 && ustf->uas_windowbase == 0) {
298 /*
299 * The algorithm doesn't require this, but without it we would
300 * need some trick to get the cylinder zero signature computed.
301 * That signature is used to identify volume zero, which we
302 * don't give a USTARFS label to. (It's platform-dependent.)
303 */
304 e = ustarfs_cylinder_read(f, 0, 0);
305 if (e)
306 return e;
307 ustf->uas_volzerosig = checksig(ustf);
308 setwindow(ustf, 0, 0);
309 }
310 /*
311 * if (vda in window)
312 * copy out and return data
313 * if (vda is on some other disk)
314 * do disk swap
315 * get physical disk address
316 * round down to cylinder boundary
317 * read cylindar
318 * set window (in vda space) and try again
319 * [ there is an implicit assumption that windowbase always identifies
320 * the current volume, even if initwindow == 0. This way, a
321 * windowbase of 0 causes the initial volume to be disk 0 ]
322 */
323 tryagain:
324 if(ustf->uas_init_window
325 && ustf->uas_windowbase <= vda && vda <
326 ustf->uas_windowbase + sizeof ustf->uas_1cyl - ustf->uas_offset) {
327 memcpy(block, ustf->uas_1cyl
328 + (vda - ustf->uas_windowbase)
329 + ustf->uas_offset, 512);
330 return 0;
331 }
332 if (dienow++)
333 panic("ustarfs read512block");
334 ustf->uas_init_window = 0;
335 e = get_volume(f, vda2vn(vda, ustf->uas_volsize));
336 if (e)
337 return e;
338 pda = lda2pda(vda2lda(vda, ustf->uas_volsize));
339 pda-= pda % sizeof ustf->uas_1cyl;
340 e = ustarfs_cylinder_read(f, pda, 0);
341 if (e)
342 return e;
343 setwindow(ustf, pda, vda);
344 goto tryagain;
345 }
346
347 int
348 ustarfs_open(path, f)
349 char *path;
350 struct open_file *f;
351
352 {
353 ust_active_t *ustf;
354 ustoffs offset;
355 char block[512];
356 int filesize;
357 int e, e2;
358 int newvolblocks;
359
360 if (*path == '/')
361 ++path;
362 e = EINVAL;
363 f->f_fsdata = ustf = alloc(sizeof *ustf);
364 memset(ustf, 0, sizeof *ustf);
365 offset = 0;
366 /* default to 2880 sector floppy */
367 ustf->uas_volsize = 80 * 2 * 18 * 512 - lda2pda(0);
368 ustf->uas_fseek = 0;
369 for(;;) {
370 ustf->uas_filestart = offset;
371 e2 = read512block(f, offset, block);
372 if (e2) {
373 e = e2;
374 break;
375 }
376 memcpy(&ustf->uas_active, block, sizeof ustf->uas_active);
377 if(strncmp(ustf->uas_active.ust_magic, "ustar", 5))
378 break;
379 e = ENOENT; /* it must be an actual ustarfs */
380 ustf->uas_init_fs = 1;
381 /* if volume metadata is found, use it */
382 if(strncmp(ustf->uas_active.ust_name, metaname,
383 strlen(metaname)) == 0) {
384 ustarfs_sscanf(ustf->uas_active.ust_name
385 + strlen(metaname), "%99o", &newvolblocks);
386 ustf->uas_volsize = newvolblocks * 512
387 - lda2pda(0);
388 }
389 ustarfs_sscanf(ustf->uas_active.ust_size,"%12o",&filesize);
390 if(strncmp(ustf->uas_active.ust_name, path,
391 sizeof ustf->uas_active.ust_name) == 0) {
392 ustf->uas_filesize = filesize;
393 e = 0;
394 break;
395 }
396 offset += USTAR_NAME_BLOCK + filesize;
397 filesize %= 512;
398 if (filesize)
399 offset += 512 - filesize;
400 }
401 if (e) {
402 free(ustf, sizeof *ustf);
403 f->f_fsdata = 0;
404 }
405 return e;
406 }
407
408 #ifndef LIBSA_NO_FS_WRITE
409 int
410 ustarfs_write(f, start, size, resid)
411 struct open_file *f;
412 void *start;
413 size_t size;
414 size_t *resid;
415 {
416 return (EROFS);
417 }
418 #endif /* !LIBSA_NO_FS_WRITE */
419
420 #ifndef LIBSA_NO_FS_SEEK
421 off_t
422 ustarfs_seek(f, offs, whence)
423 struct open_file *f;
424 off_t offs;
425 int whence;
426 {
427 ust_active_t *ustf;
428
429 ustf = f->f_fsdata;
430 switch (whence) {
431 case SEEK_SET:
432 ustf->uas_fseek = offs;
433 break;
434 case SEEK_CUR:
435 ustf->uas_fseek += offs;
436 break;
437 case SEEK_END:
438 ustf->uas_fseek = ustf->uas_filesize - offs;
439 break;
440 default:
441 return -1;
442 }
443 return ustf->uas_fseek;
444 }
445 #endif /* !LIBSA_NO_FS_CLOSE */
446
447 int
448 ustarfs_read(f, start, size, resid)
449 struct open_file *f;
450 void *start;
451 size_t size;
452 size_t *resid;
453 {
454 ust_active_t *ustf;
455 int e;
456 char *space512;
457 int blkoffs,
458 readoffs,
459 bufferoffset;
460 size_t seg;
461 int infile,
462 inbuffer;
463
464 e = 0;
465 space512 = alloc(512);
466 ustf = f->f_fsdata;
467 while(size != 0) {
468 if (ustf->uas_fseek >= ustf->uas_filesize)
469 break;
470 bufferoffset = ustf->uas_fseek % 512;
471 blkoffs = ustf->uas_fseek - bufferoffset;
472 readoffs = ustf->uas_filestart + 512 + blkoffs;
473 e = read512block(f, readoffs, space512);
474 if (e)
475 break;
476 seg = size;
477 inbuffer = 512 - bufferoffset;
478 if (inbuffer < seg)
479 seg = inbuffer;
480 infile = ustf->uas_filesize - ustf->uas_fseek;
481 if (infile < seg)
482 seg = infile;
483 memcpy(start, space512 + bufferoffset, seg);
484 ustf->uas_fseek += seg;
485 start = (caddr_t)start + seg;
486 size -= seg;
487 }
488 if (resid)
489 *resid = size;
490 free(space512, 512);
491 return e;
492 }
493
494 int
495 ustarfs_stat(f, sb)
496 struct open_file *f;
497 struct stat *sb;
498 {
499 int mode, uid, gid;
500 ust_active_t *ustf;
501
502 if (f == NULL)
503 return EINVAL;
504 ustf = f->f_fsdata;
505 memset(sb, 0, sizeof *sb);
506 ustarfs_sscanf(ustf->uas_active.ust_mode, "%8o", &mode);
507 ustarfs_sscanf(ustf->uas_active.ust_uid, "%8o", &uid);
508 ustarfs_sscanf(ustf->uas_active.ust_gid, "%8o", &gid);
509 sb->st_mode = mode;
510 sb->st_uid = uid;
511 sb->st_gid = gid;
512 sb->st_size = ustf->uas_filesize;
513 return 0;
514 }
515
516 #ifndef LIBSA_NO_FS_CLOSE
517 int
518 ustarfs_close(f)
519 struct open_file *f;
520 {
521 if (f == NULL || f->f_fsdata == NULL)
522 return EINVAL;
523 free(f->f_fsdata, sizeof(ust_active_t));
524 f->f_fsdata = 0;
525 return 0;
526 }
527 #endif /* !LIBSA_NO_FS_CLOSE */
528