venti.h 10 KB

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