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