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 54 #include <ufs/ufs/dinode.h> 55 #include <ufs/ffs/fs.h> 56 57 #include "ffs/buf.h" 58 #include "ffs/ufs_inode.h" 59 60 extern int sectorsize; /* XXX: from ffs.c & mkfs.c */ 61 62 TAILQ_HEAD(buftailhead,buf) buftail; 63 64 int 65 bread(struct vnode *vp, daddr_t blkno, int size, struct ucred *u1 __unused, 66 struct buf **bpp) 67 { 68 off_t offset; 69 ssize_t rv; 70 struct fs *fs = vp->fs; 71 72 assert (fs != NULL); 73 assert (bpp != NULL); 74 75 if (debug & DEBUG_BUF_BREAD) 76 printf("%s: blkno %lld size %d\n", __func__, (long long)blkno, 77 size); 78 *bpp = getblk(vp, blkno, size, 0, 0, 0); 79 offset = (*bpp)->b_blkno * sectorsize; /* XXX */ 80 if (debug & DEBUG_BUF_BREAD) 81 printf("%s: blkno %lld offset %lld bcount %ld\n", __func__, 82 (long long)(*bpp)->b_blkno, (long long) offset, 83 (*bpp)->b_bcount); 84 if (lseek((*bpp)->b_fd, offset, SEEK_SET) == -1) 85 err(1, "%s: lseek %lld (%lld)", __func__, 86 (long long)(*bpp)->b_blkno, (long long)offset); 87 rv = read((*bpp)->b_fd, (*bpp)->b_data, (*bpp)->b_bcount); 88 if (debug & DEBUG_BUF_BREAD) 89 printf("%s: read %ld (%lld) returned %d\n", __func__, 90 (*bpp)->b_bcount, (long long)offset, (int)rv); 91 if (rv == -1) /* read error */ 92 err(1, "%s: read %ld (%lld) returned %d", __func__, 93 (*bpp)->b_bcount, (long long)offset, (int)rv); 94 else if (rv != (*bpp)->b_bcount) /* short read */ 95 err(1, "%s: read %ld (%lld) returned %d", __func__, 96 (*bpp)->b_bcount, (long long)offset, (int)rv); 97 else 98 return (0); 99 } 100 101 void 102 brelse(struct buf *bp, int u1 __unused) 103 { 104 105 assert (bp != NULL); 106 assert (bp->b_data != NULL); 107 108 if (bp->b_lblkno < 0) { 109 /* 110 * XXX don't remove any buffers with negative logical block 111 * numbers (lblkno), so that we retain the mapping 112 * of negative lblkno -> real blkno that ffs_balloc() 113 * sets up. 114 * 115 * if we instead released these buffers, and implemented 116 * ufs_strategy() (and ufs_bmaparray()) and called those 117 * from bread() and bwrite() to convert the lblkno to 118 * a real blkno, we'd add a lot more code & complexity 119 * and reading off disk, for little gain, because this 120 * simple hack works for our purpose. 121 */ 122 bp->b_bcount = 0; 123 return; 124 } 125 126 TAILQ_REMOVE(&buftail, bp, b_tailq); 127 free(bp->b_data); 128 free(bp); 129 } 130 131 int 132 bwrite(struct buf *bp) 133 { 134 off_t offset; 135 ssize_t rv; 136 137 assert (bp != NULL); 138 offset = bp->b_blkno * sectorsize; /* XXX */ 139 if (debug & DEBUG_BUF_BWRITE) 140 printf("bwrite: blkno %lld offset %lld bcount %ld\n", 141 (long long)bp->b_blkno, (long long) offset, 142 bp->b_bcount); 143 if (lseek(bp->b_fd, offset, SEEK_SET) == -1) 144 return (errno); 145 rv = write(bp->b_fd, bp->b_data, bp->b_bcount); 146 if (debug & DEBUG_BUF_BWRITE) 147 printf("bwrite: write %ld (offset %lld) returned %lld\n", 148 bp->b_bcount, (long long)offset, (long long)rv); 149 if (rv == bp->b_bcount) 150 return (0); 151 else if (rv == -1) /* write error */ 152 return (errno); 153 else /* short write ? */ 154 return (EAGAIN); 155 } 156 157 void 158 bcleanup(void) 159 { 160 struct buf *bp; 161 162 /* 163 * XXX this really shouldn't be necessary, but i'm curious to 164 * know why there's still some buffers lying around that 165 * aren't brelse()d 166 */ 167 168 if (TAILQ_EMPTY(&buftail)) 169 return; 170 171 printf("bcleanup: unflushed buffers:\n"); 172 TAILQ_FOREACH(bp, &buftail, b_tailq) { 173 printf("\tlblkno %10lld blkno %10lld count %6ld bufsize %6ld\n", 174 (long long)bp->b_lblkno, (long long)bp->b_blkno, 175 bp->b_bcount, bp->b_bufsize); 176 } 177 printf("bcleanup: done\n"); 178 } 179 180 struct buf * 181 getblk(struct vnode *vp, daddr_t blkno, int size, int u1 __unused, 182 int u2 __unused, int u3 __unused) 183 { 184 static int buftailinitted; 185 struct buf *bp; 186 void *n; 187 int fd = vp->fd; 188 struct fs *fs = vp->fs; 189 190 blkno += vp->offset; 191 assert (fs != NULL); 192 if (debug & DEBUG_BUF_GETBLK) 193 printf("getblk: blkno %lld size %d\n", (long long)blkno, size); 194 195 bp = NULL; 196 if (!buftailinitted) { 197 if (debug & DEBUG_BUF_GETBLK) 198 printf("getblk: initialising tailq\n"); 199 TAILQ_INIT(&buftail); 200 buftailinitted = 1; 201 } else { 202 TAILQ_FOREACH(bp, &buftail, b_tailq) { 203 if (bp->b_lblkno != blkno) 204 continue; 205 break; 206 } 207 } 208 if (bp == NULL) { 209 bp = ecalloc(1, sizeof(*bp)); 210 bp->b_bufsize = 0; 211 bp->b_blkno = bp->b_lblkno = blkno; 212 bp->b_fd = fd; 213 bp->b_fs = fs; 214 bp->b_data = NULL; 215 TAILQ_INSERT_HEAD(&buftail, bp, b_tailq); 216 } 217 bp->b_bcount = size; 218 if (bp->b_data == NULL || bp->b_bcount > bp->b_bufsize) { 219 n = erealloc(bp->b_data, size); 220 memset(n, 0, size); 221 bp->b_data = n; 222 bp->b_bufsize = size; 223 } 224 225 return (bp); 226 } 227