1 a0d146ed 2005-07-12 devnull #include "stdinc.h"
2 a0d146ed 2005-07-12 devnull #include "dat.h"
3 a0d146ed 2005-07-12 devnull #include "fns.h"
5 a0d146ed 2005-07-12 devnull typedef struct AHash AHash;
8 a0d146ed 2005-07-12 devnull * hash table for finding arena's based on their names.
10 a0d146ed 2005-07-12 devnull struct AHash
12 a0d146ed 2005-07-12 devnull AHash *next;
13 a0d146ed 2005-07-12 devnull Arena *arena;
18 a0d146ed 2005-07-12 devnull AHashSize = 512
21 a0d146ed 2005-07-12 devnull static AHash *ahash[AHashSize];
23 a0d146ed 2005-07-12 devnull static u32int
24 a0d146ed 2005-07-12 devnull hashstr(char *s)
26 a0d146ed 2005-07-12 devnull u32int h;
30 a0d146ed 2005-07-12 devnull for(; c = *s; s++){
31 a0d146ed 2005-07-12 devnull c ^= c << 6;
32 a0d146ed 2005-07-12 devnull h += (c << 11) ^ (c >> 1);
34 a0d146ed 2005-07-12 devnull h ^= (c << 14) + (c << 7) + (c << 4) + c;
36 a0d146ed 2005-07-12 devnull return h;
40 a0d146ed 2005-07-12 devnull addarena(Arena *arena)
42 a0d146ed 2005-07-12 devnull AHash *a;
43 a0d146ed 2005-07-12 devnull u32int h;
45 a0d146ed 2005-07-12 devnull h = hashstr(arena->name) & (AHashSize - 1);
46 a0d146ed 2005-07-12 devnull a = MK(AHash);
47 a0d146ed 2005-07-12 devnull if(a == nil)
48 a0d146ed 2005-07-12 devnull return -1;
49 a0d146ed 2005-07-12 devnull a->arena = arena;
50 a0d146ed 2005-07-12 devnull a->next = ahash[h];
51 a0d146ed 2005-07-12 devnull ahash[h] = a;
52 a0d146ed 2005-07-12 devnull return 0;
56 a0d146ed 2005-07-12 devnull findarena(char *name)
58 a0d146ed 2005-07-12 devnull AHash *a;
59 a0d146ed 2005-07-12 devnull u32int h;
61 a0d146ed 2005-07-12 devnull h = hashstr(name) & (AHashSize - 1);
62 a0d146ed 2005-07-12 devnull for(a = ahash[h]; a != nil; a = a->next)
63 a0d146ed 2005-07-12 devnull if(strcmp(a->arena->name, name) == 0)
64 a0d146ed 2005-07-12 devnull return a->arena;
65 a0d146ed 2005-07-12 devnull return nil;
69 a0d146ed 2005-07-12 devnull delarena(Arena *arena)
71 a0d146ed 2005-07-12 devnull AHash *a, *last;
72 a0d146ed 2005-07-12 devnull u32int h;
74 a0d146ed 2005-07-12 devnull h = hashstr(arena->name) & (AHashSize - 1);
75 a0d146ed 2005-07-12 devnull last = nil;
76 a0d146ed 2005-07-12 devnull for(a = ahash[h]; a != nil; a = a->next){
77 a0d146ed 2005-07-12 devnull if(a->arena == arena){
78 a0d146ed 2005-07-12 devnull if(last != nil)
79 a0d146ed 2005-07-12 devnull last->next = a->next;
81 a0d146ed 2005-07-12 devnull ahash[h] = a->next;
83 a0d146ed 2005-07-12 devnull return 0;
85 a0d146ed 2005-07-12 devnull last = a;
87 a0d146ed 2005-07-12 devnull return -1;
90 a0d146ed 2005-07-12 devnull ArenaPart*
91 a0d146ed 2005-07-12 devnull initarenapart(Part *part)
93 a0d146ed 2005-07-12 devnull AMapN amn;
94 a0d146ed 2005-07-12 devnull ArenaPart *ap;
95 a0d146ed 2005-07-12 devnull ZBlock *b;
96 a0d146ed 2005-07-12 devnull u32int i;
99 a0d146ed 2005-07-12 devnull b = alloczblock(HeadSize, 0, 0);
100 a0d146ed 2005-07-12 devnull if(b == nil || readpart(part, PartBlank, b->data, HeadSize) < 0){
101 a0d146ed 2005-07-12 devnull seterr(EAdmin, "can't read arena partition header: %r");
102 a0d146ed 2005-07-12 devnull return nil;
105 a0d146ed 2005-07-12 devnull ap = MKZ(ArenaPart);
106 a0d146ed 2005-07-12 devnull if(ap == nil){
107 a0d146ed 2005-07-12 devnull freezblock(b);
108 a0d146ed 2005-07-12 devnull return nil;
110 a0d146ed 2005-07-12 devnull ap->part = part;
111 a0d146ed 2005-07-12 devnull ok = unpackarenapart(ap, b->data);
112 a0d146ed 2005-07-12 devnull freezblock(b);
113 a0d146ed 2005-07-12 devnull if(ok < 0){
114 a0d146ed 2005-07-12 devnull freearenapart(ap, 0);
115 a0d146ed 2005-07-12 devnull return nil;
118 a0d146ed 2005-07-12 devnull ap->tabbase = (PartBlank + HeadSize + ap->blocksize - 1) & ~(ap->blocksize - 1);
119 a0d146ed 2005-07-12 devnull if(ap->version != ArenaPartVersion){
120 a0d146ed 2005-07-12 devnull seterr(ECorrupt, "unknown arena partition version %d", ap->version);
121 a0d146ed 2005-07-12 devnull freearenapart(ap, 0);
122 a0d146ed 2005-07-12 devnull return nil;
124 a0d146ed 2005-07-12 devnull if(ap->blocksize & (ap->blocksize - 1)){
125 a0d146ed 2005-07-12 devnull seterr(ECorrupt, "illegal non-power-of-2 block size %d\n", ap->blocksize);
126 a0d146ed 2005-07-12 devnull freearenapart(ap, 0);
127 a0d146ed 2005-07-12 devnull return nil;
129 a0d146ed 2005-07-12 devnull if(ap->tabbase >= ap->arenabase){
130 a0d146ed 2005-07-12 devnull seterr(ECorrupt, "arena partition table overlaps with arena storage");
131 a0d146ed 2005-07-12 devnull freearenapart(ap, 0);
132 a0d146ed 2005-07-12 devnull return nil;
134 a0d146ed 2005-07-12 devnull ap->tabsize = ap->arenabase - ap->tabbase;
135 a0d146ed 2005-07-12 devnull partblocksize(part, ap->blocksize);
136 a0d146ed 2005-07-12 devnull ap->size = ap->part->size & ~(u64int)(ap->blocksize - 1);
138 a0d146ed 2005-07-12 devnull if(readarenamap(&amn, part, ap->tabbase, ap->tabsize) < 0){
139 a0d146ed 2005-07-12 devnull freearenapart(ap, 0);
140 a0d146ed 2005-07-12 devnull return nil;
142 a0d146ed 2005-07-12 devnull ap->narenas = amn.n;
143 a0d146ed 2005-07-12 devnull ap->map = amn.map;
144 a0d146ed 2005-07-12 devnull if(okamap(ap->map, ap->narenas, ap->arenabase, ap->size, "arena table") < 0){
145 a0d146ed 2005-07-12 devnull freearenapart(ap, 0);
146 a0d146ed 2005-07-12 devnull return nil;
149 a0d146ed 2005-07-12 devnull ap->arenas = MKNZ(Arena*, ap->narenas);
150 a0d146ed 2005-07-12 devnull for(i = 0; i < ap->narenas; i++){
151 a0d146ed 2005-07-12 devnull ap->arenas[i] = initarena(part, ap->map[i].start, ap->map[i].stop - ap->map[i].start, ap->blocksize);
152 a0d146ed 2005-07-12 devnull if(ap->arenas[i] == nil){
153 a0d146ed 2005-07-12 devnull seterr(ECorrupt, "%s: %r", ap->map[i].name);
154 a0d146ed 2005-07-12 devnull freearenapart(ap, 1);
155 a0d146ed 2005-07-12 devnull return nil;
157 a0d146ed 2005-07-12 devnull if(namecmp(ap->map[i].name, ap->arenas[i]->name) != 0){
158 a0d146ed 2005-07-12 devnull seterr(ECorrupt, "arena name mismatches with expected name: %s vs. %s",
159 a0d146ed 2005-07-12 devnull ap->map[i].name, ap->arenas[i]->name);
160 a0d146ed 2005-07-12 devnull freearenapart(ap, 1);
161 a0d146ed 2005-07-12 devnull return nil;
163 a0d146ed 2005-07-12 devnull if(findarena(ap->arenas[i]->name)){
164 a0d146ed 2005-07-12 devnull seterr(ECorrupt, "duplicate arena name %s in %s",
165 a0d146ed 2005-07-12 devnull ap->map[i].name, ap->part->name);
166 a0d146ed 2005-07-12 devnull freearenapart(ap, 1);
167 a0d146ed 2005-07-12 devnull return nil;
171 a0d146ed 2005-07-12 devnull for(i = 0; i < ap->narenas; i++)
172 a0d146ed 2005-07-12 devnull addarena(ap->arenas[i]);
174 a0d146ed 2005-07-12 devnull return ap;
177 a0d146ed 2005-07-12 devnull ArenaPart*
178 a0d146ed 2005-07-12 devnull newarenapart(Part *part, u32int blocksize, u32int tabsize)
180 a0d146ed 2005-07-12 devnull ArenaPart *ap;
182 a0d146ed 2005-07-12 devnull if(blocksize & (blocksize - 1)){
183 a0d146ed 2005-07-12 devnull seterr(ECorrupt, "illegal non-power-of-2 block size %d\n", blocksize);
184 a0d146ed 2005-07-12 devnull return nil;
186 a0d146ed 2005-07-12 devnull ap = MKZ(ArenaPart);
187 a0d146ed 2005-07-12 devnull if(ap == nil)
188 a0d146ed 2005-07-12 devnull return nil;
190 a0d146ed 2005-07-12 devnull ap->version = ArenaPartVersion;
191 a0d146ed 2005-07-12 devnull ap->part = part;
192 a0d146ed 2005-07-12 devnull ap->blocksize = blocksize;
193 a0d146ed 2005-07-12 devnull partblocksize(part, blocksize);
194 a0d146ed 2005-07-12 devnull ap->size = part->size & ~(u64int)(blocksize - 1);
195 a0d146ed 2005-07-12 devnull ap->tabbase = (PartBlank + HeadSize + blocksize - 1) & ~(blocksize - 1);
196 a0d146ed 2005-07-12 devnull ap->arenabase = (ap->tabbase + tabsize + blocksize - 1) & ~(blocksize - 1);
197 a0d146ed 2005-07-12 devnull ap->tabsize = ap->arenabase - ap->tabbase;
198 a0d146ed 2005-07-12 devnull ap->narenas = 0;
200 a0d146ed 2005-07-12 devnull if(wbarenapart(ap) < 0){
201 a0d146ed 2005-07-12 devnull freearenapart(ap, 0);
202 a0d146ed 2005-07-12 devnull return nil;
205 a0d146ed 2005-07-12 devnull return ap;
209 a0d146ed 2005-07-12 devnull wbarenapart(ArenaPart *ap)
211 a0d146ed 2005-07-12 devnull ZBlock *b;
213 a0d146ed 2005-07-12 devnull if(okamap(ap->map, ap->narenas, ap->arenabase, ap->size, "arena table") < 0)
214 a0d146ed 2005-07-12 devnull return -1;
215 a0d146ed 2005-07-12 devnull b = alloczblock(HeadSize, 1, 0);
216 a0d146ed 2005-07-12 devnull if(b == nil)
217 a0d146ed 2005-07-12 devnull //ZZZ set error message?
218 a0d146ed 2005-07-12 devnull return -1;
220 a0d146ed 2005-07-12 devnull if(packarenapart(ap, b->data) < 0){
221 a0d146ed 2005-07-12 devnull seterr(ECorrupt, "can't make arena partition header: %r");
222 a0d146ed 2005-07-12 devnull freezblock(b);
223 a0d146ed 2005-07-12 devnull return -1;
225 a0d146ed 2005-07-12 devnull if(writepart(ap->part, PartBlank, b->data, HeadSize) < 0){
226 a0d146ed 2005-07-12 devnull seterr(EAdmin, "can't write arena partition header: %r");
227 a0d146ed 2005-07-12 devnull freezblock(b);
228 a0d146ed 2005-07-12 devnull return -1;
230 a0d146ed 2005-07-12 devnull freezblock(b);
232 a0d146ed 2005-07-12 devnull return wbarenamap(ap->map, ap->narenas, ap->part, ap->tabbase, ap->tabsize);
236 a0d146ed 2005-07-12 devnull freearenapart(ArenaPart *ap, int freearenas)
240 a0d146ed 2005-07-12 devnull if(ap == nil)
242 a0d146ed 2005-07-12 devnull if(freearenas){
243 a0d146ed 2005-07-12 devnull for(i = 0; i < ap->narenas; i++){
244 a0d146ed 2005-07-12 devnull if(ap->arenas[i] == nil)
245 a0d146ed 2005-07-12 devnull continue;
246 a0d146ed 2005-07-12 devnull delarena(ap->arenas[i]);
247 a0d146ed 2005-07-12 devnull freearena(ap->arenas[i]);
250 a0d146ed 2005-07-12 devnull free(ap->map);
251 a0d146ed 2005-07-12 devnull free(ap->arenas);
252 a0d146ed 2005-07-12 devnull free(ap);
256 a0d146ed 2005-07-12 devnull okamap(AMap *am, int n, u64int start, u64int stop, char *what)
258 a0d146ed 2005-07-12 devnull u64int last;
259 a0d146ed 2005-07-12 devnull u32int i;
261 a0d146ed 2005-07-12 devnull last = start;
262 a0d146ed 2005-07-12 devnull for(i = 0; i < n; i++){
263 a0d146ed 2005-07-12 devnull if(am[i].start < last){
264 a0d146ed 2005-07-12 devnull if(i == 0)
265 a0d146ed 2005-07-12 devnull seterr(ECorrupt, "invalid start address in %s", what);
267 a0d146ed 2005-07-12 devnull seterr(ECorrupt, "overlapping ranges in %s", what);
268 a0d146ed 2005-07-12 devnull return -1;
270 a0d146ed 2005-07-12 devnull if(am[i].stop < am[i].start){
271 a0d146ed 2005-07-12 devnull seterr(ECorrupt, "invalid range in %s", what);
272 a0d146ed 2005-07-12 devnull return -1;
274 a0d146ed 2005-07-12 devnull last = am[i].stop;
276 a0d146ed 2005-07-12 devnull if(last > stop){
277 a0d146ed 2005-07-12 devnull seterr(ECorrupt, "invalid ending address in %s", what);
278 a0d146ed 2005-07-12 devnull return -1;
280 a0d146ed 2005-07-12 devnull return 0;
284 a0d146ed 2005-07-12 devnull maparenas(AMap *am, Arena **arenas, int n, char *what)
286 a0d146ed 2005-07-12 devnull u32int i;
288 a0d146ed 2005-07-12 devnull for(i = 0; i < n; i++){
289 a0d146ed 2005-07-12 devnull arenas[i] = findarena(am[i].name);
290 a0d146ed 2005-07-12 devnull if(arenas[i] == nil){
291 a0d146ed 2005-07-12 devnull seterr(EAdmin, "can't find arena '%s' for '%s'\n", am[i].name, what);
292 a0d146ed 2005-07-12 devnull return -1;
295 a0d146ed 2005-07-12 devnull return 0;
299 a0d146ed 2005-07-12 devnull readarenamap(AMapN *amn, Part *part, u64int base, u32int size)
301 a0d146ed 2005-07-12 devnull IFile f;
302 a0d146ed 2005-07-12 devnull u32int ok;
304 a0d146ed 2005-07-12 devnull if(partifile(&f, part, base, size) < 0)
305 a0d146ed 2005-07-12 devnull return -1;
306 a0d146ed 2005-07-12 devnull ok = parseamap(&f, amn);
307 a0d146ed 2005-07-12 devnull freeifile(&f);
308 a0d146ed 2005-07-12 devnull return ok;
312 a0d146ed 2005-07-12 devnull wbarenamap(AMap *am, int n, Part *part, u64int base, u64int size)
315 a0d146ed 2005-07-12 devnull ZBlock *b;
317 a0d146ed 2005-07-12 devnull b = alloczblock(size, 1, part->blocksize);
318 a0d146ed 2005-07-12 devnull if(b == nil)
319 a0d146ed 2005-07-12 devnull return -1;
321 a0d146ed 2005-07-12 devnull fmtzbinit(&f, b);
323 a0d146ed 2005-07-12 devnull if(outputamap(&f, am, n) < 0){
324 a0d146ed 2005-07-12 devnull seterr(ECorrupt, "arena set size too small");
325 a0d146ed 2005-07-12 devnull freezblock(b);
326 a0d146ed 2005-07-12 devnull return -1;
328 a0d146ed 2005-07-12 devnull if(writepart(part, base, b->data, size) < 0){
329 a0d146ed 2005-07-12 devnull seterr(EAdmin, "can't write arena set: %r");
330 a0d146ed 2005-07-12 devnull freezblock(b);
331 a0d146ed 2005-07-12 devnull return -1;
333 a0d146ed 2005-07-12 devnull freezblock(b);
334 a0d146ed 2005-07-12 devnull return 0;
338 a0d146ed 2005-07-12 devnull * amap: n '\n' amapelem * n
339 a0d146ed 2005-07-12 devnull * n: u32int
340 a0d146ed 2005-07-12 devnull * amapelem: name '\t' astart '\t' asize '\n'
341 a0d146ed 2005-07-12 devnull * astart, asize: u64int
344 a0d146ed 2005-07-12 devnull parseamap(IFile *f, AMapN *amn)
346 a0d146ed 2005-07-12 devnull AMap *am;
347 a0d146ed 2005-07-12 devnull u64int v64;
348 a0d146ed 2005-07-12 devnull u32int v;
349 a0d146ed 2005-07-12 devnull char *s, *t, *flds[4];
350 a0d146ed 2005-07-12 devnull int i, n;
353 a0d146ed 2005-07-12 devnull * arenas
355 a0d146ed 2005-07-12 devnull if(ifileu32int(f, &v) < 0){
356 a0d146ed 2005-07-12 devnull seterr(ECorrupt, "syntax error: bad number of elements in %s", f->name);
357 a0d146ed 2005-07-12 devnull return -1;
360 a0d146ed 2005-07-12 devnull if(n > MaxAMap){
361 a0d146ed 2005-07-12 devnull seterr(ECorrupt, "illegal number of elements in %s", f->name);
362 a0d146ed 2005-07-12 devnull return -1;
364 a0d146ed 2005-07-12 devnull am = MKNZ(AMap, n);
365 a0d146ed 2005-07-12 devnull if(am == nil){
366 a0d146ed 2005-07-12 devnull fprint(2, "out of memory\n");
367 a0d146ed 2005-07-12 devnull return -1;
369 a0d146ed 2005-07-12 devnull for(i = 0; i < n; i++){
370 a0d146ed 2005-07-12 devnull s = ifileline(f);
372 a0d146ed 2005-07-12 devnull t = estrdup(s);
374 a0d146ed 2005-07-12 devnull t = nil;
375 a0d146ed 2005-07-12 devnull if(s == nil || getfields(s, flds, 4, 0, "\t") != 3){
376 a0d146ed 2005-07-12 devnull fprint(2, "early eof after %d of %d, %s:#%d: %s\n", i, n, f->name, f->pos, t);
377 a0d146ed 2005-07-12 devnull free(t);
378 a0d146ed 2005-07-12 devnull return -1;
380 a0d146ed 2005-07-12 devnull free(t);
381 a0d146ed 2005-07-12 devnull if(nameok(flds[0]) < 0)
382 a0d146ed 2005-07-12 devnull return -1;
383 a0d146ed 2005-07-12 devnull namecp(am[i].name, flds[0]);
384 a0d146ed 2005-07-12 devnull if(stru64int(flds[1], &v64) < 0){
385 a0d146ed 2005-07-12 devnull seterr(ECorrupt, "syntax error: bad arena base address in %s", f->name);
386 a0d146ed 2005-07-12 devnull free(am);
387 a0d146ed 2005-07-12 devnull return -1;
389 a0d146ed 2005-07-12 devnull am[i].start = v64;
390 a0d146ed 2005-07-12 devnull if(stru64int(flds[2], &v64) < 0){
391 a0d146ed 2005-07-12 devnull seterr(ECorrupt, "syntax error: bad arena size in %s", f->name);
392 a0d146ed 2005-07-12 devnull free(am);
393 a0d146ed 2005-07-12 devnull return -1;
395 a0d146ed 2005-07-12 devnull am[i].stop = v64;
398 a0d146ed 2005-07-12 devnull amn->map = am;
399 a0d146ed 2005-07-12 devnull amn->n = n;
400 a0d146ed 2005-07-12 devnull return 0;
404 a0d146ed 2005-07-12 devnull outputamap(Fmt *f, AMap *am, int n)
408 a0d146ed 2005-07-12 devnull if(fmtprint(f, "%ud\n", n) < 0)
409 a0d146ed 2005-07-12 devnull return -1;
410 a0d146ed 2005-07-12 devnull for(i = 0; i < n; i++)
411 a0d146ed 2005-07-12 devnull if(fmtprint(f, "%s\t%llud\t%llud\n", am[i].name, am[i].start, am[i].stop) < 0)
412 a0d146ed 2005-07-12 devnull return -1;
413 a0d146ed 2005-07-12 devnull return 0;