Blob


1 #ifndef _VENTI_H_
2 #define _VENTI_H_ 1
3 #if defined(__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 /*
41 #pragma lib "libnventi.a"
42 #pragma src "/sys/src/libnventi"
43 */
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 VtCorruptType,
84 VtMaxType,
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 /* #pragma varargck type "V" uchar* */
171 int vtscorefmt(Fmt*);
173 /*
174 * error-checking malloc et al.
175 */
176 void vtfree(void *);
177 void *vtmalloc(int);
178 void *vtmallocz(int);
179 void *vtrealloc(void *p, int);
180 void *vtbrk(int n);
181 char *vtstrdup(char *);
183 /*
184 * Venti protocol
185 */
187 /*
188 * Crypto strengths
189 */
190 enum
192 VtCryptoStrengthNone,
193 VtCryptoStrengthAuth,
194 VtCryptoStrengthWeak,
195 VtCryptoStrengthStrong,
196 };
198 /*
199 * Crypto suites
200 */
201 enum
203 VtCryptoNone,
204 VtCryptoSSL3,
205 VtCryptoTLS1,
206 VtCryptoMax,
207 };
209 /*
210 * Codecs
211 */
212 enum
214 VtCodecNone,
215 VtCodecDeflate,
216 VtCodecThwack,
217 VtCodecMax
218 };
220 enum
222 VtRerror = 1,
223 VtTping = 2,
224 VtRping,
225 VtThello = 4,
226 VtRhello,
227 VtTgoodbye = 6,
228 VtRgoodbye, /* not used */
229 VtTauth0 = 8,
230 VtRauth0,
231 VtTauth1 = 10,
232 VtRauth1,
233 VtTread = 12,
234 VtRread,
235 VtTwrite = 14,
236 VtRwrite,
237 VtTsync = 16,
238 VtRsync,
240 VtTmax
241 };
243 struct VtFcall
245 uchar type;
246 uchar tag;
248 char *error; /* Rerror */
250 char *version; /* Thello */
251 char *uid; /* Thello */
252 uchar strength; /* Thello */
253 uchar *crypto; /* Thello */
254 uint ncrypto; /* Thello */
255 uchar *codec; /* Thello */
256 uint ncodec; /* Thello */
257 char *sid; /* Rhello */
258 uchar rcrypto; /* Rhello */
259 uchar rcodec; /* Rhello */
260 uchar *auth; /* TauthX, RauthX */
261 uint nauth; /* TauthX, RauthX */
262 uchar score[VtScoreSize]; /* Tread, Rwrite */
263 uchar dtype; /* Tread, Twrite */
264 ushort count; /* Tread */
265 Packet *data; /* Rread, Twrite */
266 };
268 Packet *vtfcallpack(VtFcall*);
269 int vtfcallunpack(VtFcall*, Packet*);
270 void vtfcallclear(VtFcall*);
271 int vtfcallfmt(Fmt*);
273 enum
275 VtStateAlloc,
276 VtStateConnected,
277 VtStateClosed,
278 };
280 struct VtConn
282 QLock lk;
283 QLock inlk;
284 QLock outlk;
285 int debug;
286 int infd;
287 int outfd;
288 int muxer;
289 void *writeq;
290 void *readq;
291 int state;
292 void *wait[256];
293 uint ntag;
294 uint nsleep;
295 Packet *part;
296 Rendez tagrend;
297 Rendez rpcfork;
298 char *version;
299 char *uid;
300 char *sid;
301 };
303 VtConn *vtconn(int infd, int outfd);
304 VtConn *vtdial(char*);
305 void vtfreeconn(VtConn*);
306 int vtsend(VtConn*, Packet*);
307 Packet *vtrecv(VtConn*);
308 int vtversion(VtConn *z);
309 void vtdebug(VtConn *z, char*, ...);
310 void vthangup(VtConn *z);
311 /* #pragma varargck argpos vtdebug 2 */
313 /* server */
314 typedef struct VtSrv VtSrv;
315 typedef struct VtReq VtReq;
316 struct VtReq
318 VtFcall tx;
319 VtFcall rx;
320 /* private */
321 VtSrv *srv;
322 void *sc;
323 };
325 int vtsrvhello(VtConn*);
326 VtSrv *vtlisten(char *addr);
327 VtReq *vtgetreq(VtSrv*);
328 void vtrespond(VtReq*);
330 /* client */
331 Packet *vtrpc(VtConn*, Packet*);
332 void vtrecvproc(void*); /* VtConn* */
333 void vtsendproc(void*); /* VtConn* */
335 int vtconnect(VtConn*);
336 int vthello(VtConn*);
337 int vtread(VtConn*, uchar score[VtScoreSize], uint type, uchar *buf, int n);
338 int vtwrite(VtConn*, uchar score[VtScoreSize], uint type, uchar *buf, int n);
339 Packet *vtreadpacket(VtConn*, uchar score[VtScoreSize], uint type, int n);
340 int vtwritepacket(VtConn*, uchar score[VtScoreSize], uint type, Packet *p);
341 int vtsync(VtConn*);
342 int vtping(VtConn*);
344 /*
345 * Data blocks and block cache.
346 */
347 enum
349 NilBlock = ~0,
350 };
352 typedef struct VtBlock VtBlock;
353 typedef struct VtCache VtCache;
355 struct VtBlock
357 VtCache *c;
358 QLock lk;
360 uchar *data;
361 uchar score[VtScoreSize];
362 uchar type; /* BtXXX */
364 /* internal to cache */
365 int nlock;
366 int iostate;
367 int ref;
368 u32int heap;
369 VtBlock *next;
370 VtBlock **prev;
371 u32int used;
372 u32int used2;
373 u32int addr;
375 /* internal to efile (HACK) */
376 int decrypted;
377 };
379 u32int vtglobaltolocal(uchar[VtScoreSize]);
380 void vtlocaltoglobal(u32int, uchar[VtScoreSize]);
382 VtCache *vtcachealloc(VtConn*, int blocksize, ulong nblocks, int mode);
383 void vtcachefree(VtCache*);
384 VtBlock *vtcachelocal(VtCache*, u32int addr, int type);
385 VtBlock *vtcacheglobal(VtCache*, uchar[VtScoreSize], int type);
386 VtBlock *vtcacheallocblock(VtCache*, int type);
387 void vtblockput(VtBlock*);
388 u32int vtcacheblocksize(VtCache*);
389 int vtblockwrite(VtBlock*);
390 VtBlock *vtblockcopy(VtBlock*);
391 void vtblockduplock(VtBlock*);
392 int vtblockdirty(VtBlock*);
394 /*
395 * Hash tree file tree.
396 */
397 typedef struct VtFile VtFile;
398 struct VtFile
400 QLock lk;
401 int ref;
402 int local;
403 VtBlock *b; /* block containing this file */
404 uchar score[VtScoreSize]; /* score of block containing this file */
406 /* immutable */
407 VtCache *c;
408 int mode;
409 u32int gen;
410 int dsize;
411 int dir;
412 VtFile *parent;
413 int epb; /* entries per block in parent */
414 u32int offset; /* entry offset in parent */
415 };
417 enum
419 VtOREAD,
420 VtOWRITE,
421 VtORDWR,
422 VtOCREATE = 0x100,
423 };
425 VtFile *vtfileopenroot(VtCache*, VtEntry*);
426 VtFile *vtfilecreateroot(VtCache*, int psize, int dsize, int type);
427 VtFile *vtfileopen(VtFile*, u32int, int);
428 VtFile *vtfilecreate(VtFile*, int psize, int dsize, int dir);
429 VtBlock *vtfileblock(VtFile*, u32int, int mode);
430 long vtfileread(VtFile*, void*, long, vlong);
431 long vtfilewrite(VtFile*, void*, long, vlong);
432 int vtfileflush(VtFile*);
433 void vtfileincref(VtFile*);
434 void vtfileclose(VtFile*);
435 int vtfilegetentry(VtFile*, VtEntry*);
436 int vtfilesetentry(VtFile*, VtEntry*);
437 int vtfileblockscore(VtFile*, u32int, uchar[VtScoreSize]);
438 u32int vtfilegetdirsize(VtFile*);
439 int vtfilesetdirsize(VtFile*, u32int);
440 void vtfileunlock(VtFile*);
441 int vtfilelock(VtFile*, int);
442 int vtfilelock2(VtFile*, VtFile*, int);
443 int vtfileflushbefore(VtFile*, u64int);
444 int vtfiletruncate(VtFile*);
445 uvlong vtfilegetsize(VtFile*);
446 int vtfilesetsize(VtFile*, uvlong);
447 int vtfileremove(VtFile*);
449 #if defined(__cplusplus)
451 #endif
452 #endif