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 int packetprefix(Packet*, uchar *buf, int n);
26 int packetappend(Packet*, uchar *buf, int n);
27 int 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 VtSha1 VtSha1;
47 typedef struct VtConn VtConn;
48 typedef struct VtEntry VtEntry;
49 typedef struct VtRoot VtRoot;
51 /*
52 * Fundamental constants.
53 */
54 enum
55 {
56 VtScoreSize = 20,
57 VtMaxStringSize = 1024,
58 VtMaxLumpSize = 56*1024,
59 VtPointerDepth = 7,
60 };
61 #define VtMaxFileSize ((1ULL<<48)-1)
64 /*
65 * Strings in packets.
66 */
67 int vtputstring(Packet*, char*);
68 int vtgetstring(Packet*, char**);
70 /*
71 * Block types.
72 *
73 * The initial Venti protocol had a much
74 * less regular list of block types.
75 * VtToDiskType converts from new to old.
76 */
77 enum
78 {
79 VtDataType = 0<<3,
80 /* VtDataType+1, ... */
81 VtDirType = 1<<3,
82 /* VtDirType+1, ... */
83 VtRootType = 2<<3,
84 VtCorruptType,
85 VtMaxType,
87 VtTypeDepthMask = 7,
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 enum
98 {
99 VtEntryActive = 1<<0, /* entry is in use */
100 VtEntryDir = 1<<1, /* a directory */
101 VtEntryDepthShift = 2, /* shift for pointer depth */
102 VtEntryDepthMask = 7<<2, /* mask for pointer depth */
103 VtEntryLocal = 1<<5, /* for local storage only */
104 };
105 enum
107 VtEntrySize = 40,
108 };
109 struct VtEntry
111 ulong gen; /* generation number */
112 ushort psize; /* pointer block size */
113 ushort dsize; /* data block size */
114 uchar type;
115 uchar flags;
116 uvlong size;
117 uchar score[VtScoreSize];
118 };
120 void vtentrypack(VtEntry*, uchar*, int index);
121 int vtentryunpack(VtEntry*, uchar*, int index);
123 struct VtRoot
125 char name[128];
126 char type[128];
127 uchar score[VtScoreSize]; /* to a Dir block */
128 ushort blocksize; /* maximum block size */
129 uchar prev[VtScoreSize]; /* last root block */
130 };
132 enum
134 VtRootSize = 300,
135 VtRootVersion = 2,
136 };
138 void vtrootpack(VtRoot*, uchar*);
139 int vtrootunpack(VtRoot*, uchar*);
141 /*
142 * score of zero length block
143 */
144 extern uchar vtzeroscore[VtScoreSize];
146 /*
147 * zero extend and truncate blocks
148 */
149 void vtzeroextend(int type, uchar *buf, uint n, uint nn);
150 uint vtzerotruncate(int type, uchar *buf, uint n);
152 /*
153 * parse score: mungs s
154 */
155 int vtparsescore(char *s, uint len, char **prefix, uchar[VtScoreSize]);
157 /*
158 * formatting
159 * other than noted, these formats all ignore
160 * the width and precision arguments, and all flags
162 * V a venti score
163 */
164 /* #pragma varargck type "V" uchar* */
166 int vtscorefmt(Fmt*);
168 /*
169 * error-checking malloc et al.
170 */
171 void vtfree(void *);
172 void *vtmalloc(int);
173 void *vtmallocz(int);
174 void *vtrealloc(void *p, int);
175 void *vtbrk(int n);
176 char *vtstrdup(char *);
178 /*
179 * Venti protocol
180 */
182 /*
183 * Crypto strengths
184 */
185 enum
187 VtCryptoStrengthNone,
188 VtCryptoStrengthAuth,
189 VtCryptoStrengthWeak,
190 VtCryptoStrengthStrong,
191 };
193 /*
194 * Crypto suites
195 */
196 enum
198 VtCryptoNone,
199 VtCryptoSSL3,
200 VtCryptoTLS1,
201 VtCryptoMax,
202 };
204 /*
205 * Codecs
206 */
207 enum
209 VtCodecNone,
210 VtCodecDeflate,
211 VtCodecThwack,
212 VtCodecMax
213 };
215 enum
217 VtRerror = 1,
218 VtTping = 2,
219 VtRping,
220 VtThello = 4,
221 VtRhello,
222 VtTgoodbye = 6,
223 VtRgoodbye, /* not used */
224 VtTauth0 = 8,
225 VtRauth0,
226 VtTauth1 = 10,
227 VtRauth1,
228 VtTread = 12,
229 VtRread,
230 VtTwrite = 14,
231 VtRwrite,
232 VtTsync = 16,
233 VtRsync,
235 VtTmax
236 };
238 struct VtFcall
240 uchar type;
241 uchar tag;
243 char *error; /* Rerror */
245 char *version; /* Thello */
246 char *uid; /* Thello */
247 uchar strength; /* Thello */
248 uchar *crypto; /* Thello */
249 uint ncrypto; /* Thello */
250 uchar *codec; /* Thello */
251 uint ncodec; /* Thello */
252 char *sid; /* Rhello */
253 uchar rcrypto; /* Rhello */
254 uchar rcodec; /* Rhello */
255 uchar *auth; /* TauthX, RauthX */
256 uint nauth; /* TauthX, RauthX */
257 uchar score[VtScoreSize]; /* Tread, Rwrite */
258 uchar dtype; /* Tread, Twrite */
259 ushort count; /* Tread */
260 Packet *data; /* Rread, Twrite */
261 };
263 Packet *vtfcallpack(VtFcall*);
264 int vtfcallunpack(VtFcall*, Packet*);
265 void vtfcallclear(VtFcall*);
266 int vtfcallfmt(Fmt*);
268 enum
270 VtStateAlloc,
271 VtStateConnected,
272 VtStateClosed,
273 };
275 struct VtConn
277 QLock lk;
278 QLock inlk;
279 QLock outlk;
280 int debug;
281 int infd;
282 int outfd;
283 int muxer;
284 void *writeq;
285 void *readq;
286 int state;
287 void *wait[256];
288 uint ntag;
289 uint nsleep;
290 Packet *part;
291 Rendez tagrend;
292 Rendez rpcfork;
293 char *version;
294 char *uid;
295 char *sid;
296 };
298 VtConn *vtconn(int infd, int outfd);
299 VtConn *vtdial(char*);
300 void vtfreeconn(VtConn*);
301 int vtsend(VtConn*, Packet*);
302 Packet *vtrecv(VtConn*);
303 int vtversion(VtConn *z);
304 void vtdebug(VtConn *z, char*, ...);
305 void vthangup(VtConn *z);
306 /* #pragma varargck argpos vtdebug 2 */
308 /* server */
309 typedef struct VtSrv VtSrv;
310 typedef struct VtReq VtReq;
311 struct VtReq
313 VtFcall tx;
314 VtFcall rx;
315 /* private */
316 VtSrv *srv;
317 void *sc;
318 };
320 int vtsrvhello(VtConn*);
321 VtSrv *vtlisten(char *addr);
322 VtReq *vtgetreq(VtSrv*);
323 void vtrespond(VtReq*);
325 /* client */
326 Packet *vtrpc(VtConn*, Packet*);
327 void vtrecvproc(void*); /* VtConn* */
328 void vtsendproc(void*); /* VtConn* */
330 int vtconnect(VtConn*);
331 int vthello(VtConn*);
332 int vtread(VtConn*, uchar score[VtScoreSize], uint type, uchar *buf, int n);
333 int vtwrite(VtConn*, uchar score[VtScoreSize], uint type, uchar *buf, int n);
334 Packet *vtreadpacket(VtConn*, uchar score[VtScoreSize], uint type, int n);
335 int vtwritepacket(VtConn*, uchar score[VtScoreSize], uint type, Packet *p);
336 int vtsync(VtConn*);
337 int vtping(VtConn*);
339 /*
340 * Data blocks and block cache.
341 */
342 enum
344 NilBlock = ~0,
345 };
347 typedef struct VtBlock VtBlock;
348 typedef struct VtCache VtCache;
350 struct VtBlock
352 VtCache *c;
353 QLock lk;
355 uchar *data;
356 uchar score[VtScoreSize];
357 uchar type; /* BtXXX */
359 /* internal to cache */
360 int nlock;
361 int iostate;
362 int ref;
363 u32int heap;
364 VtBlock *next;
365 VtBlock **prev;
366 u32int used;
367 u32int used2;
368 u32int addr;
370 /* internal to efile (HACK) */
371 int decrypted;
372 };
374 u32int vtglobaltolocal(uchar[VtScoreSize]);
375 void vtlocaltoglobal(u32int, uchar[VtScoreSize]);
377 VtCache *vtcachealloc(VtConn*, int blocksize, ulong nblocks, int mode);
378 void vtcachefree(VtCache*);
379 VtBlock *vtcachelocal(VtCache*, u32int addr, int type);
380 VtBlock *vtcacheglobal(VtCache*, uchar[VtScoreSize], int type);
381 VtBlock *vtcacheallocblock(VtCache*, int type);
382 void vtblockput(VtBlock*);
383 u32int vtcacheblocksize(VtCache*);
384 int vtblockwrite(VtBlock*);
385 VtBlock *vtblockcopy(VtBlock*);
386 void vtblockduplock(VtBlock*);
388 /*
389 * Hash tree file tree.
390 */
391 typedef struct VtFile VtFile;
393 enum
395 VtOREAD,
396 VtOWRITE,
397 VtORDWR,
398 VtOCREATE = 0x100,
399 };
401 VtFile *vtfileopenroot(VtCache*, VtEntry*);
402 VtFile *vtfilecreateroot(VtCache*, int psize, int dsize, int type);
403 VtFile *vtfileopen(VtFile*, u32int, int);
404 VtFile *vtfilecreate(VtFile*, int psize, int dsize, int dir);
405 VtBlock *vtfileblock(VtFile*, u32int, int mode);
406 int vtfileblockhash(VtFile*, u32int, uchar[VtScoreSize]);
407 long vtfileread(VtFile*, void*, long, vlong);
408 long vtfilewrite(VtFile*, void*, long, vlong);
409 int vtfileflush(VtFile*);
410 void vtfileincref(VtFile*);
411 void vtfileclose(VtFile*);
412 int vtfilegetentry(VtFile*, VtEntry*);
413 int vtfileblockscore(VtFile*, u32int, uchar[VtScoreSize]);
414 u32int vtfilegetdirsize(VtFile*);
415 int vtfilesetdirsize(VtFile*, u32int);
416 void vtfileunlock(VtFile*);
417 int vtfilelock(VtFile*, int);
418 int vtfilelock2(VtFile*, VtFile*, int);
419 int vtfileflushbefore(VtFile*, u64int);
421 #if defined(__cplusplus)
423 #endif
424 #endif