-
Notifications
You must be signed in to change notification settings - Fork 16
/
block.c
176 lines (161 loc) · 4.47 KB
/
block.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
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <unistd.h>
#include <stdio.h>
#include "simdisk.h"
/* only open the file once */
static int f = -1;
static int devsize = 0;
/* returns the device size (in blocks) if the operation is successful,
* and -1 otherwise */
int dev_open (const char * path)
{
struct stat st;
if (f < 0) {
f = open (path, O_RDWR);
if (f < 0) {
error ("open");
return -1;
}
if (fstat (f, &st) < 0) {
perror ("fstat");
return -1;
}
devsize = st.st_size / BLOCKSIZE;
}
return devsize;
}
/* returns 0 if the operation is successful, and -1 otherwise
* read the block_num's information into *block */
int read_block (int block_num, void * block)
{
if (block_num >= devsize) {
printf ("block number requested %d, maximum %d", block_num, devsize - 1);
return -1;
}
if (lseek (f, block_num * BLOCKSIZE, SEEK_SET) < 0) {
perror ("lseek");
return -1;
}
if (read (f, block, BLOCKSIZE) != BLOCKSIZE) {
perror ("read");
return -1;
}
return 0;
}
/* returns 0 if the operation is successful, and -1 otherwise */
int write_block (int block_num, void * block)
{
if (block_num >= devsize) {
printf ("block number requested %d, maximum %d\n", block_num, devsize - 1);
return -1;
}
if (lseek (f, block_num * BLOCKSIZE, SEEK_SET) < 0) {
perror ("lseek");
return -1;
}
if (write (f, block, BLOCKSIZE) != BLOCKSIZE) {
perror ("write");
return -1;
}
if (fsync (f) < 0)
perror ("fsync"); /* but return success anyway */
return 0;
}
int read_inode (int inum, void * inode)
{
if(inum >= MAX_FILE_NUM || inum == 0) {
printf("inode number requested %d, maximum %d\n, minimum 1", inum, MAX_FILE_NUM - 1);
return -1;
}
if (lseek(f, INODE_TABLE_START * BLOCKSIZE + inum * 256, SEEK_SET) < 0)
{
perror("lseek");
return -1;
}
if (read(f, inode, 256) != 256)
{
perror("read");
return -1;
}
return 0;
}
int write_inode (int inum, void * inode)
{
if (inum >= devsize || inum == 0) {
printf ("inode number requested %d, maximum %d\n", inum, MAX_FILE_NUM - 1);
return -1;
}
if (lseek(f, INODE_TABLE_START * BLOCKSIZE + inum * 256, SEEK_SET) < 0)
{
perror("lseek");
return -1;
}
if (write (f, inode, 256) != 256)
{
perror ("write");
return -1;
}
if (fsync (f) < 0)
perror ("fsync"); /* but return success anyway */
return 0;
}
/* acquire free block, return block num */
int get_block() {
int found = 0;
int i, j, k;
int retval;
unsigned char bitb;
char bits[BLOCKSIZE];
for (i = BLOCK_MAP_START; i < INODE_MAP_START; i++) { // i locate block num
read_block(i, bits);
for (j = 0; j < BLOCKSIZE; j++) { // j locate Byte num
if (bits[j] != 0xffffffff) { /* found! Why not 0xff ?*/
found = 1;
break;
}
}
if (found) break;
}
bitb = (unsigned char) bits[j];
for(k = 0; k < 8; k++) // k locate bit
if((~(bitb << k) & 0x80) != 0) break;
retval = k + j * 8 + (i - BLOCK_MAP_START) * 8192 + BLOCK_TABLE_START; // in bit units
if(retval >= 102400) {
printf("Error : No free block is founded");
return -1;
}
bits[retval >> 3] |= (1 << (8 - 1 - (retval & 0x07))); // set block bitmap
write_block(i, bits);
return retval;
}
/* acquire free inode, return inode num */
int get_inode() {
int found = 0;
int i, j, k;
int ret;
unsigned char bitb;
char bits[BLOCKSIZE];
for(i = INODE_MAP_START; i < INODE_TABLE_START; i ++) {
read_block(i, bits);
for(j = 0; j < BLOCKSIZE; j ++){
if(bits[j] != 0xff) {
found = 1;
break;
}
}
if(found) break;
}
bitb = (unsigned char)bits[j];
for(k = 0; k < 8; k ++)
if((~(bitb << k) & 0x80) != 0) break;
ret = k + j * 8 + (i - INODE_MAP_START) * 8192 + INODE_TABLE_START;
if (ret >= MAX_FILE_NUM) {
printf("Error : No free inode is founded");
return -1;
}
bits[ret >> 3] |= (1 << (8 - 1 - (ret & 0x07))); // set inode bitmap
write_block(i, bits);
return ret;
}