1 /* $NetBSD: buf.c,v 1.13 2004/06/20 22:20:18 jmc Exp $ */ 2 3 /*- 4 * SPDX-License-Identifier: BSD-4-Clause 5 * 6 * Copyright (c) 2001 Wasabi Systems, Inc. 7 * All rights reserved. 8 * 9 * Written by Luke Mewburn for Wasabi Systems, Inc. 10 * 11 * Redistribution and use in source and binary forms, with or without 12 * modification, are permitted provided that the following conditions 13 * are met: 14 * 1. Redistributions of source code must retain the above copyright 15 * notice, this list of conditions and the following disclaimer. 16 * 2. Redistributions in binary form must reproduce the above copyright 17 * notice, this list of conditions and the following disclaimer in the 18 * documentation and/or other materials provided with the distribution. 19 * 3. All advertising materials mentioning features or use of this software 20 * must display the following acknowledgement: 21 * This product includes software developed for the NetBSD Project by 22 * Wasabi Systems, Inc. 23 * 4. The name of Wasabi Systems, Inc. may not be used to endorse 24 * or promote products derived from this software without specific prior 25 * written permission. 26 * 27 * THIS SOFTWARE IS PROVIDED BY WASABI SYSTEMS, INC. ``AS IS'' AND 28 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED 29 * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR 30 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL WASABI SYSTEMS, INC 31 * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR 32 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF 33 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS 34 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN 35 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) 36 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE 37 * POSSIBILITY OF SUCH DAMAGE. 38 * 39 * $FreeBSD: head/usr.sbin/makefs/ffs/buf.c 336736 2018-07-26 13:33:10Z emaste $ 40 */ 41 42 #include <sys/param.h> 43 #include <sys/time.h> 44 45 #include <assert.h> 46 #include <errno.h> 47 #include <stdio.h> 48 #include <stdlib.h> 49 #include <string.h> 50 #include <unistd.h> 51 #include <util.h> 52 53 #include "makefs.h" 54 #include "buf.h" 55 56 static TAILQ_HEAD(buftailhead,buf) buftail; 57 58 int 59 bread(struct vnode *vp, makefs_daddr_t blkno, int size, struct ucred *u1 __unused, 60 struct buf **bpp) 61 { 62 off_t offset; 63 ssize_t rv; 64 fsinfo_t *fs = vp->fs; 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 = (off_t)(*bpp)->b_blkno * fs->sectorsize + fs->offset; 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_fs->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_fs->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) 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 assert(bp->b_vp); 120 if (!bp->b_vp->v_logical) 121 TAILQ_REMOVE(&buftail, bp, b_tailq); 122 free(bp->b_data); 123 free(bp); 124 } 125 126 static int 127 bwrite_impl(struct buf *bp) 128 { 129 off_t offset; 130 ssize_t rv; 131 fsinfo_t *fs = bp->b_fs; 132 133 assert (bp != NULL); 134 offset = (off_t)bp->b_blkno * fs->sectorsize + fs->offset; 135 if (debug & DEBUG_BUF_BWRITE) 136 printf("bwrite: blkno %lld offset %lld bcount %ld\n", 137 (long long)bp->b_blkno, (long long) offset, 138 bp->b_bcount); 139 if (lseek(bp->b_fs->fd, offset, SEEK_SET) == -1) 140 return (errno); 141 rv = write(bp->b_fs->fd, bp->b_data, bp->b_bcount); 142 if (debug & DEBUG_BUF_BWRITE) 143 printf("bwrite: write %ld (offset %lld) returned %lld\n", 144 bp->b_bcount, (long long)offset, (long long)rv); 145 if (rv == bp->b_bcount) 146 return (0); 147 else if (rv == -1) /* write error */ 148 return (errno); 149 else /* short write ? */ 150 return (EAGAIN); 151 } 152 153 int 154 bwrite(struct buf *bp) 155 { 156 int error = bwrite_impl(bp); 157 158 /* 159 * XXX currently limited to HAMMER2, but this is the way bwrite 160 * and its variants work, otherwise bufs may be leaked. 161 */ 162 if (bp->b_is_hammer2) 163 brelse(bp); 164 165 return (error); 166 } 167 168 void 169 bcleanup(void) 170 { 171 struct buf *bp; 172 173 /* 174 * XXX this really shouldn't be necessary, but i'm curious to 175 * know why there's still some buffers lying around that 176 * aren't brelse()d 177 */ 178 179 if (TAILQ_EMPTY(&buftail)) { 180 printf("bcleanup: clean\n"); 181 return; 182 } 183 184 printf("bcleanup: unflushed buffers:\n"); 185 TAILQ_FOREACH(bp, &buftail, b_tailq) { 186 printf("\t%p lblkno %10lld blkno %10lld count %6ld bufsize %6ld " 187 "loffset %016lx cmd %d [vp %p data %p type %d logical %d vflushed %d]\n", 188 bp, (long long)bp->b_lblkno, (long long)bp->b_blkno, 189 bp->b_bcount, bp->b_bufsize, 190 bp->b_loffset, bp->b_cmd, bp->b_vp, 191 bp->b_vp ? bp->b_vp->v_data : NULL, 192 bp->b_vp ? bp->b_vp->v_type : -1, 193 bp->b_vp ? bp->b_vp->v_logical : -1, 194 bp->b_vp ? bp->b_vp->v_vflushed : -1); 195 if (bp->b_vp) 196 assert(!bp->b_vp->v_logical); 197 } 198 printf("bcleanup: done\n"); 199 } 200 201 struct buf * 202 getblk(struct vnode *vp, makefs_daddr_t blkno, int size, int u1 __unused, 203 int u2 __unused, int u3 __unused) 204 { 205 static int buftailinitted; 206 struct buf *bp; 207 void *n; 208 209 bp = NULL; 210 if (vp->v_logical) 211 goto skip_lookup; 212 213 if (debug & DEBUG_BUF_GETBLK) 214 printf("getblk: blkno %lld size %d\n", (long long)blkno, size); 215 216 if (!buftailinitted) { 217 if (debug & DEBUG_BUF_GETBLK) 218 printf("getblk: initialising tailq\n"); 219 TAILQ_INIT(&buftail); 220 buftailinitted = 1; 221 } else { 222 TAILQ_FOREACH(bp, &buftail, b_tailq) { 223 if (bp->b_lblkno != blkno) 224 continue; 225 break; 226 } 227 } 228 skip_lookup: 229 if (bp == NULL) { 230 bp = ecalloc(1, sizeof(*bp)); 231 bp->b_bufsize = 0; 232 bp->b_blkno = bp->b_lblkno = blkno; 233 bp->b_fs = vp->fs; 234 bp->b_data = NULL; 235 bp->b_vp = vp; 236 assert(bp->b_vp); 237 if (!bp->b_vp->v_logical) 238 TAILQ_INSERT_HEAD(&buftail, bp, b_tailq); 239 } 240 bp->b_bcount = size; 241 if (bp->b_data == NULL || bp->b_bcount > bp->b_bufsize) { 242 n = erealloc(bp->b_data, size); 243 memset(n, 0, size); 244 bp->b_data = n; 245 bp->b_bufsize = size; 246 } 247 248 return (bp); 249 } 250