2017-02-11 03:33:48 +01:00
|
|
|
/* $NetBSD: buf.c,v 1.13 2004/06/20 22:20:18 jmc Exp $ */
|
2008-11-23 20:59:42 +01:00
|
|
|
|
2017-11-27 16:37:16 +01:00
|
|
|
/*-
|
|
|
|
* SPDX-License-Identifier: BSD-4-Clause
|
|
|
|
*
|
2008-11-23 20:59:42 +01:00
|
|
|
* Copyright (c) 2001 Wasabi Systems, Inc.
|
|
|
|
* All rights reserved.
|
|
|
|
*
|
|
|
|
* Written by Luke Mewburn for Wasabi Systems, Inc.
|
|
|
|
*
|
|
|
|
* Redistribution and use in source and binary forms, with or without
|
|
|
|
* modification, are permitted provided that the following conditions
|
|
|
|
* are met:
|
|
|
|
* 1. Redistributions of source code must retain the above copyright
|
|
|
|
* notice, this list of conditions and the following disclaimer.
|
|
|
|
* 2. Redistributions in binary form must reproduce the above copyright
|
|
|
|
* notice, this list of conditions and the following disclaimer in the
|
|
|
|
* documentation and/or other materials provided with the distribution.
|
|
|
|
* 3. All advertising materials mentioning features or use of this software
|
|
|
|
* must display the following acknowledgement:
|
|
|
|
* This product includes software developed for the NetBSD Project by
|
|
|
|
* Wasabi Systems, Inc.
|
|
|
|
* 4. The name of Wasabi Systems, Inc. may not be used to endorse
|
|
|
|
* or promote products derived from this software without specific prior
|
|
|
|
* written permission.
|
|
|
|
*
|
|
|
|
* THIS SOFTWARE IS PROVIDED BY WASABI SYSTEMS, INC. ``AS IS'' AND
|
|
|
|
* ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
|
|
|
|
* TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
|
|
|
|
* PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL WASABI SYSTEMS, INC
|
|
|
|
* BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
|
|
|
|
* CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
|
|
|
|
* SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
|
|
|
|
* INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
|
|
|
|
* CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
|
|
|
|
* ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
|
|
|
|
* POSSIBILITY OF SUCH DAMAGE.
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include <sys/cdefs.h>
|
2008-12-19 19:45:43 +01:00
|
|
|
__FBSDID("$FreeBSD$");
|
2008-11-23 20:59:42 +01:00
|
|
|
|
|
|
|
#include <sys/param.h>
|
|
|
|
#include <sys/time.h>
|
|
|
|
|
|
|
|
#include <assert.h>
|
|
|
|
#include <errno.h>
|
|
|
|
#include <stdio.h>
|
|
|
|
#include <stdlib.h>
|
2017-04-06 02:07:11 +02:00
|
|
|
#include <string.h>
|
2008-11-23 20:59:42 +01:00
|
|
|
#include <unistd.h>
|
2017-04-06 18:18:42 +02:00
|
|
|
#include <util.h>
|
2008-11-23 20:59:42 +01:00
|
|
|
|
|
|
|
#include "makefs.h"
|
2017-05-03 16:21:18 +02:00
|
|
|
#include "buf.h"
|
2008-11-23 20:59:42 +01:00
|
|
|
|
2021-02-18 14:43:58 +01:00
|
|
|
static TAILQ_HEAD(buftailhead, m_buf) buftail;
|
2008-11-23 20:59:42 +01:00
|
|
|
|
|
|
|
int
|
2021-02-18 14:43:58 +01:00
|
|
|
bread(struct m_vnode *vp, daddr_t blkno, int size, struct ucred *u1 __unused,
|
|
|
|
struct m_buf **bpp)
|
2008-11-23 20:59:42 +01:00
|
|
|
{
|
|
|
|
off_t offset;
|
|
|
|
ssize_t rv;
|
2017-06-03 16:28:19 +02:00
|
|
|
fsinfo_t *fs = vp->fs;
|
2008-11-23 20:59:42 +01:00
|
|
|
|
|
|
|
assert (bpp != NULL);
|
|
|
|
|
|
|
|
if (debug & DEBUG_BUF_BREAD)
|
2017-03-15 19:14:54 +01:00
|
|
|
printf("%s: blkno %lld size %d\n", __func__, (long long)blkno,
|
|
|
|
size);
|
2017-02-11 03:33:48 +01:00
|
|
|
*bpp = getblk(vp, blkno, size, 0, 0, 0);
|
2021-06-15 02:18:36 +02:00
|
|
|
offset = (off_t)(*bpp)->b_blkno * fs->sectorsize + fs->offset;
|
2008-11-23 20:59:42 +01:00
|
|
|
if (debug & DEBUG_BUF_BREAD)
|
2017-03-15 19:14:54 +01:00
|
|
|
printf("%s: blkno %lld offset %lld bcount %ld\n", __func__,
|
2017-03-15 18:26:19 +01:00
|
|
|
(long long)(*bpp)->b_blkno, (long long) offset,
|
2008-11-23 20:59:42 +01:00
|
|
|
(*bpp)->b_bcount);
|
2017-05-25 23:41:06 +02:00
|
|
|
if (lseek((*bpp)->b_fs->fd, offset, SEEK_SET) == -1)
|
2017-03-15 19:14:54 +01:00
|
|
|
err(1, "%s: lseek %lld (%lld)", __func__,
|
2008-11-23 20:59:42 +01:00
|
|
|
(long long)(*bpp)->b_blkno, (long long)offset);
|
2023-03-30 04:04:16 +02:00
|
|
|
rv = read((*bpp)->b_fs->fd, (*bpp)->b_data, (size_t)(*bpp)->b_bcount);
|
2008-11-23 20:59:42 +01:00
|
|
|
if (debug & DEBUG_BUF_BREAD)
|
2017-03-15 19:14:54 +01:00
|
|
|
printf("%s: read %ld (%lld) returned %d\n", __func__,
|
2008-11-23 20:59:42 +01:00
|
|
|
(*bpp)->b_bcount, (long long)offset, (int)rv);
|
|
|
|
if (rv == -1) /* read error */
|
2017-03-15 19:14:54 +01:00
|
|
|
err(1, "%s: read %ld (%lld) returned %d", __func__,
|
2008-11-23 20:59:42 +01:00
|
|
|
(*bpp)->b_bcount, (long long)offset, (int)rv);
|
|
|
|
else if (rv != (*bpp)->b_bcount) /* short read */
|
2017-03-15 19:14:54 +01:00
|
|
|
err(1, "%s: read %ld (%lld) returned %d", __func__,
|
2008-11-23 20:59:42 +01:00
|
|
|
(*bpp)->b_bcount, (long long)offset, (int)rv);
|
|
|
|
else
|
|
|
|
return (0);
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
2021-02-18 14:43:58 +01:00
|
|
|
brelse(struct m_buf *bp)
|
2008-11-23 20:59:42 +01:00
|
|
|
{
|
|
|
|
|
|
|
|
assert (bp != NULL);
|
|
|
|
assert (bp->b_data != NULL);
|
|
|
|
|
|
|
|
if (bp->b_lblkno < 0) {
|
|
|
|
/*
|
|
|
|
* XXX don't remove any buffers with negative logical block
|
|
|
|
* numbers (lblkno), so that we retain the mapping
|
|
|
|
* of negative lblkno -> real blkno that ffs_balloc()
|
|
|
|
* sets up.
|
|
|
|
*
|
|
|
|
* if we instead released these buffers, and implemented
|
|
|
|
* ufs_strategy() (and ufs_bmaparray()) and called those
|
|
|
|
* from bread() and bwrite() to convert the lblkno to
|
|
|
|
* a real blkno, we'd add a lot more code & complexity
|
|
|
|
* and reading off disk, for little gain, because this
|
|
|
|
* simple hack works for our purpose.
|
|
|
|
*/
|
|
|
|
bp->b_bcount = 0;
|
|
|
|
return;
|
|
|
|
}
|
2010-11-07 17:05:04 +01:00
|
|
|
|
2008-11-23 20:59:42 +01:00
|
|
|
TAILQ_REMOVE(&buftail, bp, b_tailq);
|
|
|
|
free(bp->b_data);
|
|
|
|
free(bp);
|
|
|
|
}
|
|
|
|
|
|
|
|
int
|
2021-02-18 14:43:58 +01:00
|
|
|
bwrite(struct m_buf *bp)
|
2008-11-23 20:59:42 +01:00
|
|
|
{
|
|
|
|
off_t offset;
|
|
|
|
ssize_t rv;
|
2023-03-13 22:00:20 +01:00
|
|
|
size_t bytes;
|
|
|
|
int e;
|
2017-05-25 23:41:06 +02:00
|
|
|
fsinfo_t *fs = bp->b_fs;
|
2008-11-23 20:59:42 +01:00
|
|
|
|
|
|
|
assert (bp != NULL);
|
2021-06-15 02:18:36 +02:00
|
|
|
offset = (off_t)bp->b_blkno * fs->sectorsize + fs->offset;
|
2023-03-13 22:00:20 +01:00
|
|
|
bytes = (size_t)bp->b_bcount;
|
2008-11-23 20:59:42 +01:00
|
|
|
if (debug & DEBUG_BUF_BWRITE)
|
2023-03-13 22:00:20 +01:00
|
|
|
printf("%s: blkno %lld offset %lld bcount %zu\n", __func__,
|
|
|
|
(long long)bp->b_blkno, (long long) offset, bytes);
|
|
|
|
if (lseek(bp->b_fs->fd, offset, SEEK_SET) == -1) {
|
|
|
|
brelse(bp);
|
2008-11-23 20:59:42 +01:00
|
|
|
return (errno);
|
2023-03-13 22:00:20 +01:00
|
|
|
}
|
|
|
|
rv = write(bp->b_fs->fd, bp->b_data, bytes);
|
|
|
|
e = errno;
|
2008-11-23 20:59:42 +01:00
|
|
|
if (debug & DEBUG_BUF_BWRITE)
|
2023-04-11 17:19:06 +02:00
|
|
|
printf("%s: write %ld (offset %lld) returned %lld\n", __func__,
|
2008-11-23 20:59:42 +01:00
|
|
|
bp->b_bcount, (long long)offset, (long long)rv);
|
2023-03-13 22:00:20 +01:00
|
|
|
brelse(bp);
|
|
|
|
if (rv == (ssize_t)bytes)
|
2008-11-23 20:59:42 +01:00
|
|
|
return (0);
|
2023-03-13 22:00:20 +01:00
|
|
|
if (rv == -1) /* write error */
|
|
|
|
return (e);
|
|
|
|
return (EAGAIN);
|
2008-11-23 20:59:42 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
bcleanup(void)
|
|
|
|
{
|
2021-02-18 14:43:58 +01:00
|
|
|
struct m_buf *bp;
|
2008-11-23 20:59:42 +01:00
|
|
|
|
|
|
|
/*
|
|
|
|
* XXX this really shouldn't be necessary, but i'm curious to
|
|
|
|
* know why there's still some buffers lying around that
|
|
|
|
* aren't brelse()d
|
|
|
|
*/
|
2010-11-07 17:05:04 +01:00
|
|
|
|
2008-11-23 20:59:42 +01:00
|
|
|
if (TAILQ_EMPTY(&buftail))
|
|
|
|
return;
|
|
|
|
|
2023-04-11 17:19:06 +02:00
|
|
|
printf("%s: unflushed buffers:\n", __func__);
|
2008-11-23 20:59:42 +01:00
|
|
|
TAILQ_FOREACH(bp, &buftail, b_tailq) {
|
|
|
|
printf("\tlblkno %10lld blkno %10lld count %6ld bufsize %6ld\n",
|
|
|
|
(long long)bp->b_lblkno, (long long)bp->b_blkno,
|
|
|
|
bp->b_bcount, bp->b_bufsize);
|
|
|
|
}
|
2023-04-11 17:19:06 +02:00
|
|
|
printf("%s: done\n", __func__);
|
2008-11-23 20:59:42 +01:00
|
|
|
}
|
|
|
|
|
2021-02-18 14:43:58 +01:00
|
|
|
struct m_buf *
|
|
|
|
getblk(struct m_vnode *vp, daddr_t blkno, int size, int u1 __unused,
|
2017-02-11 03:33:48 +01:00
|
|
|
int u2 __unused, int u3 __unused)
|
2008-11-23 20:59:42 +01:00
|
|
|
{
|
|
|
|
static int buftailinitted;
|
2021-02-18 14:43:58 +01:00
|
|
|
struct m_buf *bp;
|
2008-11-23 20:59:42 +01:00
|
|
|
void *n;
|
|
|
|
|
|
|
|
if (debug & DEBUG_BUF_GETBLK)
|
2023-04-11 17:19:06 +02:00
|
|
|
printf("%s: blkno %lld size %d\n", __func__, (long long)blkno,
|
|
|
|
size);
|
2008-11-23 20:59:42 +01:00
|
|
|
|
|
|
|
bp = NULL;
|
|
|
|
if (!buftailinitted) {
|
|
|
|
if (debug & DEBUG_BUF_GETBLK)
|
2023-04-11 17:19:06 +02:00
|
|
|
printf("%s: initialising tailq\n", __func__);
|
2008-11-23 20:59:42 +01:00
|
|
|
TAILQ_INIT(&buftail);
|
|
|
|
buftailinitted = 1;
|
|
|
|
} else {
|
|
|
|
TAILQ_FOREACH(bp, &buftail, b_tailq) {
|
|
|
|
if (bp->b_lblkno != blkno)
|
|
|
|
continue;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if (bp == NULL) {
|
2017-04-06 18:18:42 +02:00
|
|
|
bp = ecalloc(1, sizeof(*bp));
|
2008-11-23 20:59:42 +01:00
|
|
|
bp->b_bufsize = 0;
|
|
|
|
bp->b_blkno = bp->b_lblkno = blkno;
|
2017-05-03 16:21:18 +02:00
|
|
|
bp->b_fs = vp->fs;
|
2008-11-23 20:59:42 +01:00
|
|
|
bp->b_data = NULL;
|
|
|
|
TAILQ_INSERT_HEAD(&buftail, bp, b_tailq);
|
|
|
|
}
|
|
|
|
bp->b_bcount = size;
|
|
|
|
if (bp->b_data == NULL || bp->b_bcount > bp->b_bufsize) {
|
2023-03-30 04:04:16 +02:00
|
|
|
n = erealloc(bp->b_data, (size_t)size);
|
|
|
|
memset(n, 0, (size_t)size);
|
2008-11-23 20:59:42 +01:00
|
|
|
bp->b_data = n;
|
|
|
|
bp->b_bufsize = size;
|
|
|
|
}
|
|
|
|
|
|
|
|
return (bp);
|
|
|
|
}
|