xref: /netbsd-src/sys/lib/libsa/ustarfs.c (revision 23c8222edbfb0f0932d88a8351d3a0cf817dfb9e)
1 /*	$NetBSD: ustarfs.c,v 1.23 2003/08/31 22:40:49 fvdl 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 const 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 < (int)(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 		for (;;) {
288 			int c = getchar();
289 			if ((c == '\n') || (c == '\r'))
290 				break;
291 		}
292 #endif
293 		printf("\n");
294 		e = ustarfs_cylinder_read(f, 0, needvolume != 0);
295 		if (e)
296 			return e;
297 		if(strncmp(formatid, ustf->uas_1cyl, strlen(formatid))) {
298 			/* no magic, might be OK if we want volume 0 */
299 			if (ustf->uas_volzerosig == checksig(ustf)) {
300 				havevolume = 0;
301 				continue;
302 			}
303 			printf("Disk is not from the volume set?!\n");
304 			havevolume = -2;
305 			continue;
306 		}
307 		ustarfs_sscanf(ustf->uas_1cyl + strlen(formatid), "%9o",
308 			&havevolume);
309 		--havevolume;
310 	}
311 	return 0;
312 }
313 
314 static void
315 setwindow(ust_active_t *ustf, ustoffs pda, ustoffs vda)
316 {
317 	ustf->uas_windowbase = lda2vda(pda2lda(pda), ustf->uas_volsize,
318 					vda2vn(vda, ustf->uas_volsize))
319 			     + ustf->uas_offset;
320 	ustf->uas_init_window = 1;
321 }
322 
323 static int
324 read512block(f, vda, block)
325 	struct open_file *f;
326 	ustoffs vda;
327 	char block[512];
328 {
329 	ustoffs pda;
330 	ssize_t	e;
331 	int	dienow;
332 	ust_active_t *ustf;
333 
334 	dienow = 0;
335 	ustf = f->f_fsdata;
336 
337 	/*
338 	 * if (vda in window)
339 	 * 	copy out and return data
340 	 * if (vda is on some other disk)
341 	 * 	do disk swap
342 	 * get physical disk address
343 	 * round down to cylinder boundary
344 	 * read cylinder
345 	 * set window (in vda space) and try again
346 	 * [ there is an implicit assumption that windowbase always identifies
347 	 *    the current volume, even if initwindow == 0. This way, a
348 	 *    windowbase of 0 causes the initial volume to be disk 0 ]
349 	 */
350 tryagain:
351 	if(ustf->uas_init_window
352 	&& ustf->uas_windowbase <= vda && vda <
353 	   ustf->uas_windowbase +
354 	     (int)(sizeof ustf->uas_1cyl) - ustf->uas_offset) {
355 		memcpy(block, ustf->uas_1cyl
356 				+ (vda - ustf->uas_windowbase)
357 				+ ustf->uas_offset, 512);
358 		return 0;
359 	}
360 	if (dienow++)
361 		panic("ustarfs read512block");
362 	ustf->uas_init_window = 0;
363 	e = get_volume(f, vda2vn(vda, ustf->uas_volsize));
364 	if (e)
365 		return e;
366 	pda = lda2pda(vda2lda(vda, ustf->uas_volsize));
367 	pda-= pda % sizeof ustf->uas_1cyl;
368 	e = ustarfs_cylinder_read(f, pda, 0);
369 	if (e)
370 		return e;
371 	setwindow(ustf, pda, vda);
372 	goto tryagain;
373 }
374 
375 static int
376 init_volzero_sig(f)
377 	struct open_file *f;
378 {
379 	int e;
380 	ust_active_t *ustf;
381 
382 	ustf = f->f_fsdata;
383 	if (!ustf->uas_sigdone) {
384 		e = ustarfs_cylinder_read(f, 0, 0);
385 		if (e)
386 			return e;
387 		ustf->uas_volzerosig = checksig(ustf);
388 		setwindow(ustf, 0, 0);
389 	}
390 	return 0;
391 }
392 
393 int
394 ustarfs_open(path, f)
395 	const char *path;
396 	struct open_file *f;
397 
398 {
399 	ust_active_t *ustf;
400 	ustoffs offset;
401 	char	block[512];
402 	int	filesize;
403 	int	e, e2;
404 	int	newvolblocks;
405 
406 	if (*path == '/')
407 		++path;
408 	f->f_fsdata = ustf = alloc(sizeof *ustf);
409 	memset(ustf, 0, sizeof *ustf);
410 	offset = 0;
411 	/* default to 2880 sector floppy */
412 	ustf->uas_volsize = 80 * 2 * 18 * 512 - lda2pda(0);
413 	ustf->uas_fseek = 0;
414 	e = init_volzero_sig(f);
415 	if (e)
416 		return e;
417 	e2 = EINVAL;
418 	for(;;) {
419 		ustf->uas_filestart = offset;
420 		e = read512block(f, offset, block);
421 		if (e)
422 			break;
423 		memcpy(&ustf->uas_active, block, sizeof ustf->uas_active);
424 		if(strncmp(ustf->uas_active.ust_magic, "ustar", 5)) {
425 			e = e2;
426 			break;
427 		}
428 		e2 = ENOENT;	/* it must be an actual ustarfs */
429 		ustf->uas_init_fs = 1;
430 		/* if volume metadata is found, use it */
431 		if(strncmp(ustf->uas_active.ust_name, metaname,
432 		    strlen(metaname)) == 0) {
433 			ustarfs_sscanf(ustf->uas_active.ust_name
434 				+ strlen(metaname), "%99o", &newvolblocks);
435 			ustf->uas_volsize = newvolblocks * 512
436 					  - lda2pda(0);
437 		}
438 		ustarfs_sscanf(ustf->uas_active.ust_size,"%12o",&filesize);
439 		if(strncmp(ustf->uas_active.ust_name, path,
440 		    sizeof ustf->uas_active.ust_name) == 0) {
441 			ustf->uas_filesize = filesize;
442 			break;
443 		}
444 		offset += USTAR_NAME_BLOCK + filesize;
445 		filesize %= 512;
446 		if (filesize)
447 			offset += 512 - filesize;
448 	}
449 	if (e) {
450 		free(ustf, sizeof *ustf);
451 		f->f_fsdata = 0;
452 	}
453 	return e;
454 }
455 
456 #ifndef LIBSA_NO_FS_WRITE
457 int
458 ustarfs_write(f, start, size, resid)
459 	struct open_file *f;
460 	void *start;
461 	size_t size;
462 	size_t *resid;
463 {
464 	return (EROFS);
465 }
466 #endif /* !LIBSA_NO_FS_WRITE */
467 
468 #ifndef LIBSA_NO_FS_SEEK
469 off_t
470 ustarfs_seek(f, offs, whence)
471 	struct open_file *f;
472 	off_t offs;
473 	int whence;
474 {
475 	ust_active_t *ustf;
476 
477 	ustf = f->f_fsdata;
478 	switch (whence) {
479 	    case SEEK_SET:
480 		ustf->uas_fseek = offs;
481 		break;
482 	    case SEEK_CUR:
483 		ustf->uas_fseek += offs;
484 		break;
485 	    case SEEK_END:
486 		ustf->uas_fseek = ustf->uas_filesize - offs;
487 		break;
488 	    default:
489 		return -1;
490 	}
491 	return ustf->uas_fseek;
492 }
493 #endif /* !LIBSA_NO_FS_SEEK */
494 
495 int
496 ustarfs_read(f, start, size, resid)
497 	struct open_file *f;
498 	void *start;
499 	size_t size;
500 	size_t *resid;
501 {
502 	ust_active_t *ustf;
503 	int	e;
504 	char	*space512;
505 	int	blkoffs,
506 		readoffs,
507 		bufferoffset;
508 	size_t	seg;
509 	size_t	infile,
510 		inbuffer;
511 
512 	e = 0;
513 	space512 = alloc(512);
514 	ustf = f->f_fsdata;
515 	while(size != 0) {
516 		if (ustf->uas_fseek >= ustf->uas_filesize)
517 			break;
518 		bufferoffset = ustf->uas_fseek % 512;
519 		blkoffs  = ustf->uas_fseek - bufferoffset;
520 		readoffs = ustf->uas_filestart + 512 + blkoffs;
521 		e = read512block(f, readoffs, space512);
522 		if (e)
523 			break;
524 		seg = size;
525 		inbuffer = 512 - bufferoffset;
526 		if (inbuffer < seg)
527 			seg = inbuffer;
528 		infile = ustf->uas_filesize - ustf->uas_fseek;
529 		if (infile < seg)
530 			seg = infile;
531 		memcpy(start, space512 + bufferoffset, seg);
532 		ustf->uas_fseek += seg;
533 		start = (caddr_t)start + seg;
534 		size  -= seg;
535 	}
536 	if (resid)
537 		*resid = size;
538 	free(space512, 512);
539 	return e;
540 }
541 
542 int
543 ustarfs_stat(f, sb)
544 	struct open_file *f;
545 	struct stat *sb;
546 {
547 	int	mode, uid, gid;
548 	ust_active_t *ustf;
549 
550 	if (f == NULL)
551 		return EINVAL;
552 	ustf = f->f_fsdata;
553 	memset(sb, 0, sizeof *sb);
554 	ustarfs_sscanf(ustf->uas_active.ust_mode, "%8o", &mode);
555 	ustarfs_sscanf(ustf->uas_active.ust_uid, "%8o", &uid);
556 	ustarfs_sscanf(ustf->uas_active.ust_gid, "%8o", &gid);
557 	sb->st_mode = mode;
558 	sb->st_uid  = uid;
559 	sb->st_gid  = gid;
560 	sb->st_size = ustf->uas_filesize;
561 	return 0;
562 }
563 
564 #ifndef LIBSA_NO_FS_CLOSE
565 int
566 ustarfs_close(f)
567 	struct open_file *f;
568 {
569 	if (f == NULL || f->f_fsdata == NULL)
570 		return EINVAL;
571 	free(f->f_fsdata, sizeof(ust_active_t));
572 	f->f_fsdata = 0;
573 	return 0;
574 }
575 #endif /* !LIBSA_NO_FS_CLOSE */
576