Home | History | Annotate | Line # | Download | only in common
rawfs.c revision 1.1.60.3
      1  1.1.60.3    skrll /*	$NetBSD: rawfs.c,v 1.1.60.3 2004/09/21 13:15:27 skrll Exp $	*/
      2       1.1  thorpej 
      3       1.1  thorpej /*
      4       1.1  thorpej  * Copyright (c) 1995 Gordon W. Ross
      5       1.1  thorpej  * All rights reserved.
      6       1.1  thorpej  *
      7       1.1  thorpej  * Redistribution and use in source and binary forms, with or without
      8       1.1  thorpej  * modification, are permitted provided that the following conditions
      9       1.1  thorpej  * are met:
     10       1.1  thorpej  * 1. Redistributions of source code must retain the above copyright
     11       1.1  thorpej  *    notice, this list of conditions and the following disclaimer.
     12       1.1  thorpej  * 2. Redistributions in binary form must reproduce the above copyright
     13       1.1  thorpej  *    notice, this list of conditions and the following disclaimer in the
     14       1.1  thorpej  *    documentation and/or other materials provided with the distribution.
     15       1.1  thorpej  * 3. The name of the author may not be used to endorse or promote products
     16       1.1  thorpej  *    derived from this software without specific prior written permission.
     17       1.1  thorpej  * 4. All advertising materials mentioning features or use of this software
     18       1.1  thorpej  *    must display the following acknowledgement:
     19       1.1  thorpej  *      This product includes software developed by Gordon W. Ross
     20       1.1  thorpej  *
     21       1.1  thorpej  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
     22       1.1  thorpej  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
     23       1.1  thorpej  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
     24       1.1  thorpej  * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
     25       1.1  thorpej  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
     26       1.1  thorpej  * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
     27       1.1  thorpej  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
     28       1.1  thorpej  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
     29       1.1  thorpej  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
     30       1.1  thorpej  * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
     31       1.1  thorpej  */
     32       1.1  thorpej 
     33       1.1  thorpej /*
     34       1.1  thorpej  * Raw file system - for stream devices like tapes.
     35       1.1  thorpej  * No random access, only sequential read allowed.
     36       1.1  thorpej  * This exists only to allow upper level code to be
     37       1.1  thorpej  * shielded from the fact that the device must be
     38       1.1  thorpej  * read only with whole block position and size.
     39       1.1  thorpej  */
     40       1.1  thorpej 
     41       1.1  thorpej #include <sys/param.h>
     42       1.1  thorpej 
     43       1.1  thorpej #include <lib/libsa/stand.h>
     44       1.1  thorpej #include <hp300/stand/common/rawfs.h>
     45       1.1  thorpej 
     46       1.1  thorpej extern int debug;
     47       1.1  thorpej 
     48       1.1  thorpej /* Our devices are generally willing to do 8K transfers. */
     49       1.1  thorpej #define	RAWFS_BSIZE	0x2000
     50       1.1  thorpej 
     51       1.1  thorpej /*
     52       1.1  thorpej  * In-core open file.
     53       1.1  thorpej  */
     54       1.1  thorpej struct rawfs_file {
     55       1.1  thorpej 	daddr_t		fs_nextblk;	/* block number to read next */
     56       1.1  thorpej 	int		fs_len;		/* amount left in f_buf */
     57       1.1  thorpej 	char *		fs_ptr;		/* read pointer into f_buf */
     58       1.1  thorpej 	char		fs_buf[RAWFS_BSIZE];
     59       1.1  thorpej };
     60       1.1  thorpej 
     61  1.1.60.1    skrll static int rawfs_get_block(struct open_file *);
     62       1.1  thorpej 
     63       1.1  thorpej int
     64       1.1  thorpej rawfs_open(path, f)
     65  1.1.60.1    skrll 	const char *path;
     66       1.1  thorpej 	struct open_file *f;
     67       1.1  thorpej {
     68       1.1  thorpej 	struct rawfs_file *fs;
     69       1.1  thorpej 
     70       1.1  thorpej 	/*
     71       1.1  thorpej 	 * The actual tape driver has already been opened.
     72       1.1  thorpej 	 * Just allocate the I/O buffer, etc.
     73       1.1  thorpej 	 */
     74       1.1  thorpej 	fs = alloc(sizeof(struct rawfs_file));
     75       1.1  thorpej 	fs->fs_nextblk = 0;
     76       1.1  thorpej 	fs->fs_len = 0;
     77       1.1  thorpej 	fs->fs_ptr = fs->fs_buf;
     78       1.1  thorpej 
     79       1.1  thorpej #ifdef	DEBUG_RAWFS
     80       1.1  thorpej 	printf("rawfs_open: fs=0x%x\n", (u_long)fs);
     81       1.1  thorpej #endif
     82       1.1  thorpej 
     83       1.1  thorpej 	f->f_fsdata = fs;
     84  1.1.60.1    skrll 	return 0;
     85       1.1  thorpej }
     86       1.1  thorpej 
     87       1.1  thorpej int
     88       1.1  thorpej rawfs_close(f)
     89       1.1  thorpej 	struct open_file *f;
     90       1.1  thorpej {
     91       1.1  thorpej 	struct rawfs_file *fs;
     92       1.1  thorpej 
     93       1.1  thorpej 	fs = (struct rawfs_file *) f->f_fsdata;
     94       1.1  thorpej 	f->f_fsdata = (void *)0;
     95       1.1  thorpej 
     96       1.1  thorpej #ifdef	DEBUG_RAWFS
     97       1.1  thorpej 	printf("rawfs_close: fs=0x%x\n", (u_long)fs);
     98       1.1  thorpej #endif
     99       1.1  thorpej 
    100       1.1  thorpej 	if (fs != (struct rawfs_file *)0)
    101       1.1  thorpej 		free(fs, sizeof(*fs));
    102       1.1  thorpej 
    103  1.1.60.1    skrll 	return 0;
    104       1.1  thorpej }
    105       1.1  thorpej 
    106       1.1  thorpej int
    107       1.1  thorpej rawfs_read(f, start, size, resid)
    108       1.1  thorpej 	struct open_file *f;
    109       1.1  thorpej 	void *start;
    110       1.1  thorpej 	u_int size;
    111       1.1  thorpej 	u_int *resid;
    112       1.1  thorpej {
    113       1.1  thorpej 	struct rawfs_file *fs = (struct rawfs_file *)f->f_fsdata;
    114       1.1  thorpej 	char *addr = start;
    115       1.1  thorpej 	int error = 0;
    116       1.1  thorpej 	size_t csize;
    117       1.1  thorpej 
    118       1.1  thorpej #ifdef DEBUG_RAWFS
    119       1.1  thorpej 	printf("rawfs_read: file=0x%x, start=0x%x, size=%d, resid=0x%x\n",
    120       1.1  thorpej 	    (u_long)f, (u_long)start, size, resid);
    121       1.1  thorpej 	printf("            fs=0x%x\n", (u_long)fs);
    122       1.1  thorpej #endif
    123       1.1  thorpej 
    124       1.1  thorpej 	while (size != 0) {
    125       1.1  thorpej 
    126       1.1  thorpej 		if (fs->fs_len == 0)
    127       1.1  thorpej 			if ((error = rawfs_get_block(f)) != 0)
    128       1.1  thorpej 				break;
    129       1.1  thorpej 
    130       1.1  thorpej 		if (fs->fs_len <= 0)
    131       1.1  thorpej 			break;	/* EOF */
    132       1.1  thorpej 
    133       1.1  thorpej 		csize = size;
    134       1.1  thorpej 		if (csize > fs->fs_len)
    135       1.1  thorpej 			csize = fs->fs_len;
    136       1.1  thorpej 
    137  1.1.60.1    skrll 		memcpy(addr, fs->fs_ptr, csize);
    138       1.1  thorpej 		fs->fs_ptr += csize;
    139       1.1  thorpej 		fs->fs_len -= csize;
    140       1.1  thorpej 		addr += csize;
    141       1.1  thorpej 		size -= csize;
    142       1.1  thorpej 	}
    143       1.1  thorpej 	if (resid)
    144       1.1  thorpej 		*resid = size;
    145  1.1.60.1    skrll 	return error;
    146       1.1  thorpej }
    147       1.1  thorpej 
    148       1.1  thorpej int
    149       1.1  thorpej rawfs_write(f, start, size, resid)
    150       1.1  thorpej 	struct open_file *f;
    151       1.1  thorpej 	void *start;
    152       1.1  thorpej 	size_t size;
    153       1.1  thorpej 	size_t *resid;	/* out */
    154       1.1  thorpej {
    155       1.1  thorpej #ifdef	DEBUG_RAWFS
    156       1.1  thorpej 	printf("rawfs_write: YOU'RE NOT SUPPOSED TO GET HERE!\n");
    157       1.1  thorpej #endif
    158  1.1.60.1    skrll 	return EROFS;
    159       1.1  thorpej }
    160       1.1  thorpej 
    161       1.1  thorpej off_t
    162       1.1  thorpej rawfs_seek(f, offset, where)
    163       1.1  thorpej 	struct open_file *f;
    164       1.1  thorpej 	off_t offset;
    165       1.1  thorpej 	int where;
    166       1.1  thorpej {
    167  1.1.60.1    skrll 
    168       1.1  thorpej #ifdef	DEBUG_RAWFS
    169       1.1  thorpej 	printf("rawfs_seek: YOU'RE NOT SUPPOSED TO GET HERE!\n");
    170       1.1  thorpej #endif
    171  1.1.60.1    skrll 	return EFTYPE;
    172       1.1  thorpej }
    173       1.1  thorpej 
    174       1.1  thorpej int
    175       1.1  thorpej rawfs_stat(f, sb)
    176       1.1  thorpej 	struct open_file *f;
    177       1.1  thorpej 	struct stat *sb;
    178       1.1  thorpej {
    179  1.1.60.1    skrll 
    180       1.1  thorpej #ifdef	DEBUG_RAWFS
    181       1.1  thorpej 	printf("rawfs_stat: I'll let you live only because of exec.c\n");
    182       1.1  thorpej #endif
    183       1.1  thorpej 	/*
    184       1.1  thorpej 	 * Clear out the stat buffer so that the uid check
    185       1.1  thorpej 	 * won't fail.  See sys/lib/libsa/exec.c
    186       1.1  thorpej 	 */
    187  1.1.60.1    skrll 	memset(sb, 0, sizeof(*sb));
    188       1.1  thorpej 
    189  1.1.60.1    skrll 	return EFTYPE;
    190       1.1  thorpej }
    191       1.1  thorpej 
    192       1.1  thorpej /*
    193       1.1  thorpej  * Read a block from the underlying stream device
    194       1.1  thorpej  * (In our case, a tape drive.)
    195       1.1  thorpej  */
    196       1.1  thorpej static int
    197       1.1  thorpej rawfs_get_block(f)
    198       1.1  thorpej 	struct open_file *f;
    199       1.1  thorpej {
    200       1.1  thorpej 	struct rawfs_file *fs;
    201       1.1  thorpej 	int error, len;
    202       1.1  thorpej 
    203       1.1  thorpej 	fs = (struct rawfs_file *)f->f_fsdata;
    204       1.1  thorpej 	fs->fs_ptr = fs->fs_buf;
    205       1.1  thorpej 
    206       1.1  thorpej 	twiddle();
    207       1.1  thorpej #ifdef DEBUG_RAWFS
    208       1.1  thorpej 	printf("rawfs_get_block: calling strategy\n");
    209       1.1  thorpej #endif
    210       1.1  thorpej 	error = f->f_dev->dv_strategy(f->f_devdata, F_READ,
    211       1.1  thorpej 		fs->fs_nextblk, RAWFS_BSIZE, fs->fs_buf, &len);
    212       1.1  thorpej #ifdef DEBUG_RAWFS
    213       1.1  thorpej 	printf("rawfs_get_block: strategy returned %d\n", error);
    214       1.1  thorpej #endif
    215       1.1  thorpej 
    216       1.1  thorpej 	if (!error) {
    217       1.1  thorpej 		fs->fs_len = len;
    218       1.1  thorpej 		fs->fs_nextblk += (RAWFS_BSIZE / DEV_BSIZE);
    219       1.1  thorpej 	}
    220       1.1  thorpej 
    221  1.1.60.1    skrll 	return error;
    222       1.1  thorpej }
    223