Unified · Split
| @@ -15,7 +15,6 @@ typedef struct Blockptr Blockptr; · 14 unmodified lines | |||
| 15 | typedef struct BlockptrODF BlockptrODF; | 15 | typedef struct BlockptrODF BlockptrODF; |
| 16 | typedef struct Metablock Metablock; | 16 | typedef struct Metablock Metablock; |
| 17 | typedef struct Disk Disk; | 17 | typedef struct Disk Disk; |
| 18 | typedef struct Arena Arena; | ||
| 19 | typedef struct Fn Fn; | 18 | typedef struct Fn Fn; |
| 20 | 19 | ||
| 21 | struct Fn { | 20 | struct Fn { |
| @@ -81,6 +80,8 @@ int disksyncer(Disk*); · 59 unmodified lines | |||
| 81 | void enableinstru(void); | 80 | void enableinstru(void); |
| 82 | Fn* infn(char*); | 81 | Fn* infn(char*); |
| 83 | void action(Fn*,char*); | 82 | void action(Fn*,char*); |
| 83 | void shit(Fn*,char*); | ||
| 84 | void upanic(Fn*,char*); | ||
| 84 | void outfn(Fn*); | 85 | void outfn(Fn*); |
| 85 | void iqlock(Fn*,QLock*); | 86 | void iqlock(Fn*,QLock*); |
| 86 | void iqunlock(Fn*,QLock*); | 87 | void iqunlock(Fn*,QLock*); |
| @@ -89,3 +90,7 @@ void iqunlock(Fn*,QLock*); · 2 unmodified lines | |||
| 89 | // device, buffer, length, offset | 90 | // device, buffer, length, offset |
| 90 | u64int dread(Disk*,void*,u64int,u64int); | 91 | u64int dread(Disk*,void*,u64int,u64int); |
| 91 | u64int dwrite(Disk*,void*,u64int,u64int); | 92 | u64int dwrite(Disk*,void*,u64int,u64int); |
| 93 | |||
| 94 | /* ream.c */ | ||
| 95 | int ream(int,u64int); | ||
| 96 | int ream1(int,u64int,u64int,u64int,u64int); | ||
| @@ -129,4 +129,3 @@ fd2disk(int fd, Disk* d, uchar opts) · 128 unmodified lines | |||
| 129 | outfn(f); | 129 | outfn(f); |
| 130 | return 0; | 130 | return 0; |
| 131 | } | 131 | } |
| 132 | |||
| @@ -11,6 +11,12 @@ does the same thing. · 10 unmodified lines | |||
| 11 | 11 | ||
| 12 | int enable_instru = 0; | 12 | int enable_instru = 0; |
| 13 | 13 | ||
| 14 | /* | ||
| 15 | if using ftrace, run this *very* first. infn calls started before | ||
| 16 | this will not start logging until the next time the function gets | ||
| 17 | called. | ||
| 18 | */ | ||
| 19 | |||
| 14 | void | 20 | void |
| 15 | initinstru(void) | 21 | initinstru(void) |
| 16 | { | 22 | { |
| @@ -44,6 +50,36 @@ action(Fn* f, char* c) · 27 unmodified lines | |||
| 44 | } | 50 | } |
| 45 | } | 51 | } |
| 46 | 52 | ||
| 53 | void | ||
| 54 | shit(Fn* f, char *c) | ||
| 55 | { | ||
| 56 | if(f == nil || enable_instru == 0){ | ||
| 57 | return; | ||
| 58 | } else if (f->active == 1 && !canqlock(&f->l)){ | ||
| 59 | fprint(2,"instru: error: %s: %s (explicit)\n",f->name,c); | ||
| 60 | } else if (canqlock(&f->l)) { | ||
| 61 | fprint(2,"instru: error: action %s done outside of function (automatic)\n",c); | ||
| 62 | } | ||
| 63 | } | ||
| 64 | |||
| 65 | void | ||
| 66 | upanic(Fn* f, char *c) | ||
| 67 | { | ||
| 68 | char *msg; | ||
| 69 | if(f == nil || enable_instru == 0) | ||
| 70 | return; | ||
| 71 | msg = smprint("instru: PANIC: %s: %s [pid %d]",f->name,c,getpid()); | ||
| 72 | if (f->active == 1 && !canqlock(&f->l)){ | ||
| 73 | fprint(2,"%s\n",msg); | ||
| 74 | exits(msg); | ||
| 75 | return; | ||
| 76 | } else { | ||
| 77 | fprint(2,"%s (panic outside function calling)\n",msg); | ||
| 78 | exits(smprint("%s (panic outside function calling)",msg)); | ||
| 79 | return; | ||
| 80 | } | ||
| 81 | } | ||
| 82 | |||
| 47 | void | 83 | void |
| 48 | outfn(Fn* f) | 84 | outfn(Fn* f) |
| 49 | { | 85 | { |
| @@ -7,12 +7,14 @@ OFILES=\ · 6 unmodified lines | |||
| 7 | pointers.$O \ | 7 | pointers.$O \ |
| 8 | instru.$O \ | 8 | instru.$O \ |
| 9 | io.$O \ | 9 | io.$O \ |
| 10 | ream.$O \ | ||
| 10 | 11 | ||
| 11 | CFILES=\ | 12 | CFILES=\ |
| 12 | disk.c \ | 13 | disk.c \ |
| 13 | pointers.c \ | 14 | pointers.c \ |
| 14 | instru.c \ | 15 | instru.c \ |
| 15 | io.c \ | 16 | io.c \ |
| 17 | ream.c \ | ||
| 16 | 18 | ||
| 17 | HFILES=\ | 19 | HFILES=\ |
| 18 | blockalloc.h \ | 20 | blockalloc.h \ |
| @@ -0,0 +1,99 @@ | |||
| 1 | #include <u.h> | ||
| 2 | #include <libc.h> | ||
| 3 | #include <blockalloc.h> | ||
| 4 | |||
| 5 | int | ||
| 6 | ream(int fd, u64int blocksize) | ||
| 7 | { | ||
| 8 | Fn *f = infn("ream"); | ||
| 9 | u64int disksize = seek(fd,0,2); seek(fd,0,0); | ||
| 10 | u64int ptrmapstart = blocksize*2; | ||
| 11 | u64int bmapsize = (blocksize*(disksize-blocksize))/(sizeof(BlockptrODF)+blocksize); | ||
| 12 | u64int ptrmaplen = (bmapsize/blocksize)*sizeof(BlockptrODF); | ||
| 13 | u64int bmapstart = ptrmaplen+sizeof(BlockptrODF); | ||
| 14 | u64int totalblocks = bmapsize/blocksize; | ||
| 15 | int retval = ream1(fd,blocksize,ptrmapstart,bmapstart,totalblocks); | ||
| 16 | action(f,smprint("retval = %d",retval)); | ||
| 17 | if(retval == 0){ | ||
| 18 | action(f,smprint("disksize = %d bytes",disksize)); | ||
| 19 | action(f,smprint("ptrmapstart = %x",ptrmapstart)); | ||
| 20 | action(f,smprint("ptrmaplen = %d bytes",ptrmaplen)); | ||
| 21 | action(f,smprint("bmapstart = %x",bmapstart)); | ||
| 22 | action(f,smprint("bmapsize = %d bytes",bmapsize)); | ||
| 23 | action(f,smprint("totalblocks = %d of %d byte blocks",totalblocks,blocksize)); | ||
| 24 | } else { | ||
| 25 | switch(retval){ | ||
| 26 | case -1: | ||
| 27 | shit(f,"non-fatal error reaming. (code 1)"); | ||
| 28 | break; | ||
| 29 | default: | ||
| 30 | shit(f,smprint("fatal error reaming.")); | ||
| 31 | char *err = mallocz(1024*sizeof(char),0); | ||
| 32 | errstr(err,1024); | ||
| 33 | upanic(f,smprint("retval = %d, errstr = %s",retval,err)); | ||
| 34 | break; | ||
| 35 | } | ||
| 36 | } | ||
| 37 | outfn(f); | ||
| 38 | return retval; | ||
| 39 | } | ||
| 40 | |||
| 41 | int | ||
| 42 | ream1(int fd, u64int blocksize, u64int ptrmapstart, u64int bmapstart, u64int totalblocks) | ||
| 43 | { | ||
| 44 | Fn *f = infn("ream1"); | ||
| 45 | Metablock *m = mallocz(sizeof(Metablock),0); | ||
| 46 | BlockptrODF *blocks; | ||
| 47 | if(m == nil){ | ||
| 48 | shit(f,"malloc failed"); | ||
| 49 | outfn(f); | ||
| 50 | return -2; | ||
| 51 | } | ||
| 52 | m->blocksize = blocksize; | ||
| 53 | m->ptrmapstart = ptrmapstart; | ||
| 54 | m->totalblocks = totalblocks; | ||
| 55 | m->bitmapstart = bmapstart; | ||
| 56 | // sanity checking | ||
| 57 | if(((bmapstart-ptrmapstart)/sizeof(BlockptrODF)) >= totalblocks){ | ||
| 58 | shit(f,smprint("ptrmap not large enough (failed: %x >= %x)", | ||
| 59 | ((bmapstart-ptrmapstart)/sizeof(BlockptrODF)), | ||
| 60 | totalblocks)); | ||
| 61 | outfn(f); | ||
| 62 | return -3; | ||
| 63 | } else if (blocksize <= sizeof(BlockptrODF)){ | ||
| 64 | shit(f,smprint("blocksize too small (blockptr)")); | ||
| 65 | outfn(f); | ||
| 66 | return -4; | ||
| 67 | } else if (blocksize <= sizeof(Metablock)){ | ||
| 68 | shit(f,smprint("blocksize too small (metablock)")); | ||
| 69 | outfn(f); | ||
| 70 | return -5; | ||
| 71 | } | ||
| 72 | u64int written = pwriten(fd,m,0,sizeof(Metablock)); | ||
| 73 | if(written < sizeof(Metablock)){ | ||
| 74 | shit(f,smprint("whole metablock not written")); | ||
| 75 | outfn(f); | ||
| 76 | return -6; | ||
| 77 | } | ||
| 78 | blocks = mallocz(totalblocks*sizeof(BlockptrODF),0); | ||
| 79 | if(blocks == nil){ | ||
| 80 | upanic(f,"malloc"); | ||
| 81 | outfn(f); | ||
| 82 | return -7; | ||
| 83 | } | ||
| 84 | action(f,"formatting blocks"); | ||
| 85 | for(u64int i = 0; i<totalblocks; i++){ | ||
| 86 | blocks[i].used = 0; | ||
| 87 | blocks[i].offset = blocksize*i; | ||
| 88 | } | ||
| 89 | written = pwriten(fd,blocks,ptrmapstart,sizeof(BlockptrODF)*totalblocks); | ||
| 90 | if(written < (sizeof(BlockptrODF)*totalblocks)){ | ||
| 91 | shit(f,"all blocksptrs not written"); | ||
| 92 | outfn(f); | ||
| 93 | return -8; | ||
| 94 | } | ||
| 95 | action(f,"ream done. cleaning up"); | ||
| 96 | free(m); | ||
| 97 | free(blocks); | ||
| 98 | return 0; | ||
| 99 | } | ||