OpenCores
URL https://opencores.org/ocsvn/eco32/eco32/trunk

Subversion Repositories eco32

[/] [eco32/] [tags/] [eco32-0.22/] [disk/] [tools/] [fs-NetBSD/] [makefs/] [buf.c] - Diff between revs 17 and 21

Only display areas with differences | Details | Blame | View Log

Rev 17 Rev 21
/*      $NetBSD: buf.c,v 1.12 2004/06/20 22:20:18 jmc Exp $     */
/*      $NetBSD: buf.c,v 1.12 2004/06/20 22:20:18 jmc Exp $     */
 
 
/*
/*
 * Copyright (c) 2001 Wasabi Systems, Inc.
 * Copyright (c) 2001 Wasabi Systems, Inc.
 * All rights reserved.
 * All rights reserved.
 *
 *
 * Written by Luke Mewburn for Wasabi Systems, Inc.
 * Written by Luke Mewburn for Wasabi Systems, Inc.
 *
 *
 * Redistribution and use in source and binary forms, with or without
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions
 * modification, are permitted provided that the following conditions
 * are met:
 * are met:
 * 1. Redistributions of source code must retain the above copyright
 * 1. Redistributions of source code must retain the above copyright
 *    notice, this list of conditions and the following disclaimer.
 *    notice, this list of conditions and the following disclaimer.
 * 2. Redistributions in binary form must reproduce the above copyright
 * 2. Redistributions in binary form must reproduce the above copyright
 *    notice, this list of conditions and the following disclaimer in the
 *    notice, this list of conditions and the following disclaimer in the
 *    documentation and/or other materials provided with the distribution.
 *    documentation and/or other materials provided with the distribution.
 * 3. All advertising materials mentioning features or use of this software
 * 3. All advertising materials mentioning features or use of this software
 *    must display the following acknowledgement:
 *    must display the following acknowledgement:
 *      This product includes software developed for the NetBSD Project by
 *      This product includes software developed for the NetBSD Project by
 *      Wasabi Systems, Inc.
 *      Wasabi Systems, Inc.
 * 4. The name of Wasabi Systems, Inc. may not be used to endorse
 * 4. The name of Wasabi Systems, Inc. may not be used to endorse
 *    or promote products derived from this software without specific prior
 *    or promote products derived from this software without specific prior
 *    written permission.
 *    written permission.
 *
 *
 * THIS SOFTWARE IS PROVIDED BY WASABI SYSTEMS, INC. ``AS IS'' AND
 * THIS SOFTWARE IS PROVIDED BY WASABI SYSTEMS, INC. ``AS IS'' AND
 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
 * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
 * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
 * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL WASABI SYSTEMS, INC
 * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL WASABI SYSTEMS, INC
 * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
 * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
 * 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
 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
 * POSSIBILITY OF SUCH DAMAGE.
 * POSSIBILITY OF SUCH DAMAGE.
 */
 */
 
 
#if HAVE_NBTOOL_CONFIG_H
#if HAVE_NBTOOL_CONFIG_H
#include "nbtool_config.h"
#include "nbtool_config.h"
#endif
#endif
 
 
#include <sys/cdefs.h>
#include <sys/cdefs.h>
#if defined(__RCSID) && !defined(__lint)
#if defined(__RCSID) && !defined(__lint)
__RCSID("$NetBSD: buf.c,v 1.12 2004/06/20 22:20:18 jmc Exp $");
__RCSID("$NetBSD: buf.c,v 1.12 2004/06/20 22:20:18 jmc Exp $");
#endif  /* !__lint */
#endif  /* !__lint */
 
 
#include <sys/param.h>
#include <sys/param.h>
#include <sys/time.h>
#include <sys/time.h>
 
 
#include <assert.h>
#include <assert.h>
#include <errno.h>
#include <errno.h>
#include <stdio.h>
#include <stdio.h>
#include <stdlib.h>
#include <stdlib.h>
#include <unistd.h>
#include <unistd.h>
 
 
#include "common.h"
#include "common.h"
#include "makefs.h"
#include "makefs.h"
 
 
#include "buf.h"
#include "buf.h"
#include "fs.h"
#include "fs.h"
#include "dinode.h"
#include "dinode.h"
#include "ufs_inode.h"
#include "ufs_inode.h"
 
 
extern int sectorsize;          /* XXX: from ffs.c & mkfs.c */
extern int sectorsize;          /* XXX: from ffs.c & mkfs.c */
 
 
TAILQ_HEAD(buftailhead,buf) buftail;
TAILQ_HEAD(buftailhead,buf) buftail;
 
 
int
int
bread(int fd, struct fs *fs, daddr_t blkno, int size, struct buf **bpp)
bread(int fd, struct fs *fs, daddr_t blkno, int size, struct buf **bpp)
{
{
        off_t   offset;
        off_t   offset;
        ssize_t rv;
        ssize_t rv;
 
 
        assert (fs != NULL);
        assert (fs != NULL);
        assert (bpp != NULL);
        assert (bpp != NULL);
 
 
        if (debug & DEBUG_BUF_BREAD)
        if (debug & DEBUG_BUF_BREAD)
                printf("bread: fs %p blkno %lld size %d\n",
                printf("bread: fs %p blkno %lld size %d\n",
                    fs, (long long)blkno, size);
                    fs, (long long)blkno, size);
        *bpp = getblk(fd, fs, blkno, size);
        *bpp = getblk(fd, fs, blkno, size);
        offset = (*bpp)->b_blkno * sectorsize;  /* XXX */
        offset = (*bpp)->b_blkno * sectorsize;  /* XXX */
        if (debug & DEBUG_BUF_BREAD)
        if (debug & DEBUG_BUF_BREAD)
                printf("bread: bp %p blkno %lld offset %lld bcount %ld\n",
                printf("bread: bp %p blkno %lld offset %lld bcount %ld\n",
                    (*bpp), (long long)(*bpp)->b_blkno, (long long) offset,
                    (*bpp), (long long)(*bpp)->b_blkno, (long long) offset,
                    (*bpp)->b_bcount);
                    (*bpp)->b_bcount);
        if (lseek((*bpp)->b_fd, offset, SEEK_SET) == -1)
        if (lseek((*bpp)->b_fd, offset, SEEK_SET) == -1)
                err(1, "bread: lseek %lld (%lld)",
                err(1, "bread: lseek %lld (%lld)",
                    (long long)(*bpp)->b_blkno, (long long)offset);
                    (long long)(*bpp)->b_blkno, (long long)offset);
        rv = read((*bpp)->b_fd, (*bpp)->b_data, (*bpp)->b_bcount);
        rv = read((*bpp)->b_fd, (*bpp)->b_data, (*bpp)->b_bcount);
        if (debug & DEBUG_BUF_BREAD)
        if (debug & DEBUG_BUF_BREAD)
                printf("bread: read %ld (%lld) returned %d\n",
                printf("bread: read %ld (%lld) returned %d\n",
                    (*bpp)->b_bcount, (long long)offset, (int)rv);
                    (*bpp)->b_bcount, (long long)offset, (int)rv);
        if (rv == -1)                           /* read error */
        if (rv == -1)                           /* read error */
                err(1, "bread: read %ld (%lld) returned %d",
                err(1, "bread: read %ld (%lld) returned %d",
                    (*bpp)->b_bcount, (long long)offset, (int)rv);
                    (*bpp)->b_bcount, (long long)offset, (int)rv);
        else if (rv != (*bpp)->b_bcount)        /* short read */
        else if (rv != (*bpp)->b_bcount)        /* short read */
                err(1, "bread: read %ld (%lld) returned %d",
                err(1, "bread: read %ld (%lld) returned %d",
                    (*bpp)->b_bcount, (long long)offset, (int)rv);
                    (*bpp)->b_bcount, (long long)offset, (int)rv);
        else
        else
                return (0);
                return (0);
}
}
 
 
void
void
brelse(struct buf *bp)
brelse(struct buf *bp)
{
{
 
 
        assert (bp != NULL);
        assert (bp != NULL);
        assert (bp->b_data != NULL);
        assert (bp->b_data != NULL);
 
 
        if (bp->b_lblkno < 0) {
        if (bp->b_lblkno < 0) {
                /*
                /*
                 * XXX  don't remove any buffers with negative logical block
                 * XXX  don't remove any buffers with negative logical block
                 *      numbers (lblkno), so that we retain the mapping
                 *      numbers (lblkno), so that we retain the mapping
                 *      of negative lblkno -> real blkno that ffs_balloc()
                 *      of negative lblkno -> real blkno that ffs_balloc()
                 *      sets up.
                 *      sets up.
                 *
                 *
                 *      if we instead released these buffers, and implemented
                 *      if we instead released these buffers, and implemented
                 *      ufs_strategy() (and ufs_bmaparray()) and called those
                 *      ufs_strategy() (and ufs_bmaparray()) and called those
                 *      from bread() and bwrite() to convert the lblkno to
                 *      from bread() and bwrite() to convert the lblkno to
                 *      a real blkno, we'd add a lot more code & complexity
                 *      a real blkno, we'd add a lot more code & complexity
                 *      and reading off disk, for little gain, because this
                 *      and reading off disk, for little gain, because this
                 *      simple hack works for our purpose.
                 *      simple hack works for our purpose.
                 */
                 */
                bp->b_bcount = 0;
                bp->b_bcount = 0;
                return;
                return;
        }
        }
 
 
        TAILQ_REMOVE(&buftail, bp, b_tailq);
        TAILQ_REMOVE(&buftail, bp, b_tailq);
        free(bp->b_data);
        free(bp->b_data);
        free(bp);
        free(bp);
}
}
 
 
int
int
bwrite(struct buf *bp)
bwrite(struct buf *bp)
{
{
        off_t   offset;
        off_t   offset;
        ssize_t rv;
        ssize_t rv;
 
 
        assert (bp != NULL);
        assert (bp != NULL);
        offset = bp->b_blkno * sectorsize;      /* XXX */
        offset = bp->b_blkno * sectorsize;      /* XXX */
        if (debug & DEBUG_BUF_BWRITE)
        if (debug & DEBUG_BUF_BWRITE)
                printf("bwrite: bp %p blkno %lld offset %lld bcount %ld\n",
                printf("bwrite: bp %p blkno %lld offset %lld bcount %ld\n",
                    bp, (long long)bp->b_blkno, (long long) offset,
                    bp, (long long)bp->b_blkno, (long long) offset,
                    bp->b_bcount);
                    bp->b_bcount);
        if (lseek(bp->b_fd, offset, SEEK_SET) == -1)
        if (lseek(bp->b_fd, offset, SEEK_SET) == -1)
                return (errno);
                return (errno);
        rv = write(bp->b_fd, bp->b_data, bp->b_bcount);
        rv = write(bp->b_fd, bp->b_data, bp->b_bcount);
        if (debug & DEBUG_BUF_BWRITE)
        if (debug & DEBUG_BUF_BWRITE)
                printf("bwrite: write %ld (offset %lld) returned %lld\n",
                printf("bwrite: write %ld (offset %lld) returned %lld\n",
                    bp->b_bcount, (long long)offset, (long long)rv);
                    bp->b_bcount, (long long)offset, (long long)rv);
        if (rv == bp->b_bcount)
        if (rv == bp->b_bcount)
                return (0);
                return (0);
        else if (rv == -1)              /* write error */
        else if (rv == -1)              /* write error */
                return (errno);
                return (errno);
        else                            /* short write ? */
        else                            /* short write ? */
                return (EAGAIN);
                return (EAGAIN);
}
}
 
 
void
void
bcleanup(void)
bcleanup(void)
{
{
        struct buf *bp;
        struct buf *bp;
 
 
        /*
        /*
         * XXX  this really shouldn't be necessary, but i'm curious to
         * XXX  this really shouldn't be necessary, but i'm curious to
         *      know why there's still some buffers lying around that
         *      know why there's still some buffers lying around that
         *      aren't brelse()d
         *      aren't brelse()d
         */
         */
 
 
        if (TAILQ_EMPTY(&buftail))
        if (TAILQ_EMPTY(&buftail))
                return;
                return;
 
 
        printf("bcleanup: unflushed buffers:\n");
        printf("bcleanup: unflushed buffers:\n");
        TAILQ_FOREACH(bp, &buftail, b_tailq) {
        TAILQ_FOREACH(bp, &buftail, b_tailq) {
                printf("\tlblkno %10lld  blkno %10lld  count %6ld  bufsize %6ld\n",
                printf("\tlblkno %10lld  blkno %10lld  count %6ld  bufsize %6ld\n",
                    (long long)bp->b_lblkno, (long long)bp->b_blkno,
                    (long long)bp->b_lblkno, (long long)bp->b_blkno,
                    bp->b_bcount, bp->b_bufsize);
                    bp->b_bcount, bp->b_bufsize);
        }
        }
        printf("bcleanup: done\n");
        printf("bcleanup: done\n");
}
}
 
 
struct buf *
struct buf *
getblk(int fd, struct fs *fs, daddr_t blkno, int size)
getblk(int fd, struct fs *fs, daddr_t blkno, int size)
{
{
        static int buftailinitted;
        static int buftailinitted;
        struct buf *bp;
        struct buf *bp;
        void *n;
        void *n;
 
 
        assert (fs != NULL);
        assert (fs != NULL);
        if (debug & DEBUG_BUF_GETBLK)
        if (debug & DEBUG_BUF_GETBLK)
                printf("getblk: fs %p blkno %lld size %d\n", fs,
                printf("getblk: fs %p blkno %lld size %d\n", fs,
                    (long long)blkno, size);
                    (long long)blkno, size);
 
 
        bp = NULL;
        bp = NULL;
        if (!buftailinitted) {
        if (!buftailinitted) {
                if (debug & DEBUG_BUF_GETBLK)
                if (debug & DEBUG_BUF_GETBLK)
                        printf("getblk: initialising tailq\n");
                        printf("getblk: initialising tailq\n");
                TAILQ_INIT(&buftail);
                TAILQ_INIT(&buftail);
                buftailinitted = 1;
                buftailinitted = 1;
        } else {
        } else {
                TAILQ_FOREACH(bp, &buftail, b_tailq) {
                TAILQ_FOREACH(bp, &buftail, b_tailq) {
                        if (bp->b_lblkno != blkno)
                        if (bp->b_lblkno != blkno)
                                continue;
                                continue;
                        break;
                        break;
                }
                }
        }
        }
        if (bp == NULL) {
        if (bp == NULL) {
                if ((bp = calloc(1, sizeof(struct buf))) == NULL)
                if ((bp = calloc(1, sizeof(struct buf))) == NULL)
                        err(1, "getblk: calloc");
                        err(1, "getblk: calloc");
 
 
                bp->b_bufsize = 0;
                bp->b_bufsize = 0;
                bp->b_blkno = bp->b_lblkno = blkno;
                bp->b_blkno = bp->b_lblkno = blkno;
                bp->b_fd = fd;
                bp->b_fd = fd;
                bp->b_fs = fs;
                bp->b_fs = fs;
                bp->b_data = NULL;
                bp->b_data = NULL;
                TAILQ_INSERT_HEAD(&buftail, bp, b_tailq);
                TAILQ_INSERT_HEAD(&buftail, bp, b_tailq);
        }
        }
        bp->b_bcount = size;
        bp->b_bcount = size;
        if (bp->b_data == NULL || bp->b_bcount > bp->b_bufsize) {
        if (bp->b_data == NULL || bp->b_bcount > bp->b_bufsize) {
                n = realloc(bp->b_data, size);
                n = realloc(bp->b_data, size);
                if (n == NULL)
                if (n == NULL)
                        err(1, "getblk: realloc b_data %ld", bp->b_bcount);
                        err(1, "getblk: realloc b_data %ld", bp->b_bcount);
                bp->b_data = n;
                bp->b_data = n;
                bp->b_bufsize = size;
                bp->b_bufsize = size;
        }
        }
 
 
        return (bp);
        return (bp);
}
}
 
 

powered by: WebSVN 2.1.0

© copyright 1999-2024 OpenCores.org, equivalent to Oliscience, all rights reserved. OpenCores®, registered trademark.