12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658 |
- /*
- * Copyright (c) 2018-2020, Andreas Kling <kling@serenityos.org>
- * All rights reserved.
- *
- * Redistribution and use in source and binary forms, with or without
- * modification, are permitted provided that the following conditions are met:
- *
- * 1. Redistributions of source code must retain the above copyright notice, this
- * list of conditions and the following disclaimer.
- *
- * 2. Redistributions in binary form must reproduce the above copyright notice,
- * this list of conditions and the following disclaimer in the documentation
- * and/or other materials provided with the distribution.
- *
- * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
- * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
- * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
- * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
- * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
- * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
- * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
- * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
- * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
- * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
- */
- #include <AK/HashMap.h>
- #include <AK/MemoryStream.h>
- #include <AK/StdLibExtras.h>
- #include <AK/StringView.h>
- #include <Kernel/Debug.h>
- #include <Kernel/Devices/BlockDevice.h>
- #include <Kernel/FileSystem/Ext2FileSystem.h>
- #include <Kernel/FileSystem/FileDescription.h>
- #include <Kernel/FileSystem/ext2_fs.h>
- #include <Kernel/Process.h>
- #include <Kernel/UnixTypes.h>
- #include <LibC/errno_numbers.h>
- namespace Kernel {
- static const size_t max_link_count = 65535;
- static const size_t max_block_size = 4096;
- static const ssize_t max_inline_symlink_length = 60;
- struct Ext2FSDirectoryEntry {
- String name;
- InodeIndex inode_index { 0 };
- u8 file_type { 0 };
- };
- static u8 to_ext2_file_type(mode_t mode)
- {
- if (is_regular_file(mode))
- return EXT2_FT_REG_FILE;
- if (is_directory(mode))
- return EXT2_FT_DIR;
- if (is_character_device(mode))
- return EXT2_FT_CHRDEV;
- if (is_block_device(mode))
- return EXT2_FT_BLKDEV;
- if (is_fifo(mode))
- return EXT2_FT_FIFO;
- if (is_socket(mode))
- return EXT2_FT_SOCK;
- if (is_symlink(mode))
- return EXT2_FT_SYMLINK;
- return EXT2_FT_UNKNOWN;
- }
- static unsigned divide_rounded_up(unsigned a, unsigned b)
- {
- return (a / b) + (a % b != 0);
- }
- NonnullRefPtr<Ext2FS> Ext2FS::create(FileDescription& file_description)
- {
- return adopt(*new Ext2FS(file_description));
- }
- Ext2FS::Ext2FS(FileDescription& file_description)
- : BlockBasedFS(file_description)
- {
- }
- Ext2FS::~Ext2FS()
- {
- }
- bool Ext2FS::flush_super_block()
- {
- LOCKER(m_lock);
- VERIFY((sizeof(ext2_super_block) % logical_block_size()) == 0);
- auto super_block_buffer = UserOrKernelBuffer::for_kernel_buffer((u8*)&m_super_block);
- bool success = raw_write_blocks(2, (sizeof(ext2_super_block) / logical_block_size()), super_block_buffer);
- VERIFY(success);
- return true;
- }
- const ext2_group_desc& Ext2FS::group_descriptor(GroupIndex group_index) const
- {
- // FIXME: Should this fail gracefully somehow?
- VERIFY(group_index <= m_block_group_count);
- VERIFY(group_index > 0);
- return block_group_descriptors()[group_index.value() - 1];
- }
- bool Ext2FS::initialize()
- {
- LOCKER(m_lock);
- VERIFY((sizeof(ext2_super_block) % logical_block_size()) == 0);
- auto super_block_buffer = UserOrKernelBuffer::for_kernel_buffer((u8*)&m_super_block);
- bool success = raw_read_blocks(2, (sizeof(ext2_super_block) / logical_block_size()), super_block_buffer);
- VERIFY(success);
- auto& super_block = this->super_block();
- if constexpr (EXT2_DEBUG) {
- klog() << "ext2fs: super block magic: " << String::format("%x", super_block.s_magic) << " (super block size: " << sizeof(ext2_super_block) << ")";
- }
- if (super_block.s_magic != EXT2_SUPER_MAGIC)
- return false;
- if constexpr (EXT2_DEBUG) {
- klog() << "ext2fs: " << super_block.s_inodes_count << " inodes, " << super_block.s_blocks_count << " blocks";
- klog() << "ext2fs: block size = " << EXT2_BLOCK_SIZE(&super_block);
- klog() << "ext2fs: first data block = " << super_block.s_first_data_block;
- klog() << "ext2fs: inodes per block = " << inodes_per_block();
- klog() << "ext2fs: inodes per group = " << inodes_per_group();
- klog() << "ext2fs: free inodes = " << super_block.s_free_inodes_count;
- klog() << "ext2fs: desc per block = " << EXT2_DESC_PER_BLOCK(&super_block);
- klog() << "ext2fs: desc size = " << EXT2_DESC_SIZE(&super_block);
- }
- set_block_size(EXT2_BLOCK_SIZE(&super_block));
- VERIFY(block_size() <= (int)max_block_size);
- m_block_group_count = ceil_div(super_block.s_blocks_count, super_block.s_blocks_per_group);
- if (m_block_group_count == 0) {
- klog() << "ext2fs: no block groups :(";
- return false;
- }
- unsigned blocks_to_read = ceil_div(m_block_group_count * sizeof(ext2_group_desc), block_size());
- BlockIndex first_block_of_bgdt = block_size() == 1024 ? 2 : 1;
- m_cached_group_descriptor_table = KBuffer::try_create_with_size(block_size() * blocks_to_read, Region::Access::Read | Region::Access::Write, "Ext2FS: Block group descriptors");
- if (!m_cached_group_descriptor_table) {
- dbgln("Ext2FS: Failed to allocate memory for group descriptor table");
- return false;
- }
- auto buffer = UserOrKernelBuffer::for_kernel_buffer(m_cached_group_descriptor_table->data());
- auto result = read_blocks(first_block_of_bgdt, blocks_to_read, buffer);
- if (result.is_error()) {
- // FIXME: Propagate the error
- dbgln("Ext2FS: initialize had error: {}", result.error());
- return false;
- }
- if constexpr (EXT2_DEBUG) {
- for (unsigned i = 1; i <= m_block_group_count; ++i) {
- auto& group = group_descriptor(i);
- klog() << "ext2fs: group[" << i << "] { block_bitmap: " << group.bg_block_bitmap << ", inode_bitmap: " << group.bg_inode_bitmap << ", inode_table: " << group.bg_inode_table << " }";
- }
- }
- return true;
- }
- const char* Ext2FS::class_name() const
- {
- return "Ext2FS";
- }
- NonnullRefPtr<Inode> Ext2FS::root_inode() const
- {
- return *get_inode({ fsid(), EXT2_ROOT_INO });
- }
- bool Ext2FS::find_block_containing_inode(InodeIndex inode, BlockIndex& block_index, unsigned& offset) const
- {
- auto& super_block = this->super_block();
- if (inode != EXT2_ROOT_INO && inode < EXT2_FIRST_INO(&super_block))
- return false;
- if (inode > super_block.s_inodes_count)
- return false;
- auto& bgd = group_descriptor(group_index_from_inode(inode));
- offset = ((inode.value() - 1) % inodes_per_group()) * inode_size();
- block_index = bgd.bg_inode_table + (offset >> EXT2_BLOCK_SIZE_BITS(&super_block));
- offset &= block_size() - 1;
- return true;
- }
- Ext2FS::BlockListShape Ext2FS::compute_block_list_shape(unsigned blocks) const
- {
- BlockListShape shape;
- const unsigned entries_per_block = EXT2_ADDR_PER_BLOCK(&super_block());
- unsigned blocks_remaining = blocks;
- shape.direct_blocks = min((unsigned)EXT2_NDIR_BLOCKS, blocks_remaining);
- blocks_remaining -= shape.direct_blocks;
- if (!blocks_remaining)
- return shape;
- shape.indirect_blocks = min(blocks_remaining, entries_per_block);
- shape.meta_blocks += 1;
- blocks_remaining -= shape.indirect_blocks;
- if (!blocks_remaining)
- return shape;
- shape.doubly_indirect_blocks = min(blocks_remaining, entries_per_block * entries_per_block);
- shape.meta_blocks += 1;
- shape.meta_blocks += divide_rounded_up(shape.doubly_indirect_blocks, entries_per_block);
- blocks_remaining -= shape.doubly_indirect_blocks;
- if (!blocks_remaining)
- return shape;
- shape.triply_indirect_blocks = min(blocks_remaining, entries_per_block * entries_per_block * entries_per_block);
- shape.meta_blocks += 1;
- shape.meta_blocks += divide_rounded_up(shape.triply_indirect_blocks, entries_per_block * entries_per_block);
- shape.meta_blocks += divide_rounded_up(shape.triply_indirect_blocks, entries_per_block);
- blocks_remaining -= shape.triply_indirect_blocks;
- VERIFY(blocks_remaining == 0);
- return shape;
- }
- KResult Ext2FSInode::flush_block_list()
- {
- LOCKER(m_lock);
- if (m_block_list.is_empty()) {
- m_raw_inode.i_blocks = 0;
- memset(m_raw_inode.i_block, 0, sizeof(m_raw_inode.i_block));
- set_metadata_dirty(true);
- return KSuccess;
- }
- // NOTE: There is a mismatch between i_blocks and blocks.size() since i_blocks includes meta blocks and blocks.size() does not.
- auto old_block_count = ceil_div(static_cast<size_t>(m_raw_inode.i_size), fs().block_size());
- auto old_shape = fs().compute_block_list_shape(old_block_count);
- auto new_shape = fs().compute_block_list_shape(m_block_list.size());
- Vector<Ext2FS::BlockIndex> new_meta_blocks;
- if (new_shape.meta_blocks > old_shape.meta_blocks) {
- auto blocks_or_error = fs().allocate_blocks(fs().group_index_from_inode(index()), new_shape.meta_blocks - old_shape.meta_blocks);
- if (blocks_or_error.is_error())
- return blocks_or_error.error();
- new_meta_blocks = blocks_or_error.release_value();
- }
- m_raw_inode.i_blocks = (m_block_list.size() + new_shape.meta_blocks) * (fs().block_size() / 512);
- bool inode_dirty = false;
- unsigned output_block_index = 0;
- unsigned remaining_blocks = m_block_list.size();
- for (unsigned i = 0; i < new_shape.direct_blocks; ++i) {
- if (m_raw_inode.i_block[i] != m_block_list[output_block_index])
- inode_dirty = true;
- m_raw_inode.i_block[i] = m_block_list[output_block_index].value();
- ++output_block_index;
- --remaining_blocks;
- }
- if (inode_dirty) {
- if constexpr (EXT2_DEBUG) {
- dbgln("Ext2FS: Writing {} direct block(s) to i_block array of inode {}", min((size_t)EXT2_NDIR_BLOCKS, m_block_list.size()), index());
- for (size_t i = 0; i < min((size_t)EXT2_NDIR_BLOCKS, m_block_list.size()); ++i)
- dbgln(" + {}", m_block_list[i]);
- }
- set_metadata_dirty(true);
- inode_dirty = false;
- }
- if (!remaining_blocks)
- return KSuccess;
- const unsigned entries_per_block = EXT2_ADDR_PER_BLOCK(&fs().super_block());
- bool ind_block_new = !m_raw_inode.i_block[EXT2_IND_BLOCK];
- if (ind_block_new) {
- Ext2FS::BlockIndex new_indirect_block = new_meta_blocks.take_last();
- if (m_raw_inode.i_block[EXT2_IND_BLOCK] != new_indirect_block)
- inode_dirty = true;
- m_raw_inode.i_block[EXT2_IND_BLOCK] = new_indirect_block.value();
- if (inode_dirty) {
- dbgln_if(EXT2_DEBUG, "Ext2FS: Adding the indirect block to i_block array of inode {}", index());
- set_metadata_dirty(true);
- inode_dirty = false;
- }
- }
- if (old_shape.indirect_blocks == new_shape.indirect_blocks) {
- // No need to update the singly indirect block array.
- remaining_blocks -= new_shape.indirect_blocks;
- output_block_index += new_shape.indirect_blocks;
- } else {
- auto block_contents = ByteBuffer::create_uninitialized(fs().block_size());
- OutputMemoryStream stream { block_contents };
- VERIFY(new_shape.indirect_blocks <= entries_per_block);
- for (unsigned i = 0; i < new_shape.indirect_blocks; ++i) {
- stream << m_block_list[output_block_index++].value();
- --remaining_blocks;
- }
- stream.fill_to_end(0);
- auto buffer = UserOrKernelBuffer::for_kernel_buffer(stream.data());
- auto result = fs().write_block(m_raw_inode.i_block[EXT2_IND_BLOCK], buffer, stream.size());
- if (result.is_error())
- return result;
- }
- if (!remaining_blocks)
- return KSuccess;
- bool dind_block_dirty = false;
- bool dind_block_new = !m_raw_inode.i_block[EXT2_DIND_BLOCK];
- if (dind_block_new) {
- Ext2FS::BlockIndex new_dindirect_block = new_meta_blocks.take_last();
- if (m_raw_inode.i_block[EXT2_DIND_BLOCK] != new_dindirect_block)
- inode_dirty = true;
- m_raw_inode.i_block[EXT2_DIND_BLOCK] = new_dindirect_block.value();
- if (inode_dirty) {
- dbgln_if(EXT2_DEBUG, "Ext2FS: Adding the doubly-indirect block to i_block array of inode {}", index());
- set_metadata_dirty(true);
- inode_dirty = false;
- }
- }
- if (old_shape.doubly_indirect_blocks == new_shape.doubly_indirect_blocks) {
- // No need to update the doubly indirect block data.
- remaining_blocks -= new_shape.doubly_indirect_blocks;
- output_block_index += new_shape.doubly_indirect_blocks;
- } else {
- unsigned indirect_block_count = divide_rounded_up(new_shape.doubly_indirect_blocks, entries_per_block);
- auto dind_block_contents = ByteBuffer::create_uninitialized(fs().block_size());
- if (dind_block_new) {
- dind_block_contents.zero_fill();
- dind_block_dirty = true;
- } else {
- auto buffer = UserOrKernelBuffer::for_kernel_buffer(dind_block_contents.data());
- auto result = fs().read_block(m_raw_inode.i_block[EXT2_DIND_BLOCK], &buffer, fs().block_size());
- if (result.is_error()) {
- dbgln("Ext2FS: write_block_list_for_inode had error: {}", result.error());
- return result;
- }
- }
- auto* dind_block_as_pointers = (unsigned*)dind_block_contents.data();
- VERIFY(indirect_block_count <= entries_per_block);
- for (unsigned i = 0; i < indirect_block_count; ++i) {
- bool ind_block_dirty = false;
- Ext2FS::BlockIndex indirect_block_index = dind_block_as_pointers[i];
- bool ind_block_new = !indirect_block_index;
- if (ind_block_new) {
- indirect_block_index = new_meta_blocks.take_last();
- dind_block_as_pointers[i] = indirect_block_index.value();
- dind_block_dirty = true;
- }
- auto ind_block_contents = ByteBuffer::create_uninitialized(fs().block_size());
- if (ind_block_new) {
- ind_block_contents.zero_fill();
- ind_block_dirty = true;
- } else {
- auto buffer = UserOrKernelBuffer::for_kernel_buffer(ind_block_contents.data());
- auto result = fs().read_block(indirect_block_index, &buffer, fs().block_size());
- if (result.is_error()) {
- dbgln("Ext2FS: write_block_list_for_inode had error: {}", result.error());
- return result;
- }
- }
- auto* ind_block_as_pointers = (unsigned*)ind_block_contents.data();
- unsigned entries_to_write = new_shape.doubly_indirect_blocks - (i * entries_per_block);
- if (entries_to_write > entries_per_block)
- entries_to_write = entries_per_block;
- VERIFY(entries_to_write <= entries_per_block);
- for (unsigned j = 0; j < entries_to_write; ++j) {
- Ext2FS::BlockIndex output_block = m_block_list[output_block_index++];
- if (ind_block_as_pointers[j] != output_block) {
- ind_block_as_pointers[j] = output_block.value();
- ind_block_dirty = true;
- }
- --remaining_blocks;
- }
- for (unsigned j = entries_to_write; j < entries_per_block; ++j) {
- if (ind_block_as_pointers[j] != 0) {
- ind_block_as_pointers[j] = 0;
- ind_block_dirty = true;
- }
- }
- if (ind_block_dirty) {
- auto buffer = UserOrKernelBuffer::for_kernel_buffer(ind_block_contents.data());
- int err = fs().write_block(indirect_block_index, buffer, fs().block_size());
- VERIFY(err >= 0);
- }
- }
- for (unsigned i = indirect_block_count; i < entries_per_block; ++i) {
- if (dind_block_as_pointers[i] != 0) {
- dind_block_as_pointers[i] = 0;
- dind_block_dirty = true;
- }
- }
- if (dind_block_dirty) {
- auto buffer = UserOrKernelBuffer::for_kernel_buffer(dind_block_contents.data());
- int err = fs().write_block(m_raw_inode.i_block[EXT2_DIND_BLOCK], buffer, fs().block_size());
- VERIFY(err >= 0);
- }
- }
- if (!remaining_blocks)
- return KSuccess;
- // FIXME: Implement!
- dbgln("we don't know how to write tind ext2fs blocks yet!");
- VERIFY_NOT_REACHED();
- }
- Vector<Ext2FS::BlockIndex> Ext2FSInode::compute_block_list() const
- {
- return compute_block_list_impl(false);
- }
- Vector<Ext2FS::BlockIndex> Ext2FSInode::compute_block_list_with_meta_blocks() const
- {
- return compute_block_list_impl(true);
- }
- Vector<Ext2FS::BlockIndex> Ext2FSInode::compute_block_list_impl(bool include_block_list_blocks) const
- {
- // FIXME: This is really awkwardly factored.. foo_impl_internal :|
- auto block_list = compute_block_list_impl_internal(m_raw_inode, include_block_list_blocks);
- while (!block_list.is_empty() && block_list.last() == 0)
- block_list.take_last();
- return block_list;
- }
- Vector<Ext2FS::BlockIndex> Ext2FSInode::compute_block_list_impl_internal(const ext2_inode& e2inode, bool include_block_list_blocks) const
- {
- unsigned entries_per_block = EXT2_ADDR_PER_BLOCK(&fs().super_block());
- unsigned block_count = ceil_div(static_cast<size_t>(e2inode.i_size), fs().block_size());
- // If we are handling a symbolic link, the path is stored in the 60 bytes in
- // the inode that are used for the 12 direct and 3 indirect block pointers,
- // If the path is longer than 60 characters, a block is allocated, and the
- // block contains the destination path. The file size corresponds to the
- // path length of the destination.
- if (::is_symlink(e2inode.i_mode) && e2inode.i_blocks == 0)
- block_count = 0;
- dbgln_if(EXT2_DEBUG, "Ext2FS::block_list_for_inode(): i_size={}, i_blocks={}, block_count={}", e2inode.i_size, e2inode.i_blocks, block_count);
- unsigned blocks_remaining = block_count;
- if (include_block_list_blocks) {
- auto shape = fs().compute_block_list_shape(block_count);
- blocks_remaining += shape.meta_blocks;
- }
- Vector<Ext2FS::BlockIndex> list;
- auto add_block = [&](auto bi) {
- if (blocks_remaining) {
- list.append(bi);
- --blocks_remaining;
- }
- };
- if (include_block_list_blocks) {
- // This seems like an excessive over-estimate but w/e.
- list.ensure_capacity(blocks_remaining * 2);
- } else {
- list.ensure_capacity(blocks_remaining);
- }
- unsigned direct_count = min(block_count, (unsigned)EXT2_NDIR_BLOCKS);
- for (unsigned i = 0; i < direct_count; ++i) {
- auto block_index = e2inode.i_block[i];
- add_block(block_index);
- }
- if (!blocks_remaining)
- return list;
- // Don't need to make copy of add_block, since this capture will only
- // be called before compute_block_list_impl_internal finishes.
- auto process_block_array = [&](auto array_block_index, auto&& callback) {
- if (include_block_list_blocks)
- add_block(array_block_index);
- auto count = min(blocks_remaining, entries_per_block);
- if (!count)
- return;
- u32 array[count];
- auto buffer = UserOrKernelBuffer::for_kernel_buffer((u8*)array);
- auto result = fs().read_block(array_block_index, &buffer, sizeof(array), 0);
- if (result.is_error()) {
- // FIXME: Stop here and propagate this error.
- dbgln("Ext2FS: compute_block_list_impl_internal had error: {}", result.error());
- }
- for (unsigned i = 0; i < count; ++i)
- callback(Ext2FS::BlockIndex(array[i]));
- };
- process_block_array(e2inode.i_block[EXT2_IND_BLOCK], [&](auto block_index) {
- add_block(block_index);
- });
- if (!blocks_remaining)
- return list;
- process_block_array(e2inode.i_block[EXT2_DIND_BLOCK], [&](auto block_index) {
- process_block_array(block_index, [&](auto block_index2) {
- add_block(block_index2);
- });
- });
- if (!blocks_remaining)
- return list;
- process_block_array(e2inode.i_block[EXT2_TIND_BLOCK], [&](auto block_index) {
- process_block_array(block_index, [&](auto block_index2) {
- process_block_array(block_index2, [&](auto block_index3) {
- add_block(block_index3);
- });
- });
- });
- return list;
- }
- void Ext2FS::free_inode(Ext2FSInode& inode)
- {
- LOCKER(m_lock);
- VERIFY(inode.m_raw_inode.i_links_count == 0);
- dbgln_if(EXT2_DEBUG, "Ext2FS: Inode {} has no more links, time to delete!", inode.index());
- // Mark all blocks used by this inode as free.
- for (auto block_index : inode.compute_block_list_with_meta_blocks()) {
- VERIFY(block_index <= super_block().s_blocks_count);
- if (block_index.value()) {
- auto result = set_block_allocation_state(block_index, false);
- if (result.is_error()) {
- dbgln("Ext2FS: Failed to deallocate block {} in free_inode()", block_index);
- }
- }
- }
- // If the inode being freed is a directory, update block group directory counter.
- if (inode.is_directory()) {
- auto& bgd = const_cast<ext2_group_desc&>(group_descriptor(group_index_from_inode(inode.index())));
- --bgd.bg_used_dirs_count;
- dbgln("Ext2FS: Decremented bg_used_dirs_count to {}", bgd.bg_used_dirs_count);
- m_block_group_descriptors_dirty = true;
- }
- // NOTE: After this point, the inode metadata is wiped.
- memset(&inode.m_raw_inode, 0, sizeof(ext2_inode));
- inode.m_raw_inode.i_dtime = kgettimeofday().to_truncated_seconds();
- write_ext2_inode(inode.index(), inode.m_raw_inode);
- // Mark the inode as free.
- auto result = set_inode_allocation_state(inode.index(), false);
- if (result.is_error()) {
- dbgln("Ext2FS: Failed to free inode {}", inode.index());
- }
- }
- void Ext2FS::flush_block_group_descriptor_table()
- {
- LOCKER(m_lock);
- unsigned blocks_to_write = ceil_div(m_block_group_count * sizeof(ext2_group_desc), block_size());
- unsigned first_block_of_bgdt = block_size() == 1024 ? 2 : 1;
- auto buffer = UserOrKernelBuffer::for_kernel_buffer((u8*)block_group_descriptors());
- auto result = write_blocks(first_block_of_bgdt, blocks_to_write, buffer);
- if (result.is_error())
- dbgln("Ext2FS: flush_block_group_descriptor_table had error: {}", result.error());
- }
- void Ext2FS::flush_writes()
- {
- LOCKER(m_lock);
- if (m_super_block_dirty) {
- flush_super_block();
- m_super_block_dirty = false;
- }
- if (m_block_group_descriptors_dirty) {
- flush_block_group_descriptor_table();
- m_block_group_descriptors_dirty = false;
- }
- for (auto& cached_bitmap : m_cached_bitmaps) {
- if (cached_bitmap->dirty) {
- auto buffer = UserOrKernelBuffer::for_kernel_buffer(cached_bitmap->buffer.data());
- auto result = write_block(cached_bitmap->bitmap_block_index, buffer, block_size());
- if (result.is_error()) {
- dbgln("Ext2FS: flush_writes() had error {}", result.error());
- }
- cached_bitmap->dirty = false;
- dbgln_if(EXT2_DEBUG, "Flushed bitmap block {}", cached_bitmap->bitmap_block_index);
- }
- }
- BlockBasedFS::flush_writes();
- // Uncache Inodes that are only kept alive by the index-to-inode lookup cache.
- // We don't uncache Inodes that are being watched by at least one InodeWatcher.
- // FIXME: It would be better to keep a capped number of Inodes around.
- // The problem is that they are quite heavy objects, and use a lot of heap memory
- // for their (child name lookup) and (block list) caches.
- Vector<InodeIndex> unused_inodes;
- for (auto& it : m_inode_cache) {
- if (it.value->ref_count() != 1)
- continue;
- if (it.value->has_watchers())
- continue;
- unused_inodes.append(it.key);
- }
- for (auto index : unused_inodes)
- uncache_inode(index);
- }
- Ext2FSInode::Ext2FSInode(Ext2FS& fs, InodeIndex index)
- : Inode(fs, index)
- {
- }
- Ext2FSInode::~Ext2FSInode()
- {
- if (m_raw_inode.i_links_count == 0)
- fs().free_inode(*this);
- }
- InodeMetadata Ext2FSInode::metadata() const
- {
- LOCKER(m_lock);
- InodeMetadata metadata;
- metadata.inode = identifier();
- metadata.size = m_raw_inode.i_size;
- metadata.mode = m_raw_inode.i_mode;
- metadata.uid = m_raw_inode.i_uid;
- metadata.gid = m_raw_inode.i_gid;
- metadata.link_count = m_raw_inode.i_links_count;
- metadata.atime = m_raw_inode.i_atime;
- metadata.ctime = m_raw_inode.i_ctime;
- metadata.mtime = m_raw_inode.i_mtime;
- metadata.dtime = m_raw_inode.i_dtime;
- metadata.block_size = fs().block_size();
- metadata.block_count = m_raw_inode.i_blocks;
- if (Kernel::is_character_device(m_raw_inode.i_mode) || Kernel::is_block_device(m_raw_inode.i_mode)) {
- unsigned dev = m_raw_inode.i_block[0];
- if (!dev)
- dev = m_raw_inode.i_block[1];
- metadata.major_device = (dev & 0xfff00) >> 8;
- metadata.minor_device = (dev & 0xff) | ((dev >> 12) & 0xfff00);
- }
- return metadata;
- }
- void Ext2FSInode::flush_metadata()
- {
- LOCKER(m_lock);
- dbgln_if(EXT2_DEBUG, "Ext2FS: flush_metadata for inode {}", index());
- fs().write_ext2_inode(index(), m_raw_inode);
- if (is_directory()) {
- // Unless we're about to go away permanently, invalidate the lookup cache.
- if (m_raw_inode.i_links_count != 0) {
- // FIXME: This invalidation is way too hardcore. It's sad to throw away the whole cache.
- m_lookup_cache.clear();
- }
- }
- set_metadata_dirty(false);
- }
- RefPtr<Inode> Ext2FS::get_inode(InodeIdentifier inode) const
- {
- LOCKER(m_lock);
- VERIFY(inode.fsid() == fsid());
- {
- auto it = m_inode_cache.find(inode.index());
- if (it != m_inode_cache.end())
- return (*it).value;
- }
- auto state_or_error = get_inode_allocation_state(inode.index());
- if (state_or_error.is_error())
- return {};
- if (!state_or_error.value()) {
- m_inode_cache.set(inode.index(), nullptr);
- return {};
- }
- BlockIndex block_index;
- unsigned offset;
- if (!find_block_containing_inode(inode.index(), block_index, offset))
- return {};
- auto new_inode = adopt(*new Ext2FSInode(const_cast<Ext2FS&>(*this), inode.index()));
- auto buffer = UserOrKernelBuffer::for_kernel_buffer(reinterpret_cast<u8*>(&new_inode->m_raw_inode));
- auto result = read_block(block_index, &buffer, sizeof(ext2_inode), offset);
- if (result.is_error()) {
- // FIXME: Propagate the actual error.
- return nullptr;
- }
- m_inode_cache.set(inode.index(), new_inode);
- return new_inode;
- }
- ssize_t Ext2FSInode::read_bytes(off_t offset, ssize_t count, UserOrKernelBuffer& buffer, FileDescription* description) const
- {
- Locker inode_locker(m_lock);
- VERIFY(offset >= 0);
- if (m_raw_inode.i_size == 0)
- return 0;
- // Symbolic links shorter than 60 characters are store inline inside the i_block array.
- // This avoids wasting an entire block on short links. (Most links are short.)
- if (is_symlink() && size() < max_inline_symlink_length) {
- VERIFY(offset == 0);
- ssize_t nread = min((off_t)size() - offset, static_cast<off_t>(count));
- if (!buffer.write(((const u8*)m_raw_inode.i_block) + offset, (size_t)nread))
- return -EFAULT;
- return nread;
- }
- if (m_block_list.is_empty())
- m_block_list = compute_block_list();
- if (m_block_list.is_empty()) {
- dmesgln("Ext2FS: read_bytes: empty block list for inode {}", index());
- return -EIO;
- }
- bool allow_cache = !description || !description->is_direct();
- const int block_size = fs().block_size();
- size_t first_block_logical_index = offset / block_size;
- size_t last_block_logical_index = (offset + count) / block_size;
- if (last_block_logical_index >= m_block_list.size())
- last_block_logical_index = m_block_list.size() - 1;
- int offset_into_first_block = offset % block_size;
- ssize_t nread = 0;
- size_t remaining_count = min((off_t)count, (off_t)size() - offset);
- dbgln_if(EXT2_VERY_DEBUG, "Ext2FS: Reading up to {} bytes, {} bytes into inode {} to {}", count, offset, index(), buffer.user_or_kernel_ptr());
- for (size_t bi = first_block_logical_index; remaining_count && bi <= last_block_logical_index; ++bi) {
- auto block_index = m_block_list[bi];
- VERIFY(block_index.value());
- size_t offset_into_block = (bi == first_block_logical_index) ? offset_into_first_block : 0;
- size_t num_bytes_to_copy = min(block_size - offset_into_block, remaining_count);
- auto buffer_offset = buffer.offset(nread);
- int err = fs().read_block(block_index, &buffer_offset, num_bytes_to_copy, offset_into_block, allow_cache);
- if (err < 0) {
- dmesgln("Ext2FS: read_bytes: read_block({}) failed (bi: {})", block_index.value(), bi);
- return err;
- }
- remaining_count -= num_bytes_to_copy;
- nread += num_bytes_to_copy;
- }
- return nread;
- }
- KResult Ext2FSInode::resize(u64 new_size)
- {
- u64 old_size = size();
- if (old_size == new_size)
- return KSuccess;
- u64 block_size = fs().block_size();
- size_t blocks_needed_before = ceil_div(old_size, block_size);
- size_t blocks_needed_after = ceil_div(new_size, block_size);
- if constexpr (EXT2_DEBUG) {
- dbgln("Ext2FSInode::resize(): blocks needed before (size was {}): {}", old_size, blocks_needed_before);
- dbgln("Ext2FSInode::resize(): blocks needed after (size is {}): {}", new_size, blocks_needed_after);
- }
- if (blocks_needed_after > blocks_needed_before) {
- u32 additional_blocks_needed = blocks_needed_after - blocks_needed_before;
- if (additional_blocks_needed > fs().super_block().s_free_blocks_count)
- return ENOSPC;
- }
- Vector<Ext2FS::BlockIndex> block_list;
- if (!m_block_list.is_empty())
- block_list = m_block_list;
- else
- block_list = this->compute_block_list();
- if (blocks_needed_after > blocks_needed_before) {
- auto blocks_or_error = fs().allocate_blocks(fs().group_index_from_inode(index()), blocks_needed_after - blocks_needed_before);
- if (blocks_or_error.is_error())
- return blocks_or_error.error();
- block_list.append(blocks_or_error.release_value());
- } else if (blocks_needed_after < blocks_needed_before) {
- if constexpr (EXT2_DEBUG) {
- dbgln("Ext2FS: Shrinking inode {}. Old block list is {} entries:", index(), block_list.size());
- for (auto block_index : block_list) {
- dbgln(" # {}", block_index);
- }
- }
- while (block_list.size() != blocks_needed_after) {
- auto block_index = block_list.take_last();
- if (block_index.value()) {
- auto result = fs().set_block_allocation_state(block_index, false);
- if (result.is_error()) {
- dbgln("Ext2FS: Failed to free block {} in Ext2Inode::resize()", block_index);
- return result;
- }
- }
- }
- }
- m_block_list = move(block_list);
- auto result = flush_block_list();
- if (result.is_error())
- return result;
- m_raw_inode.i_size = new_size;
- set_metadata_dirty(true);
- if (new_size > old_size) {
- // If we're growing the inode, make sure we zero out all the new space.
- // FIXME: There are definitely more efficient ways to achieve this.
- size_t bytes_to_clear = new_size - old_size;
- size_t clear_from = old_size;
- u8 zero_buffer[PAGE_SIZE] {};
- while (bytes_to_clear) {
- auto nwritten = write_bytes(clear_from, min(sizeof(zero_buffer), bytes_to_clear), UserOrKernelBuffer::for_kernel_buffer(zero_buffer), nullptr);
- if (nwritten < 0)
- return KResult((ErrnoCode)-nwritten);
- VERIFY(nwritten != 0);
- bytes_to_clear -= nwritten;
- clear_from += nwritten;
- }
- }
- return KSuccess;
- }
- ssize_t Ext2FSInode::write_bytes(off_t offset, ssize_t count, const UserOrKernelBuffer& data, FileDescription* description)
- {
- VERIFY(offset >= 0);
- VERIFY(count >= 0);
- Locker inode_locker(m_lock);
- auto result = prepare_to_write_data();
- if (result.is_error())
- return result;
- if (is_symlink()) {
- VERIFY(offset == 0);
- if (max((size_t)(offset + count), (size_t)m_raw_inode.i_size) < max_inline_symlink_length) {
- dbgln_if(EXT2_DEBUG, "Ext2FS: write_bytes poking into i_block array for inline symlink '{}' ({} bytes)", data.copy_into_string(count), count);
- if (!data.read(((u8*)m_raw_inode.i_block) + offset, (size_t)count))
- return -EFAULT;
- if ((size_t)(offset + count) > (size_t)m_raw_inode.i_size)
- m_raw_inode.i_size = offset + count;
- set_metadata_dirty(true);
- return count;
- }
- }
- bool allow_cache = !description || !description->is_direct();
- const size_t block_size = fs().block_size();
- u64 old_size = size();
- u64 new_size = max(static_cast<u64>(offset) + count, (u64)size());
- auto resize_result = resize(new_size);
- if (resize_result.is_error())
- return resize_result;
- if (m_block_list.is_empty())
- m_block_list = compute_block_list();
- if (m_block_list.is_empty()) {
- dbgln("Ext2FSInode::write_bytes(): empty block list for inode {}", index());
- return -EIO;
- }
- size_t first_block_logical_index = offset / block_size;
- size_t last_block_logical_index = (offset + count) / block_size;
- if (last_block_logical_index >= m_block_list.size())
- last_block_logical_index = m_block_list.size() - 1;
- size_t offset_into_first_block = offset % block_size;
- ssize_t nwritten = 0;
- size_t remaining_count = min((off_t)count, (off_t)new_size - offset);
- dbgln_if(EXT2_VERY_DEBUG, "Ext2FS: Writing {} bytes, {} bytes into inode {} from {}", count, offset, index(), data.user_or_kernel_ptr());
- for (size_t bi = first_block_logical_index; remaining_count && bi <= last_block_logical_index; ++bi) {
- size_t offset_into_block = (bi == first_block_logical_index) ? offset_into_first_block : 0;
- size_t num_bytes_to_copy = min(block_size - offset_into_block, remaining_count);
- dbgln_if(EXT2_DEBUG, "Ext2FS: Writing block {} (offset_into_block: {})", m_block_list[bi], offset_into_block);
- result = fs().write_block(m_block_list[bi], data.offset(nwritten), num_bytes_to_copy, offset_into_block, allow_cache);
- if (result.is_error()) {
- dbgln("Ext2FS: write_block({}) failed (bi: {})", m_block_list[bi], bi);
- return result;
- }
- remaining_count -= num_bytes_to_copy;
- nwritten += num_bytes_to_copy;
- }
- dbgln_if(EXT2_VERY_DEBUG, "Ext2FS: After write, i_size={}, i_blocks={} ({} blocks in list)", m_raw_inode.i_size, m_raw_inode.i_blocks, m_block_list.size());
- if (old_size != new_size)
- inode_size_changed(old_size, new_size);
- inode_contents_changed(offset, count, data);
- return nwritten;
- }
- u8 Ext2FS::internal_file_type_to_directory_entry_type(const DirectoryEntryView& entry) const
- {
- switch (entry.file_type) {
- case EXT2_FT_REG_FILE:
- return DT_REG;
- case EXT2_FT_DIR:
- return DT_DIR;
- case EXT2_FT_CHRDEV:
- return DT_CHR;
- case EXT2_FT_BLKDEV:
- return DT_BLK;
- case EXT2_FT_FIFO:
- return DT_FIFO;
- case EXT2_FT_SOCK:
- return DT_SOCK;
- case EXT2_FT_SYMLINK:
- return DT_LNK;
- default:
- return DT_UNKNOWN;
- }
- }
- KResult Ext2FSInode::traverse_as_directory(Function<bool(const FS::DirectoryEntryView&)> callback) const
- {
- LOCKER(m_lock);
- VERIFY(is_directory());
- dbgln_if(EXT2_VERY_DEBUG, "Ext2FS: Traversing as directory: {}", index());
- auto buffer_or = read_entire();
- if (buffer_or.is_error())
- return buffer_or.error();
- auto& buffer = *buffer_or.value();
- auto* entry = reinterpret_cast<ext2_dir_entry_2*>(buffer.data());
- while (entry < buffer.end_pointer()) {
- if (entry->inode != 0) {
- dbgln_if(EXT2_DEBUG, "Ext2Inode::traverse_as_directory: {}, name_len: {}, rec_len: {}, file_type: {}, name: {}", entry->inode, entry->name_len, entry->rec_len, entry->file_type, StringView(entry->name, entry->name_len));
- if (!callback({ { entry->name, entry->name_len }, { fsid(), entry->inode }, entry->file_type }))
- break;
- }
- entry = (ext2_dir_entry_2*)((char*)entry + entry->rec_len);
- }
- return KSuccess;
- }
- KResult Ext2FSInode::write_directory(const Vector<Ext2FSDirectoryEntry>& entries)
- {
- LOCKER(m_lock);
- int directory_size = 0;
- for (auto& entry : entries)
- directory_size += EXT2_DIR_REC_LEN(entry.name.length());
- auto block_size = fs().block_size();
- int blocks_needed = ceil_div(static_cast<size_t>(directory_size), block_size);
- int occupied_size = blocks_needed * block_size;
- dbgln_if(EXT2_DEBUG, "Ext2FS: New directory inode {} contents to write (size {}, occupied {}):", index(), directory_size, occupied_size);
- auto directory_data = ByteBuffer::create_uninitialized(occupied_size);
- OutputMemoryStream stream { directory_data };
- for (size_t i = 0; i < entries.size(); ++i) {
- auto& entry = entries[i];
- int record_length = EXT2_DIR_REC_LEN(entry.name.length());
- if (i == entries.size() - 1)
- record_length += occupied_size - directory_size;
- dbgln_if(EXT2_DEBUG, "* Inode: {}, name_len: {}, rec_len: {}, file_type: {}, name: {}", entry.inode_index, u16(entry.name.length()), u16(record_length), u8(entry.file_type), entry.name);
- stream << u32(entry.inode_index.value());
- stream << u16(record_length);
- stream << u8(entry.name.length());
- stream << u8(entry.file_type);
- stream << entry.name.bytes();
- int padding = record_length - entry.name.length() - 8;
- for (int j = 0; j < padding; ++j)
- stream << u8(0);
- }
- stream.fill_to_end(0);
- auto buffer = UserOrKernelBuffer::for_kernel_buffer(stream.data());
- ssize_t nwritten = write_bytes(0, stream.size(), buffer, nullptr);
- if (nwritten < 0)
- return KResult((ErrnoCode)-nwritten);
- set_metadata_dirty(true);
- if (static_cast<size_t>(nwritten) != directory_data.size())
- return EIO;
- return KSuccess;
- }
- KResultOr<NonnullRefPtr<Inode>> Ext2FSInode::create_child(const String& name, mode_t mode, dev_t dev, uid_t uid, gid_t gid)
- {
- if (::is_directory(mode))
- return fs().create_directory(*this, name, mode, uid, gid);
- return fs().create_inode(*this, name, mode, dev, uid, gid);
- }
- KResult Ext2FSInode::add_child(Inode& child, const StringView& name, mode_t mode)
- {
- LOCKER(m_lock);
- VERIFY(is_directory());
- if (name.length() > EXT2_NAME_LEN)
- return ENAMETOOLONG;
- dbgln_if(EXT2_DEBUG, "Ext2FSInode::add_child: Adding inode {} with name '{}' and mode {:o} to directory {}", child.index(), name, mode, index());
- Vector<Ext2FSDirectoryEntry> entries;
- bool name_already_exists = false;
- KResult result = traverse_as_directory([&](auto& entry) {
- if (name == entry.name) {
- name_already_exists = true;
- return false;
- }
- entries.append({ entry.name, entry.inode.index(), entry.file_type });
- return true;
- });
- if (result.is_error())
- return result;
- if (name_already_exists) {
- dbgln("Ext2FSInode::add_child: Name '{}' already exists in inode {}", name, index());
- return EEXIST;
- }
- result = child.increment_link_count();
- if (result.is_error())
- return result;
- entries.empend(name, child.index(), to_ext2_file_type(mode));
- result = write_directory(entries);
- if (result.is_error())
- return result;
- m_lookup_cache.set(name, child.index());
- did_add_child(child.identifier());
- return KSuccess;
- }
- KResult Ext2FSInode::remove_child(const StringView& name)
- {
- LOCKER(m_lock);
- dbgln_if(EXT2_DEBUG, "Ext2FSInode::remove_child('{}') in inode {}", name, index());
- VERIFY(is_directory());
- auto it = m_lookup_cache.find(name);
- if (it == m_lookup_cache.end())
- return ENOENT;
- auto child_inode_index = (*it).value;
- InodeIdentifier child_id { fsid(), child_inode_index };
- dbgln_if(EXT2_DEBUG, "Ext2FSInode::remove_child(): Removing '{}' in directory {}", name, index());
- Vector<Ext2FSDirectoryEntry> entries;
- KResult result = traverse_as_directory([&](auto& entry) {
- if (name != entry.name)
- entries.append({ entry.name, entry.inode.index(), entry.file_type });
- return true;
- });
- if (result.is_error())
- return result;
- result = write_directory(entries);
- if (result.is_error())
- return result;
- m_lookup_cache.remove(name);
- auto child_inode = fs().get_inode(child_id);
- result = child_inode->decrement_link_count();
- if (result.is_error())
- return result;
- did_remove_child(child_id);
- return KSuccess;
- }
- unsigned Ext2FS::inodes_per_block() const
- {
- return EXT2_INODES_PER_BLOCK(&super_block());
- }
- unsigned Ext2FS::inodes_per_group() const
- {
- return EXT2_INODES_PER_GROUP(&super_block());
- }
- unsigned Ext2FS::inode_size() const
- {
- return EXT2_INODE_SIZE(&super_block());
- }
- unsigned Ext2FS::blocks_per_group() const
- {
- return EXT2_BLOCKS_PER_GROUP(&super_block());
- }
- bool Ext2FS::write_ext2_inode(InodeIndex inode, const ext2_inode& e2inode)
- {
- BlockIndex block_index;
- unsigned offset;
- if (!find_block_containing_inode(inode, block_index, offset))
- return false;
- auto buffer = UserOrKernelBuffer::for_kernel_buffer(const_cast<u8*>((const u8*)&e2inode));
- return write_block(block_index, buffer, inode_size(), offset) >= 0;
- }
- auto Ext2FS::allocate_blocks(GroupIndex preferred_group_index, size_t count) -> KResultOr<Vector<BlockIndex>>
- {
- LOCKER(m_lock);
- dbgln_if(EXT2_DEBUG, "Ext2FS: allocate_blocks(preferred group: {}, count {})", preferred_group_index, count);
- if (count == 0)
- return Vector<BlockIndex> {};
- Vector<BlockIndex> blocks;
- dbgln_if(EXT2_DEBUG, "Ext2FS: allocate_blocks:");
- blocks.ensure_capacity(count);
- auto group_index = preferred_group_index;
- if (!group_descriptor(preferred_group_index).bg_free_blocks_count) {
- group_index = 1;
- }
- while (blocks.size() < count) {
- bool found_a_group = false;
- if (group_descriptor(group_index).bg_free_blocks_count) {
- found_a_group = true;
- } else {
- if (group_index == preferred_group_index)
- group_index = 1;
- for (; group_index <= m_block_group_count; group_index = GroupIndex { group_index.value() + 1 }) {
- if (group_descriptor(group_index).bg_free_blocks_count) {
- found_a_group = true;
- break;
- }
- }
- }
- VERIFY(found_a_group);
- auto& bgd = group_descriptor(group_index);
- auto cached_bitmap_or_error = get_bitmap_block(bgd.bg_block_bitmap);
- if (cached_bitmap_or_error.is_error())
- return cached_bitmap_or_error.error();
- auto& cached_bitmap = *cached_bitmap_or_error.value();
- int blocks_in_group = min(blocks_per_group(), super_block().s_blocks_count);
- auto block_bitmap = cached_bitmap.bitmap(blocks_in_group);
- BlockIndex first_block_in_group = (group_index.value() - 1) * blocks_per_group() + first_block_index().value();
- size_t free_region_size = 0;
- auto first_unset_bit_index = block_bitmap.find_longest_range_of_unset_bits(count - blocks.size(), free_region_size);
- VERIFY(first_unset_bit_index.has_value());
- dbgln_if(EXT2_DEBUG, "Ext2FS: allocating free region of size: {} [{}]", free_region_size, group_index);
- for (size_t i = 0; i < free_region_size; ++i) {
- BlockIndex block_index = (first_unset_bit_index.value() + i) + first_block_in_group.value();
- auto result = set_block_allocation_state(block_index, true);
- if (result.is_error()) {
- dbgln("Ext2FS: Failed to allocate block {} in allocate_blocks()", block_index);
- return result;
- }
- blocks.unchecked_append(block_index);
- dbgln_if(EXT2_DEBUG, " allocated > {}", block_index);
- }
- }
- VERIFY(blocks.size() == count);
- return blocks;
- }
- KResultOr<InodeIndex> Ext2FS::allocate_inode(GroupIndex preferred_group)
- {
- dbgln_if(EXT2_DEBUG, "Ext2FS: allocate_inode(preferred_group: {})", preferred_group);
- LOCKER(m_lock);
- // FIXME: We shouldn't refuse to allocate an inode if there is no group that can house the whole thing.
- // In those cases we should just spread it across multiple groups.
- auto is_suitable_group = [this](auto group_index) {
- auto& bgd = group_descriptor(group_index);
- return bgd.bg_free_inodes_count && bgd.bg_free_blocks_count >= 1;
- };
- GroupIndex group_index;
- if (preferred_group.value() && is_suitable_group(preferred_group)) {
- group_index = preferred_group;
- } else {
- for (unsigned i = 1; i <= m_block_group_count; ++i) {
- if (is_suitable_group(i)) {
- group_index = i;
- break;
- }
- }
- }
- if (!group_index) {
- dmesgln("Ext2FS: allocate_inode: no suitable group found for new inode");
- return ENOSPC;
- }
- dbgln_if(EXT2_DEBUG, "Ext2FS: allocate_inode: found suitable group [{}] for new inode :^)", group_index);
- auto& bgd = group_descriptor(group_index);
- unsigned inodes_in_group = min(inodes_per_group(), super_block().s_inodes_count);
- InodeIndex first_inode_in_group = (group_index.value() - 1) * inodes_per_group() + 1;
- auto cached_bitmap_or_error = get_bitmap_block(bgd.bg_inode_bitmap);
- if (cached_bitmap_or_error.is_error())
- return cached_bitmap_or_error.error();
- auto& cached_bitmap = *cached_bitmap_or_error.value();
- auto inode_bitmap = cached_bitmap.bitmap(inodes_in_group);
- for (size_t i = 0; i < inode_bitmap.size(); ++i) {
- if (inode_bitmap.get(i))
- continue;
- inode_bitmap.set(i, true);
- auto inode_index = InodeIndex(first_inode_in_group.value() + i);
- cached_bitmap.dirty = true;
- m_super_block.s_free_inodes_count--;
- m_super_block_dirty = true;
- const_cast<ext2_group_desc&>(bgd).bg_free_inodes_count--;
- m_block_group_descriptors_dirty = true;
- // In case the inode cache had this cached as "non-existent", uncache that info.
- m_inode_cache.remove(inode_index.value());
- return inode_index;
- }
- dmesgln("Ext2FS: allocate_inode found no available inode, despite bgd claiming there are inodes :(");
- return EIO;
- }
- Ext2FS::GroupIndex Ext2FS::group_index_from_block_index(BlockIndex block_index) const
- {
- if (!block_index)
- return 0;
- return (block_index.value() - 1) / blocks_per_group() + 1;
- }
- auto Ext2FS::group_index_from_inode(InodeIndex inode) const -> GroupIndex
- {
- if (!inode)
- return 0;
- return (inode.value() - 1) / inodes_per_group() + 1;
- }
- KResultOr<bool> Ext2FS::get_inode_allocation_state(InodeIndex index) const
- {
- LOCKER(m_lock);
- if (index == 0)
- return EINVAL;
- auto group_index = group_index_from_inode(index);
- auto& bgd = group_descriptor(group_index);
- unsigned index_in_group = index.value() - ((group_index.value() - 1) * inodes_per_group());
- unsigned bit_index = (index_in_group - 1) % inodes_per_group();
- auto cached_bitmap_or_error = const_cast<Ext2FS&>(*this).get_bitmap_block(bgd.bg_inode_bitmap);
- if (cached_bitmap_or_error.is_error())
- return cached_bitmap_or_error.error();
- return cached_bitmap_or_error.value()->bitmap(inodes_per_group()).get(bit_index);
- }
- KResult Ext2FS::update_bitmap_block(BlockIndex bitmap_block, size_t bit_index, bool new_state, u32& super_block_counter, u16& group_descriptor_counter)
- {
- auto cached_bitmap_or_error = get_bitmap_block(bitmap_block);
- if (cached_bitmap_or_error.is_error())
- return cached_bitmap_or_error.error();
- auto& cached_bitmap = *cached_bitmap_or_error.value();
- bool current_state = cached_bitmap.bitmap(blocks_per_group()).get(bit_index);
- if (current_state == new_state) {
- dbgln("Ext2FS: Bit {} in bitmap block {} had unexpected state {}", bit_index, bitmap_block, current_state);
- return EIO;
- }
- cached_bitmap.bitmap(blocks_per_group()).set(bit_index, new_state);
- cached_bitmap.dirty = true;
- if (new_state) {
- --super_block_counter;
- --group_descriptor_counter;
- } else {
- ++super_block_counter;
- ++group_descriptor_counter;
- }
- m_super_block_dirty = true;
- m_block_group_descriptors_dirty = true;
- return KSuccess;
- }
- KResult Ext2FS::set_inode_allocation_state(InodeIndex inode_index, bool new_state)
- {
- LOCKER(m_lock);
- auto group_index = group_index_from_inode(inode_index);
- unsigned index_in_group = inode_index.value() - ((group_index.value() - 1) * inodes_per_group());
- unsigned bit_index = (index_in_group - 1) % inodes_per_group();
- dbgln_if(EXT2_DEBUG, "Ext2FS: set_inode_allocation_state: Inode {} -> {}", inode_index, new_state);
- auto& bgd = const_cast<ext2_group_desc&>(group_descriptor(group_index));
- return update_bitmap_block(bgd.bg_inode_bitmap, bit_index, new_state, m_super_block.s_free_inodes_count, bgd.bg_free_inodes_count);
- }
- Ext2FS::BlockIndex Ext2FS::first_block_index() const
- {
- return block_size() == 1024 ? 1 : 0;
- }
- KResultOr<Ext2FS::CachedBitmap*> Ext2FS::get_bitmap_block(BlockIndex bitmap_block_index)
- {
- for (auto& cached_bitmap : m_cached_bitmaps) {
- if (cached_bitmap->bitmap_block_index == bitmap_block_index)
- return cached_bitmap;
- }
- auto block = KBuffer::create_with_size(block_size(), Region::Access::Read | Region::Access::Write, "Ext2FS: Cached bitmap block");
- auto buffer = UserOrKernelBuffer::for_kernel_buffer(block.data());
- auto result = read_block(bitmap_block_index, &buffer, block_size());
- if (result.is_error()) {
- dbgln("Ext2FS: Failed to load bitmap block {}", bitmap_block_index);
- return result;
- }
- m_cached_bitmaps.append(make<CachedBitmap>(bitmap_block_index, move(block)));
- return m_cached_bitmaps.last();
- }
- KResult Ext2FS::set_block_allocation_state(BlockIndex block_index, bool new_state)
- {
- VERIFY(block_index != 0);
- LOCKER(m_lock);
- auto group_index = group_index_from_block_index(block_index);
- unsigned index_in_group = (block_index.value() - first_block_index().value()) - ((group_index.value() - 1) * blocks_per_group());
- unsigned bit_index = index_in_group % blocks_per_group();
- auto& bgd = const_cast<ext2_group_desc&>(group_descriptor(group_index));
- dbgln_if(EXT2_DEBUG, "Ext2FS: Block {} state -> {} (in bitmap block {})", block_index, new_state, bgd.bg_block_bitmap);
- return update_bitmap_block(bgd.bg_block_bitmap, bit_index, new_state, m_super_block.s_free_blocks_count, bgd.bg_free_blocks_count);
- }
- KResult Ext2FS::create_directory(Ext2FSInode& parent_inode, const String& name, mode_t mode, uid_t uid, gid_t gid)
- {
- LOCKER(m_lock);
- VERIFY(is_directory(mode));
- auto inode_or_error = create_inode(parent_inode, name, mode, 0, uid, gid);
- if (inode_or_error.is_error())
- return inode_or_error.error();
- auto& inode = inode_or_error.value();
- dbgln_if(EXT2_DEBUG, "Ext2FS: create_directory: created new directory named '{} with inode {}", name, inode->index());
- Vector<Ext2FSDirectoryEntry> entries;
- entries.empend(".", inode->index(), static_cast<u8>(EXT2_FT_DIR));
- entries.empend("..", parent_inode.index(), static_cast<u8>(EXT2_FT_DIR));
- auto result = static_cast<Ext2FSInode&>(*inode).write_directory(entries);
- if (result.is_error())
- return result;
- result = parent_inode.increment_link_count();
- if (result.is_error())
- return result;
- auto& bgd = const_cast<ext2_group_desc&>(group_descriptor(group_index_from_inode(inode->identifier().index())));
- ++bgd.bg_used_dirs_count;
- m_block_group_descriptors_dirty = true;
- return KSuccess;
- }
- KResultOr<NonnullRefPtr<Inode>> Ext2FS::create_inode(Ext2FSInode& parent_inode, const String& name, mode_t mode, dev_t dev, uid_t uid, gid_t gid)
- {
- if (name.length() > EXT2_NAME_LEN)
- return ENAMETOOLONG;
- if (parent_inode.m_raw_inode.i_links_count == 0)
- return ENOENT;
- ext2_inode e2inode {};
- auto now = kgettimeofday().to_truncated_seconds();
- e2inode.i_mode = mode;
- e2inode.i_uid = uid;
- e2inode.i_gid = gid;
- e2inode.i_size = 0;
- e2inode.i_atime = now;
- e2inode.i_ctime = now;
- e2inode.i_mtime = now;
- e2inode.i_dtime = 0;
- e2inode.i_flags = 0;
- // For directories, add +1 link count for the "." entry in self.
- e2inode.i_links_count = is_directory(mode);
- if (is_character_device(mode))
- e2inode.i_block[0] = dev;
- else if (is_block_device(mode))
- e2inode.i_block[1] = dev;
- auto inode_id = allocate_inode();
- if (inode_id.is_error())
- return inode_id.error();
- dbgln_if(EXT2_DEBUG, "Ext2FS: writing initial metadata for inode {}", inode_id.value());
- auto success = write_ext2_inode(inode_id.value(), e2inode);
- VERIFY(success);
- auto new_inode = get_inode({ fsid(), inode_id.value() });
- VERIFY(new_inode);
- dbgln_if(EXT2_DEBUG, "Ext2FS: Adding inode '{}' (mode {:o}) to parent directory {}", name, mode, parent_inode.index());
- auto result = parent_inode.add_child(*new_inode, name, mode);
- if (result.is_error())
- return result;
- return new_inode.release_nonnull();
- }
- bool Ext2FSInode::populate_lookup_cache() const
- {
- LOCKER(m_lock);
- if (!m_lookup_cache.is_empty())
- return true;
- HashMap<String, InodeIndex> children;
- KResult result = traverse_as_directory([&children](auto& entry) {
- children.set(entry.name, entry.inode.index());
- return true;
- });
- if (!result.is_success())
- return false;
- if (!m_lookup_cache.is_empty())
- return false;
- m_lookup_cache = move(children);
- return true;
- }
- RefPtr<Inode> Ext2FSInode::lookup(StringView name)
- {
- VERIFY(is_directory());
- if (!populate_lookup_cache())
- return {};
- LOCKER(m_lock);
- auto it = m_lookup_cache.find(name.hash(), [&](auto& entry) { return entry.key == name; });
- if (it != m_lookup_cache.end())
- return fs().get_inode({ fsid(), (*it).value });
- return {};
- }
- void Ext2FSInode::one_ref_left()
- {
- // FIXME: I would like to not live forever, but uncached Ext2FS is fucking painful right now.
- }
- int Ext2FSInode::set_atime(time_t t)
- {
- LOCKER(m_lock);
- if (fs().is_readonly())
- return -EROFS;
- m_raw_inode.i_atime = t;
- set_metadata_dirty(true);
- return 0;
- }
- int Ext2FSInode::set_ctime(time_t t)
- {
- LOCKER(m_lock);
- if (fs().is_readonly())
- return -EROFS;
- m_raw_inode.i_ctime = t;
- set_metadata_dirty(true);
- return 0;
- }
- int Ext2FSInode::set_mtime(time_t t)
- {
- LOCKER(m_lock);
- if (fs().is_readonly())
- return -EROFS;
- m_raw_inode.i_mtime = t;
- set_metadata_dirty(true);
- return 0;
- }
- KResult Ext2FSInode::increment_link_count()
- {
- LOCKER(m_lock);
- if (fs().is_readonly())
- return EROFS;
- if (m_raw_inode.i_links_count == max_link_count)
- return EMLINK;
- ++m_raw_inode.i_links_count;
- set_metadata_dirty(true);
- return KSuccess;
- }
- KResult Ext2FSInode::decrement_link_count()
- {
- LOCKER(m_lock);
- if (fs().is_readonly())
- return EROFS;
- VERIFY(m_raw_inode.i_links_count);
- --m_raw_inode.i_links_count;
- if (ref_count() == 1 && m_raw_inode.i_links_count == 0)
- fs().uncache_inode(index());
- set_metadata_dirty(true);
- return KSuccess;
- }
- void Ext2FS::uncache_inode(InodeIndex index)
- {
- LOCKER(m_lock);
- m_inode_cache.remove(index);
- }
- KResultOr<size_t> Ext2FSInode::directory_entry_count() const
- {
- VERIFY(is_directory());
- LOCKER(m_lock);
- populate_lookup_cache();
- return m_lookup_cache.size();
- }
- KResult Ext2FSInode::chmod(mode_t mode)
- {
- LOCKER(m_lock);
- if (m_raw_inode.i_mode == mode)
- return KSuccess;
- m_raw_inode.i_mode = mode;
- set_metadata_dirty(true);
- return KSuccess;
- }
- KResult Ext2FSInode::chown(uid_t uid, gid_t gid)
- {
- LOCKER(m_lock);
- if (m_raw_inode.i_uid == uid && m_raw_inode.i_gid == gid)
- return KSuccess;
- m_raw_inode.i_uid = uid;
- m_raw_inode.i_gid = gid;
- set_metadata_dirty(true);
- return KSuccess;
- }
- KResult Ext2FSInode::truncate(u64 size)
- {
- LOCKER(m_lock);
- if (static_cast<u64>(m_raw_inode.i_size) == size)
- return KSuccess;
- auto result = resize(size);
- if (result.is_error())
- return result;
- set_metadata_dirty(true);
- return KSuccess;
- }
- KResultOr<int> Ext2FSInode::get_block_address(int index)
- {
- LOCKER(m_lock);
- if (m_block_list.is_empty())
- m_block_list = compute_block_list();
- if (index < 0 || (size_t)index >= m_block_list.size())
- return 0;
- return m_block_list[index].value();
- }
- unsigned Ext2FS::total_block_count() const
- {
- LOCKER(m_lock);
- return super_block().s_blocks_count;
- }
- unsigned Ext2FS::free_block_count() const
- {
- LOCKER(m_lock);
- return super_block().s_free_blocks_count;
- }
- unsigned Ext2FS::total_inode_count() const
- {
- LOCKER(m_lock);
- return super_block().s_inodes_count;
- }
- unsigned Ext2FS::free_inode_count() const
- {
- LOCKER(m_lock);
- return super_block().s_free_inodes_count;
- }
- KResult Ext2FS::prepare_to_unmount() const
- {
- LOCKER(m_lock);
- for (auto& it : m_inode_cache) {
- if (it.value->ref_count() > 1)
- return EBUSY;
- }
- m_inode_cache.clear();
- return KSuccess;
- }
- }
|