History

made ream functions to set up the fs. imported upanic and shit from ftrace

Matthew Veety committed 10 years ago

Commit ac689a91 parent 91e59f65 files · +143 −2Browse files

Unified · Split

5 files
  1. blockalloc.h +6 −1
  2. disk.c +0 −1
  3. instru.c +36 −0
  4. mkfile +2 −0
  5. ream.c +99 −0
blockalloc.h+6−1
@@ -15,7 +15,6 @@ typedef struct Blockptr Blockptr; · 14 unmodified lines
1515 typedef struct BlockptrODF BlockptrODF;
1616 typedef struct Metablock Metablock;
1717 typedef struct Disk Disk;
18−typedef struct Arena Arena;
1918 typedef struct Fn Fn;
2019
2120 struct Fn {
@@ -81,6 +80,8 @@ int disksyncer(Disk*); · 59 unmodified lines
8180 void enableinstru(void);
8281 Fn* infn(char*);
8382 void action(Fn*,char*);
83+void shit(Fn*,char*);
84+void upanic(Fn*,char*);
8485 void outfn(Fn*);
8586 void iqlock(Fn*,QLock*);
8687 void iqunlock(Fn*,QLock*);
@@ -89,3 +90,7 @@ void iqunlock(Fn*,QLock*); · 2 unmodified lines
8990 // device, buffer, length, offset
9091 u64int dread(Disk*,void*,u64int,u64int);
9192 u64int dwrite(Disk*,void*,u64int,u64int);
93+
94+/* ream.c */
95+int ream(int,u64int);
96+int ream1(int,u64int,u64int,u64int,u64int);
disk.c+0−1
@@ -129,4 +129,3 @@ fd2disk(int fd, Disk* d, uchar opts) · 128 unmodified lines
129129 outfn(f);
130130 return 0;
131131 }
132−
instru.c+36−0
@@ -11,6 +11,12 @@ does the same thing. · 10 unmodified lines
1111
1212 int enable_instru = 0;
1313
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+
1420 void
1521 initinstru(void)
1622 {
@@ -44,6 +50,36 @@ action(Fn* f, char* c) · 27 unmodified lines
4450 }
4551 }
4652
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+
4783 void
4884 outfn(Fn* f)
4985 {
mkfile+2−0
@@ -7,12 +7,14 @@ OFILES=\ · 6 unmodified lines
77 pointers.$O \
88 instru.$O \
99 io.$O \
10+ ream.$O \
1011
1112 CFILES=\
1213 disk.c \
1314 pointers.c \
1415 instru.c \
1516 io.c \
17+ ream.c \
1618
1719 HFILES=\
1820 blockalloc.h \
ream.c+99−0
@@ -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+}