-
Notifications
You must be signed in to change notification settings - Fork 2
/
inode_manager.h
131 lines (81 loc) · 2.56 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
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
// inode layer interface.
#ifndef inode_h
#define inode_h
#include <stdint.h>
#include <map>
#include "extent_protocol.h" // TODO: delete it
#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 write_block(uint32_t id, const char *buf);
};
// block layer -----------------------------------------
typedef struct superblock {
uint32_t size;
uint32_t nblocks;
uint32_t ninodes;
} superblock_t;
class block_manager {
private:
uint32_t next_block;
disk *d;
std::map<uint32_t, int> using_blocks;
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 write_block(uint32_t id, const char *buf);
};
// inode layer -----------------------------------------
#define INODE_NUM 1024
// Inodes per block.
#define IPB 1
//(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 100
#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:
uint32_t next_inum;
block_manager *bm;
struct inode *get_inode(uint32_t inum);
void put_inode(uint32_t inum, struct inode *ino);
void __read_nth_block(struct inode *ino, uint32_t nth, std::string &buf);
void __write_nth_block(struct inode *ino, uint32_t nth, std::string &);
void __alloc_nth_block(struct inode *ino, uint32_t nth, std::string &, bool);
blockid_t __get_nth_blockid(struct inode *ino, uint32_t nth);
void __free_nth_block(struct inode *ino, uint32_t nth);
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