pa2/ext.c #include “ext.h”
#include
static FILE *fh;
int sfs_read_block(void *buf, blkid bid)
{
fseek(fh, bid * BLOCK_SIZE, SEEK_SET);
fread(buf, 1, BLOCK_SIZE, fh);
return 0;
}
int sfs_write_block(void *buf, blkid bid)
{
fseek(fh, bid * BLOCK_SIZE, SEEK_SET);
fwrite(buf, 1, BLOCK_SIZE, fh);
return 0;
}
void sfs_init_storage()
{
fh = fopen(“storage”, “w+b”);
}
void sfs_close_storage()
{
fclose(fh);
}
pa2/ext.h #ifndef EXT_H_
#define EXT_H_
#include “fs.h”
void sfs_init_storage();
void sfs_close_storage();
int sfs_read_block(void *buf, blkid bid);
int sfs_write_block(void *buf, blkid bid);
#endif
pa2/exta1.c #include “ext.h”
#undef BLOCK_SIZE
#include “a1/mydisk.h”
void report_latency(int latency) {}
int sfs_read_block(void *buf, blkid bid)
{
mydisk_read_block(bid, buf);
return 0;
}
int sfs_write_block(void *buf, blkid bid)
{
mydisk_write_block(bid, buf);
return 0;
}
void sfs_init_storage()
{
mydisk_init(“storage”, 1024, 0);
}
void sfs_close_storage()
{
mydisk_close();
}
pa2/fs.c #include “fs.h”
#include “ext.h”
#include
#include
#include
/* constant of how many bits in one freemap entry */
#define SFS_NBITS_IN_FREEMAP_ENTRY (sizeof(u32)*8)
/* in-memory superblock (in consistent with the disk copy) */
static sfs_superblock_t sb;
/* freemap, u32 array (in consistent with the disk copy) */
static u32 *freemap;
/* file descriptor table */
static fd_struct_t fdtable[SFS_MAX_OPENED_FILES];
/*
* Flush the in-memory freemap to disk
*/
static void sfs_flush_freemap()
{
size_t i;
blkid bid = 1;
char *p = (char *)freemap;
/* TODO: write freemap block one by one */
}
/*
* Allocate a free block, mark it in the freemap and flush the freemap to disk
*/
static blkid sfs_alloc_block()
{
u32 size = sb.nfreemap_blocks * BLOCK_SIZE / sizeof(u32);
u32 i, j;
/* TODO: find a freemap entry that…





