-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathblock.c
More file actions
113 lines (101 loc) · 2.94 KB
/
block.c
File metadata and controls
113 lines (101 loc) · 2.94 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
#include<stdio.h>
#include"fs.h"
#include"kernel.h"
#include"asm.h"
#include<string.h>
#define NR_BLK_DEV ((sizeof(rd_blk))/sizeof(rd_blk[0]))
struct filesystem deb;
char logical_blocks[BLOCK_SIZE*100];
int block_write(int dev,long* pos,char* buf,int count){
int block= *pos/BLOCK_SIZE;
int offset= *pos % BLOCK_SIZE;
int chars=0;
int written=0;
struct buffer_head* bh=NULL;
register char* p=NULL;
while(count > 0){
bh=bread(dev,block);
if(!bh){
if(written!=0){
return written;
}
else{
return -1;
}
}
/*
if(count < block size chars=count else chars=blksize)
*/
chars=(count < BLOCK_SIZE) ? count: BLOCK_SIZE;
p=offset+bh->b_data;
offset=0;
block++;
*pos+=chars;
written+=chars;
count-=chars;
while(chars-->0){
// *(p++)=get_fs_byte(buf++);
}
bh->b_status.b_dirt=1;
brelse(bh);
}
return written;
}
int block_read(int dev,long* pos,char* buf,int count){
int block= (*pos)/BLOCK_SIZE;
int offset=(*pos)%BLOCK_SIZE;
int chars=0;
int read=0;
struct buffer_head* bh=NULL;
register char* p=NULL;
while(count > 0){
bh=bread(dev,block);
if(!bh){
if(read!=0){
return read;
}
else{
return -1;
}
}
chars=(count < BLOCK_SIZE ) ? count: BLOCK_SIZE;
p=offset+bh->b_data;
offset=0;
block++;
*pos+= chars;
read+=chars;
count-=chars;
while(chars-->0){
// put_fs_byte(*(p++),buf++);
}
bh->b_status.b_dirt=1;
brelse(bh);
}
return read;
}
extern void rw_hd(int rw,struct buffer_head* bh);
struct buffer_head* alloc(int dev){
struct buffer_head* bh=NULL;
while(super_block.s_locked)
continue;
int blkno=super_block.s_free_blk_ls->array[super_block.s_next_free_blk--];
if(!(bh=getblk(dev,blkno)))
return NULL;
memset(bh->b_data,'\0',sizeof(bh->b_data));
super_block.s_mod=true;
return bh;
}
void free_blk(int num){};
void ll_rw_block(int rw,struct buffer_head* bh){
int blkno=bh->b_blocknr;
if(rw==READ){
printf("\nReading from %p to %p\n",&logical_blocks[blkno*BLOCK_SIZE],&logical_blocks[blkno*2*BLOCK_SIZE]);
memcpy(bh->b_data,logical_blocks+(blkno*BLOCK_SIZE),BLOCK_SIZE);
}
else if(rw==WRITE){
memcpy(logical_blocks+(blkno*BLOCK_SIZE),bh->b_data,BLOCK_SIZE);
}
else{
panic("GIve either read or write\n");
}
}