AquilaOS documentation
How it works, and its code
Loading...
Searching...
No Matches
inode.c File Reference

ext2: inodes, and the blocks of a file. More...

#include <ext2.h>
#include <ds/bitmap.h>

Go to the source code of this file.

Macros

#define SLOT_SINGLY   EXT2_DIRECT_POINTERS

Functions

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.
int ext2_inode_read (struct ext2 *desc, ino_t ino, struct ext2_inode *ref)
 Reads an inode from the disk.
int ext2_inode_write (struct ext2 *desc, ino_t ino, struct ext2_inode *ext2_inode)
 Writes an inode to the disk.
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 direct pointers, 12 to 14 for the indirect ones) in *slot, and its index under that slot in *rel.
static uint32_t slot_get (struct ext2_inode *inode, int slot)
 Returns the block pointer in a slot of an inode.
static void slot_set (struct ext2_inode *inode, int slot, uint32_t block)
 Sets the block pointer in a slot of an inode.
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 them.
int ext2_inode_block_read (struct ext2 *desc, struct ext2_inode *inode, size_t idx, void *buf)
 Reads a block of a file.
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 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 none yet.
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.
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.

Detailed Description

ext2: inodes, and the blocks of a file.

A file's blocks are found through its inode's 12 direct pointers, then a singly, a doubly and a triply indirect one: a tree of blocks of pointers.

Definition in file inode.c.

Macro Definition Documentation

◆ SLOT_SINGLY

#define SLOT_SINGLY   EXT2_DIRECT_POINTERS

Definition at line 63 of file inode.c.

Referenced by block_tree(), slot_get(), and slot_set().

Function Documentation

◆ block_tree()

int block_tree ( struct ext2 * desc,
size_t idx,
int * slot,
size_t * rel )
static

Finds where block idx of a file is in its tree: the slot of the inode it hangs from (0 to 11 for the direct pointers, 12 to 14 for the indirect ones) in *slot, and its index under that slot in *rel.

Returns the depth: 0 if the slot points to the block itself, 1 to 3 for the levels of indirect blocks above it, or -EFBIG past the largest file.

Definition at line 73 of file inode.c.

References block_tree(), ext2::bs, EXT2_DIRECT_POINTERS, and SLOT_SINGLY.

Referenced by block_tree(), ext2_inode_block_read(), and ext2_inode_block_write().

◆ ext2_indirect_block_get()

int ext2_indirect_block_get ( struct ext2 * desc,
struct ext2_inode * inode,
uint32_t table,
size_t idx,
uint32_t * block )
static

Gets the block pointer at index idx of the indirect block table, allocating a block there if there's none yet.

Definition at line 200 of file inode.c.

References ext2::bs, ext2_block_read(), ext2_block_write(), ext2_indirect_block_get(), ext2_inode_block_alloc(), kfree(), kmalloc(), and table().

Referenced by ext2_indirect_block_get(), and ext2_inode_block_write().

◆ ext2_inode_alloc()

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.

A bitmap that couldn't be read isn't written back.

Parameters
descthe mounted filesystem
inoset to the inode number
Returns
0, -ENOSPC if there's no free inode, -ENOMEM, or an I/O error

Definition at line 281 of file inode.c.

References ext2::bs, ext2_bgd_table_rewrite(), ext2_block_read(), ext2_block_write(), ext2_inode_alloc(), ext2_superblock_rewrite(), ext2_group::free_inodes, ext2_superblock::free_inodes, ext2_superblock::ginodes, ext2::groups, ext2_group::imap, kfree(), kmalloc(), LOG_DEBUG, bitmap::map, bitmap::max_idx, ext2::ngroups, and ext2::superblock.

Referenced by ext2_inode_alloc(), and ext2_vmknod().

◆ ext2_inode_block_alloc()

int ext2_inode_block_alloc ( struct ext2 * desc,
struct ext2_inode * inode,
uint32_t * block )
static

Allocates a block for a file, and counts it in the inode's blocks, which counts 512-byte sectors.

Definition at line 187 of file inode.c.

References ext2_inode::blocks, ext2::bs, ext2_block_alloc(), and ext2_inode_block_alloc().

Referenced by ext2_indirect_block_get(), ext2_inode_block_alloc(), and ext2_inode_block_write().

◆ ext2_inode_block_read()

int ext2_inode_block_read ( struct ext2 * desc,
struct ext2_inode * inode,
size_t idx,
void * buf )

Reads a block of a file.

A block that was never written (the file is sparse there) reads as zeros.

Parameters
descthe mounted filesystem
inodethe file's inode
idxthe block's index in the file
bufwhere it goes, a block's size
Returns
0, -EFBIG past the largest file, -ENOMEM, or an I/O error

Definition at line 144 of file inode.c.

References block_tree(), ext2::bs, ext2_block_read(), ext2_inode_block_read(), kfree(), kmalloc(), memset(), slot_get(), table(), and tree_span().

Referenced by dentry_update(), ext2_dentry_create(), ext2_dentry_find(), ext2_inode_block_read(), ext2_read(), ext2_readdir(), ext2_trunc(), and ext2_write().

◆ ext2_inode_block_write()

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.

The inode is written back if blocks were allocated.

Parameters
descthe mounted filesystem
inodethe file's inode
inode_nrits inode number
idxthe block's index in the file
bufthe block's new contents
Returns
0, -EFBIG, -ENOSPC, -ENOMEM, or an I/O error

Definition at line 235 of file inode.c.

References block_tree(), ext2_inode::blocks, ext2_block_write(), ext2_indirect_block_get(), ext2_inode_block_alloc(), ext2_inode_block_write(), ext2_inode_write(), slot_get(), slot_set(), and tree_span().

Referenced by dentry_update(), ext2_dentry_create(), ext2_inode_block_write(), ext2_trunc(), ext2_vmknod(), and ext2_write().

◆ ext2_inode_off()

off_t ext2_inode_off ( struct ext2 * desc,
ino_t ino )
inlinestatic

Returns where an inode is on the disk, in bytes, or -EINVAL for an inode number that doesn't exist.

Definition at line 12 of file inode.c.

References ext2::bs, ext2_inode_off(), ext2_superblock::ginodes, ext2::groups, ext2::inode_size, ext2_group::inodes, ext2_superblock::ninodes, and ext2::superblock.

Referenced by ext2_inode_off(), ext2_inode_read(), and ext2_inode_write().

◆ ext2_inode_read()

int ext2_inode_read ( struct ext2 * desc,
ino_t ino,
struct ext2_inode * ref )

Reads an inode from the disk.

Parameters
descthe mounted filesystem
inothe inode number
refwhere it goes; NULL only checks the number
Returns
0, -EINVAL for a bad inode number, or an I/O error

Definition at line 33 of file inode.c.

References ext2_inode_off(), ext2_inode_read(), and ext2_io().

Referenced by dentry_update(), ext2_dentry_create(), ext2_finddir(), ext2_inode_build(), ext2_inode_discard(), ext2_inode_read(), ext2_inode_sync(), ext2_read(), ext2_readdir(), ext2_setattr(), ext2_trunc(), ext2_vlink(), ext2_vmknod(), ext2_vrename(), and ext2_write().

◆ ext2_inode_write()

int ext2_inode_write ( struct ext2 * desc,
ino_t ino,
struct ext2_inode * ext2_inode )

Writes an inode to the disk.

Parameters
descthe mounted filesystem
inothe inode number
ext2_inodethe inode
Returns
0, -EINVAL for a bad inode number, or an I/O error

Definition at line 51 of file inode.c.

References ext2_inode_off(), ext2_inode_write(), and ext2_io().

Referenced by ext2_dentry_create(), ext2_inode_block_write(), ext2_inode_discard(), ext2_inode_sync(), ext2_inode_write(), ext2_setattr(), ext2_trunc(), ext2_vmknod(), and ext2_write().

◆ slot_get()

uint32_t slot_get ( struct ext2_inode * inode,
int slot )
static

Returns the block pointer in a slot of an inode.

A switch rather than an array, because the inode is packed, so its fields can't be pointed to.

Definition at line 101 of file inode.c.

References ext2_inode::direct_pointer, ext2_inode::doubly_indirect_pointer, ext2_inode::singly_indirect_pointer, slot_get(), SLOT_SINGLY, and ext2_inode::triply_indirect_pointer.

Referenced by ext2_inode_block_read(), ext2_inode_block_write(), and slot_get().

◆ slot_set()

void slot_set ( struct ext2_inode * inode,
int slot,
uint32_t block )
static

◆ tree_span()

size_t tree_span ( struct ext2 * desc,
int depth )
static

Returns how many data blocks are under one pointer of an indirect block that is depth levels above them.

Definition at line 124 of file inode.c.

References ext2::bs, and tree_span().

Referenced by ext2_inode_block_read(), ext2_inode_block_write(), and tree_span().