layer1 datablock allocate

This commit is contained in:
FactorialN 2023-11-05 17:02:59 -08:00
parent 5cbee55bd4
commit b961114ae1

View File

@ -32,6 +32,7 @@ class INode{
off_t gid;
off_t permissions;
off_t size;
off_t block_number;
public:
void read_get_byte(off_t &t, int &current_pos, char *buffer){
@ -44,6 +45,7 @@ public:
void inode_construct(off_t blockNumber){
char buffer[512] = {0};
rawdisk_read(blockNumber, buffer);
block_number = blockNumber;
int current_pos = 0;
// initialize blocks
for (int i = 0; i < 48; i++){
@ -66,7 +68,7 @@ public:
current_pos += 8;
}
void inode_save(off_t blockNumber){
void inode_save(){
char buffer[512] = {0};
int current_pos = 0;
for (int i = 0; i < 48; i++){
@ -79,21 +81,89 @@ public:
write_get_byte(gid, current_pos, buffer);
write_get_byte(permissions, current_pos, buffer);
write_get_byte(size, current_pos, buffer);
rawdisk_write(bloackNumber, buffer);
rawdisk_write(block_number, buffer);
}
off_t datablock_allocate_in_list(){
//find a free data block
off_t freeListHead = SuperBlock::getFreeListHead();
/*
1. initialization
2. data block starting position
3. r/w between storage and rawdisk to maintain
*/
char buffer[512] = {0};
off_t freeBlockNum = 0;
rawdisk_read(freeListHead, buffer);
for (int i = 8; i < 264; i++){
if(buffer[i] != 255){
for (int j = 0; j < 8; j++){
if ((buffer[i]&(1<<j)) == 0){
buffer[i] |= (1<<j);
break;
}
}
freeBlockNum = freeListHead + (i-8)*8 + j + 1;
}
}
bool notFull = false;
for (int i = 8; i < 264; i++){
if((i < 263 && buffer[i] != 255) || (i == 263 && buffer[i] != 127)){
notFull = true;
break;
}
}
if (!notFull){
off_t next_header = 0;
for (int j = 0; j < 8; j++)
next_header = next_header | (((off_t)buffer[j])<<(8*j));
writeFreeListHead(next_header);
}
return freeBlockNum;
}
// allowcate 1 datablock and add to the end of the file
off_t datablock_allocate(){
//do we need to check dynamic?
//find a free data block
off_t freeListHead = SuperBlock::getFreeListHead();
for (freeListHead)
//add the data block to blocks, single, double, triple
off_t freeBlockNum = datablock_allocate_in_list();
bool inBlocks = false;
for (int i = 0; i < 48; i++)
if(block[i] == 0){
inBlocks = true;
block[i] = freeBlockNum;
break;
}
if(!inBlocks){
if (single_indirect == 0){
single_indirect = datablock_allocate_in_list();
}
int inSingle = false;
char buffer[512] = {0};
rawdisk_read(single_indirect, buffer);
for (int i = 0; i < 512; i++)
if(buffer[i] == 0){
inSingle = true;
buffer[i] = freeBlockNum;
break;
}
if(i < 512){
rawdisk_write(single_indirect, buffer);
}
else{
if (double_indirect == 0){
double_indirect = datablock_allocate_in_list();
}
int inDouble = false;
rawdisk_read(double_indirect, buffer);
// w.t.f is this
}
}
//return the block number
inode_save();
return freeBlockNum;
}
// deallocate 1 datablock from the end of the file
@ -106,6 +176,21 @@ class INodeOperation{
// free list head is at super block (0): first 8 bytes
public:
//initialization of the rawdisk
void initialize(){
SuperBlock::writeFreeListHead(524288); // maximum inode number
for (off_t i = 524288; i <7864320; i += 2048){
char buffer[512] = {0};
off_t t = i + 2048;
if (t < 7864320){
for (int j = 0; j < 8; j++){
buffer[j] = t & (((off_t)1<<(8*j))-1);
t >>= 8;
}
}
rawdisk_write(i, buffer);
}
}
// allocate an inode and return the number of the inode
// the i-th inode is in the i-th block