buf.c revision 185222
180709Sjake/* $NetBSD: buf.c,v 1.12 2004/06/20 22:20:18 jmc Exp $ */ 280709Sjake 380709Sjake/* 480709Sjake * Copyright (c) 2001 Wasabi Systems, Inc. 580709Sjake * All rights reserved. 680709Sjake * 780709Sjake * Written by Luke Mewburn for Wasabi Systems, Inc. 880709Sjake * 980709Sjake * Redistribution and use in source and binary forms, with or without 1080709Sjake * modification, are permitted provided that the following conditions 1180709Sjake * are met: 1280709Sjake * 1. Redistributions of source code must retain the above copyright 1380709Sjake * notice, this list of conditions and the following disclaimer. 1481334Sobrien * 2. Redistributions in binary form must reproduce the above copyright 1580709Sjake * notice, this list of conditions and the following disclaimer in the 1680709Sjake * documentation and/or other materials provided with the distribution. 1781334Sobrien * 3. All advertising materials mentioning features or use of this software 1880709Sjake * must display the following acknowledgement: 1980709Sjake * This product includes software developed for the NetBSD Project by 2080709Sjake * Wasabi Systems, Inc. 2180709Sjake * 4. The name of Wasabi Systems, Inc. may not be used to endorse 2280709Sjake * or promote products derived from this software without specific prior 2380709Sjake * written permission. 2480709Sjake * 2580709Sjake * THIS SOFTWARE IS PROVIDED BY WASABI SYSTEMS, INC. ``AS IS'' AND 2680709Sjake * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED 2780709Sjake * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR 2880709Sjake * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL WASABI SYSTEMS, INC 2980709Sjake * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR 3080709Sjake * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF 3180709Sjake * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS 3280709Sjake * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN 3380709Sjake * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) 3480709Sjake * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE 3591224Sjake * POSSIBILITY OF SUCH DAMAGE. 3691224Sjake */ 3780709Sjake 3891224Sjake#if HAVE_NBTOOL_CONFIG_H 3991224Sjake#include "nbtool_config.h" 4091224Sjake#endif 4180709Sjake 4280709Sjake#include <sys/cdefs.h> 4380709Sjake#if defined(__RCSID) && !defined(__lint) 4480709Sjake__RCSID("$NetBSD: buf.c,v 1.12 2004/06/20 22:20:18 jmc Exp $"); 4580709Sjake#endif /* !__lint */ 4680709Sjake 4780709Sjake#include <sys/param.h> 4880709Sjake#include <sys/time.h> 4980709Sjake 5080709Sjake#include <assert.h> 5180709Sjake#include <errno.h> 5280709Sjake#include <stdio.h> 5380709Sjake#include <stdlib.h> 5480709Sjake#include <unistd.h> 5580709Sjake 5680709Sjake#include "makefs.h" 5780709Sjake 5880709Sjake#include <ufs/ufs/dinode.h> 5980709Sjake#include <ufs/ffs/fs.h> 6080709Sjake 6180709Sjake#include "ffs/buf.h" 6291613Sjake#include "ffs/ufs_inode.h" 6391613Sjake 6480709Sjakeextern int sectorsize; /* XXX: from ffs.c & mkfs.c */ 6580709Sjake 6680709SjakeTAILQ_HEAD(buftailhead,buf) buftail; 6780709Sjake 6881176Sjakeint 6981176Sjakebread(int fd, struct fs *fs, daddr_t blkno, int size, struct buf **bpp) 7081176Sjake{ 7181176Sjake off_t offset; 7281176Sjake ssize_t rv; 7381176Sjake 7481176Sjake assert (fs != NULL); 7581176Sjake assert (bpp != NULL); 7681176Sjake 7781176Sjake if (debug & DEBUG_BUF_BREAD) 7881176Sjake printf("bread: fs %p blkno %lld size %d\n", 7981176Sjake fs, (long long)blkno, size); 8081176Sjake *bpp = getblk(fd, fs, blkno, size); 8181176Sjake offset = (*bpp)->b_blkno * sectorsize; /* XXX */ 8281176Sjake if (debug & DEBUG_BUF_BREAD) 8397445Sjake printf("bread: bp %p blkno %lld offset %lld bcount %ld\n", 8497445Sjake (*bpp), (long long)(*bpp)->b_blkno, (long long) offset, 8591616Sjake (*bpp)->b_bcount); 8697445Sjake if (lseek((*bpp)->b_fd, offset, SEEK_SET) == -1) 8791616Sjake err(1, "bread: lseek %lld (%lld)", 8896998Sjake (long long)(*bpp)->b_blkno, (long long)offset); 8996998Sjake rv = read((*bpp)->b_fd, (*bpp)->b_data, (*bpp)->b_bcount); 9096998Sjake if (debug & DEBUG_BUF_BREAD) 9188629Sjake printf("bread: read %ld (%lld) returned %d\n", 9297027Sjake (*bpp)->b_bcount, (long long)offset, (int)rv); 9397027Sjake if (rv == -1) /* read error */ 9491782Sjake err(1, "bread: read %ld (%lld) returned %d", 9580709Sjake (*bpp)->b_bcount, (long long)offset, (int)rv); 96 else if (rv != (*bpp)->b_bcount) /* short read */ 97 err(1, "bread: read %ld (%lld) returned %d", 98 (*bpp)->b_bcount, (long long)offset, (int)rv); 99 else 100 return (0); 101} 102 103void 104brelse(struct buf *bp) 105{ 106 107 assert (bp != NULL); 108 assert (bp->b_data != NULL); 109 110 if (bp->b_lblkno < 0) { 111 /* 112 * XXX don't remove any buffers with negative logical block 113 * numbers (lblkno), so that we retain the mapping 114 * of negative lblkno -> real blkno that ffs_balloc() 115 * sets up. 116 * 117 * if we instead released these buffers, and implemented 118 * ufs_strategy() (and ufs_bmaparray()) and called those 119 * from bread() and bwrite() to convert the lblkno to 120 * a real blkno, we'd add a lot more code & complexity 121 * and reading off disk, for little gain, because this 122 * simple hack works for our purpose. 123 */ 124 bp->b_bcount = 0; 125 return; 126 } 127 128 TAILQ_REMOVE(&buftail, bp, b_tailq); 129 free(bp->b_data); 130 free(bp); 131} 132 133int 134bwrite(struct buf *bp) 135{ 136 off_t offset; 137 ssize_t rv; 138 139 assert (bp != NULL); 140 offset = bp->b_blkno * sectorsize; /* XXX */ 141 if (debug & DEBUG_BUF_BWRITE) 142 printf("bwrite: bp %p blkno %lld offset %lld bcount %ld\n", 143 bp, (long long)bp->b_blkno, (long long) offset, 144 bp->b_bcount); 145 if (lseek(bp->b_fd, offset, SEEK_SET) == -1) 146 return (errno); 147 rv = write(bp->b_fd, bp->b_data, bp->b_bcount); 148 if (debug & DEBUG_BUF_BWRITE) 149 printf("bwrite: write %ld (offset %lld) returned %lld\n", 150 bp->b_bcount, (long long)offset, (long long)rv); 151 if (rv == bp->b_bcount) 152 return (0); 153 else if (rv == -1) /* write error */ 154 return (errno); 155 else /* short write ? */ 156 return (EAGAIN); 157} 158 159void 160bcleanup(void) 161{ 162 struct buf *bp; 163 164 /* 165 * XXX this really shouldn't be necessary, but i'm curious to 166 * know why there's still some buffers lying around that 167 * aren't brelse()d 168 */ 169 170 if (TAILQ_EMPTY(&buftail)) 171 return; 172 173 printf("bcleanup: unflushed buffers:\n"); 174 TAILQ_FOREACH(bp, &buftail, b_tailq) { 175 printf("\tlblkno %10lld blkno %10lld count %6ld bufsize %6ld\n", 176 (long long)bp->b_lblkno, (long long)bp->b_blkno, 177 bp->b_bcount, bp->b_bufsize); 178 } 179 printf("bcleanup: done\n"); 180} 181 182struct buf * 183getblk(int fd, struct fs *fs, daddr_t blkno, int size) 184{ 185 static int buftailinitted; 186 struct buf *bp; 187 void *n; 188 189 assert (fs != NULL); 190 if (debug & DEBUG_BUF_GETBLK) 191 printf("getblk: fs %p blkno %lld size %d\n", fs, 192 (long long)blkno, size); 193 194 bp = NULL; 195 if (!buftailinitted) { 196 if (debug & DEBUG_BUF_GETBLK) 197 printf("getblk: initialising tailq\n"); 198 TAILQ_INIT(&buftail); 199 buftailinitted = 1; 200 } else { 201 TAILQ_FOREACH(bp, &buftail, b_tailq) { 202 if (bp->b_lblkno != blkno) 203 continue; 204 break; 205 } 206 } 207 if (bp == NULL) { 208 if ((bp = calloc(1, sizeof(struct buf))) == NULL) 209 err(1, "getblk: calloc"); 210 211 bp->b_bufsize = 0; 212 bp->b_blkno = bp->b_lblkno = blkno; 213 bp->b_fd = fd; 214 bp->b_fs = fs; 215 bp->b_data = NULL; 216 TAILQ_INSERT_HEAD(&buftail, bp, b_tailq); 217 } 218 bp->b_bcount = size; 219 if (bp->b_data == NULL || bp->b_bcount > bp->b_bufsize) { 220 n = realloc(bp->b_data, size); 221 if (n == NULL) 222 err(1, "getblk: realloc b_data %ld", bp->b_bcount); 223 bp->b_data = n; 224 bp->b_bufsize = size; 225 } 226 227 return (bp); 228} 229