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