Home | History | Annotate | Line # | Download | only in ffs
buf.c revision 1.3
      1 /*	$NetBSD: buf.c,v 1.3 2001/11/02 03:12:49 lukem Exp $	*/
      2 
      3 /*
      4  * Copyright (c) 2001 Wasabi Systems, Inc.
      5  * All rights reserved.
      6  *
      7  * Written by Luke Mewburn for Wasabi Systems, Inc.
      8  *
      9  * Redistribution and use in source and binary forms, with or without
     10  * modification, are permitted provided that the following conditions
     11  * are met:
     12  * 1. Redistributions of source code must retain the above copyright
     13  *    notice, this list of conditions and the following disclaimer.
     14  * 2. Redistributions in binary form must reproduce the above copyright
     15  *    notice, this list of conditions and the following disclaimer in the
     16  *    documentation and/or other materials provided with the distribution.
     17  * 3. All advertising materials mentioning features or use of this software
     18  *    must display the following acknowledgement:
     19  *      This product includes software developed for the NetBSD Project by
     20  *      Wasabi Systems, Inc.
     21  * 4. The name of Wasabi Systems, Inc. may not be used to endorse
     22  *    or promote products derived from this software without specific prior
     23  *    written permission.
     24  *
     25  * THIS SOFTWARE IS PROVIDED BY WASABI SYSTEMS, INC. ``AS IS'' AND
     26  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
     27  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
     28  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL WASABI SYSTEMS, INC
     29  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
     30  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
     31  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
     32  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
     33  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
     34  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
     35  * POSSIBILITY OF SUCH DAMAGE.
     36  */
     37 
     38 #include <sys/cdefs.h>
     39 #ifndef __lint
     40 __RCSID("$NetBSD: buf.c,v 1.3 2001/11/02 03:12:49 lukem Exp $");
     41 #endif	/* !__lint */
     42 
     43 #include <sys/param.h>
     44 #include <sys/time.h>
     45 
     46 #include <assert.h>
     47 #include <err.h>
     48 #include <errno.h>
     49 #include <stdio.h>
     50 #include <stdlib.h>
     51 #include <unistd.h>
     52 
     53 #include <ufs/ufs/inode.h>
     54 #include <ufs/ffs/fs.h>
     55 
     56 #include "makefs.h"
     57 
     58 #include "ffs/buf.h"
     59 
     60 extern int sectorsize;		/* XXX: from ffs.c & mkfs.c */
     61 
     62 TAILQ_HEAD(buftailhead,buf) buftail;
     63 
     64 int
     65 bread(int fd, struct fs *fs, daddr_t blkno, int size, struct buf **bpp)
     66 {
     67 	off_t	offset;
     68 	ssize_t	rv;
     69 
     70 	assert (fs != NULL);
     71 	assert (bpp != NULL);
     72 
     73 	if (debug & DEBUG_BUF_BREAD)
     74 		printf("bread: fs %p blkno %d size %d\n",
     75 		    fs, blkno, size);
     76 	*bpp = getblk(fd, fs, blkno, size);
     77 	offset = (*bpp)->b_blkno * sectorsize;	/* XXX */
     78 	if (debug & DEBUG_BUF_BREAD)
     79 		printf("bread: bp %p blkno %d offset %lld bcount %ld\n",
     80 		    (*bpp), (*bpp)->b_blkno, (long long) offset,
     81 		    (*bpp)->b_bcount);
     82 	if (lseek((*bpp)->b_fd, offset, SEEK_SET) == -1)
     83 		err(1, "bread: lseek %lld (%lld)",
     84 		    (long long)(*bpp)->b_blkno, (long long)offset);
     85 	rv = read((*bpp)->b_fd, (*bpp)->b_data, (*bpp)->b_bcount);
     86 	if (debug & DEBUG_BUF_BREAD)
     87 		printf("bread: read %ld (%lld) returned %d\n",
     88 		    (*bpp)->b_bcount, (long long)offset, (int)rv);
     89 	if (rv == -1)				/* read error */
     90 		err(1, "bread: read %ld (%lld) returned %d",
     91 		    (*bpp)->b_bcount, (long long)offset, (int)rv);
     92 	else if (rv != (*bpp)->b_bcount)	/* short read */
     93 		err(1, "bread: read %ld (%lld) returned %d",
     94 		    (*bpp)->b_bcount, (long long)offset, (int)rv);
     95 	else
     96 		return (0);
     97 }
     98 
     99 void
    100 brelse(struct buf *bp)
    101 {
    102 
    103 	assert (bp != NULL);
    104 	assert (bp->b_data != NULL);
    105 
    106 	if (bp->b_lblkno < 0) {
    107 		/*
    108 		 * XXX	don't remove any buffers with negative logical block
    109 		 *	numbers (lblkno), so that we retain the mapping
    110 		 *	of negative lblkno -> real blkno that ffs_balloc()
    111 		 *	sets up.
    112 		 *
    113 		 *	if we instead released these buffers, and implemented
    114 		 *	ufs_strategy() (and ufs_bmaparray()) and called those
    115 		 *	from bread() and bwrite() to convert the lblkno to
    116 		 *	a real blkno, we'd add a lot more code & complexity
    117 		 *	and reading off disk, for little gain, because this
    118 		 *	simple hack works for our purpose.
    119 		 */
    120 		bp->b_bcount = 0;
    121 		return;
    122 	}
    123 
    124 	TAILQ_REMOVE(&buftail, bp, b_tailq);
    125 	free(bp->b_data);
    126 	free(bp);
    127 }
    128 
    129 int
    130 bwrite(struct buf *bp)
    131 {
    132 	off_t	offset;
    133 	ssize_t	rv;
    134 
    135 	assert (bp != NULL);
    136 	offset = bp->b_blkno * sectorsize;	/* XXX */
    137 	if (debug & DEBUG_BUF_BWRITE)
    138 		printf("bwrite: bp %p blkno %d offset %lld bcount %ld\n",
    139 		    bp, bp->b_blkno, (long long) offset, bp->b_bcount);
    140 	if (lseek(bp->b_fd, offset, SEEK_SET) == -1)
    141 		return (errno);
    142 	rv = write(bp->b_fd, bp->b_data, bp->b_bcount);
    143 	if (debug & DEBUG_BUF_BWRITE)
    144 		printf("bwrite: write %ld (offset %lld) returned %lld\n",
    145 		    bp->b_bcount, (long long)offset, (long long)rv);
    146 	if (rv == bp->b_bcount)
    147 		return (0);
    148 	else if (rv == -1)		/* write error */
    149 		return (errno);
    150 	else				/* short write ? */
    151 		return (EAGAIN);
    152 }
    153 
    154 void
    155 bcleanup(void)
    156 {
    157 	struct buf *bp;
    158 
    159 	/*
    160 	 * XXX	this really shouldn't be necessary, but i'm curious to
    161 	 *	know why there's still some buffers lying around that
    162 	 *	aren't brelse()d
    163 	 */
    164 
    165 	if (TAILQ_EMPTY(&buftail))
    166 		return;
    167 
    168 	printf("bcleanup: unflushed buffers:\n");
    169 	TAILQ_FOREACH(bp, &buftail, b_tailq) {
    170 		printf("\tlblkno %10d  blkno %10d  count %6ld  bufsize %6ld\n",
    171 		    bp->b_lblkno, bp->b_blkno, bp->b_bcount, bp->b_bufsize);
    172 	}
    173 	printf("bcleanup: done\n");
    174 }
    175 
    176 struct buf *
    177 getblk(int fd, struct fs *fs, daddr_t blkno, int size)
    178 {
    179 	static int buftailinitted;
    180 	struct buf *bp;
    181 
    182 	assert (fs != NULL);
    183 	if (debug & DEBUG_BUF_GETBLK)
    184 		printf("getblk: fs %p blkno %d size %d\n", fs, blkno, size);
    185 
    186 	bp = NULL;
    187 	if (!buftailinitted) {
    188 		if (debug & DEBUG_BUF_GETBLK)
    189 			printf("getblk: initialising tailq\n");
    190 		TAILQ_INIT(&buftail);
    191 		buftailinitted = 1;
    192 	} else {
    193 		TAILQ_FOREACH(bp, &buftail, b_tailq) {
    194 			if (bp->b_lblkno != blkno)
    195 				continue;
    196 			break;
    197 		}
    198 	}
    199 	if (bp == NULL) {
    200 		if ((bp = calloc(1, sizeof(struct buf))) == NULL)
    201 			err(1, "getblk: calloc");
    202 
    203 		bp->b_bufsize = 0;
    204 		bp->b_blkno = bp->b_lblkno = blkno;
    205 		bp->b_fd = fd;
    206 		bp->b_fs = fs;
    207 		bp->b_data = NULL;
    208 		TAILQ_INSERT_HEAD(&buftail, bp, b_tailq);
    209 	}
    210 	bp->b_bcount = size;
    211 	if (bp->b_data == NULL || bp->b_bcount > bp->b_bufsize) {
    212 		bp->b_bufsize = size;
    213 		if ((bp->b_data = realloc(bp->b_data, bp->b_bufsize)) == NULL)
    214 			err(1, "getblk: realloc b_data %ld", bp->b_bcount);
    215 	}
    216 
    217 	return (bp);
    218 }
    219