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 }; 139 enum 140 { 141 VtEntrySize = 40 142 }; 143 struct VtEntry 144 { 145 ulong gen; /* generation number */ 146 ushort psize; /* pointer block size */ 147 ushort dsize; /* data block size */ 148 uchar type; 149 uchar flags; 150 uvlong size; 151 uchar score[VtScoreSize]; 152 }; 153 154 void vtentrypack(VtEntry*, uchar*, int index); 155 int vtentryunpack(VtEntry*, uchar*, int index); 156 157 struct VtRoot 158 { 159 char name[128]; 160 char type[128]; 161 uchar score[VtScoreSize]; /* to a Dir block */ 162 ushort blocksize; /* maximum block size */ 163 uchar prev[VtScoreSize]; /* last root block */ 164 }; 165 166 enum 167 { 168 VtRootSize = 300, 169 VtRootVersion = 2 170 }; 171 172 void vtrootpack(VtRoot*, uchar*); 173 int vtrootunpack(VtRoot*, uchar*); 174 175 /* 176 * score of zero length block 177 */ 178 extern uchar vtzeroscore[VtScoreSize]; 179 180 /* 181 * zero extend and truncate blocks 182 */ 183 void vtzeroextend(int type, uchar *buf, uint n, uint nn); 184 uint vtzerotruncate(int type, uchar *buf, uint n); 185 186 /* 187 * parse score: mungs s 188 */ 189 int vtparsescore(char *s, char **prefix, uchar[VtScoreSize]); 190 191 /* 192 * formatting 193 * other than noted, these formats all ignore 194 * the width and precision arguments, and all flags 195 * 196 * V a venti score 197 */ 198 #pragma varargck type "V" uchar* 199 #pragma varargck type "F" VtFcall* 200 #pragma varargck type "T" void 201 #pragma varargck type "lT" void 202 203 int vtscorefmt(Fmt*); 204 205 /* 206 * error-checking malloc et al. 207 */ 208 void vtfree(void *); 209 void* vtmalloc(int); 210 void* vtmallocz(int); 211 void* vtrealloc(void *p, int); 212 void* vtbrk(int n); 213 char* vtstrdup(char *); 214 215 /* 216 * Venti protocol 217 */ 218 219 /* 220 * Crypto strengths 221 */ 222 enum 223 { 224 VtCryptoStrengthNone, 225 VtCryptoStrengthAuth, 226 VtCryptoStrengthWeak, 227 VtCryptoStrengthStrong 228 }; 229 230 /* 231 * Crypto suites 232 */ 233 enum 234 { 235 VtCryptoNone, 236 VtCryptoSSL3, 237 VtCryptoTLS1, 238 VtCryptoMax 239 }; 240 241 /* 242 * Codecs 243 */ 244 enum 245 { 246 VtCodecNone, 247 VtCodecDeflate, 248 VtCodecThwack, 249 VtCodecMax 250 }; 251 252 enum 253 { 254 VtRerror = 1, 255 VtTping = 2, 256 VtRping, 257 VtThello = 4, 258 VtRhello, 259 VtTgoodbye = 6, 260 VtRgoodbye, /* not used */ 261 VtTauth0 = 8, 262 VtRauth0, 263 VtTauth1 = 10, 264 VtRauth1, 265 VtTread = 12, 266 VtRread, 267 VtTwrite = 14, 268 VtRwrite, 269 VtTsync = 16, 270 VtRsync, 271 272 VtTmax 273 }; 274 275 struct VtFcall 276 { 277 uchar msgtype; 278 uchar tag; 279 280 char *error; /* Rerror */ 281 282 char *version; /* Thello */ 283 char *uid; /* Thello */ 284 uchar strength; /* Thello */ 285 uchar *crypto; /* Thello */ 286 uint ncrypto; /* Thello */ 287 uchar *codec; /* Thello */ 288 uint ncodec; /* Thello */ 289 char *sid; /* Rhello */ 290 uchar rcrypto; /* Rhello */ 291 uchar rcodec; /* Rhello */ 292 uchar *auth; /* TauthX, RauthX */ 293 uint nauth; /* TauthX, RauthX */ 294 uchar score[VtScoreSize]; /* Tread, Rwrite */ 295 uchar blocktype; /* Tread, Twrite */ 296 ushort count; /* Tread */ 297 Packet *data; /* Rread, Twrite */ 298 }; 299 300 Packet* vtfcallpack(VtFcall*); 301 int vtfcallunpack(VtFcall*, Packet*); 302 void vtfcallclear(VtFcall*); 303 int vtfcallfmt(Fmt*); 304 305 enum 306 { 307 VtStateAlloc, 308 VtStateConnected, 309 VtStateClosed 310 }; 311 312 struct VtConn 313 { 314 QLock lk; 315 QLock inlk; 316 QLock outlk; 317 int debug; 318 int infd; 319 int outfd; 320 int muxer; 321 void *writeq; 322 void *readq; 323 int state; 324 void *wait[256]; 325 uint ntag; 326 uint nsleep; 327 Packet *part; 328 Rendez tagrend; 329 Rendez rpcfork; 330 char *version; 331 char *uid; 332 char *sid; 333 char addr[256]; /* address of other side */ 334 }; 335 336 VtConn* vtconn(int infd, int outfd); 337 VtConn* vtdial(char*); 338 void vtfreeconn(VtConn*); 339 int vtsend(VtConn*, Packet*); 340 Packet* vtrecv(VtConn*); 341 int vtversion(VtConn* z); 342 void vtdebug(VtConn* z, char*, ...); 343 void vthangup(VtConn* z); 344 int vtgoodbye(VtConn* z); 345 346 /* #pragma varargck argpos vtdebug 2 */ 347 348 /* server */ 349 typedef struct VtSrv VtSrv; 350 #pragma incomplete VtSrv 351 typedef struct VtReq VtReq; 352 struct VtReq 353 { 354 VtFcall tx; 355 VtFcall rx; 356 /* private */ 357 VtSrv *srv; 358 void *sc; 359 }; 360 361 int vtsrvhello(VtConn*); 362 VtSrv* vtlisten(char *addr); 363 VtReq* vtgetreq(VtSrv*); 364 void vtrespond(VtReq*); 365 366 /* client */ 367 Packet* vtrpc(VtConn*, Packet*); 368 Packet* _vtrpc(VtConn*, Packet*, VtFcall*); 369 void vtrecvproc(void*); /* VtConn */ 370 void vtsendproc(void*); /* VtConn */ 371 372 int vtconnect(VtConn*); 373 int vthello(VtConn*); 374 int vtread(VtConn*, uchar score[VtScoreSize], uint type, uchar *buf, int n); 375 int vtwrite(VtConn*, uchar score[VtScoreSize], uint type, uchar *buf, int n); 376 Packet* vtreadpacket(VtConn*, uchar score[VtScoreSize], uint type, int n); 377 int vtwritepacket(VtConn*, uchar score[VtScoreSize], uint type, Packet *p); 378 int vtsync(VtConn*); 379 int vtping(VtConn*); 380 381 /* 382 * Data blocks and block cache. 383 */ 384 enum 385 { 386 NilBlock = ~0 387 }; 388 389 typedef struct VtBlock VtBlock; 390 typedef struct VtCache VtCache; 391 #pragma incomplete VtCache 392 393 struct VtBlock 394 { 395 VtCache *c; 396 QLock lk; 397 398 uchar *data; 399 uchar score[VtScoreSize]; 400 uchar type; /* BtXXX */ 401 402 /* internal to cache */ 403 int nlock; 404 int iostate; 405 int ref; 406 u32int heap; 407 VtBlock *next; 408 VtBlock **prev; 409 u32int used; 410 u32int used2; 411 u32int addr; 412 uintptr pc; 413 }; 414 415 u32int vtglobaltolocal(uchar[VtScoreSize]); 416 void vtlocaltoglobal(u32int, uchar[VtScoreSize]); 417 418 VtCache*vtcachealloc(VtConn*, int blocksize, ulong nblocks); 419 void vtcachefree(VtCache*); 420 VtBlock*vtcachelocal(VtCache*, u32int addr, int type); 421 VtBlock*vtcacheglobal(VtCache*, uchar[VtScoreSize], int type); 422 VtBlock*vtcacheallocblock(VtCache*, int type); 423 void vtcachesetwrite(VtCache*, 424 int(*)(VtConn*, uchar[VtScoreSize], uint, uchar*, int)); 425 void vtblockput(VtBlock*); 426 u32int vtcacheblocksize(VtCache*); 427 int vtblockwrite(VtBlock*); 428 VtBlock*vtblockcopy(VtBlock*); 429 void vtblockduplock(VtBlock*); 430 431 extern int vtcachencopy, vtcachenread, vtcachenwrite; 432 extern int vttracelevel; 433 434 /* 435 * Hash tree file tree. 436 */ 437 typedef struct VtFile VtFile; 438 struct VtFile 439 { 440 QLock lk; 441 int ref; 442 int local; 443 VtBlock *b; /* block containing this file */ 444 uchar score[VtScoreSize]; /* score of block containing this file */ 445 446 /* immutable */ 447 VtCache *c; 448 int mode; 449 u32int gen; 450 int dsize; 451 int psize; 452 int dir; 453 VtFile *parent; 454 int epb; /* entries per block in parent */ 455 u32int offset; /* entry offset in parent */ 456 }; 457 458 enum 459 { 460 VtOREAD, 461 VtOWRITE, 462 VtORDWR 463 }; 464 465 VtBlock*vtfileblock(VtFile*, u32int, int mode); 466 int vtfileblockscore(VtFile*, u32int, uchar[VtScoreSize]); 467 void vtfileclose(VtFile*); 468 VtFile* _vtfilecreate(VtFile*, int offset, int psize, int dsize, int dir); 469 VtFile* vtfilecreate(VtFile*, int psize, int dsize, int dir); 470 VtFile* vtfilecreateroot(VtCache*, int psize, int dsize, int type); 471 int vtfileflush(VtFile*); 472 int vtfileflushbefore(VtFile*, u64int); 473 u32int vtfilegetdirsize(VtFile*); 474 int vtfilegetentry(VtFile*, VtEntry*); 475 uvlong vtfilegetsize(VtFile*); 476 void vtfileincref(VtFile*); 477 int vtfilelock2(VtFile*, VtFile*, int); 478 int vtfilelock(VtFile*, int); 479 VtFile* vtfileopen(VtFile*, u32int, int); 480 VtFile* vtfileopenroot(VtCache*, VtEntry*); 481 long vtfileread(VtFile*, void*, long, vlong); 482 int vtfileremove(VtFile*); 483 int vtfilesetdirsize(VtFile*, u32int); 484 int vtfilesetentry(VtFile*, VtEntry*); 485 int vtfilesetsize(VtFile*, u64int); 486 int vtfiletruncate(VtFile*); 487 void vtfileunlock(VtFile*); 488 long vtfilewrite(VtFile*, void*, long, vlong); 489 490 int vttimefmt(Fmt*); 491 492 extern int chattyventi; 493 extern int ventidoublechecksha1; 494 extern int ventilogging; 495 496 extern char *VtServerLog; 497