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