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