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