1 /* $NetBSD: ustarfs.c,v 1.34 2011/12/25 06:09:08 tsutsui Exp $ */
3 /* [Notice revision 2.2]
4 * Copyright (c) 1997, 1998 Avalon Computer Systems, Inc.
9 * Redistribution and use in source and binary forms, with or without
10 * modification, are permitted provided that the following conditions
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
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.
40 ******************************* USTAR FS *******************************
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.
50 * XXX - tag volume numbers and verify that the correct volume is
51 * inserted after volume swaps.
53 * XXX - stop hardwiring FS metadata for floppies...embed it in a file,
54 * file name, or something. (Remember __SYMDEF? :-)
56 * XXX Does not currently implement:
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
66 #include <lib/libkern/libkern.h>
74 #define USTAR_NAME_BLOCK 512
77 * Virtual offset: relative to start of ustar archive
78 * Logical offset: volume-relative
79 * Physical offset: the usual meaning
82 /* virtual offset to volume number */
84 #define vda2vn(_v,_volsize) ((_v) / (_volsize))
86 /* conversions between the three different levels of disk addresses */
88 #define vda2lda(_v,_volsize) ((_v) % (_volsize))
89 #define lda2vda(_v,_volsize,_volnumber) ((_v) + (_volsize) * (_volnumber))
91 #define lda2pda(_lda) ((_lda) + ustarfs_mode_offset)
92 #define pda2lda(_pda) ((_pda) - ustarfs_mode_offset)
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.
97 * It needs to be signed.
101 typedef struct ustar_struct
{
107 ust_misc
[12 + 8 + 1 + 100],
109 /* there is more, but we don't care */
110 ust_pad
[1]; /* make it aligned */
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.
119 #ifndef USTAR_SECT_PER_CYL
120 #define USTAR_SECT_PER_CYL (18 * 2)
123 typedef struct ust_active_struct
{
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 */
138 static const char formatid
[] = "USTARFS",
139 metaname
[] = "USTAR.volsize.";
141 static const int ustarfs_mode_offset
= BBSIZE
;
143 static int checksig(ust_active_t
*);
144 static int convert(const char *, int, int);
145 static int get_volume(struct open_file
*, int);
146 static void setwindow(ust_active_t
*, ustoffs
, ustoffs
);
147 static int real_fs_cylinder_read(struct open_file
*, ustoffs
, int);
148 static int ustarfs_cylinder_read(struct open_file
*, ustoffs
, int);
149 static void ustarfs_sscanf(const char *, const char *, int *);
150 static int read512block(struct open_file
*, ustoffs
, char block
[512]);
151 static int init_volzero_sig(struct open_file
*);
153 #ifdef HAVE_CHANGEDISK_HOOK
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.
159 void changedisk_hook(struct open_file
*);
163 convert(const char *f
, int base
, int fw
)
165 int i
, c
, result
= 0;
167 while(fw
> 0 && *f
== ' ') {
171 for(i
= 0; i
< fw
; ++i
) {
173 if ('0' <= c
&& c
< '0' + base
) {
175 result
= result
* base
+ c
;
182 ustarfs_sscanf(const char *s
, const char *f
, int *xi
)
185 *xi
= convert(s
, 8, convert(f
+ 1, 10, 99));
189 ustarfs_cylinder_read(struct open_file
*f
, ustoffs seek2
, int forcelabel
)
193 for (i
= 0; i
< 3; ++i
) {
194 e
= real_fs_cylinder_read(f
, seek2
, forcelabel
);
202 real_fs_cylinder_read(struct open_file
*f
, ustoffs seek2
, int forcelabel
)
205 int e
= 0; /* XXX work around gcc warning */
209 size_t xferrqst
, xfercount
;
212 xferrqst
= sizeof ustf
->uas_1cyl
;
213 xferbase
= ustf
->uas_1cyl
;
214 lda
= pda2lda(seek2
);
217 ustf
->uas_offset
= lda
;
219 * don't read the label unless we have to. (Preserve
220 * sequential block access so tape boot works.)
223 memset(xferbase
, 0, lda
);
229 ustf
->uas_offset
= 0;
231 while(xferrqst
> 0) {
232 #if !defined(LIBSA_NO_TWIDDLE)
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
);
244 if (xfercount
!= xferrqst
)
245 printf("Warning, unexpected short transfer %d/%d\n",
246 (int)xfercount
, (int)xferrqst
);
247 xferrqst
-= xfercount
;
248 xferbase
+= xfercount
;
255 checksig(ust_active_t
*ustf
)
259 for(i
= rcs
= 0; i
< (int)(sizeof ustf
->uas_1cyl
); ++i
)
260 rcs
+= ustf
->uas_1cyl
[i
];
265 get_volume(struct open_file
*f
, int vn
)
267 int e
, needvolume
, havevolume
;
271 havevolume
= vda2vn(ustf
->uas_windowbase
, ustf
->uas_volsize
);
273 while(havevolume
!= needvolume
) {
276 printf("remove disk %d, ", havevolume
+ 1);
277 printf("insert disk %d, and press return...",
279 #ifdef HAVE_CHANGEDISK_HOOK
284 if ((c
== '\n') || (c
== '\r'))
289 e
= ustarfs_cylinder_read(f
, 0, needvolume
!= 0);
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
)) {
298 printf("Disk is not from the volume set?!\n");
302 ustarfs_sscanf(ustf
->uas_1cyl
+ strlen(formatid
), "%9o",
310 setwindow(ust_active_t
*ustf
, ustoffs pda
, ustoffs vda
)
312 ustf
->uas_windowbase
= lda2vda(pda2lda(pda
), ustf
->uas_volsize
,
313 vda2vn(vda
, ustf
->uas_volsize
))
315 ustf
->uas_init_window
= 1;
319 read512block(struct open_file
*f
, ustoffs vda
, char block
[512])
331 * copy out and return data
332 * if (vda is on some other disk)
334 * get physical disk address
335 * round down to cylinder boundary
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 ]
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);
353 panic("ustarfs read512block");
354 ustf
->uas_init_window
= 0;
355 e
= get_volume(f
, vda2vn(vda
, ustf
->uas_volsize
));
358 pda
= lda2pda(vda2lda(vda
, ustf
->uas_volsize
));
359 pda
-= pda
% sizeof ustf
->uas_1cyl
;
360 e
= ustarfs_cylinder_read(f
, pda
, 0);
363 setwindow(ustf
, pda
, vda
);
368 init_volzero_sig(struct open_file
*f
)
374 if (!ustf
->uas_sigdone
) {
375 e
= ustarfs_cylinder_read(f
, 0, 0);
378 ustf
->uas_volzerosig
= checksig(ustf
);
379 setwindow(ustf
, 0, 0);
385 ustarfs_open(const char *path
, struct open_file
*f
)
396 f
->f_fsdata
= ustf
= alloc(sizeof *ustf
);
397 memset(ustf
, 0, sizeof *ustf
);
399 /* default to 2880 sector floppy */
400 ustf
->uas_volsize
= 80 * 2 * 18 * 512 - lda2pda(0);
402 e
= init_volzero_sig(f
);
407 ustf
->uas_filestart
= offset
;
408 e
= read512block(f
, offset
, block
);
411 memcpy(&ustf
->uas_active
, block
, sizeof ustf
->uas_active
);
412 if(strncmp(ustf
->uas_active
.ust_magic
, "ustar", 5)) {
416 e2
= ENOENT
; /* it must be an actual ustarfs */
417 ustf
->uas_init_fs
= 1;
418 /* if volume metadata is found, use it */
419 if(strncmp(ustf
->uas_active
.ust_name
, metaname
,
420 strlen(metaname
)) == 0) {
421 ustarfs_sscanf(ustf
->uas_active
.ust_name
422 + strlen(metaname
), "%99o", &newvolblocks
);
423 ustf
->uas_volsize
= newvolblocks
* 512
426 ustarfs_sscanf(ustf
->uas_active
.ust_size
,"%12o",&filesize
);
427 if(strncmp(ustf
->uas_active
.ust_name
, path
,
428 sizeof ustf
->uas_active
.ust_name
) == 0) {
429 ustf
->uas_filesize
= filesize
;
432 offset
+= USTAR_NAME_BLOCK
+ filesize
;
435 offset
+= 512 - filesize
;
438 dealloc(ustf
, sizeof *ustf
);
444 #ifndef LIBSA_NO_FS_WRITE
446 ustarfs_write(struct open_file
*f
, void *start
, size_t size
, size_t *resid
)
451 #endif /* !LIBSA_NO_FS_WRITE */
453 #ifndef LIBSA_NO_FS_SEEK
455 ustarfs_seek(struct open_file
*f
, off_t offs
, int whence
)
462 ustf
->uas_fseek
= offs
;
465 ustf
->uas_fseek
+= offs
;
468 ustf
->uas_fseek
= ustf
->uas_filesize
- offs
;
473 return ustf
->uas_fseek
;
475 #endif /* !LIBSA_NO_FS_SEEK */
478 ustarfs_read(struct open_file
*f
, void *start
, size_t size
, size_t *resid
)
491 space512
= alloc(512);
494 if (ustf
->uas_fseek
>= ustf
->uas_filesize
)
496 bufferoffset
= ustf
->uas_fseek
% 512;
497 blkoffs
= ustf
->uas_fseek
- bufferoffset
;
498 readoffs
= ustf
->uas_filestart
+ 512 + blkoffs
;
499 e
= read512block(f
, readoffs
, space512
);
503 inbuffer
= 512 - bufferoffset
;
506 infile
= ustf
->uas_filesize
- ustf
->uas_fseek
;
509 memcpy(start
, space512
+ bufferoffset
, seg
);
510 ustf
->uas_fseek
+= seg
;
511 start
= (char *)start
+ seg
;
516 dealloc(space512
, 512);
521 ustarfs_stat(struct open_file
*f
, struct stat
*sb
)
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
);
536 sb
->st_size
= ustf
->uas_filesize
;
541 #if defined(LIBSA_ENABLE_LS_OP)
543 ustarfs_ls(struct open_file
*f
, const char *pattern
,
544 void (*funcp
)(char* arg
), char* path
)
546 printf("Currently ls command is unsupported by ustarfs\n");
551 #ifndef LIBSA_NO_FS_CLOSE
553 ustarfs_close(struct open_file
*f
)
555 if (f
== NULL
|| f
->f_fsdata
== NULL
)
557 dealloc(f
->f_fsdata
, sizeof(ust_active_t
));
561 #endif /* !LIBSA_NO_FS_CLOSE */