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