1
0

venti.h 10 KB

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