dat.h 7.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315
  1. typedef struct Arch Arch;
  2. typedef struct BList BList;
  3. typedef struct Block Block;
  4. typedef struct Cache Cache;
  5. typedef struct Disk Disk;
  6. typedef struct Entry Entry;
  7. typedef struct Fsck Fsck;
  8. typedef struct Header Header;
  9. typedef struct Label Label;
  10. typedef struct Periodic Periodic;
  11. typedef struct Snap Snap;
  12. typedef struct Source Source;
  13. typedef struct Super Super;
  14. typedef struct WalkPtr WalkPtr;
  15. #pragma incomplete Arch
  16. #pragma incomplete BList
  17. #pragma incomplete Cache
  18. #pragma incomplete Disk
  19. #pragma incomplete Periodic
  20. #pragma incomplete Snap
  21. /* tuneable parameters - probably should not be constants */
  22. enum {
  23. BytesPerEntry = 100, /* estimate of bytes per dir entries - determines number of index entries in the block */
  24. FullPercentage = 80, /* don't allocate in block if more than this percentage full */
  25. FlushSize = 200, /* number of blocks to flush */
  26. DirtyPercentage = 50, /* maximum percentage of dirty blocks */
  27. };
  28. enum {
  29. NilBlock = (~0UL),
  30. MaxBlock = (1UL<<31),
  31. };
  32. enum {
  33. HeaderMagic = 0x3776ae89,
  34. HeaderVersion = 1,
  35. HeaderOffset = 128*1024,
  36. HeaderSize = 512,
  37. SuperMagic = 0x2340a3b1,
  38. SuperSize = 512,
  39. SuperVersion = 1,
  40. LabelSize = 14,
  41. };
  42. /* well known tags */
  43. enum {
  44. BadTag = 0, /* this tag should not be used */
  45. RootTag = 1, /* root of fs */
  46. EnumTag, /* root of a dir listing */
  47. UserTag = 32, /* all other tags should be >= UserTag */
  48. };
  49. struct Super {
  50. u16int version;
  51. u32int epochLow;
  52. u32int epochHigh;
  53. u64int qid; /* next qid */
  54. u32int active; /* root of active file system */
  55. u32int next; /* root of next snapshot to archive */
  56. u32int current; /* root of snapshot currently archiving */
  57. uchar last[VtScoreSize]; /* last snapshot successfully archived */
  58. char name[128]; /* label */
  59. };
  60. struct Fs {
  61. Arch *arch; /* immutable */
  62. Cache *cache; /* immutable */
  63. int mode; /* immutable */
  64. int blockSize; /* immutable */
  65. VtSession *z; /* immutable */
  66. Snap *snap; /* immutable */
  67. Periodic *metaFlush; /* periodically flushes meta data cached in files */
  68. /*
  69. * epoch lock.
  70. * Most operations on the fs require a read lock of elk, ensuring that
  71. * the current high and low epochs do not change under foot.
  72. * This lock is mostly acquired via a call to fileLock or fileRlock.
  73. * Deletion and creation of snapshots occurs under a write lock of elk,
  74. * ensuring no file operations are occurring concurrently.
  75. */
  76. VtLock *elk; /* epoch lock */
  77. u32int ehi; /* epoch high */
  78. u32int elo; /* epoch low */
  79. int halted; /* epoch lock is held to halt (console initiated) */
  80. Source *source; /* immutable: root of sources */
  81. File *file; /* immutable: root of files */
  82. };
  83. /*
  84. * variant on VtEntry
  85. * there are extra fields when stored locally
  86. */
  87. struct Entry {
  88. u32int gen; /* generation number */
  89. ushort psize; /* pointer block size */
  90. ushort dsize; /* data block size */
  91. uchar depth; /* unpacked from flags */
  92. uchar flags;
  93. uvlong size;
  94. uchar score[VtScoreSize];
  95. u32int tag; /* tag for local blocks: zero if stored on Venti */
  96. u32int snap; /* non zero -> entering snapshot of given epoch */
  97. uchar archive; /* archive this snapshot: only valid for snap != 0 */
  98. };
  99. struct Source {
  100. Fs *fs; /* immutable */
  101. int mode; /* immutable */
  102. u32int gen; /* immutable */
  103. int dsize; /* immutable */
  104. int dir; /* immutable */
  105. Source *parent; /* immutable */
  106. VtLock *lk;
  107. int ref;
  108. /*
  109. * epoch for the source
  110. * for ReadWrite sources, epoch is used to lazily notice
  111. * sources that must be split from the snapshots.
  112. * for ReadOnly sources, the epoch represents the minimum epoch
  113. * along the chain from the root, and is used to lazily notice
  114. * sources that have become invalid because they belong to an old
  115. * snapshot.
  116. */
  117. u32int epoch;
  118. Block *b; /* block containing this source */
  119. uchar score[VtScoreSize]; /* score of block containing this source */
  120. u32int scoreEpoch; /* epoch of block containing this source */
  121. int epb; /* immutable: entries per block in parent */
  122. u32int tag; /* immutable: tag of parent */
  123. u32int offset; /* immutable: entry offset in parent */
  124. };
  125. struct Header {
  126. ushort version;
  127. ushort blockSize;
  128. ulong super; /* super blocks */
  129. ulong label; /* start of labels */
  130. ulong data; /* end of labels - start of data blocks */
  131. ulong end; /* end of data blocks */
  132. };
  133. /*
  134. * contains a one block buffer
  135. * to avoid problems of the block changing underfoot
  136. * and to enable an interface that supports unget.
  137. */
  138. struct DirEntryEnum {
  139. File *file;
  140. u32int boff; /* block offset */
  141. int i, n;
  142. DirEntry *buf;
  143. };
  144. /* Block states */
  145. enum {
  146. BsFree = 0, /* available for allocation */
  147. BsBad = 0xFF, /* something is wrong with this block */
  148. /* bit fields */
  149. BsAlloc = 1<<0, /* block is in use */
  150. BsCopied = 1<<1, /* block has been copied (usually in preparation for unlink) */
  151. BsVenti = 1<<2, /* block has been stored on Venti */
  152. BsClosed = 1<<3, /* block has been unlinked on disk from active file system */
  153. BsMask = BsAlloc|BsCopied|BsVenti|BsClosed,
  154. };
  155. /*
  156. * block types
  157. * more regular than Venti block types
  158. * bit 3 -> block or data block
  159. * bits 2-0 -> level of block
  160. */
  161. enum {
  162. BtData,
  163. BtDir = 1<<3,
  164. BtLevelMask = 7,
  165. BtMax = 1<<4,
  166. };
  167. /* io states */
  168. enum {
  169. BioEmpty, /* label & data are not valid */
  170. BioLabel, /* label is good */
  171. BioClean, /* data is on the disk */
  172. BioDirty, /* data is not yet on the disk */
  173. BioReading, /* in process of reading data */
  174. BioWriting, /* in process of writing data */
  175. BioReadError, /* error reading: assume disk always handles write errors */
  176. BioVentiError, /* error reading from venti (probably disconnected) */
  177. BioMax
  178. };
  179. struct Label {
  180. uchar type;
  181. uchar state;
  182. u32int tag;
  183. u32int epoch;
  184. u32int epochClose;
  185. };
  186. struct Block {
  187. Cache *c;
  188. int ref;
  189. int nlock;
  190. uintptr pc; /* pc that fetched this block from the cache */
  191. VtLock *lk;
  192. int part;
  193. u32int addr;
  194. uchar score[VtScoreSize]; /* score */
  195. Label l;
  196. uchar *dmap;
  197. uchar *data;
  198. /* the following is private; used by cache */
  199. Block *next; /* doubly linked hash chains */
  200. Block **prev;
  201. u32int heap; /* index in heap table */
  202. u32int used; /* last reference times */
  203. u32int vers; /* version of dirty flag */
  204. BList *uhead; /* blocks to unlink when this block is written */
  205. BList *utail;
  206. /* block ordering for cache -> disk */
  207. BList *prior; /* list of blocks before this one */
  208. Block *ionext;
  209. int iostate;
  210. VtRendez *ioready;
  211. };
  212. /* tree walker, for gc and archiver */
  213. struct WalkPtr
  214. {
  215. uchar *data;
  216. int isEntry;
  217. int n;
  218. int m;
  219. Entry e;
  220. uchar type;
  221. u32int tag;
  222. };
  223. enum
  224. {
  225. DoClose = 1<<0,
  226. DoClre = 1<<1,
  227. DoClri = 1<<2,
  228. DoClrp = 1<<3,
  229. };
  230. struct Fsck
  231. {
  232. /* filled in by caller */
  233. int printblocks;
  234. int useventi;
  235. int flags;
  236. int printdirs;
  237. int printfiles;
  238. int walksnapshots;
  239. int walkfs;
  240. Fs *fs;
  241. int (*print)(char*, ...);
  242. void (*clre)(Fsck*, Block*, int);
  243. void (*clrp)(Fsck*, Block*, int);
  244. void (*close)(Fsck*, Block*, u32int);
  245. void (*clri)(Fsck*, char*, MetaBlock*, int, Block*);
  246. /* used internally */
  247. Cache *cache;
  248. uchar *amap; /* all blocks seen so far */
  249. uchar *emap; /* all blocks seen in this epoch */
  250. uchar *xmap; /* all blocks in this epoch with parents in this epoch */
  251. uchar *errmap; /* blocks with errors */
  252. uchar *smap; /* walked sources */
  253. int nblocks;
  254. int bsize;
  255. int walkdepth;
  256. u32int hint; /* where the next root probably is */
  257. int nseen;
  258. int quantum;
  259. int nclre;
  260. int nclrp;
  261. int nclose;
  262. int nclri;
  263. };
  264. /* disk partitions; keep in sync with partname[] in disk.c */
  265. enum {
  266. PartError,
  267. PartSuper,
  268. PartLabel,
  269. PartData,
  270. PartVenti, /* fake partition */
  271. };
  272. extern vtType[BtMax];