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