-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathalloc_dalloc.c
More file actions
209 lines (170 loc) · 3.92 KB
/
Copy pathalloc_dalloc.c
File metadata and controls
209 lines (170 loc) · 3.92 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
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
196
197
198
199
200
201
202
203
204
205
206
207
208
209
#ifndef ALLOCDALLOC
#define ALLOCDALLOC
#include "type.h"
#include "util.c"
int tst_bit(char *buf, int bit)
{
int i = bit / 8; // byte
int j = bit % 8; // bit
return buf[i] & (1 << j);
}
int set_bit(char *buf, int bit)
{
int i = bit / 8; // byte
int j = bit % 8; // bit
buf[i] |= (1 << j);
}
int clr_bit(char *buf, int bit)
{
int i = bit / 8; // byte
int j = bit % 8; // bit
buf[i] &= ~(i << j);
}
int decFreeInodes(int dev)
{
char buf[BLKSIZE];
// dec free inodes count in SUPER and GD
get_block(dev, 1, buf);
sp = (SUPER *)buf;
sp->s_free_inodes_count--;
put_block(dev, 1, buf);
get_block(dev, 2, buf);
gp = (GD *)buf;
gp->bg_free_inodes_count--;
put_block(dev, 2, buf);
}
int decFreeBlocks(int dev)
{
char buf[BLKSIZE];
// dec free blocks count in SUPER and GD
get_block(dev, 1, buf);
sp = (SUPER *)buf;
sp->s_free_blocks_count--;
put_block(dev, 1, buf);
get_block(dev, 2, buf);
gp = (GD *)buf;
gp->bg_free_blocks_count--;
put_block(dev, 2, buf);
}
int incFreeInodes(int dev)
{
char buf[BLKSIZE];
// inc free inodes count in SUPER and GD
get_block(dev, 1, buf);
sp = (SUPER *)buf;
sp->s_free_inodes_count++;
put_block(dev, 1, buf);
get_block(dev, 2, buf);
gp = (GD *)buf;
gp->bg_free_inodes_count++;
put_block(dev, 2, buf);
}
int incFreeBlocks(int dev)
{
char buf[BLKSIZE];
// inc free blocks count in SUPER and GD
get_block(dev, 1, buf);
sp = (SUPER *)buf;
sp->s_free_blocks_count++;
put_block(dev, 1, buf);
get_block(dev, 2, buf);
gp = (GD *)buf;
gp->bg_free_blocks_count++;
put_block(dev, 2, buf);
}
int ialloc(int dev)
{
int imap = getmptr(dev)->imap;
int ninodes = getmptr(dev)->ninodes;
int i;
char buf[BLKSIZE];
// read inode_bitmap block
get_block(dev, imap, buf);
for (i = 0; i < ninodes; i++)
{
if (tst_bit(buf, i) == 0)
{
// free block
set_bit(buf, i);
put_block(dev, imap, buf);
decFreeInodes(dev);
printf("Allocated ino= %d\n", i + 1);
return i + 1; // bits count from 0, ino from 1.
}
}
return 0; // failed to allocate inode
}
int balloc(int dev)
{
int bmap = getmptr(dev)->bmap;
int nblocks = getmptr(dev)->nblocks;
int i;
char buf[BLKSIZE];
// read block_bitmap block
get_block(dev, bmap, buf);
for (i = 0; i < nblocks; i++)
{
if (tst_bit(buf, i) == 0)
{
// free block
set_bit(buf, i);
put_block(dev, bmap, buf);
decFreeBlocks(dev);
// i + 1 change
printf("Allocated block= %d\n", i + 1);
return i + 1;
}
}
return 0; // failed to allocate block
}
int idalloc(int dev, int ino)
{
int imap = getmptr(dev)->imap;
int ninodes = getmptr(dev)->ninodes;
int i;
char buf[BLKSIZE];
if (ino > ninodes){
printf("inumber %d out of range\n", ino);
return -1;
}
// get inode bitmap block
get_block(dev, imap, buf);
clr_bit(buf, ino-1);
// write buf back
put_block(dev, imap, buf);
// update free inode count in SUPER and GD
incFreeInodes(dev);
}
int bdalloc(int dev, int bno)
{
int bmap = getmptr(dev)->bmap;
int nblocks = getmptr(dev)->nblocks;
int i;
char buf[BLKSIZE];
if (bno > nblocks){
printf("bno %d out of range\n", bno);
return -1;
}
// get bno bitmap block
get_block(dev, bmap, buf);
clr_bit(buf, bno - 1);
// write buf back
put_block(dev, bmap, buf);
// update free block count in SUPER and GD
incFreeBlocks(dev);
}
OFT* oget()
{
OFT *oftp;
for (int i = 0; i < NOFT; i++)
{
oftp = &oft[i];
if (oftp->refCount == 0)
{
return oftp;
}
}
printf("PANIC: no more free ofts\n");
return 0;
}
#endif