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