-
Notifications
You must be signed in to change notification settings - Fork 10
/
map.c
195 lines (173 loc) · 4.77 KB
/
map.c
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
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
#include "constants.h"
#include "HUST_fs.h"
int checkbit(uint8_t number, int x)
{
return (number >> x) & 1U;
}
int HUST_find_first_zero_bit(const void *vaddr, unsigned size)
{
const unsigned short *p = vaddr, *addr = vaddr;
unsigned short num;
if (!size)
return 0;
size >>= 4;
while (*p++ == 0xffff) {
if (--size == 0)
return (p - addr) << 4;
}
num = *--p;
return ((p - addr) << 4) + ffz(num);
}
uint64_t HUST_fs_get_empty_inode(struct super_block* sb)
{
struct HUST_fs_super_block *disk_sb = sb->s_fs_info;
//read imap
ssize_t imap_size = disk_sb->blocks_count / 8;
uint8_t *imap = kmalloc(imap_size, GFP_KERNEL);
if(0!=get_imap(sb, imap, imap_size))
{
kfree(imap);
return -EFAULT;
}
uint64_t empty_ilock_num = HUST_find_first_zero_bit(imap, disk_sb->blocks_count / 8);
printk(KERN_INFO "HUST_find_first_zero_bit at %llu\n",
empty_ilock_num);
kfree(imap);
return empty_ilock_num;
}
int get_imap(struct super_block* sb, uint8_t* imap, ssize_t imap_size)
{
if(!imap) {
return -EFAULT;
}
struct HUST_fs_super_block *disk_sb = sb->s_fs_info;
//read imap
uint64_t i;
for (i = disk_sb->imap_block;
i < disk_sb->data_block_number && imap_size != 0; ++i) {
struct buffer_head *bh;
bh = sb_bread(sb, i);
if (!bh) {
printk(KERN_ERR "bh empty\n");
}
uint8_t *imap_t = (uint8_t *) bh->b_data;
printk(KERN_INFO "imap is %x\n", imap_t[0]);
if (imap_size >= HUST_BLOCKSIZE) {
memcpy(imap, imap_t, HUST_BLOCKSIZE);
imap_size -= HUST_BLOCKSIZE;
} else {
memcpy(imap, imap_t, imap_size);
imap_size = 0;
}
brelse(bh);
}
return 0;
}
int get_bmap(struct super_block* sb, uint8_t* bmap, ssize_t bmap_size)
{
if(!bmap) {
return -EFAULT;
}
struct HUST_fs_super_block *disk_sb = sb->s_fs_info;
uint64_t i;
for (i = disk_sb->bmap_block;
i < disk_sb->imap_block && bmap_size != 0; ++i) {
struct buffer_head *bh;
bh = sb_bread(sb, i);
if (!bh) {
printk(KERN_ERR "bh empty\n");
return -EFAULT;
}
uint8_t *bmap_t = (uint8_t *) bh->b_data;
if (bmap_size >= HUST_BLOCKSIZE) {
memcpy(bmap, bmap_t, HUST_BLOCKSIZE);
bmap_size -= HUST_BLOCKSIZE;
} else {
memcpy(bmap, bmap_t, bmap_size);
bmap_size = 0;
}
brelse(bh);
}
return 0;
}
uint64_t HUST_fs_get_empty_block(struct super_block* sb)
{
struct HUST_fs_super_block *disk_sb = sb->s_fs_info;
//read imap
uint64_t bmap_empty = disk_sb->blocks_count / 8;
printk(KERN_INFO "bmap_empty is %llu\n", bmap_empty);
uint8_t *bmap = kmalloc(bmap_empty, GFP_KERNEL);
if(get_bmap(sb, bmap, bmap_empty)!=0) {
kfree(bmap);
return -EFAULT;
}
uint64_t empty_block_num = HUST_find_first_zero_bit(bmap, disk_sb->blocks_count / 8);
printk(KERN_INFO "HUST_find_first_zero_bit at %llu\n",
empty_block_num);
kfree(bmap);
return empty_block_num;
}
int set_and_save_imap(struct super_block* sb, uint64_t inode_num, uint8_t value)
{
/*
* 1. find one block we want to change;
* 2. write the block
*/
struct HUST_fs_super_block *disk_sb = sb->s_fs_info;
uint64_t block_idx = inode_num / (HUST_BLOCKSIZE*8) + disk_sb->imap_block;
uint64_t bit_off = inode_num % (HUST_BLOCKSIZE*8);
struct buffer_head* bh;
bh = sb_bread(sb, block_idx);
printk(KERN_ERR "In save imap\n");
BUG_ON(!bh);
if(value == 1){
setbit(bh->b_data[bit_off/8], bit_off%8);
}
else if(value == 0){
clearbit(bh->b_data[bit_off/8], bit_off%8);
}
else{
printk(KERN_ERR "value error\n");
}
map_bh(bh, sb, block_idx);
brelse(bh);
return 0;
}
int save_bmap(struct super_block* sb, uint8_t* bmap, ssize_t bmap_size)
{
struct HUST_fs_super_block *disk_sb = sb->s_fs_info;
uint64_t block_idx = disk_sb->bmap_block;
struct buffer_head* bh;
bh = sb_bread(sb, block_idx);
printk(KERN_ERR "In save bmap\n");
memcpy(bh->b_data, bmap, bmap_size);
map_bh(bh, sb, block_idx);
brelse(bh);
return 0;
}
int set_and_save_bmap(struct super_block* sb, uint64_t block_num, uint8_t value)
{
/*
* 1. find one block we want to change;
* 2. write the block
*/
struct HUST_fs_super_block *disk_sb = sb->s_fs_info;
uint64_t block_idx = block_num / (HUST_BLOCKSIZE*8) + disk_sb->bmap_block;
uint64_t bit_off = block_num % (HUST_BLOCKSIZE*8);
struct buffer_head* bh;
bh = sb_bread(sb, block_idx);
printk(KERN_ERR "In save bmap\n");
BUG_ON(!bh);
if(value == 1){
setbit(bh->b_data[bit_off/8], bit_off%8);
}
else if(value == 0){
clearbit(bh->b_data[bit_off/8], bit_off%8);
}
else{
printk(KERN_ERR "value error\n");
}
map_bh(bh, sb, block_idx);
brelse(bh);
return 0;
}