dat.h 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497
  1. typedef struct Config Config;
  2. typedef struct AMap AMap;
  3. typedef struct AMapN AMapN;
  4. typedef struct Arena Arena;
  5. typedef struct ArenaHead ArenaHead;
  6. typedef struct ArenaPart ArenaPart;
  7. typedef struct CIBlock CIBlock;
  8. typedef struct Clump Clump;
  9. typedef struct ClumpInfo ClumpInfo;
  10. typedef struct IAddr IAddr;
  11. typedef struct IBucket IBucket;
  12. typedef struct ICache ICache;
  13. typedef struct IEStream IEStream;
  14. typedef struct IEntry IEntry;
  15. typedef struct IFile IFile;
  16. typedef struct ISect ISect;
  17. typedef struct Index Index;
  18. typedef struct Lump Lump;
  19. typedef struct DBlock DBlock;
  20. typedef struct Part Part;
  21. typedef struct Stats Stats;
  22. typedef struct ZBlock ZBlock;
  23. #define TWID32 ((u32int)~(u32int)0)
  24. #define TWID64 ((u64int)~(u64int)0)
  25. #define TWID8 ((u8int)~(u8int)0)
  26. enum
  27. {
  28. ABlockLog = 9, /* log2(512), the quantum for reading arenas */
  29. ANameSize = 64,
  30. MaxDiskBlock = 64*1024, /* max. allowed size for a disk block */
  31. MaxIoSize = 64*1024, /* max. allowed size for a disk io operation */
  32. PartBlank = 256*1024, /* untouched section at beginning of partition */
  33. HeadSize = 512, /* size of a header after PartBlank */
  34. MinArenaSize = 1*1024*1024, /* smallest reasonable arena size */
  35. IndexBase = 1024*1024, /* initial address to use in an index */
  36. MaxIo = 64*1024, /* max size of a single read or write operation */
  37. ICacheBits = 16, /* default bits for indexing icache */
  38. ICacheDepth = 4, /* default depth of an icache hash chain */
  39. MaxAMap = 2*1024, /* max. allowed arenas in an address mapping; must be < 32*1024 */
  40. /*
  41. * return codes from syncArena
  42. */
  43. SyncDataErr = 1 << 0, /* problem reading the clump data */
  44. SyncCIErr = 1 << 1, /* found erroneous clump directory entries */
  45. SyncCIZero = 1 << 2, /* found unwritten clump directory entries */
  46. SyncFixErr = 1 << 3, /* error writing fixed data */
  47. SyncHeader = 1 << 4, /* altered header fields */
  48. /*
  49. * error severity
  50. */
  51. EOk = 0, /* error expected in normal operation */
  52. EStrange, /* strange error that should be logged */
  53. ECorrupt, /* corrupted data found in arenas */
  54. EICorrupt, /* corrupted data found in index */
  55. EAdmin, /* should be brought to administrators' attention */
  56. ECrash, /* really bad internal error */
  57. EBug, /* a limitation which should be fixed */
  58. EInconsist, /* inconsistencies between index and arena */
  59. EMax,
  60. /*
  61. * internal disk formats for the venti archival storage system
  62. */
  63. /*
  64. * magic numbers on disk
  65. */
  66. ClumpMagic = 0xd15cb10c, /* clump header */
  67. ClumpFreeMagic = 0, /* free clump; terminates active clump log */
  68. ArenaPartMagic = 0xa9e4a5e7, /* arena partition header */
  69. ArenaMagic = 0xf2a14ead, /* arena trailer */
  70. ArenaHeadMagic = 0xd15c4ead, /* arena header */
  71. ISectMagic = 0xd15c5ec7, /* index header */
  72. ArenaPartVersion = 3,
  73. ArenaVersion = 4,
  74. IndexVersion = 1,
  75. ISectVersion = 1,
  76. /*
  77. * encodings of clumps on disk
  78. */
  79. ClumpEErr = 0, /* can't happen */
  80. ClumpENone, /* plain */
  81. ClumpECompress, /* compressed */
  82. ClumpEMax,
  83. /*
  84. * marker for corrupted data on disk
  85. */
  86. VtTypeCorrupt = VtMaxType,
  87. /*
  88. * sizes in bytes on disk
  89. */
  90. U8Size = 1,
  91. U16Size = 2,
  92. U32Size = 4,
  93. U64Size = 8,
  94. ArenaPartSize = 4 * U32Size,
  95. ArenaSize = 2 * U64Size + 6 * U32Size + ANameSize + U8Size,
  96. ArenaHeadSize = U64Size + 3 * U32Size + ANameSize,
  97. ISectSize = 7 * U32Size + 2 * ANameSize,
  98. ClumpInfoSize = U8Size + 2 * U16Size + VtScoreSize,
  99. ClumpSize = ClumpInfoSize + U8Size + 3 * U32Size,
  100. IBucketSize = U32Size + U16Size,
  101. IEntrySize = U64Size + U32Size + 2*U16Size + 2*U8Size + VtScoreSize,
  102. IEntryTypeOff = VtScoreSize + U64Size + U32Size + 2 * U16Size,
  103. MaxClumpBlocks = (VtMaxLumpSize + ClumpSize + (1 << ABlockLog) - 1) >> ABlockLog,
  104. VentiZZZZZZZZ
  105. };
  106. /*
  107. * results of parsing and initializing a config file
  108. */
  109. struct Config
  110. {
  111. char *index; /* name of the index to initialize */
  112. int naparts; /* arena partitions initialized */
  113. ArenaPart **aparts;
  114. int nsects; /* index sections initialized */
  115. ISect **sects;
  116. };
  117. /*
  118. * a Part is the low level interface to files or disks.
  119. * there are two main types of partitions
  120. * arena paritions, which some number of arenas, each in a sub-partition.
  121. * index partition, which only have one subpartition.
  122. */
  123. struct Part
  124. {
  125. int fd; /* rock for accessing the disk */
  126. u64int size; /* size of the partiton */
  127. u32int blockSize; /* block size for reads and writes */
  128. char *name;
  129. };
  130. /*
  131. * a cached block from the partition
  132. * yuck -- most of this is internal structure for the cache
  133. * all other routines should only use data
  134. */
  135. struct DBlock
  136. {
  137. u8int *data;
  138. Part *part; /* partition in which cached */
  139. u64int addr; /* base address on the partition */
  140. u16int size; /* amount of data available, not amount allocated; should go away */
  141. DBlock *next; /* doubly linked hash chains */
  142. DBlock *prev;
  143. u32int heap; /* index in heap table */
  144. u32int used; /* last reference times */
  145. u32int used2;
  146. u32int ref; /* reference count */
  147. VtLock *lock; /* for access to data only */
  148. };
  149. /*
  150. * a cached block from the partition
  151. * yuck -- most of this is internal structure for the cache
  152. * all other routines should only use data
  153. * double yuck -- this is mostly the same as a DBlock
  154. */
  155. struct Lump
  156. {
  157. Packet *data;
  158. Part *part; /* partition in which cached */
  159. u8int score[VtScoreSize]; /* score of packet */
  160. u8int type; /* type of packet */
  161. u16int size; /* amount of data allocated to hold packet */
  162. Lump *next; /* doubly linked hash chains */
  163. Lump *prev;
  164. u32int heap; /* index in heap table */
  165. u32int used; /* last reference times */
  166. u32int used2;
  167. u32int ref; /* reference count */
  168. VtLock *lock; /* for access to data only */
  169. };
  170. /*
  171. * mapping between names and address ranges
  172. */
  173. struct AMap
  174. {
  175. u64int start;
  176. u64int stop;
  177. char name[ANameSize];
  178. };
  179. /*
  180. * an AMap along with a length
  181. */
  182. struct AMapN
  183. {
  184. int n;
  185. AMap *map;
  186. };
  187. /*
  188. * an ArenaPart is a partition made up of Arenas
  189. * it exists because most os's don't support many partitions,
  190. * and we want to have many different Arenas
  191. */
  192. struct ArenaPart
  193. {
  194. Part *part;
  195. u64int size; /* size of underlying partition, rounded down to blocks */
  196. Arena **arenas;
  197. u32int tabBase; /* base address of arena table on disk */
  198. u32int tabSize; /* max. bytes in arena table */
  199. /*
  200. * fields stored on disk
  201. */
  202. u32int version;
  203. u32int blockSize; /* "optimal" block size for reads and writes */
  204. u32int arenaBase; /* base address of first arena */
  205. /*
  206. * stored in the arena mapping table on disk
  207. */
  208. AMap *map;
  209. int narenas;
  210. };
  211. /*
  212. * info about one block in the clump info cache
  213. */
  214. struct CIBlock
  215. {
  216. u32int block; /* blocks in the directory */
  217. int offset; /* offsets of one clump in the data */
  218. DBlock *data;
  219. };
  220. /*
  221. * an Arena is a log of Clumps, preceeded by an ArenaHeader,
  222. * and followed by a Arena, each in one disk block.
  223. * struct on disk is not always up to date, but should be self-consistent.
  224. * to sync after reboot, follow clumps starting at used until ClumpFreeMagic if found.
  225. * <struct name="Arena" type="Arena *">
  226. * <field name="name" val="s->name" type="AName"/>
  227. * <field name="version" val="s->version" type="U32int"/>
  228. * <field name="partition" val="s->part->name" type="AName"/>
  229. * <field name="blockSize" val="s->blockSize" type="U32int"/>
  230. * <field name="start" val="s->base" type="U64int"/>
  231. * <field name="stop" val="s->base+2*s->blockSize" type="U64int"/>
  232. * <field name="created" val="s->ctime" type="U32int"/>
  233. * <field name="modified" val="s->wtime" type="U32int"/>
  234. * <field name="sealed" val="s->sealed" type="Sealed"/>
  235. * <field name="score" val="s->score" type="Score"/>
  236. * <field name="clumps" val="s->clumps" type="U32int"/>
  237. * <field name="compressedClumps" val="s->cclumps" type="U32int"/>
  238. * <field name="data" val="s->uncsize" type="U64int"/>
  239. * <field name="compressedData" val="s->used - s->clumps * ClumpSize" type="U64int"/>
  240. * <field name="storage" val="s->used + s->clumps * ClumpInfoSize" type="U64int"/>
  241. * </struct>
  242. */
  243. struct Arena
  244. {
  245. VtLock *lock; /* lock for arena fields, writing to disk */
  246. Part *part; /* partition in which arena lives */
  247. int blockSize; /* size of block to read or write */
  248. u64int base; /* base address on disk */
  249. u64int size; /* total space in the arena */
  250. u64int limit; /* storage limit for clumps */
  251. u8int score[VtScoreSize]; /* score of the entire sealed & summed arena */
  252. int clumpMax; /* ClumpInfos per block */
  253. CIBlock cib; /* dirty clump directory block */
  254. /*
  255. * fields stored on disk
  256. */
  257. u32int version;
  258. char name[ANameSize]; /* text label */
  259. u32int clumps; /* number of allocated clumps */
  260. u32int cclumps; /* clumps which are compressed; informational only */
  261. u32int ctime; /* first time a block was written */
  262. u32int wtime; /* last time a block was written */
  263. u64int used; /* number of bytes currently used */
  264. u64int uncsize; /* total of all clumps's uncsize; informational only */
  265. u8int sealed; /* arena all filled up? */
  266. };
  267. /*
  268. * redundant storage of some fields at the beginning of each arena
  269. */
  270. struct ArenaHead
  271. {
  272. u32int version;
  273. char name[ANameSize];
  274. u32int blockSize;
  275. u64int size;
  276. };
  277. /*
  278. * most interesting meta information for a clump.
  279. * stored in each clump's header and in the Arena's directory,
  280. * stored in reverse order just prior to the arena trailer
  281. */
  282. struct ClumpInfo
  283. {
  284. u8int type;
  285. u16int size; /* size of disk data, not including header */
  286. u16int uncsize; /* size of uncompressed data */
  287. u8int score[VtScoreSize]; /* score of the uncompressed data only */
  288. };
  289. /*
  290. * header for an immutable clump of data
  291. */
  292. struct Clump
  293. {
  294. ClumpInfo info;
  295. u8int encoding;
  296. u32int creator; /* initial client which wrote the block */
  297. u32int time; /* creation at gmt seconds since 1/1/1970 */
  298. };
  299. /*
  300. * index of all clumps according to their score
  301. * this is just a wrapper to tie together the index sections
  302. * <struct name="Index" type="Index *">
  303. * <field name="name" val="s->name" type="AName"/>
  304. * <field name="version" val="s->version" type="U32int"/>
  305. * <field name="blockSize" val="s->blockSize" type="U32int"/>
  306. * <field name="tabSize" val="s->tabSize" type="U32int"/>
  307. * <field name="buckets" val="s->buckets" type="U32int"/>
  308. * <field name="buckDiv" val="s->div" type="U32int"/>
  309. * <array name="sect" val="&s->smap[i]" elems="s->nsects" type="Amap"/>
  310. * <array name="amap" val="&s->amap[i]" elems="s->narenas" type="Amap"/>
  311. * <array name="arena" val="s->arenas[i]" elems="s->narenas" type="Arena"/>
  312. * </struct>
  313. * <struct name="Amap" type="AMap *">
  314. * <field name="name" val="s->name" type="AName"/>
  315. * <field name="start" val="s->start" type="U64int"/>
  316. * <field name="stop" val="s->stop" type="U64int"/>
  317. * </struct>
  318. */
  319. struct Index
  320. {
  321. u32int div; /* divisor for mapping score to bucket */
  322. u32int buckets; /* last bucket used in disk hash table */
  323. u32int blockSize;
  324. u32int tabSize; /* max. bytes in index config */
  325. int mapAlloc; /* first arena to check when adding a lump */
  326. Arena **arenas; /* arenas in the mapping */
  327. ISect **sects; /* sections which hold the buckets */
  328. /*
  329. * fields stored in config file
  330. */
  331. u32int version;
  332. char name[ANameSize]; /* text label */
  333. int nsects;
  334. AMap *smap; /* mapping of buckets to index sections */
  335. int narenas;
  336. AMap *amap; /* mapping from index addesses to arenas */
  337. };
  338. /*
  339. * one part of the bucket storage for an index.
  340. * the index blocks are sequentially allocated
  341. * across all of the sections.
  342. */
  343. struct ISect
  344. {
  345. Part *part;
  346. int blockLog; /* log2(blockSize) */
  347. int buckMax; /* max. entries in a index bucket */
  348. u32int tabBase; /* base address of index config table on disk */
  349. u32int tabSize; /* max. bytes in index config */
  350. /*
  351. * fields stored on disk
  352. */
  353. u32int version;
  354. char name[ANameSize]; /* text label */
  355. char index[ANameSize]; /* index owning the section */
  356. u32int blockSize; /* size of hash buckets in index */
  357. u32int blockBase; /* address of start of on disk index table */
  358. u32int blocks; /* total blocks on disk; some may be unused */
  359. u32int start; /* first bucket in this section */
  360. u32int stop; /* limit of buckets in this section */
  361. };
  362. /*
  363. * externally interesting part of an IEntry
  364. */
  365. struct IAddr
  366. {
  367. u64int addr;
  368. u16int size; /* uncompressed size */
  369. u8int type; /* type of block */
  370. u8int blocks; /* arena io quanta for Clump + data */
  371. };
  372. /*
  373. * entries in the index
  374. * kept in IBuckets in the disk index table,
  375. * cached in the memory ICache.
  376. */
  377. struct IEntry
  378. {
  379. u8int score[VtScoreSize];
  380. IEntry *next; /* next in hash chain */
  381. u32int wtime; /* last write time */
  382. u16int train; /* relative train containing the most recent ref; 0 if no ref, 1 if in same car */
  383. u8int rac; /* read ahead count */
  384. IAddr ia;
  385. };
  386. /*
  387. * buckets in the on disk index table
  388. */
  389. struct IBucket
  390. {
  391. u16int n; /* number of active indices */
  392. u32int next; /* overflow bucket */
  393. u8int *data;
  394. };
  395. /*
  396. * temporary buffers used by individual threads
  397. */
  398. struct ZBlock
  399. {
  400. u32int len;
  401. u8int *data;
  402. };
  403. /*
  404. * simple input buffer for a '\0' terminated text file
  405. */
  406. struct IFile
  407. {
  408. char *name; /* name of the file */
  409. ZBlock *b; /* entire contents of file */
  410. u32int pos; /* current position in the file */
  411. };
  412. /*
  413. * statistics about the operation of the server
  414. * mainly for performance monitoring and profiling.
  415. */
  416. struct Stats
  417. {
  418. VtLock *lock;
  419. long lumpWrites; /* protocol block writes */
  420. long lumpReads; /* protocol block reads */
  421. long lumpHit; /* lump cache hit */
  422. long lumpMiss; /* lump cache miss */
  423. long clumpWrites; /* clumps to disk */
  424. vlong clumpBWrites; /* clump data bytes to disk */
  425. vlong clumpBComp; /* clump bytes compressed */
  426. long clumpReads; /* clumps from disk */
  427. vlong clumpBReads; /* clump data bytes from disk */
  428. vlong clumpBUncomp; /* clump bytes uncompressed */
  429. long ciWrites; /* clump directory to disk */
  430. long ciReads; /* clump directory from disk */
  431. long indexWrites; /* index to disk */
  432. long indexReads; /* index from disk */
  433. long indexWReads; /* for writing a new entry */
  434. long indexAReads; /* for allocating an overflow block */
  435. long diskWrites; /* total disk writes */
  436. long diskReads; /* total disk reads */
  437. vlong diskBWrites; /* total disk bytes written */
  438. vlong diskBReads; /* total disk bytes read */
  439. long pcHit; /* partition cache hit */
  440. long pcMiss; /* partition cache miss */
  441. long pcReads; /* partition cache reads from disk */
  442. vlong pcBReads; /* partition cache bytes read */
  443. long icInserts; /* stores into index cache */
  444. long icLookups; /* index cache lookups */
  445. long icHits; /* hits in the cache */
  446. long icFills; /* successful fills from index */
  447. };
  448. extern Index *mainIndex;
  449. extern u32int maxBlockSize; /* max. block size used by any partition */
  450. extern int paranoid; /* should verify hashes on disk read */
  451. extern int queueWrites; /* put all lump writes on a queue and finish later */
  452. extern int readonly; /* only allowed to read the disk data */
  453. extern Stats stats;
  454. extern u8int zeroScore[VtScoreSize];