Skip to content

Commit

Permalink
finish write
Browse files Browse the repository at this point in the history
  • Loading branch information
zhouliang-yu committed Nov 20, 2021
1 parent f303745 commit b7aca8e
Showing 1 changed file with 186 additions and 50 deletions.
236 changes: 186 additions & 50 deletions Assignment_4_120040077/file_system.cu
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@

__device__ __managed__ u32 gtime = 0;
__device__ __managed__ u32 gtime_create = 0;
__device__ __managed__ u32 block_position = 0;
__device__ __managed__ u32 file_start_location = 0;
__device__ __managed__ u32 FCB_position = 4096;
__device__ __managed__ u32 current_FCB_position = 4096;

Expand Down Expand Up @@ -35,8 +35,8 @@ __device__ void fs_init(FileSystem *fs, uchar *volume, int SUPERBLOCK_SIZE,

/*
* my FCB structure
* |0|1|2|3|4|5|6|7|8|9|10|11|12|13|14|15|16|17|18|19| 20|21 | 22|23 | 24 | 25|26 | 27|28 |29 | 30| 31|
* | file name | location | size |create_t|modify_t|
* |0|1|2|3|4|5|6|7|8|9|10|11|12|13|14|15|16|17|18|19| 20|21 | 22|23 | 24 | 25|26 | 27 |28 |29 | 30| 31|
* | file name | location | size |mode|create_t|modify_t|
*/
struct My_FCB
{
Expand All @@ -51,32 +51,35 @@ struct My_FCB
__device__ u32 search_FCB(FileSystem *fs, char *s)
{
int flag_find = 1;//1 indicates that we find the s, 0 means we can't find
for (int i = fs->SUPERBLOCK_SIZE; i < fs->FILE_BASE_ADDRESS - 1; i += 32) {
if (fs->volume[i] == 0 ) // nothing has been stored
{ // cannot find
flag_find = 0
break;
}
else
{
int j = 0
while (s[j] != '\0')
{
if (fs->volume[i * fs->FCB_SIZE + fs->SUPERBLOCK_SIZE + j] != file_name[j++])
{
for (int i = fs->SUPERBLOCK_SIZE; i < fs->FILE_BASE_ADDRESS - 1; i += fs->STORAGE_BLOCK_SIZE)
{
if (fs->volume[i + 24] == 0 && fs->volume[i + 25] == 0 && fs->volume[i + 26] == 0) // nothing has been stored
{ // cannot find
flag_find = 0;
break;
}
else
{

for(int j = 0; j < 20; j++)
{
if (fs->volume[i + j] != s[j])
{

flag_find = false;
break;
}
}
}
flag_find = 0;
break;
}
}
}

if (flag_find == 1) {
return i;
}else{
continue;
}

if (flag_find == 1)
{
return i;
}
else
{
continue;
}
}

return -1;
Expand All @@ -98,19 +101,24 @@ __device__ void file_info_store(FileSystem *fs, char *s){
fs->volume[FCB_position + 30] = gtime >> 8;
fs->volume[FCB_position + 31] = gtime & 0x000000FF;

//store the start block
fs->volume[FCB_position + 20] = block_position >> 24;
fs->volume[FCB_position + 21] = block_position >> 16;
fs->volume[FCB_position + 22] = block_position >> 8;
fs->volume[FCB_position + 23] = block_position;
//store the start location of block
fs->volume[FCB_position + 20] = file_start_location >> 24;
fs->volume[FCB_position + 21] = file_start_location >> 16;
fs->volume[FCB_position + 22] = file_start_location >> 8;
fs->volume[FCB_position + 23] = file_start_location;

/** set the size to be 0*/
fs->volume[FCB_position + 24] = 0;
fs->volume[FCB_position + 25] = 0;
fs->volume[FCB_position + 26] = 0;

//update the time
gtime++;
gtime_create++;

//update FCB position
FCB_position = FCB_position + 32;
return block_position;
return file_start_location;
}


Expand All @@ -124,9 +132,10 @@ __device__ void file_info_store(FileSystem *fs, char *s){
print("cannot find file in the read mode");
}else{ //we find match s
current_FCB_position = file_exist;
u32 start_location = (fs->volume[current_FCB_position + 20] << 24) + (fs->volume[current_FCB_position + 21] << 16) + (fs->volume[current_FCB_position + 22] << 8) + (fs->volume[current_FCB_position + 23]);
// fs->volume[current_FCB_position + 26] = uchar('r');
return start_location;
u32 file_start_location = (fs->volume[current_FCB_position + 20] << 24) + (fs->volume[current_FCB_position + 21] << 16) + (fs->volume[current_FCB_position + 22] << 8) + (fs->volume[current_FCB_position + 23]);
//set the mode to be read
fs->volume[current_FCB_position + 26] = G_READ;
return file_start_location;
}
}

Expand All @@ -136,22 +145,24 @@ __device__ void file_info_store(FileSystem *fs, char *s){
file_info_store(fs, s);
}else{
current_FCB_position = file_exist;
u32 start_location = (fs->volume[current_FCB_position + 20] << 24) + (fs->volume[current_FCB_position + 21] << 16) + (fs->volume[current_FCB_position + 22] << 8) + (fs->volume[current_FCB_position + 23]);
file_start_location = (fs->volume[current_FCB_position + 20] << 24) + (fs->volume[current_FCB_position + 21] << 16) + (fs->volume[current_FCB_position + 22] << 8) + (fs->volume[current_FCB_position + 23]);



//get the size
u32 size = ((int)fs->volume[current_FCB_position + 24]) +
((int)fs->volume[current_FCB_position + 25]) * (1 << 8) +
((int)fs->volume[current_FCB_position + 26]) * (1 << 16);
//clear the old file content in storage
u32 size = (fs->volume[current_FCB_position+24] << 24) + (fs->volume[current_FCB_position + 25] << 16) + (fs->volume[current_FCB_position + 26] << 8) + (fs->volume[current_FCB_position + 27]);
for (int i = 0; i < size; i++)
{
fs->volume[start_location * 32 + i + fs->FILE_BASE_ADDRESS] = 0;
fs->volume[file_start_location * fs->STORAGE_BLOCK_SIZE + i + fs->FILE_BASE_ADDRESS] = 0;
}

//clear the old file in the superblock
//clear the old file in the superblock because each bit in superblock represent a block in storage
for (int i = 0; i < (size - 1) / 32 + 1; i++)
{
u32 super_block_position = start_location + i;
int shift_number = super_block_position % 8;
fs->volume[super_block_position / 8] = fs->volume[super_block_position / 8] - (1 << shift_number);
fs->volume[(file_start_location + i) / 8] = fs->volume[(file_start_location + i) / 8] - (1 << (super_location % 8));
}

//update the modified time
Expand All @@ -161,17 +172,14 @@ __device__ void file_info_store(FileSystem *fs, char *s){
//update gtime
gtime++;

// fs->volume[current_FCB_position + 26] = uchar('w');
return start_location; //a pointer at super block
//set the mode to write
fs->volume[current_FCB_position + 26] = G_WRITE;
return file_start_location; //a pointer at super block
}
}







__device__ void fs_read(FileSystem *fs, uchar *output, u32 size, u32 fp)
{
/* Implement read operation here */
Expand All @@ -181,16 +189,144 @@ __device__ void fs_read(FileSystem *fs, uchar *output, u32 size, u32 fp)

for (int i = 0; i < size; i++)
{
output[i] = fs->volume[fp * 32 + i + fs->FILE_BASE_ADDRESS];
output[i] = fs->volume[fp * fs->STORAGE_BLOCK_SIZE + i + fs->FILE_BASE_ADDRESS];
}
}

__device__ void manage_segmentation(FileSystem * fs, u32 fp, u32 original_size, u32 size)
{
u32 block_position = fp * 32 + fs->FILE_BASE_ADDRESS;
u32 new_size = ((original_size - size - 1) / 32 + 1) * 32;
while ((fs->volume[block_position + new_size] != 0 || (block_position + new_size) % 32 != 0) && block_position + (original_size - size) < fs->STORAGE_SIZE){
fs->volume[block_position] = fs-> volume[block_position + new_size];
fs->volume[block_position + new_size] = 0;
block_position++;
}

/** manage the superblock*/
for (int i =0 ; i < file_start_location /8 + 1; i++) {
fs->volume[i] = 0;
}

file_start_location = file_start_location - ((original_size - size) -1) / 32 - 1;
u32 file_start_location_q = file_start_location / 8;
u32 file_start_location_r = block_position % 8;

for (int i = 0; i < file_start_location_q && i < fs->SUPERBLOCK_SIZE; i ++) {
fs->volume[i] = 511;
}
for (int i = 0; i < file_start_location_r; i++)
{
fs->volume[file_start_location_q] = fs->volume[file_start_location_q] + (1 << i);
}

//change FCB
u32 FCB_block_position;
for (int i = 4096; i < 36863; i = i + 32)
{
if (fs->volume[i + 24] == 0 && fs->volume[i + 25] == 0 && fs->volume[i + 26] == 0){
break;
}
FCB_block_position = (fs->volume[i + 20] << 24) + (fs->volume[i + 21] << 16) + (fs->volume[i + 22] << 8) + (fs->volume[i + 23]);
if (FCB_block_position > fp)
{
FCB_block_position = FCB_block_position - ((original_size - size) - 1) / 32 - 1;
fs->volume[i + 20] = FCB_block_position >> 24;
fs->volume[i + 21] = FCB_block_position >> 16;
fs->volume[i + 22] = FCB_block_position >> 8;
fs->volume[i + 23] = FCB_block_position;
}
}
}

__device__ u32 fs_write(FileSystem *fs, uchar* input, u32 size, u32 fp)
{
/* Implement write operation here */

//check if the file is in the write mood
if (((fp & 0xf0000000) >> 30) != G_WRITE)
{
printf("no writing allowed \n");
}

if(size > fs->MAX_FILE_NUM) {
print("incorrect error\n")
return -1;
}

fp &= 0x0fffffff;
if (fp == -1){
printf(" error\n");
}

int enough_space = (fs->volume[(fp + (size - 1) / 32)/8] >> (fp + (size - 1) / 32) % 8) % 2;

/**get the original file size*/
// check if there is enough space for it
u32 original_size = ((int)fs->volume[current_FCB_position + 24]) +
((int)fs->volume[current_FCB_position + 25]) * (1 << 8) +
((int)fs->volume[current_FCB_position + 26]) * (1 << 16);

file_start_location = (fs->volume[current_FCB_position + 20] << 24) + (fs->volume[current_FCB_position + 21] << 16) + (fs->volume[current_FCB_position + 22] << 8) + (fs->volume[current_FCB_position + 23]);

// clear all the contents in storage
for (int i = 0; i < original_size; i ++) {
fs->volume[fp * fs->FCB_SIZE + i + fs->FILE_BASE_ADDRESS] = 0;
}

//enough space to write
if(enough_space == 0) {
//write in the storage
for (int i = 0; i < size; i++)
{
fs->volume[fp * fs->STORAGE_BLOCK_SIZE + i + fs->FILE_BASE_ADDRESS] = input[i];
}
//update the superblock
for (int i = 0; i < size; i++)
{
if (i % 32 == 0) {
fs->volume[(fp + i / 32) / 8] = fs->volume[(fp + i / 32) / 8] + (1 << (fp + i / 32));
}
}

if (int(original_size - size) < 0)
file_start_location = file_start_location + (-(original_size - size) - 1) / 32 + 1;

//update the size in FCB
fs->volume[current_FCB_position + 24] = size >> 16;
fs->volume[current_FCB_position + 25] = size >> 8;
fs->volume[current_FCB_position + 26] = size;

if (int(original_size - size) > 0 && original_size != 0 && fp != file_start_location - 1)
{
manage_segmentation(fs, fp, original_size, size);
}

}
else{ //DONT have enough space
if (file_start_location * 32 - 1 + size < fs->SUPERBLOCK_SIZE){
for (int i = 0; i < size; i ++) {
fs->volume[file_start_location * 32 + i + fs->FILE_BASE_ADDRESS] = input[i];
//update the superblock
if(i % 32 == 0){
fs->volume[(file_start_location + i / 32) / 8] = fs->volume[(file_start_location + i / 32) / 8] + (1 << (file_start_location % 8));
}

//update the FCB
fs->volume[current_FCB_position + 24] = size >> 16;
fs->volume[current_FCB_position + 25] = size >> 8;
fs->volume[current_FCB_position + 26] = size;

//update block position
fs->volume[i + 20] = file_start_location >> 24;
fs->volume[i + 21] = file_start_location >> 16;
fs->volume[i + 22] = file_start_location >> 8;
fs->volume[i + 23] = file_start_location;
}
manage_segmentation(fs, fp, original_size, size);
}
}



}
Expand Down

0 comments on commit b7aca8e

Please sign in to comment.