plan9port

fork of plan9port with libvec, libstr and libsdb
Log | Files | Refs | README | LICENSE

venti.h (10082B)


      1 #ifndef _VENTI_H_
      2 #define _VENTI_H_ 1
      3 #ifdef __cplusplus
      4 extern "C" { 
      5 #endif
      6 
      7 
      8 AUTOLIB(venti)
      9 
     10 /* XXX should be own library? */
     11 /*
     12  * Packets
     13  */
     14 enum
     15 {
     16 	MaxFragSize = 9*1024
     17 };
     18 
     19 typedef struct Packet Packet;
     20 
     21 Packet*	packetalloc(void);
     22 void	packetappend(Packet*, uchar *buf, int n);
     23 uint	packetasize(Packet*);
     24 int	packetcmp(Packet*, Packet*);
     25 int	packetcompact(Packet*);
     26 void	packetconcat(Packet*, Packet*);
     27 int	packetconsume(Packet*, uchar *buf, int n);
     28 int	packetcopy(Packet*, uchar *buf, int offset, int n);
     29 Packet*	packetdup(Packet*, int offset, int n);
     30 Packet*	packetforeign(uchar *buf, int n, void (*free)(void *a), void *a);
     31 int	packetfragments(Packet*, IOchunk*, int nio, int offset);
     32 void	packetfree(Packet*);
     33 uchar*	packetheader(Packet*, int n);
     34 uchar*	packetpeek(Packet*, uchar *buf, int offset, int n);
     35 void	packetprefix(Packet*, uchar *buf, int n);
     36 void	packetsha1(Packet*, uchar sha1[20]);
     37 uint	packetsize(Packet*);
     38 Packet*	packetsplit(Packet*, int n);
     39 void	packetstats(void);
     40 uchar*	packettrailer(Packet*, int n);
     41 int	packettrim(Packet*, int offset, int n);
     42 
     43 /* XXX should be own library? */
     44 /*
     45  * Logging
     46  */
     47 typedef struct VtLog VtLog;
     48 typedef struct VtLogChunk VtLogChunk;
     49 
     50 struct VtLog
     51 {
     52 	VtLog	*next;		/* in hash table */
     53 	char	*name;
     54 	VtLogChunk *chunk;
     55 	uint	nchunk;
     56 	VtLogChunk *w;
     57 	QLock	lk;
     58 	int	ref;
     59 };
     60 
     61 struct VtLogChunk
     62 {
     63 	char	*p;
     64 	char	*ep;
     65 	char	*wp;
     66 };
     67 
     68 VtLog*	vtlogopen(char *name, uint size);
     69 void	vtlogprint(VtLog *log, char *fmt, ...);
     70 void	vtlog(char *name, char *fmt, ...);
     71 void	vtlogclose(VtLog*);
     72 void	vtlogremove(char *name);
     73 char**	vtlognames(int*);
     74 void	vtlogdump(int fd, VtLog*);
     75 
     76 /* XXX begin actual venti.h */
     77 
     78 typedef struct VtFcall VtFcall;
     79 typedef struct VtConn VtConn;
     80 typedef struct VtEntry VtEntry;
     81 typedef struct VtRoot VtRoot;
     82 
     83 /*
     84  * Fundamental constants.
     85  */
     86 enum
     87 {
     88 	VtScoreSize	= 20,
     89 	VtMaxStringSize = 1024,
     90 	VtPointerDepth	= 7
     91 };
     92 #define VtMaxFileSize ((1ULL<<48)-1)
     93 
     94 
     95 /* 
     96  * Strings in packets.
     97  */
     98 int vtputstring(Packet*, char*);
     99 int vtgetstring(Packet*, char**);
    100 
    101 /*
    102  * Block types.
    103  * 
    104  * The initial Venti protocol had a much
    105  * less regular list of block types.
    106  * VtToDiskType converts from new to old.
    107  */
    108 enum
    109 {
    110 	VtDataType	= 0<<3,
    111 	/* VtDataType+1, ... */
    112 	VtDirType	= 1<<3,
    113 	/* VtDirType+1, ... */
    114 	VtRootType	= 2<<3,
    115 	VtMaxType,
    116 	VtCorruptType = 0xFF,
    117 
    118 	VtTypeDepthMask = 7,
    119 	VtTypeBaseMask = ~VtTypeDepthMask
    120 };
    121 
    122 /* convert to/from on-disk type numbers */
    123 uint vttodisktype(uint);
    124 uint vtfromdisktype(uint);
    125 
    126 /*
    127  * VtEntry describes a Venti stream
    128  *
    129  * The _ enums are only used on the wire.
    130  * They are not present in the VtEntry structure
    131  * and should not be used by client programs.
    132  * (The info is in the type field.)
    133  */
    134 enum
    135 {
    136 	VtEntryActive = 1<<0,		/* entry is in use */
    137 	_VtEntryDir = 1<<1,		/* a directory */
    138 	_VtEntryDepthShift = 2,		/* shift for pointer depth */
    139 	_VtEntryDepthMask = 7<<2,	/* mask for pointer depth */
    140 	VtEntryLocal = 1<<5,		/* for local storage only */
    141 	_VtEntryBig = 1<<6,
    142 	VtEntryNoArchive = 1<<7,	/* for local storage only */
    143 };
    144 enum
    145 {
    146 	VtEntrySize = 40
    147 };
    148 struct VtEntry
    149 {
    150 	ulong	gen;			/* generation number */
    151 	ulong	psize;			/* pointer block size */
    152 	ulong	dsize;			/* data block size */
    153 	uchar	type;
    154 	uchar	flags;
    155 	uvlong	size;
    156 	uchar	score[VtScoreSize];
    157 };
    158 
    159 void vtentrypack(VtEntry*, uchar*, int index);
    160 int vtentryunpack(VtEntry*, uchar*, int index);
    161 
    162 struct VtRoot
    163 {
    164 	char	name[128];
    165 	char	type[128];
    166 	uchar	score[VtScoreSize];	/* to a Dir block */
    167 	ulong	blocksize;		/* maximum block size */
    168 	uchar	prev[VtScoreSize];	/* last root block */
    169 };
    170 
    171 enum
    172 {
    173 	VtRootSize = 300,
    174 	VtRootVersion = 2,
    175 	_VtRootVersionBig = 1<<15,
    176 };
    177 
    178 void vtrootpack(VtRoot*, uchar*);
    179 int vtrootunpack(VtRoot*, uchar*);
    180 
    181 /*
    182  * score of zero length block
    183  */
    184 extern uchar vtzeroscore[VtScoreSize];
    185 
    186 /*
    187  * zero extend and truncate blocks
    188  */
    189 void vtzeroextend(int type, uchar *buf, uint n, uint nn);
    190 uint vtzerotruncate(int type, uchar *buf, uint n);
    191 
    192 /*
    193  * parse score: mungs s
    194  */
    195 int vtparsescore(char *s, char **prefix, uchar[VtScoreSize]);
    196 
    197 /*
    198  * formatting
    199  * other than noted, these formats all ignore
    200  * the width and precision arguments, and all flags
    201  *
    202  * V	a venti score
    203  */
    204 
    205 int vtscorefmt(Fmt*);
    206 
    207 /*
    208  * error-checking malloc et al.
    209  */
    210 void	vtfree(void *);
    211 void*	vtmalloc(int);
    212 void*	vtmallocz(int);
    213 void*	vtrealloc(void *p, int);
    214 void*	vtbrk(int n);
    215 char*	vtstrdup(char *);
    216 
    217 /*
    218  * Venti protocol
    219  */
    220 
    221 /*
    222  * Crypto strengths
    223  */
    224 enum
    225 {
    226 	VtCryptoStrengthNone,
    227 	VtCryptoStrengthAuth,
    228 	VtCryptoStrengthWeak,
    229 	VtCryptoStrengthStrong
    230 };
    231 
    232 /*
    233  * Crypto suites
    234  */
    235 enum
    236 {
    237 	VtCryptoNone,
    238 	VtCryptoSSL3,
    239 	VtCryptoTLS1,
    240 	VtCryptoMax
    241 };
    242 
    243 /* 
    244  * Codecs
    245  */
    246 enum
    247 {
    248 	VtCodecNone,
    249 	VtCodecDeflate,
    250 	VtCodecThwack,
    251 	VtCodecMax
    252 };
    253 
    254 enum
    255 {
    256 	VtRerror	= 1,
    257 	VtTping		= 2,
    258 	VtRping,
    259 	VtThello	= 4,
    260 	VtRhello,
    261 	VtTgoodbye	= 6,
    262 	VtRgoodbye,	/* not used */
    263 	VtTauth0	= 8,
    264 	VtRauth0,
    265 	VtTauth1	= 10,
    266 	VtRauth1,
    267 	VtTread		= 12,
    268 	VtRread,
    269 	VtTwrite	= 14,
    270 	VtRwrite,
    271 	VtTsync		= 16,
    272 	VtRsync,
    273 
    274 	VtTmax
    275 };
    276 
    277 struct VtFcall
    278 {
    279 	uchar	msgtype;
    280 	uchar	tag;
    281 
    282 	char	*error;		/* Rerror */
    283 
    284 	char	*version;	/* Thello */
    285 	char	*uid;		/* Thello */
    286 	uchar	strength;	/* Thello */
    287 	uchar	*crypto;	/* Thello */
    288 	uint	ncrypto;	/* Thello */
    289 	uchar	*codec;		/* Thello */
    290 	uint	ncodec;		/* Thello */
    291 	char	*sid;		/* Rhello */
    292 	uchar	rcrypto;	/* Rhello */
    293 	uchar	rcodec;		/* Rhello */
    294 	uchar	*auth;		/* TauthX, RauthX */
    295 	uint	nauth;		/* TauthX, RauthX */
    296 	uchar	score[VtScoreSize];	/* Tread, Rwrite */
    297 	uchar	blocktype;	/* Tread, Twrite */
    298 	uint	count;		/* Tread */
    299 	Packet	*data;		/* Rread, Twrite */
    300 };
    301 
    302 Packet*	vtfcallpack(VtFcall*);
    303 int	vtfcallunpack(VtFcall*, Packet*);
    304 void	vtfcallclear(VtFcall*);
    305 int	vtfcallfmt(Fmt*);
    306 
    307 enum
    308 {
    309 	VtStateAlloc,
    310 	VtStateConnected,
    311 	VtStateClosed
    312 };
    313 
    314 struct VtConn
    315 {
    316 	QLock	lk;
    317 	QLock	inlk;
    318 	QLock	outlk;
    319 	int	debug;
    320 	int	infd;
    321 	int	outfd;
    322 	int	muxer;
    323 	void	*writeq;
    324 	void	*readq;
    325 	int	state;
    326 	void	*wait[256];
    327 	uint	ntag;
    328 	uint	nsleep;
    329 	Packet	*part;
    330 	Rendez	tagrend;
    331 	Rendez	rpcfork;
    332 	char	*version;
    333 	char	*uid;
    334 	char	*sid;
    335 	char	addr[256];	/* address of other side */
    336 };
    337 
    338 VtConn*	vtconn(int infd, int outfd);
    339 int	vtreconn(VtConn*, int, int);
    340 VtConn*	vtdial(char*);
    341 int	vtredial(VtConn*, char *);
    342 void	vtfreeconn(VtConn*);
    343 int	vtsend(VtConn*, Packet*);
    344 Packet*	vtrecv(VtConn*);
    345 int	vtversion(VtConn* z);
    346 void	vtdebug(VtConn* z, char*, ...);
    347 void	vthangup(VtConn* z);
    348 int	vtgoodbye(VtConn* z);
    349 
    350 /* #pragma varargck argpos vtdebug 2 */
    351 
    352 /* server */
    353 typedef struct VtSrv 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 
    399 struct VtBlock
    400 {
    401 	VtCache	*c;
    402 	QLock	lk;
    403 
    404 	uchar	*data;
    405 	uchar	score[VtScoreSize];
    406 	uchar	type;	/* VtXXX */
    407 	ulong	size;
    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*, ulong maxmem);
    426 void	vtcachefree(VtCache*);
    427 VtBlock*vtcachelocal(VtCache*, u32int addr, int type);
    428 VtBlock*vtcacheglobal(VtCache*, uchar[VtScoreSize], int type, ulong size);
    429 VtBlock*vtcacheallocblock(VtCache*, int type, ulong size);
    430 void	vtcachesetwrite(VtCache*,
    431 	int(*)(VtConn*, uchar[VtScoreSize], uint, uchar*, int));
    432 void	vtblockput(VtBlock*);
    433 int	vtblockwrite(VtBlock*);
    434 VtBlock*vtblockcopy(VtBlock*);
    435 void	vtblockduplock(VtBlock*);
    436 
    437 extern int vtcachencopy, vtcachenread, vtcachenwrite;
    438 extern int vttracelevel;
    439 
    440 /*
    441  * Hash tree file tree.
    442  */
    443 typedef struct VtFile VtFile;
    444 struct VtFile
    445 {
    446 	QLock	lk;
    447 	int	ref;
    448 	int	local;
    449 	VtBlock	*b;			/* block containing this file */
    450 	uchar	score[VtScoreSize];	/* score of block containing this file */
    451 	int	bsize;			/* size of block */
    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 
    505 #ifdef __cplusplus
    506 }
    507 #endif
    508 #endif