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
40 #include <sys/cdefs.h>
41 __FBSDID("$FreeBSD$");
42
43 #include <sys/param.h>
44 #include <sys/time.h>
45
46 #include <assert.h>
47 #include <errno.h>
48 #include <stdio.h>
49 #include <stdlib.h>
50 #include <string.h>
51 #include <unistd.h>
52 #include <util.h>
53
54 #include "makefs.h"
55 #include "buf.h"
56
57 static TAILQ_HEAD(buftailhead, m_buf) buftail;
58
59 int
bread(struct m_vnode * vp,makefs_daddr_t blkno,int size,struct ucred * u1 __unused,struct m_buf ** bpp)60 bread(struct m_vnode *vp, makefs_daddr_t blkno, int size, struct ucred *u1 __unused,
61 struct m_buf **bpp)
62 {
63 off_t offset;
64 ssize_t rv;
65 fsinfo_t *fs = vp->fs;
66
67 assert (bpp != NULL);
68
69 if (debug & DEBUG_BUF_BREAD)
70 printf("%s: blkno %lld size %d\n", __func__, (long long)blkno,
71 size);
72 *bpp = getblk(vp, blkno, size, 0, 0, 0);
73 offset = (off_t)(*bpp)->b_blkno * fs->sectorsize + fs->offset;
74 if (debug & DEBUG_BUF_BREAD)
75 printf("%s: blkno %lld offset %lld bcount %ld\n", __func__,
76 (long long)(*bpp)->b_blkno, (long long) offset,
77 (*bpp)->b_bcount);
78 if (lseek((*bpp)->b_fs->fd, offset, SEEK_SET) == -1)
79 err(1, "%s: lseek %lld (%lld)", __func__,
80 (long long)(*bpp)->b_blkno, (long long)offset);
81 rv = read((*bpp)->b_fs->fd, (*bpp)->b_data, (size_t)(*bpp)->b_bcount);
82 if (debug & DEBUG_BUF_BREAD)
83 printf("%s: read %ld (%lld) returned %d\n", __func__,
84 (*bpp)->b_bcount, (long long)offset, (int)rv);
85 if (rv == -1) /* read error */
86 err(1, "%s: read %ld (%lld) returned %d", __func__,
87 (*bpp)->b_bcount, (long long)offset, (int)rv);
88 else if (rv != (*bpp)->b_bcount) /* short read */
89 err(1, "%s: read %ld (%lld) returned %d", __func__,
90 (*bpp)->b_bcount, (long long)offset, (int)rv);
91 else
92 return (0);
93 }
94
95 void
brelse(struct m_buf * bp)96 brelse(struct m_buf *bp)
97 {
98
99 assert (bp != NULL);
100 assert (bp->b_data != NULL);
101
102 if (bp->b_lblkno < 0) {
103 /*
104 * XXX don't remove any buffers with negative logical block
105 * numbers (lblkno), so that we retain the mapping
106 * of negative lblkno -> real blkno that ffs_balloc()
107 * sets up.
108 *
109 * if we instead released these buffers, and implemented
110 * ufs_strategy() (and ufs_bmaparray()) and called those
111 * from bread() and bwrite() to convert the lblkno to
112 * a real blkno, we'd add a lot more code & complexity
113 * and reading off disk, for little gain, because this
114 * simple hack works for our purpose.
115 */
116 bp->b_bcount = 0;
117 return;
118 }
119
120 assert(bp->b_vp);
121 if (!bp->b_vp->v_logical)
122 TAILQ_REMOVE(&buftail, bp, b_tailq);
123 free(bp->b_data);
124 free(bp);
125 }
126
127 int
bwrite(struct m_buf * bp)128 bwrite(struct m_buf *bp)
129 {
130 off_t offset;
131 ssize_t rv;
132 size_t bytes;
133 int e;
134 fsinfo_t *fs = bp->b_fs;
135
136 assert (bp != NULL);
137 offset = (off_t)bp->b_blkno * fs->sectorsize + fs->offset;
138 bytes = (size_t)bp->b_bcount;
139 if (debug & DEBUG_BUF_BWRITE)
140 printf("%s: blkno %lld offset %lld bcount %zu\n", __func__,
141 (long long)bp->b_blkno, (long long) offset, bytes);
142 if (lseek(bp->b_fs->fd, offset, SEEK_SET) == -1) {
143 brelse(bp);
144 return (errno);
145 }
146 rv = write(bp->b_fs->fd, bp->b_data, bytes);
147 e = errno;
148 if (debug & DEBUG_BUF_BWRITE)
149 printf("%s: write %ld (offset %lld) returned %lld\n", __func__,
150 bp->b_bcount, (long long)offset, (long long)rv);
151 brelse(bp);
152 if (rv == (ssize_t)bytes)
153 return (0);
154 if (rv == -1) /* write error */
155 return (e);
156 return (EAGAIN);
157 }
158
159 void
bcleanup(void)160 bcleanup(void)
161 {
162 struct m_buf *bp;
163
164 /*
165 * XXX this really shouldn't be necessary, but i'm curious to
166 * know why there's still some buffers lying around that
167 * aren't brelse()d
168 */
169
170 if (TAILQ_EMPTY(&buftail)) {
171 printf("%s: clean\n", __func__);
172 return;
173 }
174
175 printf("%s: unflushed buffers:\n", __func__);
176 TAILQ_FOREACH(bp, &buftail, b_tailq) {
177 printf("\t%p lblkno %10lld blkno %10lld count %6ld bufsize %6ld "
178 "loffset %016llx cmd %d [vp %p data %p type %d logical %d vflushed %d]\n",
179 bp, (long long)bp->b_lblkno, (long long)bp->b_blkno,
180 bp->b_bcount, bp->b_bufsize,
181 (long long)bp->b_loffset, bp->b_cmd, bp->b_vp,
182 bp->b_vp ? bp->b_vp->v_data : NULL,
183 bp->b_vp ? bp->b_vp->v_type : -1,
184 bp->b_vp ? bp->b_vp->v_logical : -1,
185 bp->b_vp ? bp->b_vp->v_vflushed : -1);
186 if (bp->b_vp)
187 assert(!bp->b_vp->v_logical);
188 }
189 printf("%s: done\n", __func__);
190 }
191
192 struct m_buf *
getblk(struct m_vnode * vp,makefs_daddr_t blkno,int size,int u1 __unused,int u2 __unused,int u3 __unused)193 getblk(struct m_vnode *vp, makefs_daddr_t blkno, int size, int u1 __unused,
194 int u2 __unused, int u3 __unused)
195 {
196 static int buftailinitted;
197 struct m_buf *bp;
198 void *n;
199
200 bp = NULL;
201 if (vp->v_logical)
202 goto skip_lookup;
203
204 if (debug & DEBUG_BUF_GETBLK)
205 printf("%s: blkno %lld size %d\n", __func__, (long long)blkno,
206 size);
207
208 if (!buftailinitted) {
209 if (debug & DEBUG_BUF_GETBLK)
210 printf("%s: initialising tailq\n", __func__);
211 TAILQ_INIT(&buftail);
212 buftailinitted = 1;
213 } else {
214 TAILQ_FOREACH(bp, &buftail, b_tailq) {
215 if (bp->b_lblkno != blkno)
216 continue;
217 break;
218 }
219 }
220 skip_lookup:
221 if (bp == NULL) {
222 bp = ecalloc(1, sizeof(*bp));
223 bp->b_bufsize = 0;
224 bp->b_blkno = bp->b_lblkno = blkno;
225 bp->b_fs = vp->fs;
226 bp->b_data = NULL;
227 bp->b_vp = vp;
228 assert(bp->b_vp);
229 if (!bp->b_vp->v_logical)
230 TAILQ_INSERT_HEAD(&buftail, bp, b_tailq);
231 }
232 bp->b_bcount = size;
233 if (bp->b_data == NULL || bp->b_bcount > bp->b_bufsize) {
234 n = erealloc(bp->b_data, (size_t)size);
235 memset(n, 0, (size_t)size);
236 bp->b_data = n;
237 bp->b_bufsize = size;
238 }
239
240 return (bp);
241 }
242