Basic Solution File System - BSFS
Loading...
Searching...
No Matches
allocate_bsfs.c File Reference

Functions for allocating blocks in the BSFS file system. More...

Functions

int allocate_bspan_queue (FILE *disk, superblock_t *sb, bspan_queue_t *queue, uint32_t blocks_needed, uint32_t offset)
 Allocates a sequence of free blocks and fills a bspan queue.
uint32_t allocate_single_block (FILE *disk, superblock_t *sb)
 Allocates a single free block and updates the block bitmap.
int deallocate_single_block (FILE *disk, superblock_t *sb, uint32_t block_index)
 Deallocates a single block in the filesystem.
int deallocate_bspan (FILE *disk, superblock_t *sb, const bspan_t *bspan)
 Frees a contiguous range of disk blocks described by a bspan and updates the bitmap on disk.

Detailed Description

Functions for allocating blocks in the BSFS file system.

Function Documentation

◆ allocate_bspan_queue()

int allocate_bspan_queue ( FILE * disk,
superblock_t * sb,
bspan_queue_t * queue,
uint32_t blocks_needed,
uint32_t offset )

Allocates a sequence of free blocks and fills a bspan queue.

This function scans the block bitmap on disk to find available (free) blocks and groups them into spans (bspan_t), which are enqueued into a queue. It uses a first-fit strategy and continues recursively if the entire allocation cannot be fulfilled in a single span.

The function internally loads the bitmap, updates it, and writes the updated bitmap back to disk after allocation.

Parameters
diskPointer to the FILE representing the filesystem image.
sbPointer to the superblock containing block and bitmap information.
queuePointer to the bspan queue where allocated spans will be enqueued.
blocks_neededTotal number of blocks required for allocation.
offsetStarting block index for the allocation scan (typically 0).
Returns
0 on success, or -1 if there are not enough free blocks.

◆ allocate_single_block()

uint32_t allocate_single_block ( FILE * disk,
superblock_t * sb )

Allocates a single free block and updates the block bitmap.

This function scans the block bitmap to find the first available (free) block. Once found, it marks the block as occupied in the in-memory bitmap and persists the updated bitmap back to disk to ensure consistency.

The bitmap must be previously formatted as a linked list of blocks starting from the block number indicated by the superblock. This function assumes that each block in the bitmap contains a pointer to the next block.

Parameters
diskPointer to the FILE representing the filesystem image.
sbPointer to the superblock structure containing metadata about the filesystem.
Returns
The index of the allocated block on success, or INVALID_BLOCK if no free blocks are available or if an error occurs during bitmap saving.

◆ deallocate_bspan()

int deallocate_bspan ( FILE * disk,
superblock_t * sb,
const bspan_t * bspan )

Frees a contiguous range of disk blocks described by a bspan and updates the bitmap on disk.

This function loads the block allocation bitmap from disk, marks all blocks covered by the specified bspan as free, and writes the updated bitmap back to the disk. It is used to reclaim storage space when a file or segment is removed from the filesystem.

The bitmap is read and written based on the starting block specified in the superblock. Any failure to read or write the bitmap will be reported as an error.

Parameters
diskPointer to the open FILE representing the filesystem image.
sbPointer to the superblock structure containing bitmap metadata.
bspanPointer to the bspan structure indicating which disk blocks to free.
Returns
0 on success, -1 on failure to read or write the bitmap.

◆ deallocate_single_block()

int deallocate_single_block ( FILE * disk,
superblock_t * sb,
uint32_t block_index )

Deallocates a single block in the filesystem.

This function marks the specified block as free in the block allocation bitmap and updates the bitmap on disk. It should be used whenever a block is no longer needed (e.g., during file truncation or deletion).

Parameters
diskPointer to the FILE representing the filesystem image.
sbPointer to the superblock with bitmap location information.
block_indexIndex of the block to be deallocated.
Returns
0 on success, or -1 if the block index is invalid or bitmap write fails.