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