-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathalloc_dalloc.c
More file actions
171 lines (131 loc) · 2.48 KB
/
alloc_dalloc.c
File metadata and controls
171 lines (131 loc) · 2.48 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
//Allocating the data and the inodes
//-----------------functions in this file-----------------
// tst_bit,set_bit,decFreeInodes,ialloc,balloc,decFreeBlocks
int tst_bit(char* buf, int bit)
{
return buf[bit/8] & (1 << (bit%8));
}
int set_bit(char* buf, int bit)
{
buf[bit/8] |= (1 << (bit % 8));
}
//Keep track of the nodes that are free
int decFreeInodes(int dev)
{
char buf[BLKSIZE];
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 ialloc(int dev)
{
int i;
char buf[BLKSIZE];
get_block(dev,imap,buf);
for(i=0;i<ninodes;i++)
{
if(tst_bit(buf,i) == 0)
{
set_bit(buf,i);
put_block(dev,imap,buf);
decFreeInodes(dev);
printf("Allocated ino = %d\n",i+1);
return i+1;//inodes goes from 1 not 0
}
}
return 0;
}
int balloc(int dev)
{
int i;
char buf[BLKSIZE];
get_block(dev,bmap,buf);
for(i=0; i<nblocks; i++)
{
if(tst_bit(buf,i) == 0)
{
set_bit(buf,i);
put_block(dev,bmap,buf);
decFreeBlocks(dev);
printf("Allocated block = %d\n",i+1);
return i+1;//inodes goes from 1 not 0
}
}
return 0;
}
//Decrease the amount of free blocks
int decFreeBlocks(int dev)
{
char buf[BLKSIZE];
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 clr_bits(char* buf,int bit)
{
buf[bit/8] &= ~(1 << (bit%8));
}
int incFreeInodes(int dev)
{
char buf[BLKSIZE];
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);
}
//Look Over
int idalloc(int dev,int ino)
{
int i;
char buf[BLKSIZE];
if(ino > ninodes)
{
printf("inumber not in range %d\n",ino);
return;
}
get_block(dev,imap,buf);
clr_bits(buf,ino-1);
put_block(dev,imap,buf);
incFreeInodes(dev);
}
int incFreeBlocks(int dev)
{
char buf[BLKSIZE];
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);
}
//Look Over
int bdalloc(int dev, int bno)
{
int i;
char buf[BLKSIZE];
if(bno > nblocks)
{
printf("iblock not in range %d\n",bno);
return;
}
get_block(dev,bmap,buf);
clr_bits(buf,bno-1);
put_block(dev,bmap,buf);
incFreeBlocks(dev);
}