Skip to content

Commit b9155a1

Browse files
committed
feat: add 'allocated' flag to inode data block
1 parent 90e96e5 commit b9155a1

3 files changed

Lines changed: 28 additions & 10 deletions

File tree

src/inode.c

Lines changed: 19 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -56,15 +56,20 @@ ex_status ex_inode_allocate_blocks(struct ex_inode *inode) {
5656

5757
for (size_t i = 0; i < EX_DIRECT_BLOCKS; i++) {
5858

59-
inode->blocks[i] = ex_super_allocate_block();
59+
size_t address = ex_super_allocate_block();
6060

6161
// we are unable to allocate next block, we should deallocate all
6262
// already allocated blocks
63-
if (inode->blocks[i] == EX_BLOCK_INVALID_ADDRESS) {
63+
if (address == EX_BLOCK_INVALID_ADDRESS) {
6464
warning("failing to allocate nth (%lu) block", i);
6565
ex_inode_deallocate_blocks(inode);
6666
return INODE_BLOCK_ALLOCATION_FAILED;
6767
}
68+
69+
struct ex_data_block *block = &inode->blocks[i];
70+
71+
block->address = address;
72+
block->allocated = 1;
6873
}
6974

7075
return OK;
@@ -74,11 +79,11 @@ void ex_inode_deallocate_blocks(struct ex_inode *inode) {
7479

7580
for (size_t i = 0; i < EX_DIRECT_BLOCKS; i++) {
7681

77-
if (inode->blocks[i] == EX_BLOCK_INVALID_ADDRESS) {
82+
if (inode->blocks[i].address == EX_BLOCK_INVALID_ADDRESS) {
7883
break;
7984
}
8085

81-
ex_super_deallocate_block(inode->blocks[i]);
86+
ex_super_deallocate_block(inode->blocks[i].address);
8287
}
8388

8489
ex_super_deallocate_inode_block(inode->number);
@@ -564,7 +569,12 @@ ssize_t ex_inode_write(struct ex_inode *ino, size_t off, const char *data,
564569
ino->size += (off + amount) - ino->size;
565570
}
566571

567-
block_address addr = ino->blocks[start_block_idx];
572+
block_address addr = ino->blocks[start_block_idx].address;
573+
char allocated = ino->blocks[start_block_idx].allocated;
574+
575+
if (!allocated) {
576+
// XXX: do allocation
577+
}
568578

569579
ex_device_write(ino->address, (void *)ino, sizeof(struct ex_inode));
570580
ex_device_write(addr + start_block_off, data, amount);
@@ -582,9 +592,9 @@ ssize_t ex_inode_read(struct ex_inode *ino, size_t off, char *buffer,
582592
return 0;
583593
}
584594

585-
size_t offset = ino->blocks[start_block_idx] + start_block_off;
595+
size_t offset = ino->blocks[start_block_idx].address + start_block_off;
586596
ssize_t readed = 0;
587-
597+
// XXX: check if block is allocated
588598
// XXX: ignore status for now
589599
(void)ex_device_read_to_buffer(&readed, buffer, offset, amount);
590600

@@ -650,7 +660,7 @@ struct ex_inode_block ex_inode_block_iterate(struct ex_inode *inode,
650660
goto done;
651661
}
652662

653-
block.address = inode->blocks[it->block_number];
663+
block.address = inode->blocks[it->block_number].address;
654664
block.data = it->buffer;
655665

656666
// XXX: add buffer into ex_block_iterator and use ex_device_read_to_buffer
@@ -713,3 +723,4 @@ int ex_inode_has_perm(struct ex_inode *ino, ex_permission perm, gid_t gid, uid_t
713723

714724
return 0;
715725
}
726+

src/inode.h

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -13,12 +13,18 @@
1313
#include <time.h>
1414
#include <unistd.h>
1515

16-
#define EX_DIRECT_BLOCKS 256
16+
#define EX_DIRECT_BLOCKS 64
17+
#define EX_INDIRECT_BLOCKS 8
1718

1819
extern const uint16_t EX_INODE_MAGIC1;
1920
extern const uint8_t EX_DIR_MAGIC1;
2021
extern const uint8_t EX_ENTRY_MAGIC1;
2122

23+
struct ex_data_block {
24+
uint64_t address: 63;
25+
uint64_t allocated: 1;
26+
};
27+
2228
struct ex_inode {
2329
// number of inode, it corresponds with allocated block number
2430
// e.g. number==0 means that inode has allocated first inode
@@ -50,7 +56,7 @@ struct ex_inode {
5056

5157
// if an inode is file content is saved here directly in these blocks
5258
// if an inode is directory ex_dir_entries are saved in these blocks
53-
block_address blocks[EX_DIRECT_BLOCKS];
59+
struct ex_data_block blocks[EX_DIRECT_BLOCKS + EX_INDIRECT_BLOCKS];
5460
};
5561

5662
struct ex_dir_entry {

src/super.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@
2323
typedef size_t inode_address;
2424
typedef size_t block_address;
2525

26+
// XXX: rename this, it's used only by block iterator
2627
struct ex_inode_block {
2728
// position in super block inodes block bitmap
2829
size_t id;

0 commit comments

Comments
 (0)