buf.c revision 1.26 1 /* $NetBSD: buf.c,v 1.26 2023/03/13 22:10:30 christos 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.26 2023/03/13 22:10:30 christos 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 %jd", __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 fsinfo_t *fs = bp->b_fs;
153
154 assert (bp != NULL);
155 offset = bp->b_blkno * fs->sectorsize + fs->offset;
156 bytes = (size_t)bp->b_bcount;
157 if (debug & DEBUG_BUF_BWRITE)
158 printf("%s: blkno %jd offset %jd bcount %zu\n", __func__,
159 (intmax_t)bp->b_blkno, (intmax_t) offset, bytes);
160 if (lseek(bp->b_fs->fd, offset, SEEK_SET) == -1)
161 return errno;
162 rv = write(bp->b_fs->fd, bp->b_data, bytes);
163 if (debug & DEBUG_BUF_BWRITE)
164 printf("%s: write %ld (offset %jd) returned %jd\n", __func__,
165 bp->b_bcount, (intmax_t)offset, (intmax_t)rv);
166 brelse(bp, 0);
167 if (rv == (ssize_t)bytes)
168 return 0;
169 if (rv == -1) /* write error */
170 return errno;
171 return EAGAIN;
172 }
173
174 void
175 bcleanup(void)
176 {
177 struct buf *bp;
178
179 /*
180 * XXX this really shouldn't be necessary, but i'm curious to
181 * know why there's still some buffers lying around that
182 * aren't brelse()d
183 */
184
185 if (TAILQ_EMPTY(&buftail))
186 return;
187
188 printf("%s: unflushed buffers:\n", __func__);
189 TAILQ_FOREACH(bp, &buftail, b_tailq) {
190 printf("\tlblkno %10jd blkno %10jd count %6ld bufsize %6ld\n",
191 (intmax_t)bp->b_lblkno, (intmax_t)bp->b_blkno,
192 bp->b_bcount, bp->b_bufsize);
193 }
194 printf("%s: done\n", __func__);
195 }
196
197 struct buf *
198 getblk(struct vnode *vp, daddr_t blkno, int size, int u1 __unused,
199 int u2 __unused)
200 {
201 static int buftailinitted;
202 struct buf *bp;
203 void *n;
204
205 if (debug & DEBUG_BUF_GETBLK)
206 printf("%s: blkno %jd size %d\n", __func__,
207 (intmax_t)blkno, size);
208
209 bp = NULL;
210 if (!buftailinitted) {
211 if (debug & DEBUG_BUF_GETBLK)
212 printf("%s: initialising tailq\n", __func__);
213 TAILQ_INIT(&buftail);
214 buftailinitted = 1;
215 } else {
216 TAILQ_FOREACH(bp, &buftail, b_tailq) {
217 if (bp->b_lblkno != blkno)
218 continue;
219 break;
220 }
221 }
222 if (bp == NULL) {
223 bp = ecalloc(1, sizeof(*bp));
224 bp->b_bufsize = 0;
225 bp->b_blkno = bp->b_lblkno = blkno;
226 bp->b_fs = vp->fs;
227 bp->b_data = NULL;
228 TAILQ_INSERT_HEAD(&buftail, bp, b_tailq);
229 }
230 bp->b_bcount = size;
231 if (bp->b_data == NULL || bp->b_bcount > bp->b_bufsize) {
232 n = erealloc(bp->b_data, (size_t)size);
233 memset(n, 0, (size_t)size);
234 bp->b_data = n;
235 bp->b_bufsize = size;
236 }
237
238 return bp;
239 }
240