1 /* $NetBSD: buf.c,v 1.11 2003/10/16 06:31:38 itojun Exp $ */
4 * Copyright (c) 2001 Wasabi Systems, Inc.
7 * Written by Luke Mewburn for Wasabi Systems, Inc.
9 * Redistribution and use in source and binary forms, with or without
10 * modification, are permitted provided that the following conditions
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
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.
38 #if HAVE_NBTOOL_CONFIG_H
39 #include "nbtool_config.h"
42 #include <sys/cdefs.h>
43 #if defined(__RCSID) && !defined(__lint)
44 __RCSID("$NetBSD: buf.c,v 1.11 2003/10/16 06:31:38 itojun Exp $");
47 #include <sys/param.h>
58 #include <ufs/ufs/dinode.h>
59 #include <ufs/ffs/fs.h>
62 #include "ffs/ufs_inode.h"
64 extern int sectorsize
; /* XXX: from ffs.c & mkfs.c */
66 TAILQ_HEAD(buftailhead
,buf
) buftail
;
69 bread(int fd
, struct fs
*fs
, daddr_t blkno
, int size
, struct buf
**bpp
)
77 if (debug
& DEBUG_BUF_BREAD
)
78 printf("bread: fs %p blkno %lld size %d\n",
79 fs
, (long long)blkno
, size
);
80 *bpp
= getblk(fd
, fs
, blkno
, size
);
81 offset
= (*bpp
)->b_blkno
* sectorsize
; /* XXX */
82 if (debug
& DEBUG_BUF_BREAD
)
83 printf("bread: bp %p blkno %lld offset %lld bcount %ld\n",
84 (*bpp
), (long long)(*bpp
)->b_blkno
, (long long) offset
,
86 if (lseek((*bpp
)->b_fd
, offset
, SEEK_SET
) == -1)
87 err(1, "bread: lseek %lld (%lld)",
88 (long long)(*bpp
)->b_blkno
, (long long)offset
);
89 rv
= read((*bpp
)->b_fd
, (*bpp
)->b_data
, (*bpp
)->b_bcount
);
90 if (debug
& DEBUG_BUF_BREAD
)
91 printf("bread: read %ld (%lld) returned %d\n",
92 (*bpp
)->b_bcount
, (long long)offset
, (int)rv
);
93 if (rv
== -1) /* read error */
94 err(1, "bread: read %ld (%lld) returned %d",
95 (*bpp
)->b_bcount
, (long long)offset
, (int)rv
);
96 else if (rv
!= (*bpp
)->b_bcount
) /* short read */
97 err(1, "bread: read %ld (%lld) returned %d",
98 (*bpp
)->b_bcount
, (long long)offset
, (int)rv
);
104 brelse(struct buf
*bp
)
108 assert (bp
->b_data
!= NULL
);
110 if (bp
->b_lblkno
< 0) {
112 * XXX don't remove any buffers with negative logical block
113 * numbers (lblkno), so that we retain the mapping
114 * of negative lblkno -> real blkno that ffs_balloc()
117 * if we instead released these buffers, and implemented
118 * ufs_strategy() (and ufs_bmaparray()) and called those
119 * from bread() and bwrite() to convert the lblkno to
120 * a real blkno, we'd add a lot more code & complexity
121 * and reading off disk, for little gain, because this
122 * simple hack works for our purpose.
128 TAILQ_REMOVE(&buftail
, bp
, b_tailq
);
134 bwrite(struct buf
*bp
)
140 offset
= bp
->b_blkno
* sectorsize
; /* XXX */
141 if (debug
& DEBUG_BUF_BWRITE
)
142 printf("bwrite: bp %p blkno %lld offset %lld bcount %ld\n",
143 bp
, (long long)bp
->b_blkno
, (long long) offset
,
145 if (lseek(bp
->b_fd
, offset
, SEEK_SET
) == -1)
147 rv
= write(bp
->b_fd
, bp
->b_data
, bp
->b_bcount
);
148 if (debug
& DEBUG_BUF_BWRITE
)
149 printf("bwrite: write %ld (offset %lld) returned %lld\n",
150 bp
->b_bcount
, (long long)offset
, (long long)rv
);
151 if (rv
== bp
->b_bcount
)
153 else if (rv
== -1) /* write error */
155 else /* short write ? */
165 * XXX this really shouldn't be necessary, but i'm curious to
166 * know why there's still some buffers lying around that
170 if (TAILQ_EMPTY(&buftail
))
173 printf("bcleanup: unflushed buffers:\n");
174 TAILQ_FOREACH(bp
, &buftail
, b_tailq
) {
175 printf("\tlblkno %10lld blkno %10lld count %6ld bufsize %6ld\n",
176 (long long)bp
->b_lblkno
, (long long)bp
->b_blkno
,
177 bp
->b_bcount
, bp
->b_bufsize
);
179 printf("bcleanup: done\n");
183 getblk(int fd
, struct fs
*fs
, daddr_t blkno
, int size
)
185 static int buftailinitted
;
190 if (debug
& DEBUG_BUF_GETBLK
)
191 printf("getblk: fs %p blkno %lld size %d\n", fs
,
192 (long long)blkno
, size
);
195 if (!buftailinitted
) {
196 if (debug
& DEBUG_BUF_GETBLK
)
197 printf("getblk: initialising tailq\n");
198 TAILQ_INIT(&buftail
);
201 TAILQ_FOREACH(bp
, &buftail
, b_tailq
) {
202 if (bp
->b_lblkno
!= blkno
)
208 if ((bp
= calloc(1, sizeof(struct buf
))) == NULL
)
209 err(1, "getblk: calloc");
212 bp
->b_blkno
= bp
->b_lblkno
= blkno
;
216 TAILQ_INSERT_HEAD(&buftail
, bp
, b_tailq
);
219 if (bp
->b_data
== NULL
|| bp
->b_bcount
> bp
->b_bufsize
) {
220 n
= realloc(bp
->b_data
, size
);
222 err(1, "getblk: realloc b_data %ld", bp
->b_bcount
);
224 bp
->b_bufsize
= size
;