Max OS 0.3
Loading...
Searching...
No Matches
MaxOS::filesystem::format::ext2::InodeHandler Class Reference

Simplfies the management of an inode & its blocks. More...

#include <ext2.h>

Public Member Functions

 InodeHandler (Ext2Volume *volume, uint32_t inode)
 Construct a new Inode Handler object. Reads the inode and caches the block pointers.
 
size_t size () const
 read the size upper and lower into a single size_t
 
void set_size (size_t size)
 store the size of the inode data. (does not write to disk)
 
size_t grow (size_t amount, bool flush=true)
 Increase the size of the inode's storage capacity by allocating new blocks.
 
void save ()
 Writes the inode meta data to disk.
 
void free ()
 Marks this inode's blocks as free and then the inode as free.
 

Public Attributes

uint32_t inode_number
 The index of the inode.
 
inode_t inode
 The inode metadata for this file/directory.
 
common::Vector< uint32_t > block_cache
 All the blocks used by this inode.
 

Detailed Description

Simplfies the management of an inode & its blocks.

Definition at line 372 of file ext2.h.

Constructor & Destructor Documentation

◆ InodeHandler()

InodeHandler::InodeHandler ( Ext2Volume volume,
uint32_t  inode_index 
)

Construct a new Inode Handler object. Reads the inode and caches the block pointers.

Parameters
volumeThe volume the inode belongs to
inode_indexThe inode index

Definition at line 509 of file ext2.cpp.

510 : m_volume(volume),
512 inode(m_volume->read_inode(inode_number)) {
513
514 // Read the block pointers
516 if(inode.block_pointers[direct_pointer])
517 block_cache.push_back(inode.block_pointers[direct_pointer]);
518
519 buffer_t buffer(m_volume->block_size);
520 parse_indirect(1, inode.l1_indirect, &buffer);
521 parse_indirect(2, inode.l2_indirect, &buffer);
522 parse_indirect(3, inode.l3_indirect, &buffer);
523}
Wrapper class for a region of bytes in memor in an attempt to add some memory safety....
Definition buffer.h:25
Stores the left, top, width and height of a rectangle.
Definition rectangle.h:22
iterator push_back(Type)
Adds an element to the end of the vector and returns the iterator of the element.
Definition vector.h:333
size_t block_size
How large each block is (in bytes)
Definition ext2.h:342
inode_t read_inode(uint32_t inode_num) const
read an inode from the filesystem
Definition ext2.cpp:149
uint32_t inode_number
The index of the inode.
Definition ext2.h:385
inode_t inode
The inode metadata for this file/directory.
Definition ext2.h:386
common::Vector< uint32_t > block_cache
All the blocks used by this inode.
Definition ext2.h:387

References block_cache, MaxOS::filesystem::format::ext2::Ext2Volume::block_size, inode, and MaxOS::common::Vector< Type >::push_back().

Member Function Documentation

◆ free()

void InodeHandler::free ( )

Marks this inode's blocks as free and then the inode as free.

Definition at line 705 of file ext2.cpp.

705 {
706
707 m_volume->ext2_lock.lock();
708
709 // Free the inode
710 m_volume->free_blocks(block_cache);
711 m_volume->free_inode(inode_number);
712
713 m_volume->ext2_lock.unlock();
714}
void lock()
Lock the spinlock once it is available.
Definition spinlock.cpp:24
void unlock()
Unlock the spinlock.
Definition spinlock.cpp:32
common::Spinlock ext2_lock
Lock for synchronised access to the volume (.
Definition ext2.h:350
void free_inode(uint32_t inode)
Mark an inode as free. Note: does NOT unallocated the inodes blocks, use free_group_blocks().
Definition ext2.cpp:434
void free_blocks(const common::Vector< uint32_t > &blocks)
Frees a group of blocks. Preferably with adjacent blocks apearing next to each other but not enforced...
Definition ext2.cpp:464

References block_cache, MaxOS::filesystem::format::ext2::Ext2Volume::ext2_lock, MaxOS::filesystem::format::ext2::Ext2Volume::free_blocks(), MaxOS::filesystem::format::ext2::Ext2Volume::free_inode(), inode_number, MaxOS::common::Spinlock::lock(), and MaxOS::common::Spinlock::unlock().

◆ grow()

size_t InodeHandler::grow ( size_t  amount,
bool  flush = true 
)

Increase the size of the inode's storage capacity by allocating new blocks.

Parameters
amountThe amount to grow to in bytes
flushSave the newly allocated chunks to disk, defaults to true
Returns
The new toatal size of the file (size() + amount)
Todo:
Should this return size() instead?

Definition at line 675 of file ext2.cpp.

675 {
676
677 // Nothing to grow
678 if(amount <= 0)
679 return size();
680
681 // Allocate new blocks
682 auto blocks = m_volume->allocate_blocks(m_volume->bytes_to_blocks(amount));
683 ASSERT(blocks[0] != 0, "Failed to allocate new blocks for file");
684
685 // Save the changes
686 store_blocks(blocks);
687 set_size(size() + amount);
688 if(flush)
689 save();
690
691 return size() + amount;
692}
common::Vector< uint32_t > allocate_blocks(uint32_t amount)
Allocates a set of blocks to be used by an inode.
Definition ext2.cpp:188
uint32_t bytes_to_blocks(size_t bytes) const
How many blocks are needed to contain a set amount of bytes.
Definition ext2.cpp:360
size_t size() const
read the size upper and lower into a single size_t
Definition ext2.cpp:529
void set_size(size_t size)
store the size of the inode data. (does not write to disk)
Definition ext2.cpp:537
void save()
Writes the inode meta data to disk.
Definition ext2.cpp:697
#define ASSERT(condition, format,...)
If the specified condition is not met then the kernel will crash with the specified message.
Definition logger.h:100

References MaxOS::filesystem::format::ext2::Ext2Volume::allocate_blocks(), ASSERT, MaxOS::filesystem::format::ext2::Ext2Volume::bytes_to_blocks(), save(), set_size(), and size().

Referenced by MaxOS::filesystem::format::ext2::Ext2File::write().

◆ save()

void InodeHandler::save ( )

Writes the inode meta data to disk.

Definition at line 697 of file ext2.cpp.

697 {
698
699 m_volume->write_inode(inode_number, &inode);
700}
void write_inode(uint32_t inode_num, inode_t *inode) const
write an inode to the filesystem
Definition ext2.cpp:102

References inode, inode_number, and MaxOS::filesystem::format::ext2::Ext2Volume::write_inode().

Referenced by grow(), and MaxOS::filesystem::format::ext2::Ext2File::write().

◆ set_size()

void InodeHandler::set_size ( size_t  size)

store the size of the inode data. (does not write to disk)

Parameters
sizeThe new size

Definition at line 537 of file ext2.cpp.

537 {
538
539 inode.size_lower = (uint32_t) (size & 0xFFFFFFFFULL);
540 inode.size_upper = (uint32_t) (size >> 32);
541
542}

References inode, and size().

Referenced by grow().

◆ size()

size_t InodeHandler::size ( ) const

read the size upper and lower into a single size_t

Returns
The size of the inode data (not the inode itself)

Definition at line 529 of file ext2.cpp.

529 {
530 return ((size_t) inode.size_upper << 32) | (size_t) inode.size_lower;
531}

References inode.

Referenced by MaxOS::filesystem::format::ext2::Ext2File::Ext2File(), grow(), and set_size().

Member Data Documentation

◆ block_cache

common::Vector<uint32_t> MaxOS::filesystem::format::ext2::InodeHandler::block_cache

◆ inode

inode_t MaxOS::filesystem::format::ext2::InodeHandler::inode

The inode metadata for this file/directory.

Definition at line 386 of file ext2.h.

Referenced by InodeHandler(), save(), set_size(), size(), and MaxOS::filesystem::format::ext2::Ext2File::write().

◆ inode_number

uint32_t MaxOS::filesystem::format::ext2::InodeHandler::inode_number

The index of the inode.

Definition at line 385 of file ext2.h.

Referenced by MaxOS::filesystem::format::ext2::Ext2Directory::create_subdirectory(), free(), and save().


The documentation for this class was generated from the following files: