Skip to content

Commit 922fd70

Browse files
committed
Kernel: Convert the DiskBackedFS write API to take "const u8*"
This way clients are not required to have instantiated ByteBuffers and can choose whatever memory scheme works best for them. Also converted some of the Ext2FS code to use stack buffers instead.
1 parent 1fc2612 commit 922fd70

File tree

4 files changed

+29
-32
lines changed

4 files changed

+29
-32
lines changed

Kernel/FileSystem/DiskBackedFileSystem.cpp

Lines changed: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -81,29 +81,27 @@ DiskBackedFS::~DiskBackedFS()
8181
{
8282
}
8383

84-
bool DiskBackedFS::write_block(unsigned index, const ByteBuffer& data)
84+
bool DiskBackedFS::write_block(unsigned index, const u8* data)
8585
{
8686
#ifdef DBFS_DEBUG
8787
kprintf("DiskBackedFileSystem::write_block %u, size=%u\n", index, data.size());
8888
#endif
89-
ASSERT(data.size() == block_size());
90-
9189
auto& entry = cache().get(index);
92-
memcpy(entry.data, data.data(), data.size());
90+
memcpy(entry.data, data, block_size());
9391
entry.is_dirty = true;
9492
entry.has_data = true;
9593

9694
cache().set_dirty(true);
9795
return true;
9896
}
9997

100-
bool DiskBackedFS::write_blocks(unsigned index, unsigned count, const ByteBuffer& data)
98+
bool DiskBackedFS::write_blocks(unsigned index, unsigned count, const u8* data)
10199
{
102100
#ifdef DBFS_DEBUG
103101
kprintf("DiskBackedFileSystem::write_blocks %u x%u\n", index, count);
104102
#endif
105103
for (unsigned i = 0; i < count; ++i)
106-
write_block(index + i, data.slice(i * block_size(), block_size()));
104+
write_block(index + i, data + i * block_size());
107105
return true;
108106
}
109107

Kernel/FileSystem/DiskBackedFileSystem.h

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -22,8 +22,8 @@ class DiskBackedFS : public FS {
2222
bool read_block(unsigned index, u8* buffer) const;
2323
bool read_blocks(unsigned index, unsigned count, u8* buffer) const;
2424

25-
bool write_block(unsigned index, const ByteBuffer&);
26-
bool write_blocks(unsigned index, unsigned count, const ByteBuffer&);
25+
bool write_block(unsigned index, const u8*);
26+
bool write_blocks(unsigned index, unsigned count, const u8*);
2727

2828
private:
2929
DiskCache& cache() const;

Kernel/FileSystem/Ext2FileSystem.cpp

Lines changed: 22 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010

1111
//#define EXT2_DEBUG
1212

13+
static const size_t max_block_size = 4096;
1314
static const ssize_t max_inline_symlink_length = 60;
1415

1516
static u8 to_ext2_file_type(mode_t mode)
@@ -121,8 +122,11 @@ bool Ext2FS::initialize()
121122
kprintf("ext2fs: desc size = %u\n", EXT2_DESC_SIZE(&super_block));
122123
#endif
123124

125+
124126
set_block_size(EXT2_BLOCK_SIZE(&super_block));
125127

128+
ASSERT(block_size() <= (int)max_block_size);
129+
126130
m_block_group_count = ceil_div(super_block.s_blocks_count, super_block.s_blocks_per_group);
127131

128132
if (m_block_group_count == 0) {
@@ -157,27 +161,24 @@ InodeIdentifier Ext2FS::root_inode() const
157161
return { fsid(), EXT2_ROOT_INO };
158162
}
159163

160-
ByteBuffer Ext2FS::read_block_containing_inode(unsigned inode, unsigned& block_index, unsigned& offset) const
164+
bool Ext2FS::read_block_containing_inode(unsigned inode, unsigned& block_index, unsigned& offset, u8* buffer) const
161165
{
162166
LOCKER(m_lock);
163167
auto& super_block = this->super_block();
164168

165169
if (inode != EXT2_ROOT_INO && inode < EXT2_FIRST_INO(&super_block))
166-
return {};
170+
return false;
167171

168172
if (inode > super_block.s_inodes_count)
169-
return {};
173+
return false;
170174

171175
auto& bgd = group_descriptor(group_index_from_inode(inode));
172176

173177
offset = ((inode - 1) % inodes_per_group()) * inode_size();
174178
block_index = bgd.bg_inode_table + (offset >> EXT2_BLOCK_SIZE_BITS(&super_block));
175179
offset &= block_size() - 1;
176180

177-
auto buffer = ByteBuffer::create_uninitialized(block_size());
178-
if (!read_block(block_index, buffer.data()))
179-
return {};
180-
return buffer;
181+
return read_block(block_index, buffer);
181182
}
182183

183184
Ext2FS::BlockListShape Ext2FS::compute_block_list_shape(unsigned blocks)
@@ -290,7 +291,7 @@ bool Ext2FS::write_block_list_for_inode(InodeIndex inode_index, ext2_inode& e2in
290291
--remaining_blocks;
291292
}
292293
stream.fill_to_end(0);
293-
bool success = write_block(e2inode.i_block[EXT2_IND_BLOCK], block_contents);
294+
bool success = write_block(e2inode.i_block[EXT2_IND_BLOCK], block_contents.data());
294295
ASSERT(success);
295296
}
296297

@@ -373,7 +374,7 @@ bool Ext2FS::write_block_list_for_inode(InodeIndex inode_index, ext2_inode& e2in
373374
}
374375

375376
if (ind_block_dirty) {
376-
bool success = write_block(indirect_block_index, ind_block_contents);
377+
bool success = write_block(indirect_block_index, ind_block_contents.data());
377378
ASSERT(success);
378379
}
379380
}
@@ -385,7 +386,7 @@ bool Ext2FS::write_block_list_for_inode(InodeIndex inode_index, ext2_inode& e2in
385386
}
386387

387388
if (dind_block_dirty) {
388-
bool success = write_block(e2inode.i_block[EXT2_DIND_BLOCK], dind_block_contents);
389+
bool success = write_block(e2inode.i_block[EXT2_DIND_BLOCK], dind_block_contents.data());
389390
ASSERT(success);
390391
}
391392
}
@@ -509,7 +510,7 @@ void Ext2FS::flush_block_group_descriptor_table()
509510
LOCKER(m_lock);
510511
unsigned blocks_to_write = ceil_div(m_block_group_count * (unsigned)sizeof(ext2_group_desc), block_size());
511512
unsigned first_block_of_bgdt = block_size() == 1024 ? 2 : 1;
512-
write_blocks(first_block_of_bgdt, blocks_to_write, m_cached_group_descriptor_table);
513+
write_blocks(first_block_of_bgdt, blocks_to_write, m_cached_group_descriptor_table.data());
513514
}
514515

515516
Ext2FSInode::Ext2FSInode(Ext2FS& fs, unsigned index)
@@ -585,15 +586,15 @@ RefPtr<Inode> Ext2FS::get_inode(InodeIdentifier inode) const
585586

586587
unsigned block_index;
587588
unsigned offset;
588-
auto block = read_block_containing_inode(inode.index(), block_index, offset);
589-
if (!block)
589+
u8 block[max_block_size];
590+
if (!read_block_containing_inode(inode.index(), block_index, offset, block))
590591
return {};
591592

592593
auto it = m_inode_cache.find(inode.index());
593594
if (it != m_inode_cache.end())
594595
return (*it).value;
595596
auto new_inode = adopt(*new Ext2FSInode(const_cast<Ext2FS&>(*this), inode.index()));
596-
memcpy(&new_inode->m_raw_inode, reinterpret_cast<ext2_inode*>(block.offset_pointer(offset)), sizeof(ext2_inode));
597+
memcpy(&new_inode->m_raw_inode, reinterpret_cast<ext2_inode*>(block + offset), sizeof(ext2_inode));
597598
m_inode_cache.set(inode.index(), new_inode);
598599
return new_inode;
599600
}
@@ -644,9 +645,7 @@ ssize_t Ext2FSInode::read_bytes(off_t offset, ssize_t count, u8* buffer, FileDes
644645
//kprintf("ok let's do it, read(%u, %u) -> blocks %u thru %u, oifb: %u\n", offset, count, first_block_logical_index, last_block_logical_index, offset_into_first_block);
645646
#endif
646647

647-
// Biggest block size should be 4096..
648-
u8 block[4096];
649-
ASSERT(block_size < (int)sizeof(block));
648+
u8 block[max_block_size];
650649

651650
for (int bi = first_block_logical_index; remaining_count && bi <= last_block_logical_index; ++bi) {
652651
bool success = fs().read_block(m_block_list[bi], block);
@@ -779,7 +778,7 @@ ssize_t Ext2FSInode::write_bytes(off_t offset, ssize_t count, const u8* data, Fi
779778
#ifdef EXT2_DEBUG
780779
dbgprintf("Ext2FSInode::write_bytes: writing block %u (offset_into_block: %u)\n", m_block_list[bi], offset_into_block);
781780
#endif
782-
bool success = fs().write_block(m_block_list[bi], block);
781+
bool success = fs().write_block(m_block_list[bi], block.data());
783782
if (!success) {
784783
kprintf("Ext2FSInode::write_bytes: write_block(%u) failed (lbi: %u)\n", m_block_list[bi], bi);
785784
ASSERT_NOT_REACHED();
@@ -985,10 +984,10 @@ bool Ext2FS::write_ext2_inode(unsigned inode, const ext2_inode& e2inode)
985984
LOCKER(m_lock);
986985
unsigned block_index;
987986
unsigned offset;
988-
auto block = read_block_containing_inode(inode, block_index, offset);
989-
if (!block)
987+
u8 block[max_block_size];
988+
if (!read_block_containing_inode(inode, block_index, offset, block))
990989
return false;
991-
memcpy(reinterpret_cast<ext2_inode*>(block.offset_pointer(offset)), &e2inode, inode_size());
990+
memcpy(reinterpret_cast<ext2_inode*>(block + offset), &e2inode, inode_size());
992991
bool success = write_block(block_index, block);
993992
ASSERT(success);
994993
return success;
@@ -1177,7 +1176,7 @@ bool Ext2FS::set_inode_allocation_state(InodeIndex inode_index, bool new_state)
11771176
}
11781177

11791178
bitmap.set(bit_index, new_state);
1180-
success = write_block(bgd.bg_inode_bitmap, block);
1179+
success = write_block(bgd.bg_inode_bitmap, block.data());
11811180
ASSERT(success);
11821181

11831182
// Update superblock
@@ -1241,7 +1240,7 @@ bool Ext2FS::set_block_allocation_state(BlockIndex block_index, bool new_state)
12411240
}
12421241

12431242
bitmap.set(bit_index, new_state);
1244-
success = write_block(bgd.bg_block_bitmap, block);
1243+
success = write_block(bgd.bg_block_bitmap, block.data());
12451244
ASSERT(success);
12461245

12471246
// Update superblock

Kernel/FileSystem/Ext2FileSystem.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -87,7 +87,7 @@ class Ext2FS final : public DiskBackedFS {
8787
unsigned inode_size() const;
8888

8989
bool write_ext2_inode(InodeIndex, const ext2_inode&);
90-
ByteBuffer read_block_containing_inode(InodeIndex inode, BlockIndex& block_index, unsigned& offset) const;
90+
bool read_block_containing_inode(InodeIndex inode, BlockIndex& block_index, unsigned& offset, u8* buffer) const;
9191

9292
ByteBuffer read_super_block() const;
9393
bool write_super_block(const ext2_super_block&);

0 commit comments

Comments
 (0)