-
Notifications
You must be signed in to change notification settings - Fork 0
/
inode_manager.h
105 lines (81 loc) · 2.38 KB
/
inode_manager.h
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
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
// inode layer interface.
#ifndef inode_h
#define inode_h
#include <stdint.h>
#include <math.h>
#include <bitset>
#include "extent_protocol.h" // TODO: delete it
using namespace std;
#define DISK_SIZE 1024*1024*16
#define BLOCK_SIZE 512
#define BLOCK_NUM (DISK_SIZE/BLOCK_SIZE)
typedef uint32_t blockid_t;
// disk layer -----------------------------------------
class disk {
private:
unsigned char blocks[BLOCK_NUM][BLOCK_SIZE];
public:
disk();
void read_block(uint32_t id, char *buf);
void read_indirect_block(uint32_t id, uint32_t *buf);
void write_block(uint32_t id, const char *buf);
void write_indirect_block(uint32_t id, const uint32_t *buf);
};
// block layer -----------------------------------------
typedef struct superblock {
uint32_t size;
uint32_t nblocks;
uint32_t ninodes;
} superblock_t;
class block_manager {
private:
disk *d;
std::map <uint32_t, int> using_blocks;
uint32_t count;
public:
block_manager();
struct superblock sb;
uint32_t alloc_block();
void free_block(uint32_t id);
void read_block(uint32_t id, char *buf);
void read_indirect_block(uint32_t id, uint32_t *buf);
void write_block(uint32_t id, const char *buf);
void write_indirect_block(uint32_t id, const uint32_t *buf);
};
// inode layer -----------------------------------------
#define INODE_NUM 1024
// Inodes per block.
#define IPB (BLOCK_SIZE / sizeof(struct inode))
// Block containing inode i
#define IBLOCK(i, nblocks) ((nblocks)/BPB + (i)/IPB + 3)
// Bitmap bits per block
#define BPB (BLOCK_SIZE*8)
// Block containing bit for block b
#define BBLOCK(b) ((b)/BPB + 2)
#define NDIRECT 32
#define NINDIRECT (BLOCK_SIZE / sizeof(uint))
#define MAXFILE (NDIRECT + NINDIRECT)
typedef struct inode {
short type;
unsigned int size;
unsigned int atime;
unsigned int mtime;
unsigned int ctime;
blockid_t blocks[NDIRECT+1]; // Data block addresses
} inode_t;
class inode_manager {
private:
block_manager *bm;
struct inode* get_inode(uint32_t inum);
void put_inode(uint32_t inum, struct inode *ino);
uint32_t count;
public:
inode_manager();
uint32_t alloc_inode(uint32_t type);
void free_inode(uint32_t inum);
void read_file(uint32_t inum, char **buf, int *size);
void write_file(uint32_t inum, const char *buf, int size);
void remove_file(uint32_t inum);
void getattr(uint32_t inum, extent_protocol::attr &a);
};
#endif