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 VtLogChunk *chunk;
57 uint nchunk;
58 VtLogChunk *w;
59 QLock lk;
60 int ref;
61 };
63 struct VtLogchunk
64 {
65 char *buf;
66 uint nbuf;
67 char *w;
68 };
70 VtLog *vtlogopen(char *name, uint size);
71 void vtlogprint(VtLog *log, char *fmt, ...);
72 void vtlog(char *name, char *fmt, ...);
73 void vtlogclose(char *name);
74 void vtlogremove(char *name);
75 int vtlogdump(int fd, VtLog*);
77 /* XXX begin actual venti.h */
79 typedef struct VtFcall VtFcall;
80 typedef struct VtConn VtConn;
81 typedef struct VtEntry VtEntry;
82 typedef struct VtRoot VtRoot;
84 /*
85 * Fundamental constants.
86 */
87 enum
88 {
89 VtScoreSize = 20,
90 VtMaxStringSize = 1024,
91 VtMaxLumpSize = 56*1024,
92 VtPointerDepth = 7,
93 };
94 #define VtMaxFileSize ((1ULL<<48)-1)
97 /*
98 * Strings in packets.
99 */
100 int vtputstring(Packet*, char*);
101 int vtgetstring(Packet*, char**);
103 /*
104 * Block types.
106 * The initial Venti protocol had a much
107 * less regular list of block types.
108 * VtToDiskType converts from new to old.
109 */
110 enum
112 VtDataType = 0<<3,
113 /* VtDataType+1, ... */
114 VtDirType = 1<<3,
115 /* VtDirType+1, ... */
116 VtRootType = 2<<3,
117 VtMaxType,
118 VtCorruptType = 0xFF,
120 VtTypeDepthMask = 7,
121 VtTypeBaseMask = ~VtTypeDepthMask,
122 };
124 /* convert to/from on-disk type numbers */
125 uint vttodisktype(uint);
126 uint vtfromdisktype(uint);
128 /*
129 * VtEntry describes a Venti stream
131 * The _ enums are only used on the wire.
132 * They are not present in the VtEntry structure
133 * and should not be used by client programs.
134 * (The info is in the type field.)
135 */
136 enum
138 VtEntryActive = 1<<0, /* entry is in use */
139 _VtEntryDir = 1<<1, /* a directory */
140 _VtEntryDepthShift = 2, /* shift for pointer depth */
141 _VtEntryDepthMask = 7<<2, /* mask for pointer depth */
142 VtEntryLocal = 1<<5, /* for local storage only */
143 };
144 enum
146 VtEntrySize = 40,
147 };
148 struct VtEntry
150 ulong gen; /* generation number */
151 ushort psize; /* pointer block size */
152 ushort dsize; /* data block size */
153 uchar type;
154 uchar flags;
155 uvlong size;
156 uchar score[VtScoreSize];
157 };
159 void vtentrypack(VtEntry*, uchar*, int index);
160 int vtentryunpack(VtEntry*, uchar*, int index);
162 struct VtRoot
164 char name[128];
165 char type[128];
166 uchar score[VtScoreSize]; /* to a Dir block */
167 ushort blocksize; /* maximum block size */
168 uchar prev[VtScoreSize]; /* last root block */
169 };
171 enum
173 VtRootSize = 300,
174 VtRootVersion = 2,
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 */
203 #ifndef PLAN9PORT
204 #pragma varargck type "V" uchar*
205 #pragma varargck type "F" VtFcall*
206 #endif
208 int vtscorefmt(Fmt*);
210 /*
211 * error-checking malloc et al.
212 */
213 void vtfree(void *);
214 void *vtmalloc(int);
215 void *vtmallocz(int);
216 void *vtrealloc(void *p, int);
217 void *vtbrk(int n);
218 char *vtstrdup(char *);
220 /*
221 * Venti protocol
222 */
224 /*
225 * Crypto strengths
226 */
227 enum
229 VtCryptoStrengthNone,
230 VtCryptoStrengthAuth,
231 VtCryptoStrengthWeak,
232 VtCryptoStrengthStrong,
233 };
235 /*
236 * Crypto suites
237 */
238 enum
240 VtCryptoNone,
241 VtCryptoSSL3,
242 VtCryptoTLS1,
243 VtCryptoMax,
244 };
246 /*
247 * Codecs
248 */
249 enum
251 VtCodecNone,
252 VtCodecDeflate,
253 VtCodecThwack,
254 VtCodecMax
255 };
257 enum
259 VtRerror = 1,
260 VtTping = 2,
261 VtRping,
262 VtThello = 4,
263 VtRhello,
264 VtTgoodbye = 6,
265 VtRgoodbye, /* not used */
266 VtTauth0 = 8,
267 VtRauth0,
268 VtTauth1 = 10,
269 VtRauth1,
270 VtTread = 12,
271 VtRread,
272 VtTwrite = 14,
273 VtRwrite,
274 VtTsync = 16,
275 VtRsync,
277 VtTmax
278 };
280 struct VtFcall
282 uchar type;
283 uchar tag;
285 char *error; /* Rerror */
287 char *version; /* Thello */
288 char *uid; /* Thello */
289 uchar strength; /* Thello */
290 uchar *crypto; /* Thello */
291 uint ncrypto; /* Thello */
292 uchar *codec; /* Thello */
293 uint ncodec; /* Thello */
294 char *sid; /* Rhello */
295 uchar rcrypto; /* Rhello */
296 uchar rcodec; /* Rhello */
297 uchar *auth; /* TauthX, RauthX */
298 uint nauth; /* TauthX, RauthX */
299 uchar score[VtScoreSize]; /* Tread, Rwrite */
300 uchar dtype; /* Tread, Twrite */
301 ushort count; /* Tread */
302 Packet *data; /* Rread, Twrite */
303 };
305 Packet *vtfcallpack(VtFcall*);
306 int vtfcallunpack(VtFcall*, Packet*);
307 void vtfcallclear(VtFcall*);
308 int vtfcallfmt(Fmt*);
310 enum
312 VtStateAlloc,
313 VtStateConnected,
314 VtStateClosed,
315 };
317 struct VtConn
319 QLock lk;
320 QLock inlk;
321 QLock outlk;
322 int debug;
323 int infd;
324 int outfd;
325 int muxer;
326 void *writeq;
327 void *readq;
328 int state;
329 void *wait[256];
330 uint ntag;
331 uint nsleep;
332 Packet *part;
333 Rendez tagrend;
334 Rendez rpcfork;
335 char *version;
336 char *uid;
337 char *sid;
338 };
340 VtConn *vtconn(int infd, int outfd);
341 VtConn *vtdial(char*);
342 void vtfreeconn(VtConn*);
343 int vtsend(VtConn*, Packet*);
344 Packet *vtrecv(VtConn*);
345 int vtversion(VtConn *z);
346 void vtdebug(VtConn *z, char*, ...);
347 void vthangup(VtConn *z);
348 /* #pragma varargck argpos vtdebug 2 */
350 /* server */
351 typedef struct VtSrv VtSrv;
352 typedef struct VtReq VtReq;
353 struct VtReq
355 VtFcall tx;
356 VtFcall rx;
357 /* private */
358 VtSrv *srv;
359 void *sc;
360 };
362 int vtsrvhello(VtConn*);
363 VtSrv *vtlisten(char *addr);
364 VtReq *vtgetreq(VtSrv*);
365 void vtrespond(VtReq*);
367 /* client */
368 Packet *vtrpc(VtConn*, Packet*);
369 void vtrecvproc(void*); /* VtConn* */
370 void vtsendproc(void*); /* VtConn* */
372 int vtconnect(VtConn*);
373 int vthello(VtConn*);
374 int vtread(VtConn*, uchar score[VtScoreSize], uint type, uchar *buf, int n);
375 int vtwrite(VtConn*, uchar score[VtScoreSize], uint type, uchar *buf, int n);
376 Packet *vtreadpacket(VtConn*, uchar score[VtScoreSize], uint type, int n);
377 int vtwritepacket(VtConn*, uchar score[VtScoreSize], uint type, Packet *p);
378 int vtsync(VtConn*);
379 int vtping(VtConn*);
381 /*
382 * Data blocks and block cache.
383 */
384 enum
386 NilBlock = ~0,
387 };
389 typedef struct VtBlock VtBlock;
390 typedef struct VtCache VtCache;
392 struct VtBlock
394 VtCache *c;
395 QLock lk;
397 uchar *data;
398 uchar score[VtScoreSize];
399 uchar type; /* BtXXX */
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;
412 /* internal to efile (HACK) */
413 int decrypted;
414 };
416 u32int vtglobaltolocal(uchar[VtScoreSize]);
417 void vtlocaltoglobal(u32int, uchar[VtScoreSize]);
419 VtCache *vtcachealloc(VtConn*, int blocksize, ulong nblocks, int mode);
420 void vtcachefree(VtCache*);
421 VtBlock *vtcachelocal(VtCache*, u32int addr, int type);
422 VtBlock *vtcacheglobal(VtCache*, uchar[VtScoreSize], int type);
423 VtBlock *vtcacheallocblock(VtCache*, int type);
424 void vtcachesetwrite(VtCache*, int(*)(VtConn*,uchar[VtScoreSize],uint,uchar*,int));
425 void vtblockput(VtBlock*);
426 u32int vtcacheblocksize(VtCache*);
427 int vtblockwrite(VtBlock*);
428 VtBlock *vtblockcopy(VtBlock*);
429 void vtblockduplock(VtBlock*);
430 int vtblockdirty(VtBlock*);
432 /*
433 * Hash tree file tree.
434 */
435 typedef struct VtFile VtFile;
436 struct VtFile
438 QLock lk;
439 int ref;
440 int local;
441 VtBlock *b; /* block containing this file */
442 uchar score[VtScoreSize]; /* score of block containing this file */
444 /* immutable */
445 VtCache *c;
446 int mode;
447 u32int gen;
448 int dsize;
449 int dir;
450 VtFile *parent;
451 int epb; /* entries per block in parent */
452 u32int offset; /* entry offset in parent */
453 };
455 enum
457 VtOREAD,
458 VtOWRITE,
459 VtORDWR,
460 VtOCREATE = 0x100,
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 VtBlock *vtfileblock(VtFile*, u32int, int mode);
468 long vtfileread(VtFile*, void*, long, vlong);
469 long vtfilewrite(VtFile*, void*, long, vlong);
470 int vtfileflush(VtFile*);
471 void vtfileincref(VtFile*);
472 void vtfileclose(VtFile*);
473 int vtfilegetentry(VtFile*, VtEntry*);
474 int vtfilesetentry(VtFile*, VtEntry*);
475 int vtfileblockscore(VtFile*, u32int, uchar[VtScoreSize]);
476 u32int vtfilegetdirsize(VtFile*);
477 int vtfilesetdirsize(VtFile*, u32int);
478 void vtfileunlock(VtFile*);
479 int vtfilelock(VtFile*, int);
480 int vtfilelock2(VtFile*, VtFile*, int);
481 int vtfileflushbefore(VtFile*, u64int);
482 int vtfiletruncate(VtFile*);
483 uvlong vtfilegetsize(VtFile*);
484 int vtfilesetsize(VtFile*, uvlong);
485 int vtfileremove(VtFile*);
487 extern int chattyventi;
488 extern int ventidoublechecksha1;
490 #ifdef __cplusplus
492 #endif
493 #endif