63#define SLOT_SINGLY EXT2_DIRECT_POINTERS
75 size_t p = desc->
bs / 4;
85 for (
size_t depth = 1, n = p; depth <= 3; ++depth, n *= p) {
129 span *= desc->
bs / 4;
148 int depth =
block_tree(desc, idx, &slot, &rel), err = 0;
155 if (depth && block) {
161 for (; depth && block; --depth) {
167 block =
table[rel / span];
211 if (!(*block = tmp[idx])) {
240 int depth =
block_tree(desc, idx, &slot, &rel), err = 0;
250 for (; !err && depth; --depth) {
260 if (inode->
blocks != nblocks) {
262 err = err ? err : werr;
283 vfs_log(
LOG_DEBUG,
"ext2_inode_alloc(desc=%p)\n", desc);
293 for (
unsigned i = 0; i < desc->
ngroups; ++i) {
304 for (idx = 0; idx < bm.
max_idx && bitmap_check(&bm, idx); ++idx);
318 bitmap_set(&bm, idx);
int ext2_block_write(struct ext2 *desc, uint32_t number, void *buf)
Writes a block to the disk.
int ext2_io(struct ext2 *desc, int write, off_t off, size_t size, void *buf)
Reads or writes bytes of the filesystem's disk, all of them.
int ext2_bgd_table_rewrite(struct ext2 *desc)
Writes the block group descriptor table back to the disk, after a change to a group's counts.
int ext2_block_read(struct ext2 *desc, uint32_t number, void *buf)
Reads a block from the disk.
int ext2_block_alloc(struct ext2 *desc, uint32_t *block)
Allocates a free block: its bit is set in its group's block bitmap, the free counts go down,...
static void slot_set(struct ext2_inode *inode, int slot, uint32_t block)
Sets the block pointer in a slot of an inode.
static int ext2_indirect_block_get(struct ext2 *desc, struct ext2_inode *inode, uint32_t table, size_t idx, uint32_t *block)
Gets the block pointer at index idx of the indirect block table, allocating a block there if there's ...
static int ext2_inode_block_alloc(struct ext2 *desc, struct ext2_inode *inode, uint32_t *block)
Allocates a block for a file, and counts it in the inode's blocks, which counts 512-byte sectors.
static size_t tree_span(struct ext2 *desc, int depth)
Returns how many data blocks are under one pointer of an indirect block that is depth levels above th...
static off_t ext2_inode_off(struct ext2 *desc, ino_t ino)
Returns where an inode is on the disk, in bytes, or -EINVAL for an inode number that doesn't exist.
static int block_tree(struct ext2 *desc, size_t idx, int *slot, size_t *rel)
Finds where block idx of a file is in its tree: the slot of the inode it hangs from (0 to 11 for the ...
static uint32_t slot_get(struct ext2_inode *inode, int slot)
Returns the block pointer in a slot of an inode.
int ext2_inode_alloc(struct ext2 *desc, ino_t *ino)
Allocates a free inode: its bit is set in its group's inode bitmap, and the free counts go down.
#define EXT2_DIRECT_POINTERS
int ext2_inode_read(struct ext2 *desc, ino_t inode, struct ext2_inode *ref)
Reads an inode from the disk.
int ext2_inode_block_read(struct ext2 *desc, struct ext2_inode *inode, size_t idx, void *buf)
Reads a block of a file.
int ext2_inode_write(struct ext2 *desc, ino_t inode, struct ext2_inode *i)
Writes an inode to the disk.
int ext2_superblock_rewrite(struct ext2 *desc)
Writes the in-memory copy of the superblock back to the disk, after a change to its counts of free bl...
int ext2_inode_block_write(struct ext2 *desc, struct ext2_inode *inode, uint32_t inode_nr, size_t idx, void *buf)
Writes a block of a file, allocating it and the indirect blocks above it if they don't exist yet.
void * kmalloc(size_t size, struct malloc_type *type, int flags)
Allocates memory for one of the kernel's structures.
void kfree(void *)
Frees an object that kmalloc() gave.
void * memset(void *dst, int c, size_t n)
Sets n bytes to c.
static uint64_t * table(paddr_t paddr)
Returns a page table, through the direct map.
A bitmap: its blocks, and its last bit's index.
A block group descriptor, as it is on the disk.
uint16_t free_inodes
number of unallocated inodes
uint32_t imap
inodes bitmap
uint32_t inodes
inode table
An inode, as it is on the disk.
uint32_t doubly_indirect_pointer
uint32_t direct_pointer[12]
uint32_t triply_indirect_pointer
uint32_t singly_indirect_pointer
uint32_t ginodes
number of inodes per block group
uint32_t free_inodes
number of unallocated inodes
uint32_t ninodes
number of inodes
A mounted ext2 filesystem.
struct ext2_superblock * superblock
struct ext2_group * groups
size_t ngroups
number of block group descriptors
size_t inode_size
an inode's size on the disk (revision 0's, or the superblock's)