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