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