1 /* $NetBSD: buf.c,v 1.13 2004/06/20 22:20:18 jmc Exp $ */
4 * SPDX-License-Identifier: BSD-4-Clause
6 * Copyright (c) 2001 Wasabi Systems, Inc.
9 * Written by Luke Mewburn for Wasabi Systems, Inc.
11 * Redistribution and use in source and binary forms, with or without
12 * modification, are permitted provided that the following conditions
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
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.
40 #include <sys/cdefs.h>
41 __FBSDID("$FreeBSD$");
43 #include <sys/param.h>
54 #include <ufs/ufs/dinode.h>
55 #include <ufs/ffs/fs.h>
58 #include "ffs/ufs_inode.h"
60 extern int sectorsize; /* XXX: from ffs.c & mkfs.c */
62 TAILQ_HEAD(buftailhead,buf) buftail;
65 bread(struct vnode *vp, daddr_t blkno, int size, struct ucred *u1 __unused,
70 struct fs *fs = vp->fs;
75 if (debug & DEBUG_BUF_BREAD)
76 printf("bread: fs %p blkno %lld size %d\n",
77 fs, (long long)blkno, 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("bread: bp %p blkno %lld offset %lld bcount %ld\n",
82 (*bpp), (long long)(*bpp)->b_blkno, (long long) offset,
84 if (lseek((*bpp)->b_fd, offset, SEEK_SET) == -1)
85 err(1, "bread: lseek %lld (%lld)",
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("bread: read %ld (%lld) returned %d\n",
90 (*bpp)->b_bcount, (long long)offset, (int)rv);
91 if (rv == -1) /* read error */
92 err(1, "bread: read %ld (%lld) returned %d",
93 (*bpp)->b_bcount, (long long)offset, (int)rv);
94 else if (rv != (*bpp)->b_bcount) /* short read */
95 err(1, "bread: read %ld (%lld) returned %d",
96 (*bpp)->b_bcount, (long long)offset, (int)rv);
102 brelse(struct buf *bp, int u1 __unused)
106 assert (bp->b_data != NULL);
108 if (bp->b_lblkno < 0) {
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()
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.
126 TAILQ_REMOVE(&buftail, bp, b_tailq);
132 bwrite(struct buf *bp)
138 offset = bp->b_blkno * sectorsize; /* XXX */
139 if (debug & DEBUG_BUF_BWRITE)
140 printf("bwrite: bp %p blkno %lld offset %lld bcount %ld\n",
141 bp, (long long)bp->b_blkno, (long long) offset,
143 if (lseek(bp->b_fd, offset, SEEK_SET) == -1)
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)
151 else if (rv == -1) /* write error */
153 else /* short write ? */
163 * XXX this really shouldn't be necessary, but i'm curious to
164 * know why there's still some buffers lying around that
168 if (TAILQ_EMPTY(&buftail))
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);
177 printf("bcleanup: done\n");
181 getblk(struct vnode *vp, daddr_t blkno, int size, int u1 __unused,
182 int u2 __unused, int u3 __unused)
184 static int buftailinitted;
188 struct fs *fs = vp->fs;
192 if (debug & DEBUG_BUF_GETBLK)
193 printf("getblk: fs %p blkno %lld size %d\n", fs,
194 (long long)blkno, size);
197 if (!buftailinitted) {
198 if (debug & DEBUG_BUF_GETBLK)
199 printf("getblk: initialising tailq\n");
200 TAILQ_INIT(&buftail);
203 TAILQ_FOREACH(bp, &buftail, b_tailq) {
204 if (bp->b_lblkno != blkno)
210 if ((bp = calloc(1, sizeof(struct buf))) == NULL)
211 err(1, "getblk: calloc");
214 bp->b_blkno = bp->b_lblkno = blkno;
218 TAILQ_INSERT_HEAD(&buftail, bp, b_tailq);
221 if (bp->b_data == NULL || bp->b_bcount > bp->b_bufsize) {
222 n = realloc(bp->b_data, size);
224 err(1, "getblk: realloc b_data %ld", bp->b_bcount);
226 bp->b_bufsize = size;