1185222Ssam/* $NetBSD: buf.c,v 1.12 2004/06/20 22:20:18 jmc Exp $ */ 2185222Ssam 3185222Ssam/* 4185222Ssam * Copyright (c) 2001 Wasabi Systems, Inc. 5185222Ssam * All rights reserved. 6185222Ssam * 7185222Ssam * Written by Luke Mewburn for Wasabi Systems, Inc. 8185222Ssam * 9185222Ssam * Redistribution and use in source and binary forms, with or without 10185222Ssam * modification, are permitted provided that the following conditions 11185222Ssam * are met: 12185222Ssam * 1. Redistributions of source code must retain the above copyright 13185222Ssam * notice, this list of conditions and the following disclaimer. 14185222Ssam * 2. Redistributions in binary form must reproduce the above copyright 15185222Ssam * notice, this list of conditions and the following disclaimer in the 16185222Ssam * documentation and/or other materials provided with the distribution. 17185222Ssam * 3. All advertising materials mentioning features or use of this software 18185222Ssam * must display the following acknowledgement: 19185222Ssam * This product includes software developed for the NetBSD Project by 20185222Ssam * Wasabi Systems, Inc. 21185222Ssam * 4. The name of Wasabi Systems, Inc. may not be used to endorse 22185222Ssam * or promote products derived from this software without specific prior 23185222Ssam * written permission. 24185222Ssam * 25185222Ssam * THIS SOFTWARE IS PROVIDED BY WASABI SYSTEMS, INC. ``AS IS'' AND 26185222Ssam * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED 27185222Ssam * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR 28185222Ssam * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL WASABI SYSTEMS, INC 29185222Ssam * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR 30185222Ssam * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF 31185222Ssam * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS 32185222Ssam * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN 33185222Ssam * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) 34185222Ssam * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE 35185222Ssam * POSSIBILITY OF SUCH DAMAGE. 36185222Ssam */ 37185222Ssam 38185222Ssam#include <sys/cdefs.h> 39186334Ssam__FBSDID("$FreeBSD$"); 40185222Ssam 41185222Ssam#include <sys/param.h> 42185222Ssam#include <sys/time.h> 43185222Ssam 44185222Ssam#include <assert.h> 45185222Ssam#include <errno.h> 46185222Ssam#include <stdio.h> 47185222Ssam#include <stdlib.h> 48185222Ssam#include <unistd.h> 49185222Ssam 50185222Ssam#include "makefs.h" 51185222Ssam 52185222Ssam#include <ufs/ufs/dinode.h> 53185222Ssam#include <ufs/ffs/fs.h> 54185222Ssam 55185222Ssam#include "ffs/buf.h" 56185222Ssam#include "ffs/ufs_inode.h" 57185222Ssam 58185222Ssamextern int sectorsize; /* XXX: from ffs.c & mkfs.c */ 59185222Ssam 60185222SsamTAILQ_HEAD(buftailhead,buf) buftail; 61185222Ssam 62185222Ssamint 63185222Ssambread(int fd, struct fs *fs, daddr_t blkno, int size, struct buf **bpp) 64185222Ssam{ 65185222Ssam off_t offset; 66185222Ssam ssize_t rv; 67185222Ssam 68185222Ssam assert (fs != NULL); 69185222Ssam assert (bpp != NULL); 70185222Ssam 71185222Ssam if (debug & DEBUG_BUF_BREAD) 72185222Ssam printf("bread: fs %p blkno %lld size %d\n", 73185222Ssam fs, (long long)blkno, size); 74185222Ssam *bpp = getblk(fd, fs, blkno, size); 75185222Ssam offset = (*bpp)->b_blkno * sectorsize; /* XXX */ 76185222Ssam if (debug & DEBUG_BUF_BREAD) 77185222Ssam printf("bread: bp %p blkno %lld offset %lld bcount %ld\n", 78185222Ssam (*bpp), (long long)(*bpp)->b_blkno, (long long) offset, 79185222Ssam (*bpp)->b_bcount); 80185222Ssam if (lseek((*bpp)->b_fd, offset, SEEK_SET) == -1) 81185222Ssam err(1, "bread: lseek %lld (%lld)", 82185222Ssam (long long)(*bpp)->b_blkno, (long long)offset); 83185222Ssam rv = read((*bpp)->b_fd, (*bpp)->b_data, (*bpp)->b_bcount); 84185222Ssam if (debug & DEBUG_BUF_BREAD) 85185222Ssam printf("bread: read %ld (%lld) returned %d\n", 86185222Ssam (*bpp)->b_bcount, (long long)offset, (int)rv); 87185222Ssam if (rv == -1) /* read error */ 88185222Ssam err(1, "bread: read %ld (%lld) returned %d", 89185222Ssam (*bpp)->b_bcount, (long long)offset, (int)rv); 90185222Ssam else if (rv != (*bpp)->b_bcount) /* short read */ 91185222Ssam err(1, "bread: read %ld (%lld) returned %d", 92185222Ssam (*bpp)->b_bcount, (long long)offset, (int)rv); 93185222Ssam else 94185222Ssam return (0); 95185222Ssam} 96185222Ssam 97185222Ssamvoid 98185222Ssambrelse(struct buf *bp) 99185222Ssam{ 100185222Ssam 101185222Ssam assert (bp != NULL); 102185222Ssam assert (bp->b_data != NULL); 103185222Ssam 104185222Ssam if (bp->b_lblkno < 0) { 105185222Ssam /* 106185222Ssam * XXX don't remove any buffers with negative logical block 107185222Ssam * numbers (lblkno), so that we retain the mapping 108185222Ssam * of negative lblkno -> real blkno that ffs_balloc() 109185222Ssam * sets up. 110185222Ssam * 111185222Ssam * if we instead released these buffers, and implemented 112185222Ssam * ufs_strategy() (and ufs_bmaparray()) and called those 113185222Ssam * from bread() and bwrite() to convert the lblkno to 114185222Ssam * a real blkno, we'd add a lot more code & complexity 115185222Ssam * and reading off disk, for little gain, because this 116185222Ssam * simple hack works for our purpose. 117185222Ssam */ 118185222Ssam bp->b_bcount = 0; 119185222Ssam return; 120185222Ssam } 121214921Scognet 122185222Ssam TAILQ_REMOVE(&buftail, bp, b_tailq); 123185222Ssam free(bp->b_data); 124185222Ssam free(bp); 125185222Ssam} 126185222Ssam 127185222Ssamint 128185222Ssambwrite(struct buf *bp) 129185222Ssam{ 130185222Ssam off_t offset; 131185222Ssam ssize_t rv; 132185222Ssam 133185222Ssam assert (bp != NULL); 134185222Ssam offset = bp->b_blkno * sectorsize; /* XXX */ 135185222Ssam if (debug & DEBUG_BUF_BWRITE) 136185222Ssam printf("bwrite: bp %p blkno %lld offset %lld bcount %ld\n", 137185222Ssam bp, (long long)bp->b_blkno, (long long) offset, 138185222Ssam bp->b_bcount); 139185222Ssam if (lseek(bp->b_fd, offset, SEEK_SET) == -1) 140185222Ssam return (errno); 141185222Ssam rv = write(bp->b_fd, bp->b_data, bp->b_bcount); 142185222Ssam if (debug & DEBUG_BUF_BWRITE) 143185222Ssam printf("bwrite: write %ld (offset %lld) returned %lld\n", 144185222Ssam bp->b_bcount, (long long)offset, (long long)rv); 145185222Ssam if (rv == bp->b_bcount) 146185222Ssam return (0); 147185222Ssam else if (rv == -1) /* write error */ 148185222Ssam return (errno); 149185222Ssam else /* short write ? */ 150185222Ssam return (EAGAIN); 151185222Ssam} 152185222Ssam 153185222Ssamvoid 154185222Ssambcleanup(void) 155185222Ssam{ 156185222Ssam struct buf *bp; 157185222Ssam 158185222Ssam /* 159185222Ssam * XXX this really shouldn't be necessary, but i'm curious to 160185222Ssam * know why there's still some buffers lying around that 161185222Ssam * aren't brelse()d 162185222Ssam */ 163214921Scognet 164185222Ssam if (TAILQ_EMPTY(&buftail)) 165185222Ssam return; 166185222Ssam 167185222Ssam printf("bcleanup: unflushed buffers:\n"); 168185222Ssam TAILQ_FOREACH(bp, &buftail, b_tailq) { 169185222Ssam printf("\tlblkno %10lld blkno %10lld count %6ld bufsize %6ld\n", 170185222Ssam (long long)bp->b_lblkno, (long long)bp->b_blkno, 171185222Ssam bp->b_bcount, bp->b_bufsize); 172185222Ssam } 173185222Ssam printf("bcleanup: done\n"); 174185222Ssam} 175185222Ssam 176185222Ssamstruct buf * 177185222Ssamgetblk(int fd, struct fs *fs, daddr_t blkno, int size) 178185222Ssam{ 179185222Ssam static int buftailinitted; 180185222Ssam struct buf *bp; 181185222Ssam void *n; 182185222Ssam 183185222Ssam assert (fs != NULL); 184185222Ssam if (debug & DEBUG_BUF_GETBLK) 185185222Ssam printf("getblk: fs %p blkno %lld size %d\n", fs, 186185222Ssam (long long)blkno, size); 187185222Ssam 188185222Ssam bp = NULL; 189185222Ssam if (!buftailinitted) { 190185222Ssam if (debug & DEBUG_BUF_GETBLK) 191185222Ssam printf("getblk: initialising tailq\n"); 192185222Ssam TAILQ_INIT(&buftail); 193185222Ssam buftailinitted = 1; 194185222Ssam } else { 195185222Ssam TAILQ_FOREACH(bp, &buftail, b_tailq) { 196185222Ssam if (bp->b_lblkno != blkno) 197185222Ssam continue; 198185222Ssam break; 199185222Ssam } 200185222Ssam } 201185222Ssam if (bp == NULL) { 202185222Ssam if ((bp = calloc(1, sizeof(struct buf))) == NULL) 203185222Ssam err(1, "getblk: calloc"); 204214921Scognet 205185222Ssam bp->b_bufsize = 0; 206185222Ssam bp->b_blkno = bp->b_lblkno = blkno; 207185222Ssam bp->b_fd = fd; 208185222Ssam bp->b_fs = fs; 209185222Ssam bp->b_data = NULL; 210185222Ssam TAILQ_INSERT_HEAD(&buftail, bp, b_tailq); 211185222Ssam } 212185222Ssam bp->b_bcount = size; 213185222Ssam if (bp->b_data == NULL || bp->b_bcount > bp->b_bufsize) { 214185222Ssam n = realloc(bp->b_data, size); 215185222Ssam if (n == NULL) 216185222Ssam err(1, "getblk: realloc b_data %ld", bp->b_bcount); 217185222Ssam bp->b_data = n; 218185222Ssam bp->b_bufsize = size; 219185222Ssam } 220185222Ssam 221185222Ssam return (bp); 222185222Ssam} 223