uint8_t *bread(uint16_t dev, blkno_t blk, bool rewrite) { register bufptr bp; if ((bp = bfind(dev, blk)) != NULL) { if (bp->bf_busy == BF_BUSY) panic(PANIC_WANTBSYB); else if (bp->bf_busy == BF_FREE) bp->bf_busy = BF_BUSY; /* BF_SUPERBLOCK is fine */ } else { bp = freebuf(); bp->bf_dev = dev; bp->bf_blk = blk; bp->bf_busy = BF_BUSY; /* If rewrite is set, we are about to write over the entire block, so we don't need the previous contents */ if (!rewrite) { if (bdread(bp) == -1) { udata.u_error = EIO; return (NULL); } } } bp->bf_time = ++bufclock; /* Time stamp it */ return (bp->bf_data); }
char *bread(int dev, blkno_t blk, int rewrite) { register bufptr bp; /*printf("Reading block %d\n", blk);*/ bp = bfind(dev, blk); if (bp) { if (bp->bf_busy) panic("want busy block"); goto done; } bp = freebuf(); bp->bf_dev = dev; bp->bf_blk = blk; /* If rewrite is set, we are about to write over the entire block, so we don't need the previous contents */ ifnot(rewrite) if (bdread(bp) == -1) { udata.u_error = EIO; return 0; } done: bp->bf_busy = 1; bp->bf_time = ++bufclock; /* Time stamp it */ return (bp->bf_data); }
/* * Make an entry in the buffer cache and fill it. If rewrite is * set then we are not keeping any of the old data but overwriting * it all. * * Hands back either a locked buffer, or NULL on an error. */ bufptr bread(uint16_t dev, blkno_t blk, bool rewrite) { regptr bufptr bp; if ((bp = bfind(dev, blk)) == NULL) { bp = freebuf(); bp->bf_dev = dev; bp->bf_blk = blk; /* If rewrite is set, we are about to write over the entire block, so we don't need the previous contents */ if (!rewrite) { if (bdread(bp) != BLKSIZE) { udata.u_error = EIO; /* Don't cache the failure */ bp->bf_dev = NO_DEVICE; bp->bf_dirty = false; bunlock(bp); return (NULL); } } } return bp; }