1 /* $NetBSD: v7fs_io.c,v 1.1 2011/06/27 11:52:25 uch Exp $ */ 2 3 /*- 4 * Copyright (c) 2011 The NetBSD Foundation, Inc. 5 * All rights reserved. 6 * 7 * This code is derived from software contributed to The NetBSD Foundation 8 * by UCHIYAMA Yasushi. 9 * 10 * Redistribution and use in source and binary forms, with or without 11 * modification, are permitted provided that the following conditions 12 * are met: 13 * 1. Redistributions of source code must retain the above copyright 14 * notice, this list of conditions and the following disclaimer. 15 * 2. Redistributions in binary form must reproduce the above copyright 16 * notice, this list of conditions and the following disclaimer in the 17 * documentation and/or other materials provided with the distribution. 18 * 19 * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS 20 * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED 21 * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR 22 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS 23 * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR 24 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF 25 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS 26 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN 27 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) 28 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE 29 * POSSIBILITY OF SUCH DAMAGE. 30 */ 31 32 #include <sys/cdefs.h> 33 __KERNEL_RCSID(0, "$NetBSD: v7fs_io.c,v 1.1 2011/06/27 11:52:25 uch Exp $"); 34 #if defined _KERNEL_OPT 35 #include "opt_v7fs.h" 36 #endif 37 38 #ifdef _KERNEL 39 #include <sys/param.h> 40 #else 41 #include <stdio.h> 42 #include <errno.h> 43 #include <stdlib.h> 44 #endif 45 46 #include "v7fs.h" 47 #include "v7fs_impl.h" 48 49 #if defined _KERNEL 50 #define STATIC_BUFFER 51 #endif 52 53 #ifdef V7FS_IO_DEBUG 54 #define DPRINTF(fmt, args...) printf("%s: " fmt, __func__, ##args) 55 #else 56 #define DPRINTF(fmt, args...) ((void)0) 57 #endif 58 59 void * 60 scratch_read(struct v7fs_self *fs, daddr_t blk) 61 { 62 #ifdef STATIC_BUFFER 63 int i; 64 MEM_LOCK(fs); 65 for (i = 0; i < V7FS_SELF_NSCRATCH; i++) { 66 if (fs->scratch_free & (1 << i)) { 67 fs->scratch_free &= ~(1 << i); 68 break; 69 } 70 } 71 if (i == V7FS_SELF_NSCRATCH) { 72 DPRINTF("No scratch area. increase V7FS_SELF_NSCRATCH\n"); 73 assert(0); 74 MEM_UNLOCK(fs); 75 return NULL; 76 } 77 78 if (!fs->io.read(fs->io.cookie, fs->scratch[i], blk)) { 79 DPRINTF("*** I/O error block %ld\n", (long)blk); 80 fs->scratch_free |= (1 << i); 81 MEM_UNLOCK(fs); 82 return NULL; 83 } 84 MEM_UNLOCK(fs); 85 /* Statistic */ 86 int n; 87 if ((n = scratch_remain(fs)) < fs->scratch_remain) 88 fs->scratch_remain = n; 89 90 return fs->scratch[i]; 91 #else 92 uint8_t *buf = malloc(V7FS_BSIZE); 93 if (!fs->io.read(fs->io.cookie, buf, blk)) { 94 DPRINTF("*** I/O error block %ld\n",(long)blk); 95 return NULL; 96 } 97 return buf; 98 #endif 99 } 100 101 int 102 scratch_remain(const struct v7fs_self *fs) 103 { 104 #ifdef STATIC_BUFFER 105 int nfree; 106 int i; 107 MEM_LOCK(fs); 108 for (i = 0, nfree = 0; i < V7FS_SELF_NSCRATCH; i++) { 109 if (fs->scratch_free & (1 << i)) { 110 nfree++; 111 } 112 } 113 MEM_UNLOCK(fs); 114 return nfree; 115 #else 116 return -1; 117 #endif 118 } 119 120 void 121 scratch_free(struct v7fs_self *fs __unused, void *p) 122 { 123 #ifdef STATIC_BUFFER 124 int i; 125 MEM_LOCK(fs); 126 for (i = 0; i < V7FS_SELF_NSCRATCH; i++) 127 if (fs->scratch[i] == p) { 128 fs->scratch_free |= (1 << i); 129 break; 130 } 131 MEM_UNLOCK(fs); 132 assert(i != V7FS_SELF_NSCRATCH); 133 #else 134 free(p); 135 #endif 136 } 137