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