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