13 #include "Hashtable.h"
18 struct filedesc files[MAXFILES];
19 struct InodeBitmap ib;
20 struct BlockBitmap bb;
21 int bbbptr,ibbptr,itbptr,rdiptr;
22 int main(int argc, char **argv) {
23 for(int i=0;i<MAXFILES;i++)
26 case '0': createdisk();
29 struct block * ptr=mountdisk("disk");
30 for(int i=0;i<145;i++) {
32 sprintf(filename,"fil%d",i);
33 openfile(ptr,filename);
35 for(int j=0;j<90;j++) {
36 for(int i=0;i<145;i++) {
37 char *buf="01234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123";
38 writefile(ptr,i,buf,122);
41 for(int i=0;i<145;i++) {
48 struct block * ptr=chmountdisk("disk");
50 Hashtable *env=exportmodel->gethashtable();
52 addmapping(dstring,ptr,"Disk");
53 // env->put(dstring,new Element(ptr,exportmodel->getstructure("Disk")));//should be of badstruct
60 struct block * ptr=mountdisk("disk");
62 for(int i=1;i<145;i++) {
64 sprintf(filename,"fil%d",i);
65 printfile(filename,ptr);
71 struct block * ptr=mountdisk("disk");
72 for(int i=145;i>1;i--) {
74 sprintf(filename,"fil%d",i);
75 openfile(ptr,filename);
77 for(int j=0;j<90;j++) {
78 for(int i=145;i>1;i--) {
79 char *buf="01234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123";
80 writefile(ptr,i,buf,122);
83 for(int i=145;i>1;i--) {
90 struct block * ptr=mountdisk("disk");
91 for(int i=145;i>=0;i--) {
93 sprintf(filename,"fil%d",i);
94 openfile(ptr,filename);
96 for(int j=0;j<6000;j++) {
97 for(int i=145;i>=0;i--) {
99 int len=sprintf(name, "%d ",i);
100 writefile(ptr,i,name,len);
103 for(int i=145;i>=0;i--) {
106 for(int i=145;i>=0;i--) {
108 sprintf(filename,"fil%d",i);
109 openfile(ptr,filename);
111 for(int j=0;j<400;j++) {
112 for(int i=145;i>=0;i--) {
115 int len=sprintf(name, "%d ",i);
116 readfile(ptr,i,name,len);
117 sscanf(name, "%d ", &l);
119 printf("ERROR in benchmark\n");
123 for(int i=145;i>=0;i--) {
131 struct block * ptr=chmountdisk("disk");
132 initializeanalysis();
133 Hashtable *env=exportmodel->gethashtable();
135 addmapping(dstring,ptr,"Disk");
140 struct block * ptr=mountdisk("disk");
141 for(int i=145;i>=0;i--) {
143 sprintf(filename,"fil%d",i);
144 openfile(ptr,filename);
146 for(int j=0;j<6000;j++) {
147 for(int i=145;i>=0;i--) {
149 int len=sprintf(name, "%d ",i);
150 writefile(ptr,i,name,len);
153 for(int i=145;i>=0;i--) {
156 for(int i=145;i>=0;i--) {
158 sprintf(filename,"fil%d",i);
159 openfile(ptr,filename);
161 for(int j=0;j<400;j++) {
162 for(int i=145;i>=0;i--) {
165 int len=sprintf(name, "%d ",i);
166 readfile(ptr,i,name,len);
167 sscanf(name, "%d ", &l);
169 printf("ERROR in benchmark\n");
173 for(int i=145;i>=0;i--) {
181 struct block * chmountdisk(char *filename) {
182 int fd=open(filename,O_CREAT|O_RDWR);
183 struct block *ptr=(struct block *) mmap(NULL,LENGTH,PROT_READ|PROT_WRITE|PROT_EXEC,MAP_SHARED,fd,0);
187 void chunmountdisk(struct block *vptr) {
188 int val=munmap(vptr,LENGTH);
191 struct block * mountdisk(char *filename) {
192 int fd=open(filename,O_CREAT|O_RDWR);
193 struct block *ptr=(struct block *) mmap(NULL,LENGTH,PROT_READ|PROT_WRITE|PROT_EXEC,MAP_SHARED,fd,0);
194 struct SuperBlock *sb=(struct SuperBlock *) &ptr[0];
195 struct GroupBlock *gb=(struct GroupBlock *) &ptr[1];
196 bbbptr=gb->BlockBitmapBlock;
197 ibbptr=gb->InodeBitmapBlock;
198 itbptr=gb->InodeTableBlock;
199 rdiptr=sb->RootDirectoryInode;
201 struct InodeBitmap *ibb=(struct InodeBitmap *) &ptr[ibbptr];
202 for(int i=0;i<(NUMINODES/8+1);i++)
203 ib.inode[i]=ibb->inode[i];
204 struct BlockBitmap *bbb=(struct BlockBitmap *) &ptr[bbbptr];
205 for(int i=0;i<(NUMBLOCK/8+1);i++)
206 bb.blocks[i]=bbb->blocks[i];
210 void unmountdisk(struct block *vptr) {
211 struct InodeBitmap *ibb=(struct InodeBitmap *) &vptr[ibbptr];
212 for(int i=0;i<(NUMINODES/8+1);i++)
213 ibb->inode[i]=ib.inode[i];
215 struct BlockBitmap *bbb=(struct BlockBitmap *) &vptr[bbbptr];
216 for(int i=0;i<(NUMBLOCK/8+1);i++)
217 bbb->blocks[i]=bb.blocks[i];
218 int val=munmap(vptr,LENGTH);
221 void printdirectory(struct block *ptr) {
222 struct InodeBlock * itb=(struct InodeBlock *) &ptr[itbptr];
223 for(int i=0;i<12;i++) {
224 struct DirectoryBlock *db=(struct DirectoryBlock *) &ptr[itb->entries[rdiptr].Blockptr[i]];
225 for(int j=0;j<BLOCKSIZE/128;j++) {
226 if (db->entries[j].name[0]!=0) {
228 printf("%s %d inode %d bytes\n",db->entries[j].name, db->entries[j].inodenumber,itb->entries[db->entries[j].inodenumber].filesize);
234 void printfile(char *filename, struct block *ptr) {
235 struct InodeBlock * itb=(struct InodeBlock *) &ptr[itbptr];
236 for(int i=0;i<12;i++) {
237 struct DirectoryBlock *db=(struct DirectoryBlock *) &ptr[itb->entries[rdiptr].Blockptr[i]];
238 for(int j=0;j<BLOCKSIZE/128;j++) {
239 if (db->entries[j].name[0]!=0) {
240 if(strcmp(filename,db->entries[j].name)==0) {
242 int inode=db->entries[j].inodenumber;
244 struct InodeBlock * itb=(struct InodeBlock *) &ptr[itbptr];
245 for(int i=0;i<((itb->entries[inode].filesize+BLOCKSIZE-1)/BLOCKSIZE);i++) {
246 struct block *b=&ptr[itb->entries[inode].Blockptr[i]];
247 write(0,b,BLOCKSIZE);
255 void removefile(char *filename, struct block *ptr) {
256 struct InodeBlock * itb=(struct InodeBlock *) &ptr[itbptr];
257 for(int i=0;i<12;i++) {
258 struct DirectoryBlock *db=(struct DirectoryBlock *) &ptr[itb->entries[rdiptr].Blockptr[i]];
259 for(int j=0;j<BLOCKSIZE/128;j++) {
260 if (db->entries[j].name[0]!=0) {
261 if(strcmp(filename,db->entries[j].name)==0) {
263 db->entries[j].name[0]=0; //Delete entry
264 int inode=db->entries[j].inodenumber;
266 struct InodeBlock * itb=(struct InodeBlock *) &ptr[itbptr];
267 itb->entries[inode].referencecount--;
269 if (itb->entries[inode].referencecount==0) {
270 for(int i=0;i<((itb->entries[inode].filesize+BLOCKSIZE-1)/BLOCKSIZE);i++) {
271 int blocknum=itb->entries[inode].Blockptr[i];
272 bb.blocks[blocknum/8]^=(1<<(blocknum%8));
274 ib.inode[inode/8]^=(1<<(inode%8));
282 void createlink(struct block *ptr,char *filename, char *linkname) {
283 struct InodeBlock * itb=(struct InodeBlock *) &ptr[itbptr];
284 for(int i=0;i<12;i++) {
285 struct DirectoryBlock *db=(struct DirectoryBlock *) &ptr[itb->entries[rdiptr].Blockptr[i]];
286 for(int j=0;j<BLOCKSIZE/128;j++) {
287 if (db->entries[j].name[0]!=0) {
288 if(strcmp(filename,db->entries[j].name)==0) {
290 int inode=db->entries[j].inodenumber;
291 struct InodeBlock * itb=(struct InodeBlock *) &ptr[4];
292 itb->entries[inode].referencecount++;
293 addtode(ptr, inode, linkname);
300 void closefile(struct block *ptr, int fd) {
301 struct InodeBlock * itb=(struct InodeBlock *) &ptr[4];
304 msync(&itb->entries[fd],sizeof(DirectoryEntry),MS_SYNC);
305 files[fd].used=false;
308 bool writefile(struct block *ptr, int fd, char *s) {
309 return (writefile(ptr,fd,s,1)==1);
312 int writefile(struct block *ptr, int fd, char *s, int len) {
313 struct filedesc *tfd=&files[fd];
314 if (tfd->used==false)
316 struct InodeBlock * itb=(struct InodeBlock *) &ptr[4];
317 int filelen=itb->entries[tfd->inode].filesize;
318 if ((12*BLOCKSIZE-tfd->offset)<len)
319 len=12*BLOCKSIZE-tfd->offset;
320 for(int i=0;i<len;i++) {
321 int nbuffer=tfd->offset/BLOCKSIZE;
322 int noffset=tfd->offset%BLOCKSIZE;
323 if (tfd->offset>=filelen) {
325 int bptr=getblock(ptr);
327 if (itb->entries[files[fd].inode].filesize<files[fd].offset)
328 itb->entries[files[fd].inode].filesize=files[fd].offset;
331 itb->entries[tfd->inode].Blockptr[nbuffer]=bptr;
334 int block=itb->entries[tfd->inode].Blockptr[nbuffer];
335 char *fchar=(char *)&ptr[block];
337 if (tocopy>(BLOCKSIZE-noffset))
338 tocopy=BLOCKSIZE-noffset;
339 memcpy(&fchar[noffset],&s[i],tocopy);
340 msync(&fchar[noffset],tocopy,MS_SYNC);
344 if (itb->entries[files[fd].inode].filesize<files[fd].offset)
345 itb->entries[files[fd].inode].filesize=files[fd].offset;
350 char readfile(struct block *ptr, int fd) {
352 if (readfile(ptr,fd,array,1)==1)
358 int readfile(struct block *ptr, int fd, char *buf, int len) {
359 struct filedesc *tfd=&files[fd];
360 if (tfd->used==false)
363 struct InodeBlock * itb=(struct InodeBlock *) &ptr[itbptr];
364 int filelen=itb->entries[tfd->inode].filesize;
365 if ((filelen-tfd->offset)<len)
366 len=filelen-tfd->offset;
367 for(int i=0;i<len;) {
368 int nbuffer=tfd->offset/BLOCKSIZE;
369 int noffset=tfd->offset%BLOCKSIZE;
370 int block=itb->entries[tfd->inode].Blockptr[nbuffer];
371 char *fchar=(char *)&ptr[block];
373 if (tocopy>(BLOCKSIZE-noffset))
374 tocopy=BLOCKSIZE-noffset;
375 memcpy(&buf[i],&fchar[noffset],tocopy);
382 int openfile(struct block *ptr, char *filename) {
385 for(int k=0;k<MAXFILES;k++) {
393 if (fd==-1) return fd;
395 /* Check to see if file exists*/
396 struct InodeBlock * itb=(struct InodeBlock *) &ptr[itbptr];
397 for(int i=0;i<12;i++) {
398 struct DirectoryBlock *db=(struct DirectoryBlock *) &ptr[itb->entries[rdiptr].Blockptr[i]];
399 for(int j=0;j<BLOCKSIZE/128;j++) {
400 if (db->entries[j].name[0]!=0) {
401 if(strcmp(filename,db->entries[j].name)==0) {
402 files[fd].inode=db->entries[j].inodenumber;
411 int inode=getinode(ptr);
413 files[fd].used=false;
416 itb->entries[inode].filesize=0;
417 itb->entries[inode].referencecount=1;
419 addtode(ptr, inode, filename);
420 files[fd].inode=inode;
425 void createfile(struct block *ptr,char *filename, char *buf,int buflen) {
426 int fd=openfile(ptr,filename);
427 writefile(ptr,fd,buf,buflen);
431 void addtode(struct block *ptr, int inode, char * filename) {
432 struct InodeBlock * itb=(struct InodeBlock *) &ptr[itbptr];
433 for(int i=0;i<12;i++) {
434 struct DirectoryBlock *db=(struct DirectoryBlock *) &ptr[itb->entries[rdiptr].Blockptr[i]];
435 for(int j=0;j<BLOCKSIZE/128;j++) {
436 if (db->entries[j].name[0]==0) {
438 strncpy(db->entries[j].name,filename,124);
439 db->entries[j].inodenumber=inode;
440 msync(&db->entries[j],sizeof(DirectoryEntry),MS_SYNC);
447 int getinode(struct block *ptr) {
448 for(int i=0;i<NUMINODES;i++) {
449 if (!(ib.inode[i/8]&(1<<(i%8)))) {
450 ib.inode[i/8]=ib.inode[i/8]|(1<<(i%8));
457 int getblock(struct block * ptr) {
458 for(int i=0;i<NUMBLOCK;i++) {
459 if (!(bb.blocks[i/8]&(1<<(i%8)))) {
460 bb.blocks[i/8]=bb.blocks[i/8]|(1<<(i%8));
470 int blocksize=BLOCKSIZE;
471 int numblocks=NUMBLOCK;
472 int fd=open("disk",O_CREAT|O_RDWR|O_TRUNC);
473 char *buf=(char *)calloc(1,blocksize);
474 for(int i=0;i<numblocks;i++) {
475 write(fd,buf,blocksize);
478 void *vptr=mmap(NULL,LENGTH,PROT_READ|PROT_WRITE|PROT_EXEC,MAP_SHARED,fd,0);
479 struct block *ptr=(struct block *)vptr;
481 struct SuperBlock * sb=(struct SuperBlock*) &ptr[0];
482 sb->FreeBlockCount=BLOCKSIZE-5;
483 sb->FreeInodeCount=NUMINODES-1;
484 sb->NumberofInodes=NUMINODES;
485 sb->NumberofBlocks=NUMBLOCK;
486 sb->RootDirectoryInode=0;
487 sb->blocksize=BLOCKSIZE;
490 struct GroupBlock * gb=(struct GroupBlock *) &ptr[1];
491 gb->BlockBitmapBlock=2;
492 gb->InodeBitmapBlock=3;
493 gb->InodeTableBlock=4;
494 gb->GroupFreeBlockCount=BLOCKSIZE-5;
495 gb->GroupFreeInodeCount=NUMINODES-1;
498 struct BlockBitmap * bb=(struct BlockBitmap *) &ptr[2];
499 for(int i=0;i<(5+12);i++)
500 bb->blocks[i/8]=bb->blocks[i/8]|(1<<(i%8));
503 struct InodeBitmap * ib=(struct InodeBitmap *) &ptr[3];
507 struct InodeBlock * itb=(struct InodeBlock *) &ptr[4];
508 itb->entries[0].filesize=12*BLOCKSIZE;
509 for(int i=0;i<12;i++)
510 itb->entries[0].Blockptr[i]=i+5;
511 itb->entries[0].referencecount=1;
514 int val=munmap(vptr,LENGTH);