Blob


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