Blob


1 #include "stdinc.h"
2 #include "dat.h"
3 #include "fns.h"
4 #include "whack.h"
6 /*
7 * Write a lump to disk. Updates ia with an index address
8 * for the newly-written lump. Upon return, the lump will
9 * have been placed in the disk cache but will likely not be on disk yet.
10 */
11 int
12 storeclump(Index *ix, ZBlock *zb, u8int *sc, int type, u32int creator, IAddr *ia)
13 {
14 ZBlock *cb;
15 Clump cl;
16 u64int a;
17 u8int bh[VtScoreSize];
18 int size, dsize;
20 trace(TraceLump, "storeclump enter", sc, type);
21 size = zb->len;
22 if(size > VtMaxLumpSize){
23 seterr(EStrange, "lump too large");
24 return -1;
25 }
26 if(vttypevalid(type) < 0){
27 seterr(EStrange, "invalid lump type");
28 return -1;
29 }
31 if(0){
32 scoremem(bh, zb->data, size);
33 if(scorecmp(sc, bh) != 0){
34 seterr(ECorrupt, "storing clump: corrupted; expected=%V got=%V, size=%d", sc, bh, size);
35 return -1;
36 }
37 }
39 cb = alloczblock(size + ClumpSize + U32Size, 0, 0);
40 if(cb == nil)
41 return -1;
43 cl.info.type = type;
44 cl.info.uncsize = size;
45 cl.creator = creator;
46 cl.time = now();
47 scorecp(cl.info.score, sc);
49 trace(TraceLump, "storeclump whackblock");
50 dsize = whackblock(&cb->data[ClumpSize], zb->data, size);
51 if(dsize > 0 && dsize < size){
52 cl.encoding = ClumpECompress;
53 }else{
54 if(dsize > size){
55 fprint(2, "whack error: dsize=%d size=%d\n", dsize, size);
56 abort();
57 }
58 cl.encoding = ClumpENone;
59 dsize = size;
60 memmove(&cb->data[ClumpSize], zb->data, size);
61 }
62 memset(cb->data+ClumpSize+dsize, 0, 4);
63 cl.info.size = dsize;
65 ia->addr = 0;
66 ia->type = type;
67 ia->size = size;
68 ia->blocks = (dsize + ClumpSize + (1 << ABlockLog) - 1) >> ABlockLog;
70 a = writeiclump(ix, &cl, cb->data, &ia->addr);
72 trace(TraceLump, "storeclump exit %lld", a);
74 freezblock(cb);
75 if(a == TWID64)
76 return -1;
78 /*
79 qlock(&stats.lock);
80 stats.clumpwrites++;
81 stats.clumpbwrites += size;
82 stats.clumpbcomp += dsize;
83 qunlock(&stats.lock);
84 */
86 return 0;
87 }
89 u32int
90 clumpmagic(Arena *arena, u64int aa)
91 {
92 u8int buf[U32Size];
94 if(readarena(arena, aa, buf, U32Size) < 0)
95 return TWID32;
96 return unpackmagic(buf);
97 }
99 /*
100 * fetch a block based at addr.
101 * score is filled in with the block's score.
102 * blocks is roughly the length of the clump on disk;
103 * if zero, the length is unknown.
104 */
105 ZBlock*
106 loadclump(Arena *arena, u64int aa, int blocks, Clump *cl, u8int *score, int verify)
108 Unwhack uw;
109 ZBlock *zb, *cb;
110 u8int bh[VtScoreSize], *buf;
111 u32int n;
112 int nunc;
114 /*
115 qlock(&stats.lock);
116 stats.clumpreads++;
117 qunlock(&stats.lock);
118 */
120 if(blocks <= 0)
121 blocks = 1;
123 trace(TraceLump, "loadclump enter");
125 cb = alloczblock(blocks << ABlockLog, 0, 0);
126 if(cb == nil)
127 return nil;
128 n = readarena(arena, aa, cb->data, blocks << ABlockLog);
129 if(n < ClumpSize){
130 if(n != 0)
131 seterr(ECorrupt, "loadclump read less than a header");
132 freezblock(cb);
133 return nil;
135 trace(TraceLump, "loadclump unpack");
136 if(unpackclump(cl, cb->data, arena->clumpmagic) < 0){
137 seterr(ECorrupt, "loadclump %s %llud: %r", arena->name, aa);
138 freezblock(cb);
139 return nil;
141 n -= ClumpSize;
142 if(n < cl->info.size){
143 freezblock(cb);
144 n = cl->info.size;
145 cb = alloczblock(n, 0, 0);
146 if(cb == nil)
147 return nil;
148 if(readarena(arena, aa + ClumpSize, cb->data, n) != n){
149 seterr(ECorrupt, "loadclump read too little data");
150 freezblock(cb);
151 return nil;
153 buf = cb->data;
154 }else
155 buf = cb->data + ClumpSize;
157 scorecp(score, cl->info.score);
159 zb = alloczblock(cl->info.uncsize, 0, 0);
160 if(zb == nil){
161 freezblock(cb);
162 return nil;
164 switch(cl->encoding){
165 case ClumpECompress:
166 trace(TraceLump, "loadclump decompress");
167 unwhackinit(&uw);
168 nunc = unwhack(&uw, zb->data, cl->info.uncsize, buf, cl->info.size);
169 if(nunc != cl->info.uncsize){
170 if(nunc < 0)
171 seterr(ECorrupt, "decompression of %llud failed: %s", aa, uw.err);
172 else
173 seterr(ECorrupt, "decompression of %llud gave partial block: %d/%d\n", aa, nunc, cl->info.uncsize);
174 freezblock(cb);
175 freezblock(zb);
176 return nil;
178 break;
179 case ClumpENone:
180 if(cl->info.size != cl->info.uncsize){
181 seterr(ECorrupt, "loading clump: bad uncompressed size for uncompressed block %llud", aa);
182 freezblock(cb);
183 freezblock(zb);
184 return nil;
186 scoremem(bh, buf, cl->info.uncsize);
187 if(scorecmp(cl->info.score, bh) != 0)
188 seterr(ECorrupt, "pre-copy sha1 wrong at %s %llud: expected=%V got=%V", arena->name, aa, cl->info.score, bh);
189 memmove(zb->data, buf, cl->info.uncsize);
190 break;
191 default:
192 seterr(ECorrupt, "unknown encoding in loadlump %llud", aa);
193 freezblock(cb);
194 freezblock(zb);
195 return nil;
197 freezblock(cb);
199 if(verify){
200 trace(TraceLump, "loadclump verify");
201 scoremem(bh, zb->data, cl->info.uncsize);
202 if(scorecmp(cl->info.score, bh) != 0){
203 seterr(ECorrupt, "loading clump: corrupted at %s %llud; expected=%V got=%V", arena->name, aa, cl->info.score, bh);
204 freezblock(zb);
205 return nil;
207 if(vttypevalid(cl->info.type) < 0){
208 seterr(ECorrupt, "loading lump at %s %llud: invalid lump type %d", arena->name, aa, cl->info.type);
209 freezblock(zb);
210 return nil;
214 trace(TraceLump, "loadclump exit");
215 /*
216 qlock(&stats.lock);
217 stats.clumpbreads += cl->info.size;
218 stats.clumpbuncomp += cl->info.uncsize;
219 qunlock(&stats.lock);
220 */
221 return zb;