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