Blob


1 #ifndef _VENTI_H_
2 #define _VENTI_H_ 1
3 #ifdef __cplusplus
4 extern "C" {
5 #endif
7 AUTOLIB(venti)
9 /* XXX should be own library? */
10 /*
11 * Packets
12 */
13 enum
14 {
15 MaxFragSize = 9*1024,
16 };
18 typedef struct Packet Packet;
19 Packet *packetalloc(void);
20 void packetfree(Packet*);
21 Packet *packetforeign(uchar *buf, int n, void (*free)(void *a), void *a);
22 Packet *packetdup(Packet*, int offset, int n);
23 Packet *packetsplit(Packet*, int n);
24 int packetconsume(Packet*, uchar *buf, int n);
25 int packettrim(Packet*, int offset, int n);
26 uchar *packetheader(Packet*, int n);
27 uchar *packettrailer(Packet*, int n);
28 void packetprefix(Packet*, uchar *buf, int n);
29 void packetappend(Packet*, uchar *buf, int n);
30 void packetconcat(Packet*, Packet*);
31 uchar *packetpeek(Packet*, uchar *buf, int offset, int n);
32 int packetcopy(Packet*, uchar *buf, int offset, int n);
33 int packetfragments(Packet*, IOchunk*, int nio, int offset);
34 uint packetsize(Packet*);
35 uint packetasize(Packet*);
36 int packetcompact(Packet*);
37 int packetcmp(Packet*, Packet*);
38 void packetstats(void);
39 void packetsha1(Packet*, uchar sha1[20]);
41 /* XXX begin actual venti.h */
43 #ifndef PLAN9PORT
44 #pragma lib "libventi.a"
45 #pragma src "/sys/src/libventi"
46 #endif
48 typedef struct VtFcall VtFcall;
49 typedef struct VtConn VtConn;
50 typedef struct VtEntry VtEntry;
51 typedef struct VtRoot VtRoot;
53 /*
54 * Fundamental constants.
55 */
56 enum
57 {
58 VtScoreSize = 20,
59 VtMaxStringSize = 1024,
60 VtMaxLumpSize = 56*1024,
61 VtPointerDepth = 7,
62 };
63 #define VtMaxFileSize ((1ULL<<48)-1)
66 /*
67 * Strings in packets.
68 */
69 int vtputstring(Packet*, char*);
70 int vtgetstring(Packet*, char**);
72 /*
73 * Block types.
74 *
75 * The initial Venti protocol had a much
76 * less regular list of block types.
77 * VtToDiskType converts from new to old.
78 */
79 enum
80 {
81 VtDataType = 0<<3,
82 /* VtDataType+1, ... */
83 VtDirType = 1<<3,
84 /* VtDirType+1, ... */
85 VtRootType = 2<<3,
86 VtMaxType,
87 VtCorruptType = 0xFF,
89 VtTypeDepthMask = 7,
90 VtTypeBaseMask = ~VtTypeDepthMask,
91 };
93 /* convert to/from on-disk type numbers */
94 uint vttodisktype(uint);
95 uint vtfromdisktype(uint);
97 /*
98 * VtEntry describes a Venti stream
99 *
100 * The _ enums are only used on the wire.
101 * They are not present in the VtEntry structure
102 * and should not be used by client programs.
103 * (The info is in the type field.)
104 */
105 enum
107 VtEntryActive = 1<<0, /* entry is in use */
108 _VtEntryDir = 1<<1, /* a directory */
109 _VtEntryDepthShift = 2, /* shift for pointer depth */
110 _VtEntryDepthMask = 7<<2, /* mask for pointer depth */
111 VtEntryLocal = 1<<5, /* for local storage only */
112 };
113 enum
115 VtEntrySize = 40,
116 };
117 struct VtEntry
119 ulong gen; /* generation number */
120 ushort psize; /* pointer block size */
121 ushort dsize; /* data block size */
122 uchar type;
123 uchar flags;
124 uvlong size;
125 uchar score[VtScoreSize];
126 };
128 void vtentrypack(VtEntry*, uchar*, int index);
129 int vtentryunpack(VtEntry*, uchar*, int index);
131 struct VtRoot
133 char name[128];
134 char type[128];
135 uchar score[VtScoreSize]; /* to a Dir block */
136 ushort blocksize; /* maximum block size */
137 uchar prev[VtScoreSize]; /* last root block */
138 };
140 enum
142 VtRootSize = 300,
143 VtRootVersion = 2,
144 };
146 void vtrootpack(VtRoot*, uchar*);
147 int vtrootunpack(VtRoot*, uchar*);
149 /*
150 * score of zero length block
151 */
152 extern uchar vtzeroscore[VtScoreSize];
154 /*
155 * zero extend and truncate blocks
156 */
157 void vtzeroextend(int type, uchar *buf, uint n, uint nn);
158 uint vtzerotruncate(int type, uchar *buf, uint n);
160 /*
161 * parse score: mungs s
162 */
163 int vtparsescore(char *s, char **prefix, uchar[VtScoreSize]);
165 /*
166 * formatting
167 * other than noted, these formats all ignore
168 * the width and precision arguments, and all flags
170 * V a venti score
171 */
172 #ifndef PLAN9PORT
173 #pragma varargck type "V" uchar*
174 #pragma varargck type "F" VtFcall*
175 #endif
177 int vtscorefmt(Fmt*);
179 /*
180 * error-checking malloc et al.
181 */
182 void vtfree(void *);
183 void *vtmalloc(int);
184 void *vtmallocz(int);
185 void *vtrealloc(void *p, int);
186 void *vtbrk(int n);
187 char *vtstrdup(char *);
189 /*
190 * Venti protocol
191 */
193 /*
194 * Crypto strengths
195 */
196 enum
198 VtCryptoStrengthNone,
199 VtCryptoStrengthAuth,
200 VtCryptoStrengthWeak,
201 VtCryptoStrengthStrong,
202 };
204 /*
205 * Crypto suites
206 */
207 enum
209 VtCryptoNone,
210 VtCryptoSSL3,
211 VtCryptoTLS1,
212 VtCryptoMax,
213 };
215 /*
216 * Codecs
217 */
218 enum
220 VtCodecNone,
221 VtCodecDeflate,
222 VtCodecThwack,
223 VtCodecMax
224 };
226 enum
228 VtRerror = 1,
229 VtTping = 2,
230 VtRping,
231 VtThello = 4,
232 VtRhello,
233 VtTgoodbye = 6,
234 VtRgoodbye, /* not used */
235 VtTauth0 = 8,
236 VtRauth0,
237 VtTauth1 = 10,
238 VtRauth1,
239 VtTread = 12,
240 VtRread,
241 VtTwrite = 14,
242 VtRwrite,
243 VtTsync = 16,
244 VtRsync,
246 VtTmax
247 };
249 struct VtFcall
251 uchar type;
252 uchar tag;
254 char *error; /* Rerror */
256 char *version; /* Thello */
257 char *uid; /* Thello */
258 uchar strength; /* Thello */
259 uchar *crypto; /* Thello */
260 uint ncrypto; /* Thello */
261 uchar *codec; /* Thello */
262 uint ncodec; /* Thello */
263 char *sid; /* Rhello */
264 uchar rcrypto; /* Rhello */
265 uchar rcodec; /* Rhello */
266 uchar *auth; /* TauthX, RauthX */
267 uint nauth; /* TauthX, RauthX */
268 uchar score[VtScoreSize]; /* Tread, Rwrite */
269 uchar dtype; /* Tread, Twrite */
270 ushort count; /* Tread */
271 Packet *data; /* Rread, Twrite */
272 };
274 Packet *vtfcallpack(VtFcall*);
275 int vtfcallunpack(VtFcall*, Packet*);
276 void vtfcallclear(VtFcall*);
277 int vtfcallfmt(Fmt*);
279 enum
281 VtStateAlloc,
282 VtStateConnected,
283 VtStateClosed,
284 };
286 struct VtConn
288 QLock lk;
289 QLock inlk;
290 QLock outlk;
291 int debug;
292 int infd;
293 int outfd;
294 int muxer;
295 void *writeq;
296 void *readq;
297 int state;
298 void *wait[256];
299 uint ntag;
300 uint nsleep;
301 Packet *part;
302 Rendez tagrend;
303 Rendez rpcfork;
304 char *version;
305 char *uid;
306 char *sid;
307 };
309 VtConn *vtconn(int infd, int outfd);
310 VtConn *vtdial(char*);
311 void vtfreeconn(VtConn*);
312 int vtsend(VtConn*, Packet*);
313 Packet *vtrecv(VtConn*);
314 int vtversion(VtConn *z);
315 void vtdebug(VtConn *z, char*, ...);
316 void vthangup(VtConn *z);
317 /* #pragma varargck argpos vtdebug 2 */
319 /* server */
320 typedef struct VtSrv VtSrv;
321 typedef struct VtReq VtReq;
322 struct VtReq
324 VtFcall tx;
325 VtFcall rx;
326 /* private */
327 VtSrv *srv;
328 void *sc;
329 };
331 int vtsrvhello(VtConn*);
332 VtSrv *vtlisten(char *addr);
333 VtReq *vtgetreq(VtSrv*);
334 void vtrespond(VtReq*);
336 /* client */
337 Packet *vtrpc(VtConn*, Packet*);
338 void vtrecvproc(void*); /* VtConn* */
339 void vtsendproc(void*); /* VtConn* */
341 int vtconnect(VtConn*);
342 int vthello(VtConn*);
343 int vtread(VtConn*, uchar score[VtScoreSize], uint type, uchar *buf, int n);
344 int vtwrite(VtConn*, uchar score[VtScoreSize], uint type, uchar *buf, int n);
345 Packet *vtreadpacket(VtConn*, uchar score[VtScoreSize], uint type, int n);
346 int vtwritepacket(VtConn*, uchar score[VtScoreSize], uint type, Packet *p);
347 int vtsync(VtConn*);
348 int vtping(VtConn*);
350 /*
351 * Data blocks and block cache.
352 */
353 enum
355 NilBlock = ~0,
356 };
358 typedef struct VtBlock VtBlock;
359 typedef struct VtCache VtCache;
361 struct VtBlock
363 VtCache *c;
364 QLock lk;
366 uchar *data;
367 uchar score[VtScoreSize];
368 uchar type; /* BtXXX */
370 /* internal to cache */
371 int nlock;
372 int iostate;
373 int ref;
374 u32int heap;
375 VtBlock *next;
376 VtBlock **prev;
377 u32int used;
378 u32int used2;
379 u32int addr;
381 /* internal to efile (HACK) */
382 int decrypted;
383 };
385 u32int vtglobaltolocal(uchar[VtScoreSize]);
386 void vtlocaltoglobal(u32int, uchar[VtScoreSize]);
388 VtCache *vtcachealloc(VtConn*, int blocksize, ulong nblocks, int mode);
389 void vtcachefree(VtCache*);
390 VtBlock *vtcachelocal(VtCache*, u32int addr, int type);
391 VtBlock *vtcacheglobal(VtCache*, uchar[VtScoreSize], int type);
392 VtBlock *vtcacheallocblock(VtCache*, int type);
393 void vtcachesetwrite(VtCache*, int(*)(VtConn*,uchar[VtScoreSize],uint,uchar*,int));
394 void vtblockput(VtBlock*);
395 u32int vtcacheblocksize(VtCache*);
396 int vtblockwrite(VtBlock*);
397 VtBlock *vtblockcopy(VtBlock*);
398 void vtblockduplock(VtBlock*);
399 int vtblockdirty(VtBlock*);
401 /*
402 * Hash tree file tree.
403 */
404 typedef struct VtFile VtFile;
405 struct VtFile
407 QLock lk;
408 int ref;
409 int local;
410 VtBlock *b; /* block containing this file */
411 uchar score[VtScoreSize]; /* score of block containing this file */
413 /* immutable */
414 VtCache *c;
415 int mode;
416 u32int gen;
417 int dsize;
418 int dir;
419 VtFile *parent;
420 int epb; /* entries per block in parent */
421 u32int offset; /* entry offset in parent */
422 };
424 enum
426 VtOREAD,
427 VtOWRITE,
428 VtORDWR,
429 VtOCREATE = 0x100,
430 };
432 VtFile *vtfileopenroot(VtCache*, VtEntry*);
433 VtFile *vtfilecreateroot(VtCache*, int psize, int dsize, int type);
434 VtFile *vtfileopen(VtFile*, u32int, int);
435 VtFile *vtfilecreate(VtFile*, int psize, int dsize, int dir);
436 VtBlock *vtfileblock(VtFile*, u32int, int mode);
437 long vtfileread(VtFile*, void*, long, vlong);
438 long vtfilewrite(VtFile*, void*, long, vlong);
439 int vtfileflush(VtFile*);
440 void vtfileincref(VtFile*);
441 void vtfileclose(VtFile*);
442 int vtfilegetentry(VtFile*, VtEntry*);
443 int vtfilesetentry(VtFile*, VtEntry*);
444 int vtfileblockscore(VtFile*, u32int, uchar[VtScoreSize]);
445 u32int vtfilegetdirsize(VtFile*);
446 int vtfilesetdirsize(VtFile*, u32int);
447 void vtfileunlock(VtFile*);
448 int vtfilelock(VtFile*, int);
449 int vtfilelock2(VtFile*, VtFile*, int);
450 int vtfileflushbefore(VtFile*, u64int);
451 int vtfiletruncate(VtFile*);
452 uvlong vtfilegetsize(VtFile*);
453 int vtfilesetsize(VtFile*, uvlong);
454 int vtfileremove(VtFile*);
456 extern int chattyventi;
457 extern int ventidoublechecksha1;
459 #ifdef __cplusplus
461 #endif
462 #endif