Skip to content

Commit 66d9126

Browse files
committed
fixup! fixup! feat: add 'allocated' flag to inode data block
1 parent 1c9df3d commit 66d9126

6 files changed

Lines changed: 42 additions & 0 deletions

File tree

src/ex.c

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -124,6 +124,8 @@ int ex_getattr(const char *pathname, struct stat *st) {
124124

125125
st->st_nlink = inode->nlinks;
126126
st->st_size = inode->size;
127+
st->st_blocks = inode->nblocks;
128+
st->st_blksize = EX_BLOCK_SIZE;
127129
st->st_mode = inode->mode;
128130
st->st_ino = inode->number;
129131

src/inode.c

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -68,6 +68,7 @@ ex_status ex_inode_allocate_blocks(struct ex_inode *inode) {
6868

6969
struct ex_data_block *block = &inode->blocks[i];
7070

71+
inode->nblocks += 1;
7172
block->address = address;
7273
block->allocated = 1;
7374
}
@@ -99,6 +100,7 @@ void ex_inode_print(const struct ex_inode *inode) {
99100
info("address: %lu", inode->address);
100101
info("links: %u", inode->nlinks);
101102
info("size: %lu", inode->size);
103+
info("nblocks: %lu", inode->nblocks);
102104

103105
info("uid: %u", inode->uid);
104106
info("gid: %u", inode->gid);
@@ -140,6 +142,8 @@ struct ex_inode *ex_copy_inode(const struct ex_inode *inode) {
140142
copy->gid = inode->gid;
141143
copy->uid = inode->uid;
142144

145+
copy->nblocks = inode->nblocks;
146+
143147
for (size_t i = 0; i < EX_DIRECT_BLOCKS; i++) {
144148
copy->blocks[i] = inode->blocks[i];
145149
}
@@ -183,6 +187,8 @@ ex_status ex_inode_create(struct ex_inode *inode, uint16_t mode, gid_t gid, uid_
183187
inode->nlinks = 1;
184188
}
185189

190+
inode->nblocks = 0;
191+
186192
if (ex_inode_allocate_blocks(inode) != OK) {
187193
goto free_inode_block;
188194
}

src/inode.h

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -50,6 +50,9 @@ struct ex_inode {
5050
// number of links
5151
uint16_t nlinks;
5252

53+
// number of allocated blocks
54+
size_t nblocks;
55+
5356
// size of file when inode is file
5457
// size of file metadata (struct ex_inode) if inode is directory
5558
size_t size;

test/CMakeLists.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@ set(TEST_SOURCES
2020
test_write_max_size.c
2121
test_chmod.c
2222
test_chown.c
23+
test_stat_block_number.c
2324
)
2425

2526
find_package(PkgConfig REQUIRED)

test/test_main.c

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@ void test_can_create_maximum_inodes(void);
2121
void test_inode_link(void);
2222
void test_chmod(void);
2323
void test_chown(void);
24+
void test_stat_block_number(void);
2425

2526
int main(int argc, char **argv) {
2627
ex_set_log_level(fatal);
@@ -52,6 +53,7 @@ int main(int argc, char **argv) {
5253
g_test_add_func("/exfuse/test_inode_link", test_inode_link);
5354
g_test_add_func("/exfuse/test_chmod", test_chmod);
5455
g_test_add_func("/exfuse/test_chown", test_chown);
56+
g_test_add_func("/exfuse/test_stat_block_number", test_stat_block_number);
5557

5658
return g_test_run();
5759
}

test/test_stat_block_number.c

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
#include "../src/ex.h"
2+
#include "../src/mkfs.h"
3+
#include <err.h>
4+
#include <glib.h>
5+
#include <sys/stat.h>
6+
#include <sys/types.h>
7+
#include <unistd.h>
8+
9+
void test_stat_block_number(void) {
10+
// create new device
11+
unlink(EX_DEVICE);
12+
ex_mkfs_test_init();
13+
14+
// create new file
15+
int rv = ex_create("/fname", S_IRWXU, getgid(), getuid());
16+
g_assert(!rv);
17+
18+
struct stat st;
19+
20+
rv = ex_getattr("/fname", &st);
21+
g_assert(!rv);
22+
23+
// current file should be empty, but because we do preallocate
24+
// all direct blocks nblocks should be EX_DIRECT_BLOCKS
25+
g_assert_cmpint(st.st_blocks, ==, EX_DIRECT_BLOCKS);
26+
27+
ex_deinit();
28+
}

0 commit comments

Comments
 (0)