blob: af5bb78841d15ad49b3c6eba0ad8957a0afa85fd [file] [log] [blame]
rscf7012582003-11-25 01:40:27 +00001#ifndef _VENTI_H_
2#define _VENTI_H_ 1
rsc819822c2004-06-16 16:45:22 +00003#ifdef __cplusplus
rscf7012582003-11-25 01:40:27 +00004extern "C" {
5#endif
rsc1a0954a2005-01-04 21:18:08 +00006
rsc6c0209f2005-02-11 20:32:41 +00007#ifndef PLAN9PORT
8#pragma lib "libventi.a"
9#pragma src "/sys/src/libventi"
10#endif
11
rsc1a0954a2005-01-04 21:18:08 +000012AUTOLIB(venti)
13
rsc97a5e5f2003-11-23 18:25:35 +000014/* XXX should be own library? */
15/*
16 * Packets
17 */
18enum
19{
rsccbeb0b22006-04-01 19:24:03 +000020 MaxFragSize = 9*1024
rsc97a5e5f2003-11-23 18:25:35 +000021};
22
23typedef struct Packet Packet;
24Packet *packetalloc(void);
25void packetfree(Packet*);
26Packet *packetforeign(uchar *buf, int n, void (*free)(void *a), void *a);
27Packet *packetdup(Packet*, int offset, int n);
28Packet *packetsplit(Packet*, int n);
29int packetconsume(Packet*, uchar *buf, int n);
30int packettrim(Packet*, int offset, int n);
31uchar *packetheader(Packet*, int n);
32uchar *packettrailer(Packet*, int n);
rscd23a6172004-03-15 01:57:29 +000033void packetprefix(Packet*, uchar *buf, int n);
34void packetappend(Packet*, uchar *buf, int n);
35void packetconcat(Packet*, Packet*);
rsc97a5e5f2003-11-23 18:25:35 +000036uchar *packetpeek(Packet*, uchar *buf, int offset, int n);
37int packetcopy(Packet*, uchar *buf, int offset, int n);
38int packetfragments(Packet*, IOchunk*, int nio, int offset);
39uint packetsize(Packet*);
40uint packetasize(Packet*);
41int packetcompact(Packet*);
42int packetcmp(Packet*, Packet*);
43void packetstats(void);
44void packetsha1(Packet*, uchar sha1[20]);
45
rsc6c0209f2005-02-11 20:32:41 +000046/* XXX should be own library? */
47/*
48 * Logging
49 */
50typedef struct VtLog VtLog;
51typedef struct VtLogChunk VtLogChunk;
rsc97a5e5f2003-11-23 18:25:35 +000052
rsc6c0209f2005-02-11 20:32:41 +000053struct VtLog
54{
55 VtLog *next; /* in hash table */
rsc587f8f42005-02-13 18:32:06 +000056 char *name;
rsc6c0209f2005-02-11 20:32:41 +000057 VtLogChunk *chunk;
58 uint nchunk;
59 VtLogChunk *w;
60 QLock lk;
61 int ref;
62};
63
rsc587f8f42005-02-13 18:32:06 +000064struct VtLogChunk
rsc6c0209f2005-02-11 20:32:41 +000065{
rsc587f8f42005-02-13 18:32:06 +000066 char *p;
67 char *ep;
68 char *wp;
rsc6c0209f2005-02-11 20:32:41 +000069};
70
71VtLog *vtlogopen(char *name, uint size);
72void vtlogprint(VtLog *log, char *fmt, ...);
73void vtlog(char *name, char *fmt, ...);
rsc587f8f42005-02-13 18:32:06 +000074void vtlogclose(VtLog*);
rsc6c0209f2005-02-11 20:32:41 +000075void vtlogremove(char *name);
rsc1c485da2005-05-12 14:03:16 +000076char **vtlognames(int*);
rsc587f8f42005-02-13 18:32:06 +000077void vtlogdump(int fd, VtLog*);
rsc6c0209f2005-02-11 20:32:41 +000078
79/* XXX begin actual venti.h */
rsc97a5e5f2003-11-23 18:25:35 +000080
81typedef struct VtFcall VtFcall;
rsc97a5e5f2003-11-23 18:25:35 +000082typedef struct VtConn VtConn;
83typedef struct VtEntry VtEntry;
84typedef struct VtRoot VtRoot;
85
86/*
87 * Fundamental constants.
88 */
89enum
90{
91 VtScoreSize = 20,
92 VtMaxStringSize = 1024,
rsc97a5e5f2003-11-23 18:25:35 +000093 VtMaxLumpSize = 56*1024,
rsccbeb0b22006-04-01 19:24:03 +000094 VtPointerDepth = 7
rsc97a5e5f2003-11-23 18:25:35 +000095};
rsc2db9e482003-11-24 20:16:30 +000096#define VtMaxFileSize ((1ULL<<48)-1)
97
rsc97a5e5f2003-11-23 18:25:35 +000098
99/*
100 * Strings in packets.
101 */
102int vtputstring(Packet*, char*);
103int vtgetstring(Packet*, char**);
104
105/*
106 * Block types.
107 *
108 * The initial Venti protocol had a much
109 * less regular list of block types.
110 * VtToDiskType converts from new to old.
111 */
112enum
113{
114 VtDataType = 0<<3,
115 /* VtDataType+1, ... */
116 VtDirType = 1<<3,
117 /* VtDirType+1, ... */
118 VtRootType = 2<<3,
rsc97a5e5f2003-11-23 18:25:35 +0000119 VtMaxType,
rscd3232ca2004-06-14 21:43:32 +0000120 VtCorruptType = 0xFF,
rsc97a5e5f2003-11-23 18:25:35 +0000121
122 VtTypeDepthMask = 7,
rsccbeb0b22006-04-01 19:24:03 +0000123 VtTypeBaseMask = ~VtTypeDepthMask
rsc97a5e5f2003-11-23 18:25:35 +0000124};
125
126/* convert to/from on-disk type numbers */
127uint vttodisktype(uint);
128uint vtfromdisktype(uint);
129
130/*
131 * VtEntry describes a Venti stream
rsceacbfb22004-03-15 01:58:31 +0000132 *
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.)
rsc97a5e5f2003-11-23 18:25:35 +0000137 */
138enum
139{
140 VtEntryActive = 1<<0, /* entry is in use */
rscd23a6172004-03-15 01:57:29 +0000141 _VtEntryDir = 1<<1, /* a directory */
142 _VtEntryDepthShift = 2, /* shift for pointer depth */
143 _VtEntryDepthMask = 7<<2, /* mask for pointer depth */
rsccbeb0b22006-04-01 19:24:03 +0000144 VtEntryLocal = 1<<5 /* for local storage only */
rsc97a5e5f2003-11-23 18:25:35 +0000145};
146enum
147{
rsccbeb0b22006-04-01 19:24:03 +0000148 VtEntrySize = 40
rsc97a5e5f2003-11-23 18:25:35 +0000149};
150struct VtEntry
151{
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};
160
161void vtentrypack(VtEntry*, uchar*, int index);
162int vtentryunpack(VtEntry*, uchar*, int index);
163
164struct VtRoot
165{
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};
172
173enum
174{
175 VtRootSize = 300,
rsccbeb0b22006-04-01 19:24:03 +0000176 VtRootVersion = 2
rsc97a5e5f2003-11-23 18:25:35 +0000177};
178
179void vtrootpack(VtRoot*, uchar*);
180int vtrootunpack(VtRoot*, uchar*);
181
182/*
183 * score of zero length block
184 */
185extern uchar vtzeroscore[VtScoreSize];
186
187/*
188 * zero extend and truncate blocks
189 */
190void vtzeroextend(int type, uchar *buf, uint n, uint nn);
191uint vtzerotruncate(int type, uchar *buf, uint n);
192
193/*
194 * parse score: mungs s
195 */
rscd23a6172004-03-15 01:57:29 +0000196int vtparsescore(char *s, char **prefix, uchar[VtScoreSize]);
rsc97a5e5f2003-11-23 18:25:35 +0000197
198/*
199 * formatting
200 * other than noted, these formats all ignore
201 * the width and precision arguments, and all flags
202 *
203 * V a venti score
204 */
rsc819822c2004-06-16 16:45:22 +0000205#ifndef PLAN9PORT
206#pragma varargck type "V" uchar*
207#pragma varargck type "F" VtFcall*
208#endif
rsc97a5e5f2003-11-23 18:25:35 +0000209
210int vtscorefmt(Fmt*);
211
212/*
213 * error-checking malloc et al.
214 */
215void vtfree(void *);
216void *vtmalloc(int);
217void *vtmallocz(int);
218void *vtrealloc(void *p, int);
219void *vtbrk(int n);
220char *vtstrdup(char *);
221
222/*
223 * Venti protocol
224 */
225
226/*
227 * Crypto strengths
228 */
229enum
230{
231 VtCryptoStrengthNone,
232 VtCryptoStrengthAuth,
233 VtCryptoStrengthWeak,
rsccbeb0b22006-04-01 19:24:03 +0000234 VtCryptoStrengthStrong
rsc97a5e5f2003-11-23 18:25:35 +0000235};
236
237/*
238 * Crypto suites
239 */
240enum
241{
242 VtCryptoNone,
243 VtCryptoSSL3,
244 VtCryptoTLS1,
rsccbeb0b22006-04-01 19:24:03 +0000245 VtCryptoMax
rsc97a5e5f2003-11-23 18:25:35 +0000246};
247
248/*
249 * Codecs
250 */
251enum
252{
253 VtCodecNone,
254 VtCodecDeflate,
255 VtCodecThwack,
256 VtCodecMax
257};
258
259enum
260{
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,
278
279 VtTmax
280};
281
282struct VtFcall
283{
rsc7e6f40b2005-07-13 10:46:26 +0000284 uchar msgtype;
rsc97a5e5f2003-11-23 18:25:35 +0000285 uchar tag;
286
287 char *error; /* Rerror */
288
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 */
rsc7e6f40b2005-07-13 10:46:26 +0000302 uchar blocktype; /* Tread, Twrite */
rsc97a5e5f2003-11-23 18:25:35 +0000303 ushort count; /* Tread */
304 Packet *data; /* Rread, Twrite */
305};
306
307Packet *vtfcallpack(VtFcall*);
308int vtfcallunpack(VtFcall*, Packet*);
309void vtfcallclear(VtFcall*);
310int vtfcallfmt(Fmt*);
311
312enum
313{
314 VtStateAlloc,
315 VtStateConnected,
rsccbeb0b22006-04-01 19:24:03 +0000316 VtStateClosed
rsc97a5e5f2003-11-23 18:25:35 +0000317};
318
319struct VtConn
320{
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;
rsc587f8f42005-02-13 18:32:06 +0000340 char addr[256]; /* address of other side */
rsc97a5e5f2003-11-23 18:25:35 +0000341};
342
343VtConn *vtconn(int infd, int outfd);
344VtConn *vtdial(char*);
345void vtfreeconn(VtConn*);
346int vtsend(VtConn*, Packet*);
347Packet *vtrecv(VtConn*);
348int vtversion(VtConn *z);
349void vtdebug(VtConn *z, char*, ...);
350void vthangup(VtConn *z);
rsc6fc7da32006-10-19 21:58:59 +0000351int vtgoodbye(VtConn *z);
352
rsc97a5e5f2003-11-23 18:25:35 +0000353/* #pragma varargck argpos vtdebug 2 */
354
355/* server */
356typedef struct VtSrv VtSrv;
357typedef struct VtReq VtReq;
358struct VtReq
359{
360 VtFcall tx;
361 VtFcall rx;
362/* private */
363 VtSrv *srv;
364 void *sc;
365};
366
367int vtsrvhello(VtConn*);
368VtSrv *vtlisten(char *addr);
369VtReq *vtgetreq(VtSrv*);
370void vtrespond(VtReq*);
371
372/* client */
373Packet *vtrpc(VtConn*, Packet*);
rsc72520362005-11-02 19:08:43 +0000374Packet *_vtrpc(VtConn*, Packet*, VtFcall*);
rsc97a5e5f2003-11-23 18:25:35 +0000375void vtrecvproc(void*); /* VtConn* */
376void vtsendproc(void*); /* VtConn* */
377
378int vtconnect(VtConn*);
379int vthello(VtConn*);
380int vtread(VtConn*, uchar score[VtScoreSize], uint type, uchar *buf, int n);
381int vtwrite(VtConn*, uchar score[VtScoreSize], uint type, uchar *buf, int n);
382Packet *vtreadpacket(VtConn*, uchar score[VtScoreSize], uint type, int n);
383int vtwritepacket(VtConn*, uchar score[VtScoreSize], uint type, Packet *p);
384int vtsync(VtConn*);
385int vtping(VtConn*);
386
387/*
388 * Data blocks and block cache.
389 */
390enum
391{
rsccbeb0b22006-04-01 19:24:03 +0000392 NilBlock = ~0
rsc97a5e5f2003-11-23 18:25:35 +0000393};
394
395typedef struct VtBlock VtBlock;
396typedef struct VtCache VtCache;
397
398struct VtBlock
399{
400 VtCache *c;
401 QLock lk;
402
403 uchar *data;
404 uchar score[VtScoreSize];
405 uchar type; /* BtXXX */
406
407 /* internal to cache */
408 int nlock;
409 int iostate;
410 int ref;
411 u32int heap;
412 VtBlock *next;
413 VtBlock **prev;
414 u32int used;
415 u32int used2;
416 u32int addr;
rsc6fc7da32006-10-19 21:58:59 +0000417 uintptr pc;
rsc97a5e5f2003-11-23 18:25:35 +0000418};
419
420u32int vtglobaltolocal(uchar[VtScoreSize]);
421void vtlocaltoglobal(u32int, uchar[VtScoreSize]);
422
rsc88dc23a2005-07-13 13:34:57 +0000423VtCache *vtcachealloc(VtConn*, int blocksize, ulong nblocks);
rsc97a5e5f2003-11-23 18:25:35 +0000424void vtcachefree(VtCache*);
425VtBlock *vtcachelocal(VtCache*, u32int addr, int type);
426VtBlock *vtcacheglobal(VtCache*, uchar[VtScoreSize], int type);
427VtBlock *vtcacheallocblock(VtCache*, int type);
rscb98ae9b2004-06-17 19:18:31 +0000428void vtcachesetwrite(VtCache*, int(*)(VtConn*,uchar[VtScoreSize],uint,uchar*,int));
rsc97a5e5f2003-11-23 18:25:35 +0000429void vtblockput(VtBlock*);
430u32int vtcacheblocksize(VtCache*);
431int vtblockwrite(VtBlock*);
432VtBlock *vtblockcopy(VtBlock*);
433void vtblockduplock(VtBlock*);
434
rsc72520362005-11-02 19:08:43 +0000435extern int vtcachencopy, vtcachenread, vtcachenwrite;
rsc11b07722006-05-05 02:42:57 +0000436extern int vttracelevel;
rsc72520362005-11-02 19:08:43 +0000437
rsc97a5e5f2003-11-23 18:25:35 +0000438/*
439 * Hash tree file tree.
440 */
441typedef struct VtFile VtFile;
rscd23a6172004-03-15 01:57:29 +0000442struct VtFile
443{
444 QLock lk;
445 int ref;
446 int local;
447 VtBlock *b; /* block containing this file */
448 uchar score[VtScoreSize]; /* score of block containing this file */
449
450/* immutable */
451 VtCache *c;
452 int mode;
453 u32int gen;
454 int dsize;
rsc2e3b8092005-07-13 14:00:00 +0000455 int psize;
rscd23a6172004-03-15 01:57:29 +0000456 int dir;
457 VtFile *parent;
458 int epb; /* entries per block in parent */
459 u32int offset; /* entry offset in parent */
460};
rsc97a5e5f2003-11-23 18:25:35 +0000461
462enum
463{
464 VtOREAD,
465 VtOWRITE,
rsccbeb0b22006-04-01 19:24:03 +0000466 VtORDWR
rsc97a5e5f2003-11-23 18:25:35 +0000467};
468
469VtFile *vtfileopenroot(VtCache*, VtEntry*);
470VtFile *vtfilecreateroot(VtCache*, int psize, int dsize, int type);
471VtFile *vtfileopen(VtFile*, u32int, int);
472VtFile *vtfilecreate(VtFile*, int psize, int dsize, int dir);
rsc23fb2ed2005-07-24 20:15:44 +0000473VtFile *_vtfilecreate(VtFile*, int offset, int psize, int dsize, int dir);
rsc97a5e5f2003-11-23 18:25:35 +0000474VtBlock *vtfileblock(VtFile*, u32int, int mode);
rsc97a5e5f2003-11-23 18:25:35 +0000475long vtfileread(VtFile*, void*, long, vlong);
476long vtfilewrite(VtFile*, void*, long, vlong);
477int vtfileflush(VtFile*);
478void vtfileincref(VtFile*);
479void vtfileclose(VtFile*);
480int vtfilegetentry(VtFile*, VtEntry*);
rscd23a6172004-03-15 01:57:29 +0000481int vtfilesetentry(VtFile*, VtEntry*);
rsc97a5e5f2003-11-23 18:25:35 +0000482int vtfileblockscore(VtFile*, u32int, uchar[VtScoreSize]);
483u32int vtfilegetdirsize(VtFile*);
484int vtfilesetdirsize(VtFile*, u32int);
485void vtfileunlock(VtFile*);
486int vtfilelock(VtFile*, int);
487int vtfilelock2(VtFile*, VtFile*, int);
rsc3f6a5012005-12-30 02:06:05 +0000488int vtfileflushbefore(VtFile*, u64int);
rscd23a6172004-03-15 01:57:29 +0000489int vtfiletruncate(VtFile*);
490uvlong vtfilegetsize(VtFile*);
rsc3f6a5012005-12-30 02:06:05 +0000491int vtfilesetsize(VtFile*, u64int);
rscd23a6172004-03-15 01:57:29 +0000492int vtfileremove(VtFile*);
rsc97a5e5f2003-11-23 18:25:35 +0000493
rscd20564a2006-07-18 15:23:58 +0000494extern int vttimefmt(Fmt*);
495
rsca09e80f2004-05-23 00:59:17 +0000496extern int chattyventi;
497extern int ventidoublechecksha1;
rsc5810cad2005-02-15 05:03:44 +0000498extern int ventilogging;
rsca09e80f2004-05-23 00:59:17 +0000499
rsc587f8f42005-02-13 18:32:06 +0000500extern char *VtServerLog;
501
rsc819822c2004-06-16 16:45:22 +0000502#ifdef __cplusplus
rscf7012582003-11-25 01:40:27 +0000503}
504#endif
505#endif