Blob


1 #ifndef _VENTI_H_
2 #define _VENTI_H_ 1
3 #ifdef __cplusplus
4 extern "C" {
5 #endif
8 AUTOLIB(venti)
10 /* XXX should be own library? */
11 /*
12 * Packets
13 */
14 enum
15 {
16 MaxFragSize = 9*1024
17 };
19 typedef struct Packet Packet;
21 Packet* packetalloc(void);
22 void packetappend(Packet*, uchar *buf, int n);
23 uint packetasize(Packet*);
24 int packetcmp(Packet*, Packet*);
25 int packetcompact(Packet*);
26 void packetconcat(Packet*, Packet*);
27 int packetconsume(Packet*, uchar *buf, int n);
28 int packetcopy(Packet*, uchar *buf, int offset, int n);
29 Packet* packetdup(Packet*, int offset, int n);
30 Packet* packetforeign(uchar *buf, int n, void (*free)(void *a), void *a);
31 int packetfragments(Packet*, IOchunk*, int nio, int offset);
32 void packetfree(Packet*);
33 uchar* packetheader(Packet*, int n);
34 uchar* packetpeek(Packet*, uchar *buf, int offset, int n);
35 void packetprefix(Packet*, uchar *buf, int n);
36 void packetsha1(Packet*, uchar sha1[20]);
37 uint packetsize(Packet*);
38 Packet* packetsplit(Packet*, int n);
39 void packetstats(void);
40 uchar* packettrailer(Packet*, int n);
41 int packettrim(Packet*, int offset, int n);
43 /* XXX should be own library? */
44 /*
45 * Logging
46 */
47 typedef struct VtLog VtLog;
48 typedef struct VtLogChunk VtLogChunk;
50 struct VtLog
51 {
52 VtLog *next; /* in hash table */
53 char *name;
54 VtLogChunk *chunk;
55 uint nchunk;
56 VtLogChunk *w;
57 QLock lk;
58 int ref;
59 };
61 struct VtLogChunk
62 {
63 char *p;
64 char *ep;
65 char *wp;
66 };
68 VtLog* vtlogopen(char *name, uint size);
69 void vtlogprint(VtLog *log, char *fmt, ...);
70 void vtlog(char *name, char *fmt, ...);
71 void vtlogclose(VtLog*);
72 void vtlogremove(char *name);
73 char** vtlognames(int*);
74 void vtlogdump(int fd, VtLog*);
76 /* XXX begin actual venti.h */
78 typedef struct VtFcall VtFcall;
79 typedef struct VtConn VtConn;
80 typedef struct VtEntry VtEntry;
81 typedef struct VtRoot VtRoot;
83 /*
84 * Fundamental constants.
85 */
86 enum
87 {
88 VtScoreSize = 20,
89 VtMaxStringSize = 1024,
90 VtPointerDepth = 7
91 };
92 #define VtMaxFileSize ((1ULL<<48)-1)
95 /*
96 * Strings in packets.
97 */
98 int vtputstring(Packet*, char*);
99 int vtgetstring(Packet*, char**);
101 /*
102 * Block types.
104 * The initial Venti protocol had a much
105 * less regular list of block types.
106 * VtToDiskType converts from new to old.
107 */
108 enum
110 VtDataType = 0<<3,
111 /* VtDataType+1, ... */
112 VtDirType = 1<<3,
113 /* VtDirType+1, ... */
114 VtRootType = 2<<3,
115 VtMaxType,
116 VtCorruptType = 0xFF,
118 VtTypeDepthMask = 7,
119 VtTypeBaseMask = ~VtTypeDepthMask
120 };
122 /* convert to/from on-disk type numbers */
123 uint vttodisktype(uint);
124 uint vtfromdisktype(uint);
126 /*
127 * VtEntry describes a Venti stream
129 * The _ enums are only used on the wire.
130 * They are not present in the VtEntry structure
131 * and should not be used by client programs.
132 * (The info is in the type field.)
133 */
134 enum
136 VtEntryActive = 1<<0, /* entry is in use */
137 _VtEntryDir = 1<<1, /* a directory */
138 _VtEntryDepthShift = 2, /* shift for pointer depth */
139 _VtEntryDepthMask = 7<<2, /* mask for pointer depth */
140 VtEntryLocal = 1<<5, /* for local storage only */
141 _VtEntryBig = 1<<6,
142 };
143 enum
145 VtEntrySize = 40
146 };
147 struct VtEntry
149 ulong gen; /* generation number */
150 ulong psize; /* pointer block size */
151 ulong dsize; /* data block size */
152 uchar type;
153 uchar flags;
154 uvlong size;
155 uchar score[VtScoreSize];
156 };
158 void vtentrypack(VtEntry*, uchar*, int index);
159 int vtentryunpack(VtEntry*, uchar*, int index);
161 struct VtRoot
163 char name[128];
164 char type[128];
165 uchar score[VtScoreSize]; /* to a Dir block */
166 ulong blocksize; /* maximum block size */
167 uchar prev[VtScoreSize]; /* last root block */
168 };
170 enum
172 VtRootSize = 300,
173 VtRootVersion = 2,
174 _VtRootVersionBig = 1<<15,
175 };
177 void vtrootpack(VtRoot*, uchar*);
178 int vtrootunpack(VtRoot*, uchar*);
180 /*
181 * score of zero length block
182 */
183 extern uchar vtzeroscore[VtScoreSize];
185 /*
186 * zero extend and truncate blocks
187 */
188 void vtzeroextend(int type, uchar *buf, uint n, uint nn);
189 uint vtzerotruncate(int type, uchar *buf, uint n);
191 /*
192 * parse score: mungs s
193 */
194 int vtparsescore(char *s, char **prefix, uchar[VtScoreSize]);
196 /*
197 * formatting
198 * other than noted, these formats all ignore
199 * the width and precision arguments, and all flags
201 * V a venti score
202 */
204 int vtscorefmt(Fmt*);
206 /*
207 * error-checking malloc et al.
208 */
209 void vtfree(void *);
210 void* vtmalloc(int);
211 void* vtmallocz(int);
212 void* vtrealloc(void *p, int);
213 void* vtbrk(int n);
214 char* vtstrdup(char *);
216 /*
217 * Venti protocol
218 */
220 /*
221 * Crypto strengths
222 */
223 enum
225 VtCryptoStrengthNone,
226 VtCryptoStrengthAuth,
227 VtCryptoStrengthWeak,
228 VtCryptoStrengthStrong
229 };
231 /*
232 * Crypto suites
233 */
234 enum
236 VtCryptoNone,
237 VtCryptoSSL3,
238 VtCryptoTLS1,
239 VtCryptoMax
240 };
242 /*
243 * Codecs
244 */
245 enum
247 VtCodecNone,
248 VtCodecDeflate,
249 VtCodecThwack,
250 VtCodecMax
251 };
253 enum
255 VtRerror = 1,
256 VtTping = 2,
257 VtRping,
258 VtThello = 4,
259 VtRhello,
260 VtTgoodbye = 6,
261 VtRgoodbye, /* not used */
262 VtTauth0 = 8,
263 VtRauth0,
264 VtTauth1 = 10,
265 VtRauth1,
266 VtTread = 12,
267 VtRread,
268 VtTwrite = 14,
269 VtRwrite,
270 VtTsync = 16,
271 VtRsync,
273 VtTmax
274 };
276 struct VtFcall
278 uchar msgtype;
279 uchar tag;
281 char *error; /* Rerror */
283 char *version; /* Thello */
284 char *uid; /* Thello */
285 uchar strength; /* Thello */
286 uchar *crypto; /* Thello */
287 uint ncrypto; /* Thello */
288 uchar *codec; /* Thello */
289 uint ncodec; /* Thello */
290 char *sid; /* Rhello */
291 uchar rcrypto; /* Rhello */
292 uchar rcodec; /* Rhello */
293 uchar *auth; /* TauthX, RauthX */
294 uint nauth; /* TauthX, RauthX */
295 uchar score[VtScoreSize]; /* Tread, Rwrite */
296 uchar blocktype; /* Tread, Twrite */
297 uint count; /* Tread */
298 Packet *data; /* Rread, Twrite */
299 };
301 Packet* vtfcallpack(VtFcall*);
302 int vtfcallunpack(VtFcall*, Packet*);
303 void vtfcallclear(VtFcall*);
304 int vtfcallfmt(Fmt*);
306 enum
308 VtStateAlloc,
309 VtStateConnected,
310 VtStateClosed
311 };
313 struct VtConn
315 QLock lk;
316 QLock inlk;
317 QLock outlk;
318 int debug;
319 int infd;
320 int outfd;
321 int muxer;
322 void *writeq;
323 void *readq;
324 int state;
325 void *wait[256];
326 uint ntag;
327 uint nsleep;
328 Packet *part;
329 Rendez tagrend;
330 Rendez rpcfork;
331 char *version;
332 char *uid;
333 char *sid;
334 char addr[256]; /* address of other side */
335 };
337 VtConn* vtconn(int infd, int outfd);
338 int vtreconn(VtConn*, int, int);
339 VtConn* vtdial(char*);
340 int vtredial(VtConn*, char *);
341 void vtfreeconn(VtConn*);
342 int vtsend(VtConn*, Packet*);
343 Packet* vtrecv(VtConn*);
344 int vtversion(VtConn* z);
345 void vtdebug(VtConn* z, char*, ...);
346 void vthangup(VtConn* z);
347 int vtgoodbye(VtConn* z);
349 /* #pragma varargck argpos vtdebug 2 */
351 /* server */
352 typedef struct VtSrv VtSrv;
353 typedef struct VtReq VtReq;
354 struct VtReq
356 VtFcall tx;
357 VtFcall rx;
358 /* private */
359 VtSrv *srv;
360 void *sc;
361 };
363 int vtsrvhello(VtConn*);
364 VtSrv* vtlisten(char *addr);
365 VtReq* vtgetreq(VtSrv*);
366 void vtrespond(VtReq*);
368 /* client */
369 Packet* vtrpc(VtConn*, Packet*);
370 Packet* _vtrpc(VtConn*, Packet*, VtFcall*);
371 void vtrecvproc(void*); /* VtConn */
372 void vtsendproc(void*); /* VtConn */
374 int vtconnect(VtConn*);
375 int vthello(VtConn*);
376 int vtread(VtConn*, uchar score[VtScoreSize], uint type, uchar *buf, int n);
377 int vtwrite(VtConn*, uchar score[VtScoreSize], uint type, uchar *buf, int n);
378 Packet* vtreadpacket(VtConn*, uchar score[VtScoreSize], uint type, int n);
379 int vtwritepacket(VtConn*, uchar score[VtScoreSize], uint type, Packet *p);
380 int vtsync(VtConn*);
381 int vtping(VtConn*);
383 /*
384 * Data blocks and block cache.
385 */
386 enum
388 NilBlock = ~0
389 };
391 typedef struct VtBlock VtBlock;
392 typedef struct VtCache VtCache;
394 struct VtBlock
396 VtCache *c;
397 QLock lk;
399 uchar *data;
400 uchar score[VtScoreSize];
401 uchar type; /* VtXXX */
402 ulong size;
404 /* internal to cache */
405 int nlock;
406 int iostate;
407 int ref;
408 u32int heap;
409 VtBlock *next;
410 VtBlock **prev;
411 u32int used;
412 u32int used2;
413 u32int addr;
414 uintptr pc;
415 };
417 u32int vtglobaltolocal(uchar[VtScoreSize]);
418 void vtlocaltoglobal(u32int, uchar[VtScoreSize]);
420 VtCache*vtcachealloc(VtConn*, ulong maxmem);
421 void vtcachefree(VtCache*);
422 VtBlock*vtcachelocal(VtCache*, u32int addr, int type);
423 VtBlock*vtcacheglobal(VtCache*, uchar[VtScoreSize], int type, ulong size);
424 VtBlock*vtcacheallocblock(VtCache*, int type, ulong size);
425 void vtcachesetwrite(VtCache*,
426 int(*)(VtConn*, uchar[VtScoreSize], uint, uchar*, int));
427 void vtblockput(VtBlock*);
428 int vtblockwrite(VtBlock*);
429 VtBlock*vtblockcopy(VtBlock*);
430 void vtblockduplock(VtBlock*);
432 extern int vtcachencopy, vtcachenread, vtcachenwrite;
433 extern int vttracelevel;
435 /*
436 * Hash tree file tree.
437 */
438 typedef struct VtFile VtFile;
439 struct VtFile
441 QLock lk;
442 int ref;
443 int local;
444 VtBlock *b; /* block containing this file */
445 uchar score[VtScoreSize]; /* score of block containing this file */
446 int bsize; /* size of block */
448 /* immutable */
449 VtCache *c;
450 int mode;
451 u32int gen;
452 int dsize;
453 int psize;
454 int dir;
455 VtFile *parent;
456 int epb; /* entries per block in parent */
457 u32int offset; /* entry offset in parent */
458 };
460 enum
462 VtOREAD,
463 VtOWRITE,
464 VtORDWR
465 };
467 VtBlock*vtfileblock(VtFile*, u32int, int mode);
468 int vtfileblockscore(VtFile*, u32int, uchar[VtScoreSize]);
469 void vtfileclose(VtFile*);
470 VtFile* _vtfilecreate(VtFile*, int offset, int psize, int dsize, int dir);
471 VtFile* vtfilecreate(VtFile*, int psize, int dsize, int dir);
472 VtFile* vtfilecreateroot(VtCache*, int psize, int dsize, int type);
473 int vtfileflush(VtFile*);
474 int vtfileflushbefore(VtFile*, u64int);
475 u32int vtfilegetdirsize(VtFile*);
476 int vtfilegetentry(VtFile*, VtEntry*);
477 uvlong vtfilegetsize(VtFile*);
478 void vtfileincref(VtFile*);
479 int vtfilelock2(VtFile*, VtFile*, int);
480 int vtfilelock(VtFile*, int);
481 VtFile* vtfileopen(VtFile*, u32int, int);
482 VtFile* vtfileopenroot(VtCache*, VtEntry*);
483 long vtfileread(VtFile*, void*, long, vlong);
484 int vtfileremove(VtFile*);
485 int vtfilesetdirsize(VtFile*, u32int);
486 int vtfilesetentry(VtFile*, VtEntry*);
487 int vtfilesetsize(VtFile*, u64int);
488 int vtfiletruncate(VtFile*);
489 void vtfileunlock(VtFile*);
490 long vtfilewrite(VtFile*, void*, long, vlong);
492 int vttimefmt(Fmt*);
494 extern int chattyventi;
495 extern int ventidoublechecksha1;
496 extern int ventilogging;
498 extern char *VtServerLog;
500 #ifdef __cplusplus
502 #endif
503 #endif