Skip to content

Commit

Permalink
Disentangle block size from the disk's sector size. Set block size to…
Browse files Browse the repository at this point in the history
… 1024 to show

that they can be different.  Clean up mkfs, simplifying specifying fs parameters,
remove some redundancy between fs and mkfs, and fix disk layout bugs. Call blocks
in the file system blocks instead of sectors.  Passes usertests for different
block sizes.
  • Loading branch information
kaashoek committed Apr 3, 2015
1 parent 7443b96 commit c24ac5d
Show file tree
Hide file tree
Showing 11 changed files with 91 additions and 86 deletions.
20 changes: 11 additions & 9 deletions bio.c
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@
#include "defs.h"
#include "param.h"
#include "spinlock.h"
#include "fs.h"
#include "buf.h"

struct {
Expand Down Expand Up @@ -55,20 +56,20 @@ binit(void)
}
}

// Look through buffer cache for sector on device dev.
// Look through buffer cache for block on device dev.
// If not found, allocate a buffer.
// In either case, return B_BUSY buffer.
static struct buf*
bget(uint dev, uint sector)
bget(uint dev, uint blockno)
{
struct buf *b;

acquire(&bcache.lock);

loop:
// Is the sector already cached?
// Is the block already cached?
for(b = bcache.head.next; b != &bcache.head; b = b->next){
if(b->dev == dev && b->sector == sector){
if(b->dev == dev && b->blockno == blockno){
if(!(b->flags & B_BUSY)){
b->flags |= B_BUSY;
release(&bcache.lock);
Expand All @@ -85,7 +86,7 @@ bget(uint dev, uint sector)
for(b = bcache.head.prev; b != &bcache.head; b = b->prev){
if((b->flags & B_BUSY) == 0 && (b->flags & B_DIRTY) == 0){
b->dev = dev;
b->sector = sector;
b->blockno = blockno;
b->flags = B_BUSY;
release(&bcache.lock);
return b;
Expand All @@ -94,15 +95,16 @@ bget(uint dev, uint sector)
panic("bget: no buffers");
}

// Return a B_BUSY buf with the contents of the indicated disk sector.
// Return a B_BUSY buf with the contents of the indicated block.
struct buf*
bread(uint dev, uint sector)
bread(uint dev, uint blockno)
{
struct buf *b;

b = bget(dev, sector);
if(!(b->flags & B_VALID))
b = bget(dev, blockno);
if(!(b->flags & B_VALID)) {
iderw(b);
}
return b;
}

Expand Down
2 changes: 1 addition & 1 deletion bootmain.c
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
// Boot loader.
//
// Part of the boot sector, along with bootasm.S, which calls bootmain().
// Part of the boot block, along with bootasm.S, which calls bootmain().
// bootasm.S has put the processor into protected 32-bit mode.
// bootmain() loads an ELF kernel image from the disk starting at
// sector 1 and then jumps to the kernel entry routine.
Expand Down
4 changes: 2 additions & 2 deletions buf.h
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
struct buf {
int flags;
uint dev;
uint sector;
uint blockno;
struct buf *prev; // LRU cache list
struct buf *next;
struct buf *qnext; // disk queue
uchar data[512];
uchar data[BSIZE];
};
#define B_BUSY 0x1 // buffer is locked by some process
#define B_VALID 0x2 // buffer has been read from disk
Expand Down
2 changes: 1 addition & 1 deletion fs.c
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,8 @@
#include "mmu.h"
#include "proc.h"
#include "spinlock.h"
#include "buf.h"
#include "fs.h"
#include "buf.h"
#include "file.h"

#define min(a, b) ((a) < (b) ? (a) : (b))
Expand Down
2 changes: 1 addition & 1 deletion fs.h
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
// Then sb.nlog log blocks.

#define ROOTINO 1 // root i-number
#define BSIZE 512 // block size
#define BSIZE 1024 // block size

// File system super block
struct superblock {
Expand Down
20 changes: 13 additions & 7 deletions ide.c
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,10 @@
#include "x86.h"
#include "traps.h"
#include "spinlock.h"
#include "fs.h"
#include "buf.h"

#define SECTOR_SIZE 512
#define IDE_BSY 0x80
#define IDE_DRDY 0x40
#define IDE_DF 0x20
Expand Down Expand Up @@ -71,17 +73,21 @@ idestart(struct buf *b)
{
if(b == 0)
panic("idestart");
int sector_per_block = BSIZE/SECTOR_SIZE;
int sector = b->blockno * sector_per_block;

if (sector_per_block > 7) panic("idestart");

idewait(0);
outb(0x3f6, 0); // generate interrupt
outb(0x1f2, 1); // number of sectors
outb(0x1f3, b->sector & 0xff);
outb(0x1f4, (b->sector >> 8) & 0xff);
outb(0x1f5, (b->sector >> 16) & 0xff);
outb(0x1f6, 0xe0 | ((b->dev&1)<<4) | ((b->sector>>24)&0x0f));
outb(0x1f2, sector_per_block); // number of sectors
outb(0x1f3, sector & 0xff);
outb(0x1f4, (sector >> 8) & 0xff);
outb(0x1f5, (sector >> 16) & 0xff);
outb(0x1f6, 0xe0 | ((b->dev&1)<<4) | ((sector>>24)&0x0f));
if(b->flags & B_DIRTY){
outb(0x1f7, IDE_CMD_WRITE);
outsl(0x1f0, b->data, 512/4);
outsl(0x1f0, b->data, BSIZE/4);
} else {
outb(0x1f7, IDE_CMD_READ);
}
Expand All @@ -104,7 +110,7 @@ ideintr(void)

// Read data if needed.
if(!(b->flags & B_DIRTY) && idewait(1) >= 0)
insl(0x1f0, b->data, 512/4);
insl(0x1f0, b->data, BSIZE/4);

// Wake process waiting for this buf.
b->flags |= B_VALID;
Expand Down
2 changes: 1 addition & 1 deletion kill.c
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ main(int argc, char **argv)
{
int i;

if(argc < 1){
if(argc < 2){
printf(2, "usage: kill pid...\n");
exit();
}
Expand Down
18 changes: 9 additions & 9 deletions log.c
Original file line number Diff line number Diff line change
Expand Up @@ -21,18 +21,18 @@
//
// The log is a physical re-do log containing disk blocks.
// The on-disk log format:
// header block, containing sector #s for block A, B, C, ...
// header block, containing block #s for block A, B, C, ...
// block A
// block B
// block C
// ...
// Log appends are synchronous.

// Contents of the header block, used for both the on-disk header block
// and to keep track in memory of logged sector #s before commit.
// and to keep track in memory of logged block# before commit.
struct logheader {
int n;
int sector[LOGSIZE];
int block[LOGSIZE];
};

struct log {
Expand Down Expand Up @@ -72,7 +72,7 @@ install_trans(void)

for (tail = 0; tail < log.lh.n; tail++) {
struct buf *lbuf = bread(log.dev, log.start+tail+1); // read log block
struct buf *dbuf = bread(log.dev, log.lh.sector[tail]); // read dst
struct buf *dbuf = bread(log.dev, log.lh.block[tail]); // read dst
memmove(dbuf->data, lbuf->data, BSIZE); // copy block to dst
bwrite(dbuf); // write dst to disk
brelse(lbuf);
Expand All @@ -89,7 +89,7 @@ read_head(void)
int i;
log.lh.n = lh->n;
for (i = 0; i < log.lh.n; i++) {
log.lh.sector[i] = lh->sector[i];
log.lh.block[i] = lh->block[i];
}
brelse(buf);
}
Expand All @@ -105,7 +105,7 @@ write_head(void)
int i;
hb->n = log.lh.n;
for (i = 0; i < log.lh.n; i++) {
hb->sector[i] = log.lh.sector[i];
hb->block[i] = log.lh.block[i];
}
bwrite(buf);
brelse(buf);
Expand Down Expand Up @@ -178,7 +178,7 @@ write_log(void)

for (tail = 0; tail < log.lh.n; tail++) {
struct buf *to = bread(log.dev, log.start+tail+1); // log block
struct buf *from = bread(log.dev, log.lh.sector[tail]); // cache block
struct buf *from = bread(log.dev, log.lh.block[tail]); // cache block
memmove(to->data, from->data, BSIZE);
bwrite(to); // write the log
brelse(from);
Expand Down Expand Up @@ -219,10 +219,10 @@ log_write(struct buf *b)

acquire(&log.lock);
for (i = 0; i < log.lh.n; i++) {
if (log.lh.sector[i] == b->sector) // log absorbtion
if (log.lh.block[i] == b->blockno) // log absorbtion
break;
}
log.lh.sector[i] = b->sector;
log.lh.block[i] = b->blockno;
if (i == log.lh.n)
log.lh.n++;
b->flags |= B_DIRTY; // prevent eviction
Expand Down
12 changes: 6 additions & 6 deletions memide.c
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ void
ideinit(void)
{
memdisk = _binary_fs_img_start;
disksize = (uint)_binary_fs_img_size/512;
disksize = (uint)_binary_fs_img_size/BSIZE;
}

// Interrupt handler.
Expand All @@ -44,15 +44,15 @@ iderw(struct buf *b)
panic("iderw: nothing to do");
if(b->dev != 1)
panic("iderw: request not for disk 1");
if(b->sector >= disksize)
panic("iderw: sector out of range");
if(b->block >= disksize)
panic("iderw: block out of range");

p = memdisk + b->sector*512;
p = memdisk + b->block*BSIZE;

if(b->flags & B_DIRTY){
b->flags &= ~B_DIRTY;
memmove(p, b->data, 512);
memmove(p, b->data, BSIZE);
} else
memmove(b->data, p, 512);
memmove(b->data, p, BSIZE);
b->flags |= B_VALID;
}
Loading

0 comments on commit c24ac5d

Please sign in to comment.