1170477Salc/* $NetBSD: ustarfs.c,v 1.37 2019/11/21 21:45:34 mrg Exp $ */ 2170477Salc 3170477Salc/* [Notice revision 2.2] 4170477Salc * Copyright (c) 1997, 1998 Avalon Computer Systems, Inc. 5170477Salc * All rights reserved. 6170477Salc * 7170477Salc * Author: Ross Harvey 8170477Salc * 9170477Salc * Redistribution and use in source and binary forms, with or without 10170477Salc * modification, are permitted provided that the following conditions 11170477Salc * are met: 12170477Salc * 1. Redistributions of source code must retain the above copyright and 13170477Salc * author notice, this list of conditions, and the following disclaimer. 14170477Salc * 2. Redistributions in binary form must reproduce the above copyright 15170477Salc * notice, this list of conditions and the following disclaimer in the 16170477Salc * documentation and/or other materials provided with the distribution. 17170477Salc * 3. Neither the name of Avalon Computer Systems, Inc. nor the names of 18170477Salc * its contributors may be used to endorse or promote products derived 19170477Salc * from this software without specific prior written permission. 20170477Salc * 4. This copyright will be assigned to The NetBSD Foundation on 21170477Salc * 1/1/2000 unless these terms (including possibly the assignment 22170477Salc * date) are updated in writing by Avalon prior to the latest specified 23170477Salc * assignment date. 24170477Salc * 25170477Salc * THIS SOFTWARE IS PROVIDED BY AVALON COMPUTER SYSTEMS, INC. AND CONTRIBUTORS 26170477Salc * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED 27170477Salc * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR 28170477Salc * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL AVALON OR THE CONTRIBUTORS 29170477Salc * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR 30170477Salc * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF 31170477Salc * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS 32170477Salc * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN 33170477Salc * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) 34170477Salc * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE 35170477Salc * POSSIBILITY OF SUCH DAMAGE. 36170477Salc */ 37170477Salc 38170477Salc 39170477Salc/* 40170477Salc ******************************* USTAR FS ******************************* 41170477Salc */ 42170477Salc 43170477Salc/* 44170477Salc * Implement an ROFS with an 8K boot area followed by ustar-format data. 45170477Salc * The point: minimal FS overhead, and it's easy (well, `possible') to 46170477Salc * split files over multiple volumes. 47170477Salc * 48170477Salc * XXX - TODO LIST 49170477Salc * --- - ---- ---- 50170477Salc * XXX - tag volume numbers and verify that the correct volume is 51170477Salc * inserted after volume swaps. 52170477Salc * 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 */ 99typedef int ustoffs; 100 101typedef 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 ust_pad[1]; /* make it aligned */ 111} ustar_t; 112 113/* 114 * We buffer one even cylinder of data...it's actually only really one 115 * cyl on a 1.44M floppy, but on other devices it's fast enough with any 116 * kind of block buffering, so we optimize for the slowest device. 117 */ 118 119#ifndef USTAR_SECT_PER_CYL 120#define USTAR_SECT_PER_CYL (18 * 2) 121#endif 122 123typedef struct ust_active_struct { 124 ustar_t uas_active; 125 char uas_1cyl[USTAR_SECT_PER_CYL * 512]; 126 ustoffs uas_volsize; /* XXX this is hardwired now */ 127 ustoffs uas_windowbase; /* relative to volume 0 */ 128 ustoffs uas_filestart; /* relative to volume 0 */ 129 ustoffs uas_fseek; /* relative to file */ 130 ustoffs uas_filesize; /* relative to volume 0 */ 131 int uas_init_window; /* data present in window */ 132 int uas_init_fs; /* ust FS actually found */ 133 int uas_volzerosig; /* ID volume 0 by signature */ 134 int uas_sigdone; /* did sig already */ 135 int uas_offset; /* amount of cylinder below lba 0 */ 136} ust_active_t; 137 138static const char formatid[] = "USTARFS", 139 metaname[] = "USTAR.volsize."; 140 141static const int ustarfs_mode_offset = BBSIZE; 142 143static int checksig(ust_active_t *); 144static int convert(const char *, int, int); 145static int get_volume(struct open_file *, int); 146static void setwindow(ust_active_t *, ustoffs, ustoffs); 147static int real_fs_cylinder_read(struct open_file *, ustoffs, int); 148static int ustarfs_cylinder_read(struct open_file *, ustoffs, int); 149static void ustarfs_sscanf(const char *, const char *, int *); 150static int read512block(struct open_file *, ustoffs, char block[512]); 151static int init_volzero_sig(struct open_file *); 152 153#ifdef HAVE_CHANGEDISK_HOOK 154/* 155 * Called when the next volume is prompted. 156 * Machine dependent code can eject the medium etc. 157 * The new medium must be ready when this hook returns. 158 */ 159void changedisk_hook(struct open_file *); 160#endif 161 162static int 163convert(const char *f, int base, int fw) 164{ 165 int i, c, result = 0; 166 167 while(fw > 0 && *f == ' ') { 168 --fw; 169 ++f; 170 } 171 for(i = 0; i < fw; ++i) { 172 c = f[i]; 173 if ('0' <= c && c < '0' + base) { 174 c -= '0'; 175 result = result * base + c; 176 } else break; 177 } 178 return result; 179} 180 181static void 182ustarfs_sscanf(const char *s, const char *f, int *xi) 183{ 184 185 *xi = convert(s, 8, convert(f + 1, 10, 99)); 186} 187 188static int 189ustarfs_cylinder_read(struct open_file *f, ustoffs seek2, int forcelabel) 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 201static int 202real_fs_cylinder_read(struct open_file *f, ustoffs seek2, int forcelabel) 203{ 204 int i; 205 int e = 0; /* XXX work around gcc warning */ 206 ustoffs lda; 207 char *xferbase; 208 ust_active_t *ustf; 209 size_t xferrqst, xfercount; 210 211 ustf = f->f_fsdata; 212 xferrqst = sizeof ustf->uas_1cyl; 213 xferbase = ustf->uas_1cyl; 214 lda = pda2lda(seek2); 215 if (lda < 0) { 216 lda = -lda; 217 ustf->uas_offset = lda; 218 /* 219 * don't read the label unless we have to. (Preserve 220 * sequential block access so tape boot works.) 221 */ 222 if (!forcelabel) { 223 memset(xferbase, 0, lda); 224 xferrqst -= lda; 225 xferbase += lda; 226 seek2 += lda; 227 } 228 } else { 229 ustf->uas_offset = 0; 230 } 231 while(xferrqst > 0) { 232#if !defined(LIBSA_NO_TWIDDLE) 233 twiddle(); 234#endif 235 for (i = 0; i < 3; ++i) { 236 e = DEV_STRATEGY(f->f_dev)(f->f_devdata, F_READ, 237 seek2 / 512, xferrqst, xferbase, &xfercount); 238 if (e == 0) 239 break; 240 printf("@"); 241 } 242 if (e) 243 break; 244 if (xfercount != xferrqst) 245 printf("Warning, unexpected short transfer %d/%d\n", 246 (int)xfercount, (int)xferrqst); 247 xferrqst -= xfercount; 248 xferbase += xfercount; 249 seek2 += xfercount; 250 } 251 return e; 252} 253 254static int 255checksig(ust_active_t *ustf) 256{ 257 int i, rcs; 258 259 for(i = rcs = 0; i < (int)(sizeof ustf->uas_1cyl); ++i) 260 rcs += ustf->uas_1cyl[i]; 261 return rcs; 262} 263 264static int 265get_volume(struct open_file *f, int vn) 266{ 267 int e, needvolume, havevolume; 268 ust_active_t *ustf; 269 270 ustf = f->f_fsdata; 271 havevolume = vda2vn(ustf->uas_windowbase, ustf->uas_volsize); 272 needvolume = vn; 273 while(havevolume != needvolume) { 274 printf("\nPlease "); 275 if (havevolume >= 0) 276 printf("remove disk %d, ", havevolume + 1); 277 printf("insert disk %d, and press return...", 278 needvolume + 1); 279#ifdef HAVE_CHANGEDISK_HOOK 280 changedisk_hook(f); 281#else 282 for (;;) { 283 int c = getchar(); 284 if ((c == '\n') || (c == '\r')) 285 break; 286 } 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 309static void 310setwindow(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 318static int 319read512block(struct open_file *f, ustoffs vda, char block[512]) 320{ 321 ustoffs pda; 322 ssize_t e; 323 int dienow; 324 ust_active_t *ustf; 325 326 dienow = 0; 327 ustf = f->f_fsdata; 328 329 /* 330 * if (vda in window) 331 * copy out and return data 332 * if (vda is on some other disk) 333 * do disk swap 334 * get physical disk address 335 * round down to cylinder boundary 336 * read cylinder 337 * set window (in vda space) and try again 338 * [ there is an implicit assumption that windowbase always identifies 339 * the current volume, even if initwindow == 0. This way, a 340 * windowbase of 0 causes the initial volume to be disk 0 ] 341 */ 342tryagain: 343 if(ustf->uas_init_window 344 && ustf->uas_windowbase <= vda && vda < 345 ustf->uas_windowbase + 346 (int)(sizeof ustf->uas_1cyl) - ustf->uas_offset) { 347 memcpy(block, ustf->uas_1cyl 348 + (vda - ustf->uas_windowbase) 349 + ustf->uas_offset, 512); 350 return 0; 351 } 352 if (dienow++) 353 panic("ustarfs read512block"); 354 ustf->uas_init_window = 0; 355 e = get_volume(f, vda2vn(vda, ustf->uas_volsize)); 356 if (e) 357 return e; 358 pda = lda2pda(vda2lda(vda, ustf->uas_volsize)); 359 pda-= pda % sizeof ustf->uas_1cyl; 360 e = ustarfs_cylinder_read(f, pda, 0); 361 if (e) 362 return e; 363 setwindow(ustf, pda, vda); 364 goto tryagain; 365} 366 367static int 368init_volzero_sig(struct open_file *f) 369{ 370 int e; 371 ust_active_t *ustf; 372 373 ustf = f->f_fsdata; 374 if (!ustf->uas_sigdone) { 375 e = ustarfs_cylinder_read(f, 0, 0); 376 if (e) 377 return e; 378 ustf->uas_volzerosig = checksig(ustf); 379 setwindow(ustf, 0, 0); 380 } 381 return 0; 382} 383 384/* 385 * XXX Hack alert. GCC 8.3 mis-compiles this function and calls 386 * strncmp() with the wrong second pointer, as seen in PR#54703. 387 * 388 * Until the real cause is located, work around it by using -O1 389 * for this function. 390 */ 391#if defined(__i386__) && !defined(__clang__) 392__attribute__((__optimize__("O1"))) 393#endif 394__compactcall int 395ustarfs_open(const char *path, struct open_file *f) 396{ 397 ust_active_t *ustf; 398 ustoffs offset; 399 char block[512]; 400 int filesize; 401 int e, e2; 402 int newvolblocks; 403 404 if (*path == '/') 405 ++path; 406 f->f_fsdata = ustf = alloc(sizeof *ustf); 407 memset(ustf, 0, sizeof *ustf); 408 offset = 0; 409 /* default to 2880 sector floppy */ 410 ustf->uas_volsize = 80 * 2 * 18 * 512 - lda2pda(0); 411 ustf->uas_fseek = 0; 412 e = init_volzero_sig(f); 413 if (e) 414 return e; 415 e2 = EINVAL; 416 for(;;) { 417 ustf->uas_filestart = offset; 418 e = read512block(f, offset, block); 419 if (e) 420 break; 421 memcpy(&ustf->uas_active, block, sizeof ustf->uas_active); 422 if(strncmp(ustf->uas_active.ust_magic, "ustar", 5)) { 423 e = e2; 424 break; 425 } 426 e2 = ENOENT; /* it must be an actual ustarfs */ 427 ustf->uas_init_fs = 1; 428 /* if volume metadata is found, use it */ 429 if(strncmp(ustf->uas_active.ust_name, metaname, 430 strlen(metaname)) == 0) { 431 ustarfs_sscanf(ustf->uas_active.ust_name 432 + strlen(metaname), "%99o", &newvolblocks); 433 ustf->uas_volsize = newvolblocks * 512 434 - lda2pda(0); 435 } 436 ustarfs_sscanf(ustf->uas_active.ust_size,"%12o",&filesize); 437 if(strncmp(ustf->uas_active.ust_name, path, 438 sizeof ustf->uas_active.ust_name) == 0) { 439 ustf->uas_filesize = filesize; 440 break; 441 } 442 offset += USTAR_NAME_BLOCK + filesize; 443 filesize %= 512; 444 if (filesize) 445 offset += 512 - filesize; 446 } 447 if (e) { 448 dealloc(ustf, sizeof *ustf); 449 f->f_fsdata = 0; 450 } 451 return e; 452} 453 454#ifndef LIBSA_NO_FS_WRITE 455__compactcall int 456ustarfs_write(struct open_file *f, void *start, size_t size, size_t *resid) 457{ 458 459 return EROFS; 460} 461#endif /* !LIBSA_NO_FS_WRITE */ 462 463#ifndef LIBSA_NO_FS_SEEK 464__compactcall off_t 465ustarfs_seek(struct open_file *f, off_t offs, int whence) 466{ 467 ust_active_t *ustf; 468 469 ustf = f->f_fsdata; 470 switch (whence) { 471 case SEEK_SET: 472 ustf->uas_fseek = offs; 473 break; 474 case SEEK_CUR: 475 ustf->uas_fseek += offs; 476 break; 477 case SEEK_END: 478 ustf->uas_fseek = ustf->uas_filesize - offs; 479 break; 480 default: 481 return -1; 482 } 483 return ustf->uas_fseek; 484} 485#endif /* !LIBSA_NO_FS_SEEK */ 486 487__compactcall int 488ustarfs_read(struct open_file *f, void *start, size_t size, size_t *resid) 489{ 490 ust_active_t *ustf; 491 int e; 492 char *space512; 493 int blkoffs; 494 int readoffs; 495 int bufferoffset; 496 size_t seg; 497 size_t infile; 498 size_t inbuffer; 499 500 e = 0; 501 space512 = alloc(512); 502 ustf = f->f_fsdata; 503 while(size != 0) { 504 if (ustf->uas_fseek >= ustf->uas_filesize) 505 break; 506 bufferoffset = ustf->uas_fseek % 512; 507 blkoffs = ustf->uas_fseek - bufferoffset; 508 readoffs = ustf->uas_filestart + 512 + blkoffs; 509 e = read512block(f, readoffs, space512); 510 if (e) 511 break; 512 seg = size; 513 inbuffer = 512 - bufferoffset; 514 if (inbuffer < seg) 515 seg = inbuffer; 516 infile = ustf->uas_filesize - ustf->uas_fseek; 517 if (infile < seg) 518 seg = infile; 519 memcpy(start, space512 + bufferoffset, seg); 520 ustf->uas_fseek += seg; 521 start = (char *)start + seg; 522 size -= seg; 523 } 524 if (resid) 525 *resid = size; 526 dealloc(space512, 512); 527 return e; 528} 529 530__compactcall int 531ustarfs_stat(struct open_file *f, struct stat *sb) 532{ 533 int mode, uid, gid; 534 ust_active_t *ustf; 535 536 if (f == NULL) 537 return EINVAL; 538 ustf = f->f_fsdata; 539 memset(sb, 0, sizeof *sb); 540 ustarfs_sscanf(ustf->uas_active.ust_mode, "%8o", &mode); 541 ustarfs_sscanf(ustf->uas_active.ust_uid, "%8o", &uid); 542 ustarfs_sscanf(ustf->uas_active.ust_gid, "%8o", &gid); 543 sb->st_mode = mode; 544 sb->st_uid = uid; 545 sb->st_gid = gid; 546 sb->st_size = ustf->uas_filesize; 547 return 0; 548} 549 550 551#if defined(LIBSA_ENABLE_LS_OP) 552#include "ls.h" 553__compactcall void 554ustarfs_ls(struct open_file *f, const char *pattern) 555{ 556 lsunsup("ustarfs"); 557 return; 558} 559#endif 560 561#ifndef LIBSA_NO_FS_CLOSE 562__compactcall int 563ustarfs_close(struct open_file *f) 564{ 565 if (f == NULL || f->f_fsdata == NULL) 566 return EINVAL; 567 dealloc(f->f_fsdata, sizeof(ust_active_t)); 568 f->f_fsdata = 0; 569 return 0; 570} 571#endif /* !LIBSA_NO_FS_CLOSE */ 572