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