-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdisk_driver.h
More file actions
51 lines (41 loc) · 1.69 KB
/
disk_driver.h
File metadata and controls
51 lines (41 loc) · 1.69 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
#pragma once
#include "bitmap.h"
#define BLOCK_SIZE 512
// this is stored in the 1st block of the disk
typedef struct {
int num_blocks;
int bitmap_blocks; // how many blocks in the bitmap
int bitmap_entries; // how many bytes are needed to store the bitmap
int free_blocks; // free blocks
int first_free_block;// first block index
} DiskHeader;
typedef struct {
DiskHeader* header; // mmapped
char* bitmap_data; // mmapped (bitmap)
int fd; // for us
} DiskDriver;
/**
The blocks indices seen by the read/write functions
have to be calculated after the space occupied by the bitmap
*/
// opens the file (creating it if necessary)
// allocates the necessary space on the disk
// calculates how big the bitmap should be
// if the file was new
// compiles a disk header, and fills in the bitmap of appropriate size
// with all 0 (to denote the free space);
void DiskDriver_init(DiskDriver* disk, const char* filename, int num_blocks);
// reads the block in position block_num
// returns -1 if the block is free accrding to the bitmap
// 0 otherwise
int DiskDriver_readBlock(DiskDriver* disk, void* dest, int block_num);
// writes a block in position block_num, and alters the bitmap accordingly
// returns -1 if operation not possible
int DiskDriver_writeBlock(DiskDriver* disk, void* src, int block_num);
// frees a block in position block_num, and alters the bitmap accordingly
// returns -1 if operation not possible
int DiskDriver_freeBlock(DiskDriver* disk, int block_num);
// returns the first free blockin the disk from position (checking the bitmap)
int DiskDriver_getFreeBlock(DiskDriver* disk, int start);
// writes the data (flushing the mmaps)
int DiskDriver_flush(DiskDriver* disk);