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 VtMaxLumpSize = 56*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 };
142 enum
144 VtEntrySize = 40
145 };
146 struct VtEntry
148 ulong gen; /* generation number */
149 ushort psize; /* pointer block size */
150 ushort 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 ushort blocksize; /* maximum block size */
166 uchar prev[VtScoreSize]; /* last root block */
167 };
169 enum
171 VtRootSize = 300,
172 VtRootVersion = 2
173 };
175 void vtrootpack(VtRoot*, uchar*);
176 int vtrootunpack(VtRoot*, uchar*);
178 /*
179 * score of zero length block
180 */
181 extern uchar vtzeroscore[VtScoreSize];
183 /*
184 * zero extend and truncate blocks
185 */
186 void vtzeroextend(int type, uchar *buf, uint n, uint nn);
187 uint vtzerotruncate(int type, uchar *buf, uint n);
189 /*
190 * parse score: mungs s
191 */
192 int vtparsescore(char *s, char **prefix, uchar[VtScoreSize]);
194 /*
195 * formatting
196 * other than noted, these formats all ignore
197 * the width and precision arguments, and all flags
199 * V a venti score
200 */
202 int vtscorefmt(Fmt*);
204 /*
205 * error-checking malloc et al.
206 */
207 void vtfree(void *);
208 void *vtmalloc(int);
209 void *vtmallocz(int);
210 void *vtrealloc(void *p, int);
211 void *vtbrk(int n);
212 char *vtstrdup(char *);
214 /*
215 * Venti protocol
216 */
218 /*
219 * Crypto strengths
220 */
221 enum
223 VtCryptoStrengthNone,
224 VtCryptoStrengthAuth,
225 VtCryptoStrengthWeak,
226 VtCryptoStrengthStrong
227 };
229 /*
230 * Crypto suites
231 */
232 enum
234 VtCryptoNone,
235 VtCryptoSSL3,
236 VtCryptoTLS1,
237 VtCryptoMax
238 };
240 /*
241 * Codecs
242 */
243 enum
245 VtCodecNone,
246 VtCodecDeflate,
247 VtCodecThwack,
248 VtCodecMax
249 };
251 enum
253 VtRerror = 1,
254 VtTping = 2,
255 VtRping,
256 VtThello = 4,
257 VtRhello,
258 VtTgoodbye = 6,
259 VtRgoodbye, /* not used */
260 VtTauth0 = 8,
261 VtRauth0,
262 VtTauth1 = 10,
263 VtRauth1,
264 VtTread = 12,
265 VtRread,
266 VtTwrite = 14,
267 VtRwrite,
268 VtTsync = 16,
269 VtRsync,
271 VtTmax
272 };
274 struct VtFcall
276 uchar msgtype;
277 uchar tag;
279 char *error; /* Rerror */
281 char *version; /* Thello */
282 char *uid; /* Thello */
283 uchar strength; /* Thello */
284 uchar *crypto; /* Thello */
285 uint ncrypto; /* Thello */
286 uchar *codec; /* Thello */
287 uint ncodec; /* Thello */
288 char *sid; /* Rhello */
289 uchar rcrypto; /* Rhello */
290 uchar rcodec; /* Rhello */
291 uchar *auth; /* TauthX, RauthX */
292 uint nauth; /* TauthX, RauthX */
293 uchar score[VtScoreSize]; /* Tread, Rwrite */
294 uchar blocktype; /* Tread, Twrite */
295 ushort count; /* Tread */
296 Packet *data; /* Rread, Twrite */
297 };
299 Packet *vtfcallpack(VtFcall*);
300 int vtfcallunpack(VtFcall*, Packet*);
301 void vtfcallclear(VtFcall*);
302 int vtfcallfmt(Fmt*);
304 enum
306 VtStateAlloc,
307 VtStateConnected,
308 VtStateClosed
309 };
311 struct VtConn
313 QLock lk;
314 QLock inlk;
315 QLock outlk;
316 int debug;
317 int infd;
318 int outfd;
319 int muxer;
320 void *writeq;
321 void *readq;
322 int state;
323 void *wait[256];
324 uint ntag;
325 uint nsleep;
326 Packet *part;
327 Rendez tagrend;
328 Rendez rpcfork;
329 char *version;
330 char *uid;
331 char *sid;
332 char addr[256]; /* address of other side */
333 };
335 VtConn *vtconn(int infd, int outfd);
336 VtConn *vtdial(char*);
337 void vtfreeconn(VtConn*);
338 int vtsend(VtConn*, Packet*);
339 Packet *vtrecv(VtConn*);
340 int vtversion(VtConn *z);
341 void vtdebug(VtConn *z, char*, ...);
342 void vthangup(VtConn *z);
343 int vtgoodbye(VtConn *z);
345 /* #pragma varargck argpos vtdebug 2 */
347 /* server */
348 typedef struct VtSrv VtSrv;
349 typedef struct VtReq VtReq;
350 struct VtReq
352 VtFcall tx;
353 VtFcall rx;
354 /* private */
355 VtSrv *srv;
356 void *sc;
357 };
359 int vtsrvhello(VtConn*);
360 VtSrv *vtlisten(char *addr);
361 VtReq *vtgetreq(VtSrv*);
362 void vtrespond(VtReq*);
364 /* client */
365 Packet *vtrpc(VtConn*, Packet*);
366 Packet *_vtrpc(VtConn*, Packet*, VtFcall*);
367 void vtrecvproc(void*); /* VtConn* */
368 void vtsendproc(void*); /* VtConn* */
370 int vtconnect(VtConn*);
371 int vthello(VtConn*);
372 int vtread(VtConn*, uchar score[VtScoreSize], uint type, uchar *buf, int n);
373 int vtwrite(VtConn*, uchar score[VtScoreSize], uint type, uchar *buf, int n);
374 Packet *vtreadpacket(VtConn*, uchar score[VtScoreSize], uint type, int n);
375 int vtwritepacket(VtConn*, uchar score[VtScoreSize], uint type, Packet *p);
376 int vtsync(VtConn*);
377 int vtping(VtConn*);
379 /*
380 * Data blocks and block cache.
381 */
382 enum
384 NilBlock = ~0
385 };
387 typedef struct VtBlock VtBlock;
388 typedef struct VtCache VtCache;
390 struct VtBlock
392 VtCache *c;
393 QLock lk;
395 uchar *data;
396 uchar score[VtScoreSize];
397 uchar type; /* BtXXX */
399 /* internal to cache */
400 int nlock;
401 int iostate;
402 int ref;
403 u32int heap;
404 VtBlock *next;
405 VtBlock **prev;
406 u32int used;
407 u32int used2;
408 u32int addr;
409 uintptr pc;
410 };
412 u32int vtglobaltolocal(uchar[VtScoreSize]);
413 void vtlocaltoglobal(u32int, uchar[VtScoreSize]);
415 VtCache *vtcachealloc(VtConn*, int blocksize, ulong nblocks);
416 void vtcachefree(VtCache*);
417 VtBlock *vtcachelocal(VtCache*, u32int addr, int type);
418 VtBlock *vtcacheglobal(VtCache*, uchar[VtScoreSize], int type);
419 VtBlock *vtcacheallocblock(VtCache*, int type);
420 void vtcachesetwrite(VtCache*, int(*)(VtConn*,uchar[VtScoreSize],uint,uchar*,int));
421 void vtblockput(VtBlock*);
422 u32int vtcacheblocksize(VtCache*);
423 int vtblockwrite(VtBlock*);
424 VtBlock *vtblockcopy(VtBlock*);
425 void vtblockduplock(VtBlock*);
427 extern int vtcachencopy, vtcachenread, vtcachenwrite;
428 extern int vttracelevel;
430 /*
431 * Hash tree file tree.
432 */
433 typedef struct VtFile VtFile;
434 struct VtFile
436 QLock lk;
437 int ref;
438 int local;
439 VtBlock *b; /* block containing this file */
440 uchar score[VtScoreSize]; /* score of block containing this file */
442 /* immutable */
443 VtCache *c;
444 int mode;
445 u32int gen;
446 int dsize;
447 int psize;
448 int dir;
449 VtFile *parent;
450 int epb; /* entries per block in parent */
451 u32int offset; /* entry offset in parent */
452 };
454 enum
456 VtOREAD,
457 VtOWRITE,
458 VtORDWR
459 };
461 VtFile *vtfileopenroot(VtCache*, VtEntry*);
462 VtFile *vtfilecreateroot(VtCache*, int psize, int dsize, int type);
463 VtFile *vtfileopen(VtFile*, u32int, int);
464 VtFile *vtfilecreate(VtFile*, int psize, int dsize, int dir);
465 VtFile *_vtfilecreate(VtFile*, int offset, int psize, int dsize, int dir);
466 VtBlock *vtfileblock(VtFile*, u32int, int mode);
467 long vtfileread(VtFile*, void*, long, vlong);
468 long vtfilewrite(VtFile*, void*, long, vlong);
469 int vtfileflush(VtFile*);
470 void vtfileincref(VtFile*);
471 void vtfileclose(VtFile*);
472 int vtfilegetentry(VtFile*, VtEntry*);
473 int vtfilesetentry(VtFile*, VtEntry*);
474 int vtfileblockscore(VtFile*, u32int, uchar[VtScoreSize]);
475 u32int vtfilegetdirsize(VtFile*);
476 int vtfilesetdirsize(VtFile*, u32int);
477 void vtfileunlock(VtFile*);
478 int vtfilelock(VtFile*, int);
479 int vtfilelock2(VtFile*, VtFile*, int);
480 int vtfileflushbefore(VtFile*, u64int);
481 int vtfiletruncate(VtFile*);
482 uvlong vtfilegetsize(VtFile*);
483 int vtfilesetsize(VtFile*, u64int);
484 int vtfileremove(VtFile*);
486 extern int vttimefmt(Fmt*);
488 extern int chattyventi;
489 extern int ventidoublechecksha1;
490 extern int ventilogging;
492 extern char *VtServerLog;
494 #ifdef __cplusplus
496 #endif
497 #endif