Skip to content

Commit

Permalink
task 3 - mkfs.c supports partitions
Browse files Browse the repository at this point in the history
  • Loading branch information
asafch committed Jun 19, 2016
1 parent f1d0e70 commit 1752fdc
Show file tree
Hide file tree
Showing 4 changed files with 68 additions and 36 deletions.
3 changes: 3 additions & 0 deletions file.h
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
// #include "mbr.h"

struct file {
enum { FD_NONE, FD_PIPE, FD_INODE } type;
int ref; // reference count
Expand All @@ -22,6 +24,7 @@ struct inode {
short nlink;
uint size;
uint addrs[NDIRECT+1];
struct partition *partitions;
};
#define I_BUSY 0x1
#define I_VALID 0x2
Expand Down
34 changes: 26 additions & 8 deletions fs.c
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
// + Directories: inode with special contents (list of other inodes!)
// + Names: paths like /usr/rtm/xv6/fs.c for convenient naming.
//
// This file contains the low-level file system manipulation
// This file contains the low-level file system manipulation
// routines. The (higher-level) system call implementations
// are in sysfile.c.

Expand All @@ -25,6 +25,20 @@
static void itrunc(struct inode*);
struct superblock sb; // there should be one per dev, but we run with one dev
struct mbr mbr;
int boot_partition = 0;
struct superblock sbs[4] = {{0, 0, 0, 0, 0, 0, 0},
{0, 0, 0, 0, 0, 0, 0},
{0, 0, 0, 0, 0, 0, 0},
{0, 0, 0, 0, 0, 0, 0}};
struct partition partitions[4] = {{0, 0, 0, 0, 0},
{0, 0, 0, 0, 0},
{0, 0, 0, 0, 0},
{0, 0, 0, 0, 0}};

int checkForBootPrograms(int index) {
// TODO implement
return 0;
}

void
readmbr(int dev, struct mbr *mbr)
Expand All @@ -45,6 +59,7 @@ readmbr(int dev, struct mbr *mbr)
if((mbr->partitions[i].flags & PART_ALLOCATED) == 1){
if((mbr->partitions[i].flags & PART_BOOTABLE) == 1){
bootable = yes;
boot_partition = checkForBootPrograms(i); // mark first bootable partition as boot partition
}
else{
bootable = no;
Expand All @@ -56,16 +71,19 @@ readmbr(int dev, struct mbr *mbr)
type = fat;
}
cprintf("Partition %d: bootable: %s, type:%s, offset:%d, size:%d \n", i, bootable, type, mbr->partitions[i].offset, mbr->partitions[i].size);
memmove(&partitions[i] + sizeof(uint), &mbr->partitions[i], sizeof(struct dpartition));
partitions[i].dev = dev;
}
}

brelse(bp);
}
// Read the super block.
void
readsb(int dev, struct superblock *sb)
{
struct buf *bp;

bp = bread(dev, 1);
memmove(sb, bp->data, sizeof(*sb));
brelse(bp);
Expand All @@ -76,14 +94,14 @@ static void
bzero(int dev, int bno)
{
struct buf *bp;

bp = bread(dev, bno);
memset(bp->data, 0, BSIZE);
log_write(bp);
brelse(bp);
}

// Blocks.
// Blocks.

// Allocate a zeroed disk block.
static uint
Expand Down Expand Up @@ -199,10 +217,10 @@ void
iinit(int dev)
{
initlock(&icache.lock, "icache");
readmbr(dev,&mbr);
readsb(dev, &sb);
cprintf("sb: size %d nblocks %d ninodes %d nlog %d logstart %d inodestart %d bmap start %d\n", sb.size,
sb.nblocks, sb.ninodes, sb.nlog, sb.logstart, sb.inodestart, sb.bmapstart);
readmbr(dev,&mbr);
}

static struct inode* iget(uint dev, uint inum);
Expand Down Expand Up @@ -385,7 +403,7 @@ iunlockput(struct inode *ip)
//
// The content (data) associated with each inode is stored
// in blocks on the disk. The first NDIRECT block numbers
// are listed in ip->addrs[]. The next NINDIRECT blocks are
// are listed in ip->addrs[]. The next NINDIRECT blocks are
// listed in block ip->addrs[NDIRECT].

// Return the disk block address of the nth block in inode ip.
Expand Down Expand Up @@ -438,7 +456,7 @@ itrunc(struct inode *ip)
ip->addrs[i] = 0;
}
}

if(ip->addrs[NDIRECT]){
bp = bread(ip->dev, ip->addrs[NDIRECT]);
a = (uint*)bp->data;
Expand Down Expand Up @@ -591,7 +609,7 @@ dirlink(struct inode *dp, char *name, uint inum)
de.inum = inum;
if(writei(dp, (char*)&de, off, sizeof(de)) != sizeof(de))
panic("dirlink");

return 0;
}

Expand Down
9 changes: 4 additions & 5 deletions mbr.h
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@

struct dpartition {
uint flags;
uint type;
uint type;
uint offset;
uint size;
};
Expand All @@ -24,9 +24,8 @@ struct mbr {

struct partition {
uint dev;

uint flags;
uint type;
uint flags;
uint type;
uint offset;
uint size;
};
};
58 changes: 35 additions & 23 deletions mkfs.c
Original file line number Diff line number Diff line change
Expand Up @@ -23,16 +23,18 @@

int nbitmap = FSSIZE/(BSIZE*8) + 1;
int ninodeblocks = NINODES / IPB + 1;
int nlog = LOGSIZE;
int nlog = LOGSIZE;
int nmeta; // Number of meta blocks (boot, sb, nlog, inode, bitmap)
int nblocks; // Number of data blocks

int fsfd;
struct superblock sb;
struct superblock sbs[4];
struct mbr mbr;
char zeroes[BSIZE];
uint freeinode = 1;
uint freeblock;
uint master_freeblock;
int current_partition = 0;


void balloc(int);
Expand Down Expand Up @@ -74,7 +76,7 @@ main(int argc, char *argv[])
struct dirent de;
char buf[BSIZE];
struct dinode din;

struct dpartition partitions[4];

static_assert(sizeof(int) == 4, "Integers must be 4 bytes!");

Expand All @@ -96,29 +98,39 @@ main(int argc, char *argv[])
nmeta = 2 + nlog + ninodeblocks + nbitmap;
nblocks = FSSIZE - nmeta;

//make sure no junk is in mbr before setting it
//make sure no junk is in mbr before setting it
memset(&mbr.bootstrap[0],0,sizeof(uchar)*446);
memset(&mbr.partitions[0],0,sizeof(struct dpartition)*NPARTITIONS);
memset(&mbr.magic[0],0,sizeof(uchar)*2);

// allocate partition 0
mbr.partitions[0].flags |= PART_ALLOCATED;
mbr.partitions[0].type = FS_INODE;
mbr.partitions[0].offset = 0;
mbr.partitions[0].offset = 1;
mbr.partitions[0].size = FSSIZE;

memset(&partitions, 0, sizeof(struct dpartition) * 4);
partitions[0].offset = 1;
partitions[1].offset = 1 + FSSIZE;
partitions[2].offset = 1 + FSSIZE * 2;
partitions[3].offset = 1 + FSSIZE * 3;

// initialize super blocks
for (i = 0; i < NPARTITIONS; i++) {
sbs[i].size = xint(FSSIZE);
sbs[i].nblocks = xint(nblocks);
sbs[i].ninodes = xint(NINODES);
sbs[i].nlog = xint(nlog);
sbs[i].logstart = xint(1);
sbs[i].inodestart = xint(1 + nlog);
sbs[i].bmapstart = xint(1 + nlog + ninodeblocks);
}

sb.size = xint(FSSIZE);
sb.nblocks = xint(nblocks);
sb.ninodes = xint(NINODES);
sb.nlog = xint(nlog);
sb.logstart = xint(2);
sb.inodestart = xint(2+nlog);
sb.bmapstart = xint(2+nlog+ninodeblocks);

printf("nmeta %d (boot, super, log blocks %u inode blocks %u, bitmap blocks %u) blocks %d total %d\n",
printf("Each partition has the following composition: nmeta %d (boot, super, log blocks %u inode blocks %u, bitmap blocks %u) blocks %d total %d\n",
nmeta, nlog, ninodeblocks, nbitmap, nblocks, FSSIZE);

freeblock = nmeta; // the first free block that we can allocate
master_freeblock = freeblock; // this is to remember the first free block in every partition

for(i = 0; i < FSSIZE; i++)
wsect(i, zeroes);
Expand All @@ -130,8 +142,8 @@ main(int argc, char *argv[])


memset(buf, 0, sizeof(buf));
memmove(buf, &sb, sizeof(sb));
wsect(1, buf);
memmove(buf, &sbs[current_partition], sizeof(sbs[current_partition]));
wsect(1, buf); //TODO switch to relative, write 4 sbs

rootino = ialloc(T_DIR);
assert(rootino == ROOTINO);
Expand All @@ -153,7 +165,7 @@ main(int argc, char *argv[])
perror(argv[i]);
exit(1);
}

// Skip leading _ in name when writing to file system.
// The binaries are named _rm, _cat, etc. to keep the
// build operating system from trying to execute them
Expand Down Expand Up @@ -189,7 +201,7 @@ main(int argc, char *argv[])
void
wsect(uint sec, void *buf)
{
if(lseek(fsfd, sec * BSIZE, 0) != sec * BSIZE){
if(lseek(fsfd, sec * BSIZE + FSSIZE * current_partition, 0) != sec * BSIZE+ FSSIZE * current_partition){
perror("lseek");
exit(1);
}
Expand All @@ -206,7 +218,7 @@ winode(uint inum, struct dinode *ip)
uint bn;
struct dinode *dip;

bn = IBLOCK(inum, sb);
bn = IBLOCK(inum, sbs[current_partition]);
rsect(bn, buf);
dip = ((struct dinode*)buf) + (inum % IPB);
*dip = *ip;
Expand All @@ -220,7 +232,7 @@ rinode(uint inum, struct dinode *ip)
uint bn;
struct dinode *dip;

bn = IBLOCK(inum, sb);
bn = IBLOCK(inum, sbs[current_partition]);
rsect(bn, buf);
dip = ((struct dinode*)buf) + (inum % IPB);
*ip = *dip;
Expand All @@ -229,7 +241,7 @@ rinode(uint inum, struct dinode *ip)
void
rsect(uint sec, void *buf)
{
if(lseek(fsfd, sec * BSIZE, 0) != sec * BSIZE){
if(lseek(fsfd, sec * BSIZE + FSSIZE * current_partition, 0) != sec * BSIZE+ FSSIZE * current_partition){
perror("lseek");
exit(1);
}
Expand Down Expand Up @@ -265,8 +277,8 @@ balloc(int used)
for(i = 0; i < used; i++){
buf[i/8] = buf[i/8] | (0x1 << (i%8));
}
printf("balloc: write bitmap block at sector %d\n", sb.bmapstart);
wsect(sb.bmapstart, buf);
printf("balloc: write bitmap block at sector %d\n", sbs[current_partition].bmapstart);
wsect(sbs[current_partition].bmapstart, buf);
}

#define min(a, b) ((a) < (b) ? (a) : (b))
Expand Down

0 comments on commit 1752fdc

Please sign in to comment.