buf.c revision 1.28 1 1.28 kre /* $NetBSD: buf.c,v 1.28 2023/03/14 10:36:06 kre Exp $ */
2 1.1 lukem
3 1.1 lukem /*
4 1.3 lukem * Copyright (c) 2001 Wasabi Systems, Inc.
5 1.1 lukem * All rights reserved.
6 1.1 lukem *
7 1.1 lukem * Written by Luke Mewburn for Wasabi Systems, Inc.
8 1.1 lukem *
9 1.1 lukem * Redistribution and use in source and binary forms, with or without
10 1.1 lukem * modification, are permitted provided that the following conditions
11 1.1 lukem * are met:
12 1.1 lukem * 1. Redistributions of source code must retain the above copyright
13 1.1 lukem * notice, this list of conditions and the following disclaimer.
14 1.1 lukem * 2. Redistributions in binary form must reproduce the above copyright
15 1.1 lukem * notice, this list of conditions and the following disclaimer in the
16 1.1 lukem * documentation and/or other materials provided with the distribution.
17 1.1 lukem * 3. All advertising materials mentioning features or use of this software
18 1.1 lukem * must display the following acknowledgement:
19 1.1 lukem * This product includes software developed for the NetBSD Project by
20 1.1 lukem * Wasabi Systems, Inc.
21 1.1 lukem * 4. The name of Wasabi Systems, Inc. may not be used to endorse
22 1.1 lukem * or promote products derived from this software without specific prior
23 1.1 lukem * written permission.
24 1.1 lukem *
25 1.1 lukem * THIS SOFTWARE IS PROVIDED BY WASABI SYSTEMS, INC. ``AS IS'' AND
26 1.1 lukem * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
27 1.1 lukem * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
28 1.1 lukem * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL WASABI SYSTEMS, INC
29 1.1 lukem * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
30 1.1 lukem * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
31 1.1 lukem * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
32 1.1 lukem * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
33 1.1 lukem * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
34 1.1 lukem * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
35 1.1 lukem * POSSIBILITY OF SUCH DAMAGE.
36 1.1 lukem */
37 1.2 lukem
38 1.12 jmc #if HAVE_NBTOOL_CONFIG_H
39 1.12 jmc #include "nbtool_config.h"
40 1.12 jmc #endif
41 1.12 jmc
42 1.2 lukem #include <sys/cdefs.h>
43 1.9 tv #if defined(__RCSID) && !defined(__lint)
44 1.28 kre __RCSID("$NetBSD: buf.c,v 1.28 2023/03/14 10:36:06 kre Exp $");
45 1.2 lukem #endif /* !__lint */
46 1.1 lukem
47 1.1 lukem #include <sys/param.h>
48 1.1 lukem #include <sys/time.h>
49 1.1 lukem
50 1.1 lukem #include <assert.h>
51 1.1 lukem #include <errno.h>
52 1.1 lukem #include <stdio.h>
53 1.1 lukem #include <stdlib.h>
54 1.1 lukem #include <unistd.h>
55 1.17 christos #include <util.h>
56 1.1 lukem
57 1.5 lukem #include "makefs.h"
58 1.18 christos #include "buf.h"
59 1.1 lukem
60 1.1 lukem TAILQ_HEAD(buftailhead,buf) buftail;
61 1.1 lukem
62 1.1 lukem int
63 1.22 agc bread(struct vnode *vp, daddr_t blkno, int size, int u2 __unused,
64 1.22 agc struct buf **bpp)
65 1.1 lukem {
66 1.1 lukem off_t offset;
67 1.1 lukem ssize_t rv;
68 1.18 christos fsinfo_t *fs = vp->fs;
69 1.26 christos int saved_errno;
70 1.1 lukem
71 1.1 lukem assert (bpp != NULL);
72 1.1 lukem
73 1.1 lukem if (debug & DEBUG_BUF_BREAD)
74 1.26 christos printf("%s: blkno %jd size %d\n", __func__,
75 1.26 christos (intmax_t)blkno, size);
76 1.13 christos *bpp = getblk(vp, blkno, size, 0, 0);
77 1.20 christos offset = (*bpp)->b_blkno * fs->sectorsize + fs->offset;
78 1.1 lukem if (debug & DEBUG_BUF_BREAD)
79 1.26 christos printf("%s: blkno %jd offset %jd bcount %ld\n", __func__,
80 1.26 christos (intmax_t)(*bpp)->b_blkno, (intmax_t) offset,
81 1.1 lukem (*bpp)->b_bcount);
82 1.26 christos if (lseek((*bpp)->b_fs->fd, offset, SEEK_SET) == -1) {
83 1.26 christos saved_errno = errno;
84 1.26 christos warn("%s: lseek %jd (%jd)", __func__,
85 1.26 christos (intmax_t)(*bpp)->b_blkno, (intmax_t)offset);
86 1.26 christos goto out;
87 1.26 christos }
88 1.23 christos rv = read((*bpp)->b_fs->fd, (*bpp)->b_data, (size_t)(*bpp)->b_bcount);
89 1.26 christos saved_errno = errno;
90 1.1 lukem if (debug & DEBUG_BUF_BREAD)
91 1.26 christos printf("%s: read %ld (%jd) returned %zd\n", __func__,
92 1.26 christos (*bpp)->b_bcount, (intmax_t)offset, rv);
93 1.26 christos if (rv == -1) { /* read error */
94 1.27 kre warn("%s: read %ld (%jd) returned %zd", __func__,
95 1.26 christos (*bpp)->b_bcount, (intmax_t)offset, rv);
96 1.26 christos goto out;
97 1.26 christos }
98 1.26 christos if (rv != (*bpp)->b_bcount) { /* short read */
99 1.26 christos saved_errno = ENOSPC;
100 1.26 christos warn("%s: read %ld (%jd) returned %zd", __func__,
101 1.26 christos (*bpp)->b_bcount, (intmax_t)offset, rv);
102 1.26 christos goto out;
103 1.26 christos }
104 1.26 christos return 0;
105 1.26 christos out:
106 1.26 christos brelse(*bpp, 0);
107 1.26 christos *bpp = NULL;
108 1.26 christos #if 1
109 1.26 christos __USE(saved_errno);
110 1.26 christos exit(EXIT_FAILURE);
111 1.26 christos #else
112 1.26 christos return saved_errno;
113 1.26 christos #endif
114 1.1 lukem }
115 1.1 lukem
116 1.1 lukem void
117 1.13 christos brelse(struct buf *bp, int u1 __unused)
118 1.1 lukem {
119 1.1 lukem
120 1.1 lukem assert (bp != NULL);
121 1.1 lukem assert (bp->b_data != NULL);
122 1.1 lukem
123 1.1 lukem if (bp->b_lblkno < 0) {
124 1.1 lukem /*
125 1.1 lukem * XXX don't remove any buffers with negative logical block
126 1.1 lukem * numbers (lblkno), so that we retain the mapping
127 1.1 lukem * of negative lblkno -> real blkno that ffs_balloc()
128 1.1 lukem * sets up.
129 1.1 lukem *
130 1.1 lukem * if we instead released these buffers, and implemented
131 1.1 lukem * ufs_strategy() (and ufs_bmaparray()) and called those
132 1.1 lukem * from bread() and bwrite() to convert the lblkno to
133 1.1 lukem * a real blkno, we'd add a lot more code & complexity
134 1.1 lukem * and reading off disk, for little gain, because this
135 1.1 lukem * simple hack works for our purpose.
136 1.1 lukem */
137 1.1 lukem bp->b_bcount = 0;
138 1.1 lukem return;
139 1.1 lukem }
140 1.25 riastrad
141 1.1 lukem TAILQ_REMOVE(&buftail, bp, b_tailq);
142 1.1 lukem free(bp->b_data);
143 1.1 lukem free(bp);
144 1.1 lukem }
145 1.1 lukem
146 1.1 lukem int
147 1.1 lukem bwrite(struct buf *bp)
148 1.1 lukem {
149 1.1 lukem off_t offset;
150 1.1 lukem ssize_t rv;
151 1.23 christos size_t bytes;
152 1.28 kre int e;
153 1.18 christos fsinfo_t *fs = bp->b_fs;
154 1.1 lukem
155 1.1 lukem assert (bp != NULL);
156 1.20 christos offset = bp->b_blkno * fs->sectorsize + fs->offset;
157 1.23 christos bytes = (size_t)bp->b_bcount;
158 1.1 lukem if (debug & DEBUG_BUF_BWRITE)
159 1.26 christos printf("%s: blkno %jd offset %jd bcount %zu\n", __func__,
160 1.26 christos (intmax_t)bp->b_blkno, (intmax_t) offset, bytes);
161 1.19 christos if (lseek(bp->b_fs->fd, offset, SEEK_SET) == -1)
162 1.26 christos return errno;
163 1.19 christos rv = write(bp->b_fs->fd, bp->b_data, bytes);
164 1.28 kre e = errno;
165 1.1 lukem if (debug & DEBUG_BUF_BWRITE)
166 1.26 christos printf("%s: write %ld (offset %jd) returned %jd\n", __func__,
167 1.26 christos bp->b_bcount, (intmax_t)offset, (intmax_t)rv);
168 1.15 christos brelse(bp, 0);
169 1.23 christos if (rv == (ssize_t)bytes)
170 1.26 christos return 0;
171 1.26 christos if (rv == -1) /* write error */
172 1.28 kre return e;
173 1.26 christos return EAGAIN;
174 1.1 lukem }
175 1.1 lukem
176 1.1 lukem void
177 1.1 lukem bcleanup(void)
178 1.1 lukem {
179 1.1 lukem struct buf *bp;
180 1.1 lukem
181 1.1 lukem /*
182 1.1 lukem * XXX this really shouldn't be necessary, but i'm curious to
183 1.1 lukem * know why there's still some buffers lying around that
184 1.1 lukem * aren't brelse()d
185 1.1 lukem */
186 1.25 riastrad
187 1.1 lukem if (TAILQ_EMPTY(&buftail))
188 1.1 lukem return;
189 1.1 lukem
190 1.26 christos printf("%s: unflushed buffers:\n", __func__);
191 1.1 lukem TAILQ_FOREACH(bp, &buftail, b_tailq) {
192 1.26 christos printf("\tlblkno %10jd blkno %10jd count %6ld bufsize %6ld\n",
193 1.26 christos (intmax_t)bp->b_lblkno, (intmax_t)bp->b_blkno,
194 1.10 fvdl bp->b_bcount, bp->b_bufsize);
195 1.1 lukem }
196 1.26 christos printf("%s: done\n", __func__);
197 1.1 lukem }
198 1.1 lukem
199 1.1 lukem struct buf *
200 1.13 christos getblk(struct vnode *vp, daddr_t blkno, int size, int u1 __unused,
201 1.13 christos int u2 __unused)
202 1.1 lukem {
203 1.1 lukem static int buftailinitted;
204 1.1 lukem struct buf *bp;
205 1.11 itojun void *n;
206 1.1 lukem
207 1.1 lukem if (debug & DEBUG_BUF_GETBLK)
208 1.26 christos printf("%s: blkno %jd size %d\n", __func__,
209 1.26 christos (intmax_t)blkno, size);
210 1.1 lukem
211 1.1 lukem bp = NULL;
212 1.1 lukem if (!buftailinitted) {
213 1.1 lukem if (debug & DEBUG_BUF_GETBLK)
214 1.26 christos printf("%s: initialising tailq\n", __func__);
215 1.1 lukem TAILQ_INIT(&buftail);
216 1.1 lukem buftailinitted = 1;
217 1.1 lukem } else {
218 1.1 lukem TAILQ_FOREACH(bp, &buftail, b_tailq) {
219 1.1 lukem if (bp->b_lblkno != blkno)
220 1.1 lukem continue;
221 1.1 lukem break;
222 1.1 lukem }
223 1.1 lukem }
224 1.1 lukem if (bp == NULL) {
225 1.17 christos bp = ecalloc(1, sizeof(*bp));
226 1.1 lukem bp->b_bufsize = 0;
227 1.1 lukem bp->b_blkno = bp->b_lblkno = blkno;
228 1.18 christos bp->b_fs = vp->fs;
229 1.1 lukem bp->b_data = NULL;
230 1.1 lukem TAILQ_INSERT_HEAD(&buftail, bp, b_tailq);
231 1.1 lukem }
232 1.1 lukem bp->b_bcount = size;
233 1.1 lukem if (bp->b_data == NULL || bp->b_bcount > bp->b_bufsize) {
234 1.23 christos n = erealloc(bp->b_data, (size_t)size);
235 1.23 christos memset(n, 0, (size_t)size);
236 1.11 itojun bp->b_data = n;
237 1.1 lukem bp->b_bufsize = size;
238 1.1 lukem }
239 1.1 lukem
240 1.26 christos return bp;
241 1.1 lukem }
242