forked from chouqin/simpleFs
-
Notifications
You must be signed in to change notification settings - Fork 0
/
cache.c
196 lines (169 loc) · 5.55 KB
/
cache.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
196
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include "buf.h"
#include "super.h"
#include "./socket/client.h"
void rm_lru(struct buf *bp) ;
void zero_block(struct buf * bp){
char * data = (char *)&bp->b;
memset(data, 0, S_BLOCK_SIZE);
bp->b_dirt = DIRTY;
}
/*===========================================================================*
* get_block *
*===========================================================================*/
struct buf *get_block(block_t block)
{
int b;
struct buf *bp, *prev_ptr;
/* Search the hash chain for (dev, block). Do_read() can use
* get_block(NO_DEV ...) to get an unnamed block to fill with zeros when
* someone wants to read from a hole in a file, in which case this search
* is skipped
*/
b = (int) block & HASH_MASK;
bp = buf_hash[b];
while (bp != NIL_BUF) {
if (bp->b_blocknr == block ) {
/* printf("here\n"); */
/*bp->b_count = 0, means this block is in the lru chain*/
if (bp->b_count == 0)
rm_lru(bp);
bp->b_count ++;
return(bp);
} else {
bp = bp->b_hash;
}
}
bp = front;
if (bp == NIL_BUF){
printf("Get_Block: error occurs: all buffers in use\n");
exit(1);
}
/* if (block == 129) { */
/* printf("front block number is %d\n", bp->b_blocknr); */
/* } */
rm_lru(bp);
/* Remove the block that was just taken from its hash chain. */
/* if (bp->b_blocknr == 1){ */
/* printf("here"); */
/* } */
b = (int) bp->b_blocknr & HASH_MASK;
prev_ptr = buf_hash[b];
if (prev_ptr == bp) {
buf_hash[b] = bp->b_hash;
} else {
/* The block just taken is not on the front of its hash chain. */
while (prev_ptr->b_hash != NIL_BUF)
if (prev_ptr->b_hash == bp) {
prev_ptr->b_hash = bp->b_hash; /* found it */
break;
} else {
prev_ptr = prev_ptr->b_hash; /* keep looking */
}
}
/* If the block taken is dirty, make it clean by writing it to the disk.
*/
if (bp->b_dirt == DIRTY)
write_block(bp->b_blocknr, (char *)&bp->b);
/* Fill in block's parameters and add it to the hash chain where it goes. */
bp->b_blocknr = block; /* fill in block number */
bp->b_count = 1; /* fill in block number */
b = (int) bp->b_blocknr & HASH_MASK;
bp->b_hash = buf_hash[b];
buf_hash[b] = bp; /* add to hash list */
read_block(block, (char *)&bp->b);
return(bp); /* return the newly acquired block */
}
/*===========================================================================*
* put_block *
*===========================================================================*/
void put_block(struct buf *bp)
{
if (bp == NIL_BUF) return; /* it is easier to check here than in caller */
if (--bp->b_count == 0){
bufs_in_use--; /* one fewer block buffers in use */
bp->b_prev = rear;
bp->b_next = NIL_BUF;
if (rear == NIL_BUF){
printf("here\n");
exit(0);
front = bp;
}
else {
rear->b_next = bp;
/* if (bp->b_blocknr == 1) */
/* printf("rear buf number is %d, and pre buf number is %d\n",rear->b_next->b_blocknr, rear->b_blocknr); */
}
rear = bp;
}
}
/*===========================================================================*
* alloc_zone *
*===========================================================================*/
zone_t alloc_zone(zone_t z)
{
int major, minor;
bit_t b, bit;
struct super_block *sp;
/* Note that the routine alloc_bit() returns 1 for the lowest possible
* zone, which corresponds to S_FIRSTDATAZONE. To convert a value
* between the bit number, 'b', used by alloc_bit() and the zone number, 'z',
* stored in the inode, use the formula:
* z = b + S_FIRSTDATAZONE - 1
* Alloc_bit() never returns 0, since this is used for NO_BIT (failure).
*/
sp = &super_block;
/* If z is 0, skip initial part of the map known to be fully in use. */
if (z == S_FIRSTDATAZONE) {
bit = sp->s_zsearch;
} else {
bit = (bit_t) z - (S_FIRSTDATAZONE);
}
b = alloc_bit(ZMAP, bit);
if (b == NO_BIT) {
printf("No space to allocate zone\n");
exit(1);
}
if (z == S_FIRSTDATAZONE) sp->s_zsearch = b; /* for next time */
return(S_FIRSTDATAZONE + (zone_t) b);
}
/*===========================================================================*
* free_zone *
*===========================================================================*/
void free_zone(zone_t numb)
{
/* Return a zone. */
struct super_block *sp;
bit_t bit;
/* Locate the appropriate super_block and return bit. */
sp = &super_block;
if (numb < S_FIRSTDATAZONE || numb >= S_ZONES) return;
bit = (bit_t) (numb - (S_FIRSTDATAZONE));
free_bit(ZMAP, bit);
if (bit < sp->s_zsearch) sp->s_zsearch = bit;
}
/*===========================================================================*
* rm_lru *
*===========================================================================*/
void rm_lru(struct buf *bp)
{
/* Remove a block from its LRU chain. */
struct buf *next_ptr, *prev_ptr;
/* printf("first buf block number is %d\n", bp->b_blocknr); */
bufs_in_use++;
next_ptr = bp->b_next; /* successor on LRU chain */
prev_ptr = bp->b_prev; /* predecessor on LRU chain */
if (prev_ptr != NIL_BUF)
prev_ptr->b_next = next_ptr;
else
front = next_ptr; /* this block was at front of chain */
/* if ((bp->b_blocknr == 0) && (next_ptr == NIL_BUF)){ */
/* printf("here"); */
/* } */
if (next_ptr != NIL_BUF)
next_ptr->b_prev = prev_ptr;
else
rear = prev_ptr; /* this block was at rear of chain */
}