Blob


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