1 /* $NetBSD: buf.c,v 1.13 2004/06/20 22:20:18 jmc Exp $ */ 2 3 /* 4 * Copyright (c) 2001 Wasabi Systems, Inc. 5 * All rights reserved. 6 * 7 * Written by Luke Mewburn for Wasabi Systems, Inc. 8 * 9 * Redistribution and use in source and binary forms, with or without 10 * modification, are permitted provided that the following conditions 11 * are met: 12 * 1. Redistributions of source code must retain the above copyright 13 * notice, this list of conditions and the following disclaimer. 14 * 2. Redistributions in binary form must reproduce the above copyright 15 * notice, this list of conditions and the following disclaimer in the 16 * documentation and/or other materials provided with the distribution. 17 * 3. All advertising materials mentioning features or use of this software 18 * must display the following acknowledgement: 19 * This product includes software developed for the NetBSD Project by 20 * Wasabi Systems, Inc. 21 * 4. The name of Wasabi Systems, Inc. may not be used to endorse 22 * or promote products derived from this software without specific prior 23 * written permission. 24 * 25 * THIS SOFTWARE IS PROVIDED BY WASABI SYSTEMS, INC. ``AS IS'' AND 26 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED 27 * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR 28 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL WASABI SYSTEMS, INC 29 * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR 30 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF 31 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS 32 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN 33 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) 34 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE 35 * POSSIBILITY OF SUCH DAMAGE. 36 */ 37 38 #include <sys/cdefs.h> 39 __FBSDID("$FreeBSD$"); 40 41 #include <sys/param.h> 42 #include <sys/time.h> 43 44 #include <assert.h> 45 #include <errno.h> 46 #include <stdio.h> 47 #include <stdlib.h> 48 #include <string.h> 49 #include <unistd.h> 50 #include <util.h> 51 52 #include "makefs.h" 53 #include "buf.h" 54 55 extern int sectorsize; /* XXX: from ffs.c & mkfs.c */ 56 57 static TAILQ_HEAD(buftailhead,buf) buftail; 58 59 int 60 bread(struct vnode *vp, daddr_t blkno, int size, struct ucred *u1 __unused, 61 struct buf **bpp) 62 { 63 off_t offset; 64 ssize_t rv; 65 66 assert (bpp != NULL); 67 68 if (debug & DEBUG_BUF_BREAD) 69 printf("%s: blkno %lld size %d\n", __func__, (long long)blkno, 70 size); 71 *bpp = getblk(vp, blkno, size, 0, 0, 0); 72 offset = (*bpp)->b_blkno * sectorsize; /* XXX */ 73 if (debug & DEBUG_BUF_BREAD) 74 printf("%s: blkno %lld offset %lld bcount %ld\n", __func__, 75 (long long)(*bpp)->b_blkno, (long long) offset, 76 (*bpp)->b_bcount); 77 if (lseek((*bpp)->b_fd, offset, SEEK_SET) == -1) 78 err(1, "%s: lseek %lld (%lld)", __func__, 79 (long long)(*bpp)->b_blkno, (long long)offset); 80 rv = read((*bpp)->b_fd, (*bpp)->b_data, (*bpp)->b_bcount); 81 if (debug & DEBUG_BUF_BREAD) 82 printf("%s: read %ld (%lld) returned %d\n", __func__, 83 (*bpp)->b_bcount, (long long)offset, (int)rv); 84 if (rv == -1) /* read error */ 85 err(1, "%s: read %ld (%lld) returned %d", __func__, 86 (*bpp)->b_bcount, (long long)offset, (int)rv); 87 else if (rv != (*bpp)->b_bcount) /* short read */ 88 err(1, "%s: read %ld (%lld) returned %d", __func__, 89 (*bpp)->b_bcount, (long long)offset, (int)rv); 90 else 91 return (0); 92 } 93 94 void 95 brelse(struct buf *bp, int u1 __unused) 96 { 97 98 assert (bp != NULL); 99 assert (bp->b_data != NULL); 100 101 if (bp->b_lblkno < 0) { 102 /* 103 * XXX don't remove any buffers with negative logical block 104 * numbers (lblkno), so that we retain the mapping 105 * of negative lblkno -> real blkno that ffs_balloc() 106 * sets up. 107 * 108 * if we instead released these buffers, and implemented 109 * ufs_strategy() (and ufs_bmaparray()) and called those 110 * from bread() and bwrite() to convert the lblkno to 111 * a real blkno, we'd add a lot more code & complexity 112 * and reading off disk, for little gain, because this 113 * simple hack works for our purpose. 114 */ 115 bp->b_bcount = 0; 116 return; 117 } 118 119 TAILQ_REMOVE(&buftail, bp, b_tailq); 120 free(bp->b_data); 121 free(bp); 122 } 123 124 int 125 bwrite(struct buf *bp) 126 { 127 off_t offset; 128 ssize_t rv; 129 130 assert (bp != NULL); 131 offset = bp->b_blkno * sectorsize; /* XXX */ 132 if (debug & DEBUG_BUF_BWRITE) 133 printf("bwrite: blkno %lld offset %lld bcount %ld\n", 134 (long long)bp->b_blkno, (long long) offset, 135 bp->b_bcount); 136 if (lseek(bp->b_fd, offset, SEEK_SET) == -1) 137 return (errno); 138 rv = write(bp->b_fd, bp->b_data, bp->b_bcount); 139 if (debug & DEBUG_BUF_BWRITE) 140 printf("bwrite: write %ld (offset %lld) returned %lld\n", 141 bp->b_bcount, (long long)offset, (long long)rv); 142 if (rv == bp->b_bcount) 143 return (0); 144 else if (rv == -1) /* write error */ 145 return (errno); 146 else /* short write ? */ 147 return (EAGAIN); 148 } 149 150 void 151 bcleanup(void) 152 { 153 struct buf *bp; 154 155 /* 156 * XXX this really shouldn't be necessary, but i'm curious to 157 * know why there's still some buffers lying around that 158 * aren't brelse()d 159 */ 160 161 if (TAILQ_EMPTY(&buftail)) 162 return; 163 164 printf("bcleanup: unflushed buffers:\n"); 165 TAILQ_FOREACH(bp, &buftail, b_tailq) { 166 printf("\tlblkno %10lld blkno %10lld count %6ld bufsize %6ld\n", 167 (long long)bp->b_lblkno, (long long)bp->b_blkno, 168 bp->b_bcount, bp->b_bufsize); 169 } 170 printf("bcleanup: done\n"); 171 } 172 173 struct buf * 174 getblk(struct vnode *vp, daddr_t blkno, int size, int u1 __unused, 175 int u2 __unused, int u3 __unused) 176 { 177 static int buftailinitted; 178 struct buf *bp; 179 void *n; 180 181 if (debug & DEBUG_BUF_GETBLK) 182 printf("getblk: blkno %lld size %d\n", (long long)blkno, size); 183 184 bp = NULL; 185 if (!buftailinitted) { 186 if (debug & DEBUG_BUF_GETBLK) 187 printf("getblk: initialising tailq\n"); 188 TAILQ_INIT(&buftail); 189 buftailinitted = 1; 190 } else { 191 TAILQ_FOREACH(bp, &buftail, b_tailq) { 192 if (bp->b_lblkno != blkno) 193 continue; 194 break; 195 } 196 } 197 if (bp == NULL) { 198 bp = ecalloc(1, sizeof(*bp)); 199 bp->b_bufsize = 0; 200 bp->b_blkno = bp->b_lblkno = blkno; 201 bp->b_fd = vp->fd; 202 bp->b_fs = vp->fs; 203 bp->b_data = NULL; 204 TAILQ_INSERT_HEAD(&buftail, bp, b_tailq); 205 } 206 bp->b_bcount = size; 207 if (bp->b_data == NULL || bp->b_bcount > bp->b_bufsize) { 208 n = erealloc(bp->b_data, size); 209 memset(n, 0, size); 210 bp->b_data = n; 211 bp->b_bufsize = size; 212 } 213 214 return (bp); 215 } 216