6 int writestodevnull = 0;
8 static Packet *readilump(Lump *u, IAddr *ia, u8int *score, int rac);
11 readlump(u8int *score, int type, u32int size, int *cached)
19 trace(TraceLump, "readlump enter");
25 if(scorecmp(score, zeroscore) == 0)
27 u = lookuplump(score, type);
29 trace(TraceLump, "readlump lookuplump hit");
32 n = packetsize(u->data);
34 seterr(EOk, "read too small: asked for %d need at least %d", size, n);
39 p = packetdup(u->data, 0, n);
47 if(lookupscore(score, type, &ia, &rac) < 0){
48 //ZZZ place to check for someone trying to guess scores
49 seterr(EOk, "no block with score %V/%d exists", score, type);
55 seterr(EOk, "read too small 1: asked for %d need at least %d", size, ia.size);
61 trace(TraceLump, "readlump readilump");
62 p = readilump(u, &ia, score, rac);
65 trace(TraceLump, "readlump exit");
70 * save away a lump, and return it's score.
71 * doesn't store duplicates, but checks that the data is really the same.
74 writelump(Packet *p, u8int *score, int type, u32int creator, uint ms)
86 if(packetsize(p) == 0 || writestodevnull==1){
91 u = lookuplump(score, type);
94 if(packetcmp(p, u->data) != 0){
95 seterr(EStrange, "score collision");
103 if(writestodevnull==2){
109 return queuewrite(u, p, creator, ms);
111 ok = writeqlump(u, p, creator, ms);
118 writeqlump(Lump *u, Packet *p, int creator, uint ms)
126 if(lookupscore(u->score, u->type, &ia, &rac) == 0){
127 /* assume the data is here! XXX */
130 addstat2(StatRpcWriteOld, 1, StatRpcWriteOldTime, ms);
135 * assume it was corrupted data and store the block again
137 old = readilump(u, &ia, u->score, rac);
140 if(packetcmp(p, old) != 0){
141 seterr(EStrange, "score collision");
148 addstat2(StatRpcWriteOld, 1, StatRpcWriteOldTime, ms);
151 logerr(EAdmin, "writelump: read %V failed, rewriting: %r\n", u->score);
154 flat = packet2zblock(p, packetsize(p));
155 ok = storeclump(mainindex, flat, u->score, u->type, creator, &ia);
158 ok = insertscore(u->score, &ia, 1);
165 addstat2(StatRpcWriteNew, 1, StatRpcWriteNewTime, ms);
170 lreadahead(u64int a, Arena *arena, u64int aa, int n)
172 u8int buf[ClumpSize];
177 if (aa >= arena->memstats.used)
179 if(readarena(arena, aa, buf, ClumpSize) < ClumpSize)
181 if(unpackclump(&cl, buf, arena->clumpmagic) < 0)
184 ia.type = cl.info.type;
185 ia.size = cl.info.uncsize;
186 ia.blocks = (cl.info.size + ClumpSize + (1 << ABlockLog) - 1) >> ABlockLog;
187 insertscore(cl.info.score, &ia, 0);
188 a += ClumpSize + cl.info.size;
189 aa += ClumpSize + cl.info.size;
195 readilump(Lump *u, IAddr *ia, u8int *score, int rac)
202 u8int sc[VtScoreSize];
204 trace(TraceLump, "readilump enter");
205 arena = amapitoa(mainindex, ia->addr, &aa);
207 trace(TraceLump, "readilump amapitoa failed");
211 trace(TraceLump, "readilump loadclump");
212 zb = loadclump(arena, aa, ia->blocks, &cl, sc, paranoid);
214 trace(TraceLump, "readilump loadclump failed");
218 if(ia->size != cl.info.uncsize){
219 seterr(EInconsist, "index and clump size mismatch");
223 if(ia->type != cl.info.type){
224 seterr(EInconsist, "index and clump type mismatch");
228 if(scorecmp(score, sc) != 0){
229 seterr(ECrash, "score mismatch");
235 trace(TraceLump, "readilump readahead");
236 a = ia->addr + ClumpSize + cl.info.size;
237 aa += ClumpSize + cl.info.size;
238 lreadahead(a, arena, aa, 20);
241 trace(TraceLump, "readilump success");
242 p = zblock2packet(zb, cl.info.uncsize);
244 pp = packetdup(p, 0, packetsize(p));
245 trace(TraceLump, "readilump insertlump");
247 trace(TraceLump, "readilump exit");