venti.h 9.6 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*, u8 *buf, int n);
  20. u32 packetasize(Packet*);
  21. int packetcmp(Packet*, Packet*);
  22. int packetcompact(Packet*);
  23. void packetconcat(Packet*, Packet*);
  24. int packetconsume(Packet*, u8 *buf, int n);
  25. int packetcopy(Packet*, u8 *buf, int offset, int n);
  26. Packet* packetdup(Packet*, int offset, int n);
  27. Packet* packetforeign(u8 *buf, int n, void (*free)(void *a),
  28. void *a);
  29. int packetfragments(Packet*, IOchunk*, int nio, int offset);
  30. void packetfree(Packet*);
  31. u8 * packetheader(Packet*, int n);
  32. u8 * packetpeek(Packet*, u8 *buf, int offset, int n);
  33. void packetprefix(Packet*, u8 *buf, int n);
  34. void packetsha1(Packet*, u8 sha1[20]);
  35. u32 packetsize(Packet*);
  36. Packet* packetsplit(Packet*, int n);
  37. void packetstats(void);
  38. u8 * 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. u32 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, u32 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. u32 vttodisktype(u32);
  111. u32 vtfromdisktype(u32);
  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. u32 gen; /* generation number */
  135. u16 psize; /* pointer block size */
  136. u16 dsize; /* data block size */
  137. u8 type;
  138. u8 flags;
  139. u64 size;
  140. u8 score[VtScoreSize];
  141. };
  142. void vtentrypack(VtEntry*, u8*, int index);
  143. int vtentryunpack(VtEntry*, u8*, int index);
  144. struct VtRoot
  145. {
  146. char name[128];
  147. char type[128];
  148. u8 score[VtScoreSize]; /* to a Dir block */
  149. u16 blocksize; /* maximum block size */
  150. u8 prev[VtScoreSize]; /* last root block */
  151. };
  152. enum
  153. {
  154. VtRootSize = 300,
  155. VtRootVersion = 2
  156. };
  157. void vtrootpack(VtRoot*, u8*);
  158. int vtrootunpack(VtRoot*, u8*);
  159. /*
  160. * score of zero length block
  161. */
  162. extern u8 vtzeroscore[VtScoreSize];
  163. /*
  164. * zero extend and truncate blocks
  165. */
  166. void vtzeroextend(int type, u8 *buf, u32 n, u32 nn);
  167. u32 vtzerotruncate(int type, u8 *buf, u32 n);
  168. /*
  169. * parse score: mungs s
  170. */
  171. int vtparsescore(char *s, char **prefix, u8[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. u8 msgtype;
  246. u8 tag;
  247. char *error; /* Rerror */
  248. char *version; /* Thello */
  249. char *uid; /* Thello */
  250. u8 strength; /* Thello */
  251. u8 *crypto; /* Thello */
  252. u32 ncrypto; /* Thello */
  253. u8 *codec; /* Thello */
  254. u32 ncodec; /* Thello */
  255. char *sid; /* Rhello */
  256. u8 rcrypto; /* Rhello */
  257. u8 rcodec; /* Rhello */
  258. u8 *auth; /* TauthX, RauthX */
  259. u32 nauth; /* TauthX, RauthX */
  260. u8 score[VtScoreSize]; /* Tread, Rwrite */
  261. u8 blocktype; /* Tread, Twrite */
  262. u16 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. u32 ntag;
  289. u32 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*, u8 score[VtScoreSize], u32 type,
  331. u8 *buf, int n);
  332. int vtwrite(VtConn*, u8 score[VtScoreSize], u32 type,
  333. u8 *buf, int n);
  334. Packet* vtreadpacket(VtConn*, u8 score[VtScoreSize], u32 type,
  335. int n);
  336. int vtwritepacket(VtConn*, u8 score[VtScoreSize], u32 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. u8 *data;
  354. u8 score[VtScoreSize];
  355. u8 type; /* BtXXX */
  356. /* internal to cache */
  357. int nlock;
  358. int iostate;
  359. int ref;
  360. u32 heap;
  361. VtBlock *next;
  362. VtBlock **prev;
  363. u32 used;
  364. u32 used2;
  365. u32 addr;
  366. usize pc;
  367. };
  368. u32 vtglobaltolocal(u8[VtScoreSize]);
  369. void vtlocaltoglobal(u32, u8[VtScoreSize]);
  370. VtCache*vtcachealloc(VtConn*, int blocksize, u32 nblocks);
  371. void vtcachefree(VtCache*);
  372. VtBlock*vtcachelocal(VtCache*, u32 addr, int type);
  373. VtBlock*vtcacheglobal(VtCache*, u8[VtScoreSize], int type);
  374. VtBlock*vtcacheallocblock(VtCache*, int type);
  375. void vtcachesetwrite(VtCache*,
  376. int(*)(VtConn*, u8[VtScoreSize], u32, u8*, int));
  377. void vtblockput(VtBlock*);
  378. u32 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. u8 score[VtScoreSize]; /* score of block containing this file */
  395. /* immutable */
  396. VtCache *c;
  397. int mode;
  398. u32 gen;
  399. int dsize;
  400. int psize;
  401. int dir;
  402. VtFile *parent;
  403. int epb; /* entries per block in parent */
  404. u32 offset; /* entry offset in parent */
  405. };
  406. enum
  407. {
  408. VtOREAD,
  409. VtOWRITE,
  410. VtORDWR
  411. };
  412. VtBlock*vtfileblock(VtFile*, u32, int mode);
  413. int vtfileblockscore(VtFile*, u32, u8[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*, u64);
  420. u32 vtfilegetdirsize(VtFile*);
  421. int vtfilegetentry(VtFile*, VtEntry*);
  422. u64 vtfilegetsize(VtFile*);
  423. void vtfileincref(VtFile*);
  424. int vtfilelock2(VtFile*, VtFile*, int);
  425. int vtfilelock(VtFile*, int);
  426. VtFile* vtfileopen(VtFile*, u32, int);
  427. VtFile* vtfileopenroot(VtCache*, VtEntry*);
  428. i32 vtfileread(VtFile*, void*, i32, i64);
  429. int vtfileremove(VtFile*);
  430. int vtfilesetdirsize(VtFile*, u32);
  431. int vtfilesetentry(VtFile*, VtEntry*);
  432. int vtfilesetsize(VtFile*, u64);
  433. int vtfiletruncate(VtFile*);
  434. void vtfileunlock(VtFile*);
  435. i32 vtfilewrite(VtFile*, void*, i32, i64);
  436. int vttimefmt(Fmt*);
  437. extern int chattyventi;
  438. extern int ventidoublechecksha1;
  439. extern int ventilogging;
  440. extern char *VtServerLog;