xref: /netbsd/sys/lib/libsa/ustarfs.c (revision bf9ec67e)
1 /*	$NetBSD: ustarfs.c,v 1.19 2002/05/10 11:07:01 lukem 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 		ust_pad[1];	/* make it aligned */
111 } ustar_t;
112 
113 /*
114  * We buffer one even cylinder of data...it's actually only really one
115  * cyl on a 1.44M floppy, but on other devices it's fast enough with any
116  * kind of block buffering, so we optimize for the slowest device.
117  */
118 
119 typedef struct ust_active_struct {
120 	ustar_t	uas_active;
121 	char	uas_1cyl[18 * 2 * 512];
122 	ustoffs	uas_volsize;		/* XXX this is hardwired now */
123 	ustoffs	uas_windowbase;		/* relative to volume 0 */
124 	ustoffs	uas_filestart;		/* relative to volume 0 */
125 	ustoffs	uas_fseek;		/* relative to file */
126 	ustoffs	uas_filesize;		/* relative to volume 0 */
127 	int	uas_init_window;	/* data present in window */
128 	int	uas_init_fs;		/* ust FS actually found */
129 	int	uas_volzerosig;		/* ID volume 0 by signature */
130 	int	uas_sigdone;		/* did sig already */
131 	int	uas_offset;		/* amount of cylinder below lba 0 */
132 } ust_active_t;
133 
134 static const char formatid[] = "USTARFS",
135 		  metaname[] = "USTAR.volsize.";
136 
137 static int ustarfs_mode_offset = BBSIZE;
138 
139 static int checksig __P((ust_active_t *));
140 static int convert __P((const char *, int, int));
141 static int get_volume __P((struct open_file *, int));
142 static void setwindow(ust_active_t *, ustoffs, ustoffs);
143 static int real_fs_cylinder_read __P((struct open_file *, ustoffs, int));
144 static int ustarfs_cylinder_read __P((struct open_file *, ustoffs, int));
145 static void ustarfs_sscanf __P((const char *, const char *, int *));
146 static int read512block __P((struct open_file *, ustoffs, char block[512]));
147 static int init_volzero_sig __P((struct open_file *));
148 
149 #ifdef HAVE_CHANGEDISK_HOOK
150 /*
151  * Called when the next volume is prompted.
152  * Machine dependent code can eject the medium etc.
153  * The new medium must be ready when this hook returns.
154  */
155 void changedisk_hook __P((struct open_file *));
156 #endif
157 
158 static int
159 convert(f, base, fw)
160 	const char *f;
161 	int base, fw;
162 {
163 	int	i, c, result = 0;
164 
165 	while(fw > 0 && *f == ' ') {
166 		--fw;
167 		++f;
168 	}
169 	for(i = 0; i < fw; ++i) {
170 		c = f[i];
171 		if ('0' <= c && c < '0' + base) {
172 			c -= '0';
173 			result = result * base + c;
174 		} else	break;
175 	}
176 	return result;
177 }
178 
179 static void
180 ustarfs_sscanf(s,f,xi)
181 	const char *s,*f;
182 	int *xi;
183 {
184 	*xi = convert(s, 8, convert(f + 1, 10, 99));
185 }
186 
187 static int
188 ustarfs_cylinder_read(f, seek2, forcelabel)
189 	struct open_file *f;
190 	ustoffs seek2;
191 {
192 	int i, e;
193 
194 	for (i = 0; i < 3; ++i) {
195 		e = real_fs_cylinder_read(f, seek2, forcelabel);
196 		if (e == 0)
197 			return 0;
198 	}
199 	return e;
200 }
201 
202 static int
203 real_fs_cylinder_read(f, seek2, forcelabel)
204 	struct open_file *f;
205 	ustoffs seek2;
206 {
207 	int i;
208 	int e = 0;	/* XXX work around gcc warning */
209 	ustoffs	lda;
210 	char *xferbase;
211 	ust_active_t *ustf;
212 	size_t	xferrqst, xfercount;
213 
214 	ustf = f->f_fsdata;
215 	xferrqst = sizeof ustf->uas_1cyl;
216 	xferbase = ustf->uas_1cyl;
217 	lda = pda2lda(seek2);
218 	if (lda < 0) {
219 		lda = -lda;
220 		ustf->uas_offset = lda;
221 		/*
222 		 * don't read the label unless we have to. (Preserve
223 		 * sequential block access so tape boot works.)
224 		 */
225 		if (!forcelabel) {
226 			memset(xferbase, 0, lda);
227 			xferrqst -= lda;
228 			xferbase += lda;
229 			seek2    += lda;
230 		}
231 	} else
232 		ustf->uas_offset = 0;
233 	while(xferrqst > 0) {
234 #if !defined(LIBSA_NO_TWIDDLE)
235 		twiddle();
236 #endif
237 		for (i = 0; i < 3; ++i) {
238 			e = DEV_STRATEGY(f->f_dev)(f->f_devdata, F_READ,
239 			    seek2 / 512, xferrqst, xferbase, &xfercount);
240 			if (e == 0)
241 				break;
242 			printf("@");
243 		}
244 		if (e)
245 			break;
246 		if (xfercount != xferrqst)
247 			printf("Warning, unexpected short transfer %d/%d\n",
248 				(int)xfercount, (int)xferrqst);
249 		xferrqst -= xfercount;
250 		xferbase += xfercount;
251 		seek2    += xfercount;
252 	}
253 	return e;
254 }
255 
256 static int
257 checksig(ustf)
258 	ust_active_t *ustf;
259 {
260 	int	i, rcs;
261 
262 	for(i = rcs = 0; i < sizeof ustf->uas_1cyl; ++i)
263 		rcs += ustf->uas_1cyl[i];
264 	return rcs;
265 }
266 
267 static int
268 get_volume(f, vn)
269 	struct open_file *f;
270 	int vn;
271 {
272 	int	e, needvolume, havevolume;
273 	ust_active_t *ustf;
274 
275 	ustf = f->f_fsdata;
276 	havevolume = vda2vn(ustf->uas_windowbase, ustf->uas_volsize);
277 	needvolume = vn;
278 	while(havevolume != needvolume) {
279 		printf("\nPlease ");
280 		if (havevolume >= 0)
281 			printf("remove disk %d, ", havevolume + 1);
282 		printf("insert disk %d, and press return...",
283 			needvolume + 1);
284 #ifdef HAVE_CHANGEDISK_HOOK
285 		changedisk_hook(f);
286 #else
287 		getchar();
288 #endif
289 		printf("\n");
290 		e = ustarfs_cylinder_read(f, 0, needvolume != 0);
291 		if (e)
292 			return e;
293 		if(strncmp(formatid, ustf->uas_1cyl, strlen(formatid))) {
294 			/* no magic, might be OK if we want volume 0 */
295 			if (ustf->uas_volzerosig == checksig(ustf)) {
296 				havevolume = 0;
297 				continue;
298 			}
299 			printf("Disk is not from the volume set?!\n");
300 			havevolume = -2;
301 			continue;
302 		}
303 		ustarfs_sscanf(ustf->uas_1cyl + strlen(formatid), "%9o",
304 			&havevolume);
305 		--havevolume;
306 	}
307 	return 0;
308 }
309 
310 static void
311 setwindow(ust_active_t *ustf, ustoffs pda, ustoffs vda)
312 {
313 	ustf->uas_windowbase = lda2vda(pda2lda(pda), ustf->uas_volsize,
314 					vda2vn(vda, ustf->uas_volsize))
315 			     + ustf->uas_offset;
316 	ustf->uas_init_window = 1;
317 }
318 
319 static int
320 read512block(f, vda, block)
321 	struct open_file *f;
322 	ustoffs vda;
323 	char block[512];
324 {
325 	ustoffs pda;
326 	ssize_t	e;
327 	int	dienow;
328 	ust_active_t *ustf;
329 
330 	dienow = 0;
331 	ustf = f->f_fsdata;
332 
333 	/*
334 	 * if (vda in window)
335 	 * 	copy out and return data
336 	 * if (vda is on some other disk)
337 	 * 	do disk swap
338 	 * get physical disk address
339 	 * round down to cylinder boundary
340 	 * read cylinder
341 	 * set window (in vda space) and try again
342 	 * [ there is an implicit assumption that windowbase always identifies
343 	 *    the current volume, even if initwindow == 0. This way, a
344 	 *    windowbase of 0 causes the initial volume to be disk 0 ]
345 	 */
346 tryagain:
347 	if(ustf->uas_init_window
348 	&& ustf->uas_windowbase <= vda && vda <
349 	   ustf->uas_windowbase + sizeof ustf->uas_1cyl - ustf->uas_offset) {
350 		memcpy(block, ustf->uas_1cyl
351 				+ (vda - ustf->uas_windowbase)
352 				+ ustf->uas_offset, 512);
353 		return 0;
354 	}
355 	if (dienow++)
356 		panic("ustarfs read512block");
357 	ustf->uas_init_window = 0;
358 	e = get_volume(f, vda2vn(vda, ustf->uas_volsize));
359 	if (e)
360 		return e;
361 	pda = lda2pda(vda2lda(vda, ustf->uas_volsize));
362 	pda-= pda % sizeof ustf->uas_1cyl;
363 	e = ustarfs_cylinder_read(f, pda, 0);
364 	if (e)
365 		return e;
366 	setwindow(ustf, pda, vda);
367 	goto tryagain;
368 }
369 
370 static int
371 init_volzero_sig(f)
372 	struct open_file *f;
373 {
374 	int e;
375 	ust_active_t *ustf;
376 
377 	ustf = f->f_fsdata;
378 	if (!ustf->uas_sigdone) {
379 		e = ustarfs_cylinder_read(f, 0, 0);
380 		if (e)
381 			return e;
382 		ustf->uas_volzerosig = checksig(ustf);
383 		setwindow(ustf, 0, 0);
384 	}
385 	return 0;
386 }
387 
388 int
389 ustarfs_open(path, f)
390 	char *path;
391 	struct open_file *f;
392 
393 {
394 	ust_active_t *ustf;
395 	ustoffs offset;
396 	char	block[512];
397 	int	filesize;
398 	int	e, e2;
399 	int	newvolblocks;
400 
401 	if (*path == '/')
402 		++path;
403 	f->f_fsdata = ustf = alloc(sizeof *ustf);
404 	memset(ustf, 0, sizeof *ustf);
405 	offset = 0;
406 	/* default to 2880 sector floppy */
407 	ustf->uas_volsize = 80 * 2 * 18 * 512 - lda2pda(0);
408 	ustf->uas_fseek = 0;
409 	e = init_volzero_sig(f);
410 	if (e)
411 		return e;
412 	e2 = EINVAL;
413 	for(;;) {
414 		ustf->uas_filestart = offset;
415 		e = read512block(f, offset, block);
416 		if (e)
417 			break;
418 		memcpy(&ustf->uas_active, block, sizeof ustf->uas_active);
419 		if(strncmp(ustf->uas_active.ust_magic, "ustar", 5)) {
420 			e = e2;
421 			break;
422 		}
423 		e2 = ENOENT;	/* it must be an actual ustarfs */
424 		ustf->uas_init_fs = 1;
425 		/* if volume metadata is found, use it */
426 		if(strncmp(ustf->uas_active.ust_name, metaname,
427 		    strlen(metaname)) == 0) {
428 			ustarfs_sscanf(ustf->uas_active.ust_name
429 				+ strlen(metaname), "%99o", &newvolblocks);
430 			ustf->uas_volsize = newvolblocks * 512
431 					  - lda2pda(0);
432 		}
433 		ustarfs_sscanf(ustf->uas_active.ust_size,"%12o",&filesize);
434 		if(strncmp(ustf->uas_active.ust_name, path,
435 		    sizeof ustf->uas_active.ust_name) == 0) {
436 			ustf->uas_filesize = filesize;
437 			break;
438 		}
439 		offset += USTAR_NAME_BLOCK + filesize;
440 		filesize %= 512;
441 		if (filesize)
442 			offset += 512 - filesize;
443 	}
444 	if (e) {
445 		free(ustf, sizeof *ustf);
446 		f->f_fsdata = 0;
447 	}
448 	return e;
449 }
450 
451 #ifndef LIBSA_NO_FS_WRITE
452 int
453 ustarfs_write(f, start, size, resid)
454 	struct open_file *f;
455 	void *start;
456 	size_t size;
457 	size_t *resid;
458 {
459 	return (EROFS);
460 }
461 #endif /* !LIBSA_NO_FS_WRITE */
462 
463 #ifndef LIBSA_NO_FS_SEEK
464 off_t
465 ustarfs_seek(f, offs, whence)
466 	struct open_file *f;
467 	off_t offs;
468 	int whence;
469 {
470 	ust_active_t *ustf;
471 
472 	ustf = f->f_fsdata;
473 	switch (whence) {
474 	    case SEEK_SET:
475 		ustf->uas_fseek = offs;
476 		break;
477 	    case SEEK_CUR:
478 		ustf->uas_fseek += offs;
479 		break;
480 	    case SEEK_END:
481 		ustf->uas_fseek = ustf->uas_filesize - offs;
482 		break;
483 	    default:
484 		return -1;
485 	}
486 	return ustf->uas_fseek;
487 }
488 #endif /* !LIBSA_NO_FS_SEEK */
489 
490 int
491 ustarfs_read(f, start, size, resid)
492 	struct open_file *f;
493 	void *start;
494 	size_t size;
495 	size_t *resid;
496 {
497 	ust_active_t *ustf;
498 	int	e;
499 	char	*space512;
500 	int	blkoffs,
501 		readoffs,
502 		bufferoffset;
503 	size_t	seg;
504 	int	infile,
505 		inbuffer;
506 
507 	e = 0;
508 	space512 = alloc(512);
509 	ustf = f->f_fsdata;
510 	while(size != 0) {
511 		if (ustf->uas_fseek >= ustf->uas_filesize)
512 			break;
513 		bufferoffset = ustf->uas_fseek % 512;
514 		blkoffs  = ustf->uas_fseek - bufferoffset;
515 		readoffs = ustf->uas_filestart + 512 + blkoffs;
516 		e = read512block(f, readoffs, space512);
517 		if (e)
518 			break;
519 		seg = size;
520 		inbuffer = 512 - bufferoffset;
521 		if (inbuffer < seg)
522 			seg = inbuffer;
523 		infile = ustf->uas_filesize - ustf->uas_fseek;
524 		if (infile < seg)
525 			seg = infile;
526 		memcpy(start, space512 + bufferoffset, seg);
527 		ustf->uas_fseek += seg;
528 		start = (caddr_t)start + seg;
529 		size  -= seg;
530 	}
531 	if (resid)
532 		*resid = size;
533 	free(space512, 512);
534 	return e;
535 }
536 
537 int
538 ustarfs_stat(f, sb)
539 	struct open_file *f;
540 	struct stat *sb;
541 {
542 	int	mode, uid, gid;
543 	ust_active_t *ustf;
544 
545 	if (f == NULL)
546 		return EINVAL;
547 	ustf = f->f_fsdata;
548 	memset(sb, 0, sizeof *sb);
549 	ustarfs_sscanf(ustf->uas_active.ust_mode, "%8o", &mode);
550 	ustarfs_sscanf(ustf->uas_active.ust_uid, "%8o", &uid);
551 	ustarfs_sscanf(ustf->uas_active.ust_gid, "%8o", &gid);
552 	sb->st_mode = mode;
553 	sb->st_uid  = uid;
554 	sb->st_gid  = gid;
555 	sb->st_size = ustf->uas_filesize;
556 	return 0;
557 }
558 
559 #ifndef LIBSA_NO_FS_CLOSE
560 int
561 ustarfs_close(f)
562 	struct open_file *f;
563 {
564 	if (f == NULL || f->f_fsdata == NULL)
565 		return EINVAL;
566 	free(f->f_fsdata, sizeof(ust_active_t));
567 	f->f_fsdata = 0;
568 	return 0;
569 }
570 #endif /* !LIBSA_NO_FS_CLOSE */
571