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