Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 1 addition & 2 deletions lab6/src/cmd/cd_ls_pwd.c
Original file line number Diff line number Diff line change
Expand Up @@ -36,12 +36,11 @@ int my_chdir(char *pathname)
printf("ERROR: Chdir() - ino can't be found\n");
return 0;
}

MINODE *mip = iget(dev, ino);

if (!S_ISDIR(mip->INODE.i_mode))
{
printf("Error: Chdir() - mip is not a directory");
printf("Error: Chdir() - mip is not a directory\n");
return 0;
}

Expand Down
6 changes: 6 additions & 0 deletions lab6/src/cmd/mount_umount.c
Original file line number Diff line number Diff line change
Expand Up @@ -104,6 +104,12 @@ int my_mount(char *filesys, char *mount_dest) {
mtptr->ninodes = sp->s_inodes_count;
mtptr->nblocks = sp->s_blocks_count;

get_block(fd, 2, buf);
gp = (GD *)buf;
mtptr->bmap = gp->bg_block_bitmap;
mtptr->imap = gp->bg_inode_bitmap;
mtptr->iblock = gp->bg_inode_table;

// mark mount_point's minode as being mounted on and let it point at the MOUNT table entry, which points back to the
// m_p minode
mip->mounted = 1;
Expand Down
16 changes: 10 additions & 6 deletions lab6/src/main.c
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,7 @@ int init()
for (j = 0; j < NFD; j++)
p->fd[j] = 0;
}
for (i = 0; i < NMOUNT; i++)
for (i = 1; i < NMOUNT; i++)
{
mtptr = &mount_table[i];
mtptr->dev = 0;
Expand All @@ -75,6 +75,7 @@ int mount_root()
{
printf("mount_root()\n");
root = iget(dev, 2);
mount_table[0].mntDirPtr = root;
}

char *disk = "diskimage";
Expand All @@ -94,6 +95,7 @@ int main(int argc, char *argv[])
int ino;
char buf[BLKSIZE];
char line[128], cmd[32], pathname[128], pathname_two[128];
MTABLE *mtptr;

if (argc > 1)
disk = argv[1];
Expand All @@ -117,15 +119,17 @@ int main(int argc, char *argv[])
exit(1);
}
printf("EXT2 FS OK\n");
ninodes = sp->s_inodes_count;
nblocks = sp->s_blocks_count;
mtptr = &mount_table[0];
mtptr->dev = root_dev;
mtptr->ninodes = sp->s_inodes_count;
mtptr->nblocks = sp->s_blocks_count;

get_block(dev, 2, buf);
gp = (GD *)buf;

bmap = gp->bg_block_bitmap;
imap = gp->bg_inode_bitmap;
inode_start = gp->bg_inode_table;
mtptr->bmap = gp->bg_block_bitmap;
mtptr->imap = gp->bg_inode_bitmap;
mtptr->iblock = gp->bg_inode_table;
printf("bmp=%d imap=%d inode_start = %d\n", bmap, imap, inode_start);

init();
Expand Down
56 changes: 38 additions & 18 deletions lab6/src/util.c
Original file line number Diff line number Diff line change
Expand Up @@ -74,6 +74,16 @@ int clr_bit(char *buf, int bitnum)
return 0;
}

MTABLE* getmptr(int dev){
int i;
for (int i = 0; i < NMOUNT; i++){
if (dev == mount_table[i].dev){
return &mount_table[i];
}
}
return 0;
}

/****************************************************************
* Function: *
* Date Created: *
Expand Down Expand Up @@ -187,15 +197,15 @@ int ialloc(int dev) // allocate an inode number from inode_bitmap
{
int i;
char buf[BLKSIZE];
MTABLE* mptr = getmptr(dev);
get_block(dev, mptr->imap, buf); // read inode_bitmap block

get_block(dev, imap, buf); // read inode_bitmap block

for (i = 0; i < ninodes; i++)
for (i = 0; i < mptr->ninodes; i++)
{
if (tst_bit(buf, i) == 0)
{
set_bit(buf, i);
put_block(dev, imap, buf);
put_block(dev, mptr->imap, buf);
decFreeInodes(dev);
printf("allocated ino = %d\n", i + 1); // bits count from 0; ino from 1
return i + 1;
Expand All @@ -218,16 +228,16 @@ int balloc(int dev)
{ //returns a FREE disk block number NOTE: Not 100% sure if this works
int i;
char buf[BLKSIZE];
MTABLE* mptr = getmptr(dev);
get_block(dev, mptr->bmap, buf);

get_block(dev, bmap, buf);

for (i = 0; i < nblocks; i++)
for (i = 0; i < mptr->nblocks; i++)
{
if (tst_bit(buf, i) == 0)
{
set_bit(buf, i);
decFreeBlocks(dev);
put_block(dev, bmap, buf);
put_block(dev, mptr->bmap, buf);
printf("Free disk block at %d\n", i + 1); // bits count from 0; ino from 1
return i + 1;
}
Expand All @@ -249,16 +259,16 @@ int idealloc(int dev, int ino)
{ // deallocating inode (number)
int i;
char buf[BLKSIZE];

if (ino > ninodes)
MTABLE* mptr = getmptr(dev);
if (ino > mptr->ninodes)
{
printf("inumber %d out of range\n", ino);
return;
}
get_block(dev, imap, buf);
get_block(dev, mptr->imap, buf);
clr_bit(buf, ino - 1);
// write buf back
put_block(dev, imap, buf);
put_block(dev, mptr->imap, buf);
// update free inode count in SUPER and GD
incFreeInodes(dev);
}
Expand All @@ -276,10 +286,10 @@ int idealloc(int dev, int ino)
int bdealloc(int dev, int bno)
{
char buf[BLKSIZE]; // a sweet buffer

get_block(dev, bmap, buf); // get the block
MTABLE* mptr = getmptr(dev);
get_block(dev, mptr->bmap, buf); // get the block
clr_bit(buf, bno - 1); // clear the bits to 0
put_block(dev, bmap, buf); // write the block back
put_block(dev, mptr->bmap, buf); // write the block back
incFreeBlocks(dev); // increment the free block count
return 0;
}
Expand Down Expand Up @@ -404,7 +414,7 @@ MINODE *iget(int dev, int ino)
INODE *ip;
int i, block, offset;
char buf[BLKSIZE]; // a siiiiick buffer

MTABLE* mptr = getmptr(dev);
// serach in-memory minodes first
for (i = 0; i < NMINODE; i++)
{
Expand All @@ -420,7 +430,7 @@ MINODE *iget(int dev, int ino)
mip = mialloc(); // allocate a FREE minode
mip->dev = dev; // set the device to current
mip->ino = ino; // assign to (dev, ino)
block = (ino - 1) / 8 + inode_start; // find the block to read from
block = (ino - 1) / 8 + mptr->iblock; // find the block to read from
offset = (ino - 1) % 8;
get_block(dev, block, buf); // read the block
ip = (INODE *)buf + offset;
Expand Down Expand Up @@ -449,6 +459,8 @@ int iput(MINODE *mip)
INODE *ip;
int i, block, offset;
char buf[BLKSIZE];
MTABLE* mptr = getmptr(dev);

if (mip == 0)
return -1;
mip->refCount--;
Expand All @@ -460,7 +472,7 @@ int iput(MINODE *mip)
// still has user
// no need to write back
// write INODE back to disk
block = (mip->ino - 1) / 8 + inode_start;
block = (mip->ino - 1) / 8 + mptr->iblock;
offset = (mip->ino - 1) % 8;
// get block containing this inode
get_block(mip->dev, block, buf);
Expand Down Expand Up @@ -594,6 +606,7 @@ int getino(char *pathname)
// downward traversal
if (mip->mounted)
{
printf("Downward Traversal\n");
MTABLE *mtptr = mip->mptr;
dev = mtptr->dev;
ino = 2;
Expand Down Expand Up @@ -681,6 +694,13 @@ int findino(MINODE *mip, u32 *myino) // myino = ino of . return ino of ..
char buf[BLKSIZE], *temp_ptr;
DIR *dp;

if(mip->ino == 2 && mip->dev != root->dev){
MTABLE* mptr = getmptr(mip->dev);
iput(mip);
mip = mptr->mntDirPtr;
dev = mip->dev;
}

get_block(mip->dev, mip->INODE.i_block[0], buf);
temp_ptr = buf;
dp = (DIR *)buf;
Expand Down
1 change: 1 addition & 0 deletions lab6/src/util.h
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ int mount_root();
int quit();

// Function prototypes, util.c
MTABLE* getmptr(int dev);
int get_block(int dev, int blk, char *buf);
int put_block(int dev, int blk, char *buf);
int tokenize(char *pathname);
Expand Down