Skip to content

Commit c24ac5d

Browse files
committed
Disentangle block size from the disk's sector size. Set block size to 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.
1 parent 7443b96 commit c24ac5d

File tree

11 files changed

+91
-86
lines changed

11 files changed

+91
-86
lines changed

bio.c

+11-9
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@
2424
#include "defs.h"
2525
#include "param.h"
2626
#include "spinlock.h"
27+
#include "fs.h"
2728
#include "buf.h"
2829

2930
struct {
@@ -55,20 +56,20 @@ binit(void)
5556
}
5657
}
5758

58-
// Look through buffer cache for sector on device dev.
59+
// Look through buffer cache for block on device dev.
5960
// If not found, allocate a buffer.
6061
// In either case, return B_BUSY buffer.
6162
static struct buf*
62-
bget(uint dev, uint sector)
63+
bget(uint dev, uint blockno)
6364
{
6465
struct buf *b;
6566

6667
acquire(&bcache.lock);
6768

6869
loop:
69-
// Is the sector already cached?
70+
// Is the block already cached?
7071
for(b = bcache.head.next; b != &bcache.head; b = b->next){
71-
if(b->dev == dev && b->sector == sector){
72+
if(b->dev == dev && b->blockno == blockno){
7273
if(!(b->flags & B_BUSY)){
7374
b->flags |= B_BUSY;
7475
release(&bcache.lock);
@@ -85,7 +86,7 @@ bget(uint dev, uint sector)
8586
for(b = bcache.head.prev; b != &bcache.head; b = b->prev){
8687
if((b->flags & B_BUSY) == 0 && (b->flags & B_DIRTY) == 0){
8788
b->dev = dev;
88-
b->sector = sector;
89+
b->blockno = blockno;
8990
b->flags = B_BUSY;
9091
release(&bcache.lock);
9192
return b;
@@ -94,15 +95,16 @@ bget(uint dev, uint sector)
9495
panic("bget: no buffers");
9596
}
9697

97-
// Return a B_BUSY buf with the contents of the indicated disk sector.
98+
// Return a B_BUSY buf with the contents of the indicated block.
9899
struct buf*
99-
bread(uint dev, uint sector)
100+
bread(uint dev, uint blockno)
100101
{
101102
struct buf *b;
102103

103-
b = bget(dev, sector);
104-
if(!(b->flags & B_VALID))
104+
b = bget(dev, blockno);
105+
if(!(b->flags & B_VALID)) {
105106
iderw(b);
107+
}
106108
return b;
107109
}
108110

bootmain.c

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
// Boot loader.
22
//
3-
// Part of the boot sector, along with bootasm.S, which calls bootmain().
3+
// Part of the boot block, along with bootasm.S, which calls bootmain().
44
// bootasm.S has put the processor into protected 32-bit mode.
55
// bootmain() loads an ELF kernel image from the disk starting at
66
// sector 1 and then jumps to the kernel entry routine.

buf.h

+2-2
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,11 @@
11
struct buf {
22
int flags;
33
uint dev;
4-
uint sector;
4+
uint blockno;
55
struct buf *prev; // LRU cache list
66
struct buf *next;
77
struct buf *qnext; // disk queue
8-
uchar data[512];
8+
uchar data[BSIZE];
99
};
1010
#define B_BUSY 0x1 // buffer is locked by some process
1111
#define B_VALID 0x2 // buffer has been read from disk

fs.c

+1-1
Original file line numberDiff line numberDiff line change
@@ -16,8 +16,8 @@
1616
#include "mmu.h"
1717
#include "proc.h"
1818
#include "spinlock.h"
19-
#include "buf.h"
2019
#include "fs.h"
20+
#include "buf.h"
2121
#include "file.h"
2222

2323
#define min(a, b) ((a) < (b) ? (a) : (b))

fs.h

+1-1
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@
99
// Then sb.nlog log blocks.
1010

1111
#define ROOTINO 1 // root i-number
12-
#define BSIZE 512 // block size
12+
#define BSIZE 1024 // block size
1313

1414
// File system super block
1515
struct superblock {

ide.c

+13-7
Original file line numberDiff line numberDiff line change
@@ -9,8 +9,10 @@
99
#include "x86.h"
1010
#include "traps.h"
1111
#include "spinlock.h"
12+
#include "fs.h"
1213
#include "buf.h"
1314

15+
#define SECTOR_SIZE 512
1416
#define IDE_BSY 0x80
1517
#define IDE_DRDY 0x40
1618
#define IDE_DF 0x20
@@ -71,17 +73,21 @@ idestart(struct buf *b)
7173
{
7274
if(b == 0)
7375
panic("idestart");
76+
int sector_per_block = BSIZE/SECTOR_SIZE;
77+
int sector = b->blockno * sector_per_block;
7478

79+
if (sector_per_block > 7) panic("idestart");
80+
7581
idewait(0);
7682
outb(0x3f6, 0); // generate interrupt
77-
outb(0x1f2, 1); // number of sectors
78-
outb(0x1f3, b->sector & 0xff);
79-
outb(0x1f4, (b->sector >> 8) & 0xff);
80-
outb(0x1f5, (b->sector >> 16) & 0xff);
81-
outb(0x1f6, 0xe0 | ((b->dev&1)<<4) | ((b->sector>>24)&0x0f));
83+
outb(0x1f2, sector_per_block); // number of sectors
84+
outb(0x1f3, sector & 0xff);
85+
outb(0x1f4, (sector >> 8) & 0xff);
86+
outb(0x1f5, (sector >> 16) & 0xff);
87+
outb(0x1f6, 0xe0 | ((b->dev&1)<<4) | ((sector>>24)&0x0f));
8288
if(b->flags & B_DIRTY){
8389
outb(0x1f7, IDE_CMD_WRITE);
84-
outsl(0x1f0, b->data, 512/4);
90+
outsl(0x1f0, b->data, BSIZE/4);
8591
} else {
8692
outb(0x1f7, IDE_CMD_READ);
8793
}
@@ -104,7 +110,7 @@ ideintr(void)
104110

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

109115
// Wake process waiting for this buf.
110116
b->flags |= B_VALID;

kill.c

+1-1
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ main(int argc, char **argv)
77
{
88
int i;
99

10-
if(argc < 1){
10+
if(argc < 2){
1111
printf(2, "usage: kill pid...\n");
1212
exit();
1313
}

log.c

+9-9
Original file line numberDiff line numberDiff line change
@@ -21,18 +21,18 @@
2121
//
2222
// The log is a physical re-do log containing disk blocks.
2323
// The on-disk log format:
24-
// header block, containing sector #s for block A, B, C, ...
24+
// header block, containing block #s for block A, B, C, ...
2525
// block A
2626
// block B
2727
// block C
2828
// ...
2929
// Log appends are synchronous.
3030

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

3838
struct log {
@@ -72,7 +72,7 @@ install_trans(void)
7272

7373
for (tail = 0; tail < log.lh.n; tail++) {
7474
struct buf *lbuf = bread(log.dev, log.start+tail+1); // read log block
75-
struct buf *dbuf = bread(log.dev, log.lh.sector[tail]); // read dst
75+
struct buf *dbuf = bread(log.dev, log.lh.block[tail]); // read dst
7676
memmove(dbuf->data, lbuf->data, BSIZE); // copy block to dst
7777
bwrite(dbuf); // write dst to disk
7878
brelse(lbuf);
@@ -89,7 +89,7 @@ read_head(void)
8989
int i;
9090
log.lh.n = lh->n;
9191
for (i = 0; i < log.lh.n; i++) {
92-
log.lh.sector[i] = lh->sector[i];
92+
log.lh.block[i] = lh->block[i];
9393
}
9494
brelse(buf);
9595
}
@@ -105,7 +105,7 @@ write_head(void)
105105
int i;
106106
hb->n = log.lh.n;
107107
for (i = 0; i < log.lh.n; i++) {
108-
hb->sector[i] = log.lh.sector[i];
108+
hb->block[i] = log.lh.block[i];
109109
}
110110
bwrite(buf);
111111
brelse(buf);
@@ -178,7 +178,7 @@ write_log(void)
178178

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

220220
acquire(&log.lock);
221221
for (i = 0; i < log.lh.n; i++) {
222-
if (log.lh.sector[i] == b->sector) // log absorbtion
222+
if (log.lh.block[i] == b->blockno) // log absorbtion
223223
break;
224224
}
225-
log.lh.sector[i] = b->sector;
225+
log.lh.block[i] = b->blockno;
226226
if (i == log.lh.n)
227227
log.lh.n++;
228228
b->flags |= B_DIRTY; // prevent eviction

memide.c

+6-6
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ void
2020
ideinit(void)
2121
{
2222
memdisk = _binary_fs_img_start;
23-
disksize = (uint)_binary_fs_img_size/512;
23+
disksize = (uint)_binary_fs_img_size/BSIZE;
2424
}
2525

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

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

5252
if(b->flags & B_DIRTY){
5353
b->flags &= ~B_DIRTY;
54-
memmove(p, b->data, 512);
54+
memmove(p, b->data, BSIZE);
5555
} else
56-
memmove(b->data, p, 512);
56+
memmove(b->data, p, BSIZE);
5757
b->flags |= B_VALID;
5858
}

0 commit comments

Comments
 (0)