venti.h 9.7 KB

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