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