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