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