| @@ -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 | +} |