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