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