-
Notifications
You must be signed in to change notification settings - Fork 0
/
disk.c
executable file
·439 lines (346 loc) · 12.6 KB
/
disk.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
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
#include <stdio.h>
#include <stdlib.h>
#include <signal.h>
#include "defs.h"
#include "disk.h"
#include "interrupts_private.h"
#include "random.h"
pthread_mutex_t disk_mutex;
/* disk operation parameters */
double crash_rate = 0.0;
double failure_rate = 0.0;
double reordering_rate = 0.0;
/* parameters to control startup behavior. To create a new disk, set use_existing_disk
to 0, specify the disk_name, disk_flags and disk_size, then call disk_initialize().
To startup an existing disk, set use_existing_disk to 1 and specify the disk_name,
then call disk_initialize. Upon successful startup, disk_flags and disk_size will
be correctly initialized.
*/
int use_existing_disk; /* Set to 1 if using an existing disk, 0 to create a new one */
const char* disk_name; /* Linux filename that stores your virtual disk */
int disk_flags; /* Set to DISK_READWRITE or DISK_READONLY */
int disk_size; /* Set to the number of blocks allocated for disk */
void start_disk_poll(disk_t* disk); /* forward declaration */
int disk_send_request(disk_t* disk, int blocknum, char* buffer,
disk_request_type_t type){
disk_queue_elem_t* saved_last=NULL;
disk_queue_elem_t* disk_request
= (disk_queue_elem_t*) malloc(sizeof(disk_queue_elem_t));
if (disk_request == NULL)
return -1;
/* put data in the request */
disk_request->request.blocknum = blocknum;
disk_request->request.buffer = buffer;
disk_request->request.type = type;
/* queue the request */
pthread_mutex_lock(&disk_mutex);
if (type == DISK_SHUTDOWN){
/* optimistically put the request on top of the queue */
disk_request->next=disk->queue;
disk->queue=disk_request;
} else {
/* optimistically put it at the end */
disk_request->next=NULL;
saved_last=disk->last;
if (disk->last != NULL){
disk->last->next=disk_request;
} else {
disk->queue=disk_request;
}
disk->last=disk_request;
}
/* signal the task that simulates the disk */
if (sem_post(&disk->semaphore)){
kprintf("You have exceeded the maximum number of requests pending.\n");
/* undo changes made to the request queue */
if (type == DISK_SHUTDOWN)
disk->queue=disk->queue->next;
else {
disk->last=saved_last;
if (disk->last != NULL)
disk->last->next=NULL;
else
disk->queue=NULL;
}
free(disk_request);
pthread_mutex_unlock(&disk_mutex);
return -2;
}
pthread_mutex_unlock(&disk_mutex);
return 0;
}
static int
disk_create(disk_t* disk, const char* name, int size, int flags) {
if ((disk->file = fopen(name, "w+b")) == NULL)
return -1;
disk->layout.size = size;
disk->layout.flags = flags;
/* write the disk layout to the "disk" */
if (fwrite(&disk->layout, 1, sizeof(disk_layout_t), disk->file)
!= sizeof(disk_layout_t)) {
fclose(disk->file);
return -1;
}
start_disk_poll((void*)disk);
return 0;
}
static int
disk_startup(disk_t* disk, const char* name) {
if ((disk->file = fopen(name, "r+b")) == NULL)
return -1;
if (fread(&disk->layout, 1, sizeof(disk_layout_t), disk->file) !=
sizeof(disk_layout_t)) {
fclose(disk->file);
return -1;
}
start_disk_poll((void*)disk);
return 0;
}
int disk_initialize(disk_t* disk){
int result=0;
/* create new disk or startup existing disk */
if (use_existing_disk) {
result = disk_startup(disk, disk_name);
if (result == 0) {
disk_size = disk->layout.size;
disk_flags = disk->layout.flags;
} else {
disk_size = 0;
disk_flags = DISK_READONLY;
}
} else {
result = disk_create(disk, disk_name, disk_size, disk_flags);
}
return result;
}
static void
disk_write_layout(disk_t* disk) {
if (fseek(disk->file, 0, SEEK_SET) != 0)
fclose(disk->file);
else if (fwrite(&disk->layout, 1, sizeof(disk_layout_t), disk->file) !=
sizeof(disk_layout_t))
fclose(disk->file);
else
fflush(disk->file);
}
int
disk_shutdown(disk_t* disk) {
disk_send_request(disk, 0, NULL, DISK_SHUTDOWN);
return 0;
}
void
disk_set_flags(disk_t* disk, int flags) {
pthread_mutex_lock(&disk_mutex);
disk->layout.flags |= flags;
disk_write_layout(disk);
pthread_mutex_unlock(&disk_mutex);
}
void
disk_unset_flags(disk_t* disk, int flags) {
pthread_mutex_lock(&disk_mutex);
disk->layout.flags ^= disk->layout.flags | flags;
disk_write_layout(disk);
pthread_mutex_unlock(&disk_mutex);
}
int
disk_read_block(disk_t* disk, int blocknum, char* buffer) {
return
disk_send_request(disk,blocknum,buffer,DISK_READ);
}
int
disk_write_block(disk_t* disk, int blocknum, char* buffer) {
return
disk_send_request(disk,blocknum,buffer,DISK_WRITE);
}
/* handle read and write to disk. Watch the job queue and
submit the requests to the operating system one by one.
On completion, the user suplied function with the user
suplied parameter is called
The interrupt mechanism is used much like in the network
case. One such process per disk.
The argument is a disk_t with the disk description.
*/
void disk_poll(void* arg) {
enum { DISK_OK, DISK_CRASHED } disk_state;
disk_t* disk = (disk_t*) arg;
disk_layout_t layout;
disk_interrupt_arg_t* disk_interrupt;
disk_queue_elem_t* disk_request;
disk_state=DISK_OK;
for (;;){
int blocknum;
char* buffer;
disk_request_type_t type;
int offset;
/* We use mutex to protect queue handling, as should
the code that inserts requests in the queue
*/
/* wait for somebody to put something in the queue */
sem_wait(&disk->semaphore);
if (DEBUG)
kprintf("Disk Controler: got a request.\n");
/* get exclusive access to queue handling and dequeue a request */
pthread_mutex_lock(&disk_mutex);
layout = disk->layout; /* this is the layout used until the request is fulfilled */
/* this is safe since a disk can only grow */
if (disk->queue != NULL){
disk_interrupt = (disk_interrupt_arg_t*)
malloc(sizeof(disk_interrupt_arg_t));
assert( disk_interrupt != NULL);
disk_interrupt->disk = disk;
/* we look first at the first request in the queue
to see if it is special.
*/
disk_interrupt->request =
disk->queue->request;
/* check if we shut down the disk */
if (disk->queue->request.type == DISK_SHUTDOWN){
if (DEBUG)
kprintf("Disk: Shutting down.\n");
disk_interrupt->reply=DISK_REPLY_OK;
fclose(disk->file);
pthread_mutex_unlock(&disk_mutex);
sem_destroy(&disk->semaphore);
send_interrupt(DISK_INTERRUPT_TYPE, mini_disk_handler, (void*)disk_interrupt);
break; /* end the disk task */
}
/* check if we got to reset the disk */
if (disk->queue->request.type == DISK_RESET){
disk_queue_elem_t* curr;
disk_queue_elem_t *next;
if (DEBUG)
kprintf("Disk: Resetting.\n");
disk_interrupt->reply=DISK_REPLY_OK;
/* empty the queue */
curr=disk->queue;
while (curr!=NULL){
next=curr->next;
free(curr);
curr=next;
}
disk->queue = disk->last = NULL;
disk_state = DISK_OK;
pthread_mutex_unlock(&disk_mutex);
goto sendinterrupt;
}
/* permute the first two elements in the queue
probabilistically if queue has two elements
*/
if (disk->queue->next !=NULL &&
(genrand() < reordering_rate)){
disk_queue_elem_t* first = disk->queue;
disk_queue_elem_t* second = first->next;
first->next = second->next;
second->next = first;
disk->queue = second;
if (disk->last == second)
disk->last = first;
}
/* dequeue the first request */
disk_request = disk->queue;
disk->queue = disk_request->next;
if (disk->queue == NULL)
disk->last = NULL;
} else {
/* empty queue, release the lock and leave */
pthread_mutex_unlock(&disk_mutex);
break;
}
pthread_mutex_unlock(&disk_mutex);
disk_interrupt->request = disk_request->request;
/* crash the disk ocasionally */
if (genrand() < crash_rate){
disk_state = DISK_CRASHED;
/* if (DEBUG) */
kprintf("Disk: Crashing disk.\n");
}
/* check if disk crashed */
if (disk_state == DISK_CRASHED){
disk_interrupt->reply=DISK_REPLY_CRASHED;
goto sendinterrupt;
}
if ( genrand() < failure_rate ) {
/* Trash the request */
disk_interrupt->reply = DISK_REPLY_FAILED;
if (DEBUG)
kprintf("Disk: Request failed.\n");
goto sendinterrupt;
}
/* Check validity of request */
disk_interrupt->reply = DISK_REPLY_OK;
blocknum = disk_request->request.blocknum;
buffer = disk_request->request.buffer;
type = disk_request->request.type;
if (DEBUG)
kprintf("Disk Controler: got a request for block %d type %d .\n",
blocknum, type);
/* If we got here is a read or a write request */
offset = DISK_BLOCK_SIZE*(blocknum + 1);
if ( (blocknum >= layout.size) ||
(fseek(disk->file, offset, SEEK_SET) != 0) ) {
disk_interrupt->reply = DISK_REPLY_ERROR;
if (DEBUG)
kprintf("Disk Controler: Block too big or failed fseek, block=%d, offset=%d, disk_size=%d.\n",
blocknum, offset, layout.size);
goto sendinterrupt;
}
switch (type) {
case DISK_READ:
if (fread(buffer, 1, DISK_BLOCK_SIZE, disk->file)
< DISK_BLOCK_SIZE)
disk_interrupt->reply = DISK_REPLY_ERROR;
if (DEBUG)
kprintf("Disk: Read request.\n");
break;
case DISK_WRITE:
if (fwrite(buffer, 1, DISK_BLOCK_SIZE, disk->file)
< DISK_BLOCK_SIZE)
disk_interrupt->reply = DISK_REPLY_ERROR;
fflush(disk->file);
if (DEBUG)
kprintf("Disk: Write request.\n");
break;
default:
break;
}
sendinterrupt:
if (DEBUG)
kprintf("Disk Controler: sending an interrupt for block %d, request type %d, with reply %d.\n",
disk_interrupt->request.blocknum,
disk_interrupt->request.type,
disk_interrupt->reply);
send_interrupt(DISK_INTERRUPT_TYPE, mini_disk_handler, (void*)disk_interrupt);
}
}
void start_disk_poll(disk_t* disk){
//int id;
pthread_t disk_thread;
sigset_t set;
struct sigaction sa;
sigset_t old_set;
sigemptyset(&set);
sigaddset(&set,SIGRTMAX-1);
sigaddset(&set,SIGRTMAX-2);
sigprocmask(SIG_BLOCK,&set,&old_set);
sa.sa_handler = (void*)handle_interrupt;
sa.sa_flags = SA_SIGINFO | SA_RESTART | SA_ONSTACK;
sa.sa_sigaction= (void*)handle_interrupt;
sigemptyset(&sa.sa_mask);
sigaddset(&sa.sa_mask,SIGRTMAX-2);
sigaddset(&sa.sa_mask,SIGRTMAX-1);
if (sigaction(SIGRTMAX-2, &sa, NULL) == -1)
AbortOnError(0);
/* reset the request queue */
disk->queue = disk->last = NULL;
/* create request semaphore */
AbortOnCondition(sem_init(&disk->semaphore, 0, 0),"sem_init");
AbortOnCondition(pthread_create(&disk_thread, NULL, (void*)disk_poll, (void*)disk),
"pthread");
pthread_sigmask(SIG_SETMASK,&old_set,NULL);
}
void install_disk_handler(interrupt_handler_t disk_handler){
kprintf("Starting disk interrupt.\n");
mini_disk_handler = disk_handler;
/* create mutex used to protect disk datastructures */
AbortOnCondition(pthread_mutex_init(&disk_mutex, NULL),"mutex");
}