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