dat.h 7.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316
  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. int issnapshot; /* immutable */
  103. u32int gen; /* immutable */
  104. int dsize; /* immutable */
  105. int dir; /* immutable */
  106. Source *parent; /* immutable */
  107. VtLock *lk;
  108. int ref;
  109. /*
  110. * epoch for the source
  111. * for ReadWrite sources, epoch is used to lazily notice
  112. * sources that must be split from the snapshots.
  113. * for ReadOnly sources, the epoch represents the minimum epoch
  114. * along the chain from the root, and is used to lazily notice
  115. * sources that have become invalid because they belong to an old
  116. * snapshot.
  117. */
  118. u32int epoch;
  119. Block *b; /* block containing this source */
  120. uchar score[VtScoreSize]; /* score of block containing this source */
  121. u32int scoreEpoch; /* epoch of block containing this source */
  122. int epb; /* immutable: entries per block in parent */
  123. u32int tag; /* immutable: tag of parent */
  124. u32int offset; /* immutable: entry offset in parent */
  125. };
  126. struct Header {
  127. ushort version;
  128. ushort blockSize;
  129. ulong super; /* super blocks */
  130. ulong label; /* start of labels */
  131. ulong data; /* end of labels - start of data blocks */
  132. ulong end; /* end of data blocks */
  133. };
  134. /*
  135. * contains a one block buffer
  136. * to avoid problems of the block changing underfoot
  137. * and to enable an interface that supports unget.
  138. */
  139. struct DirEntryEnum {
  140. File *file;
  141. u32int boff; /* block offset */
  142. int i, n;
  143. DirEntry *buf;
  144. };
  145. /* Block states */
  146. enum {
  147. BsFree = 0, /* available for allocation */
  148. BsBad = 0xFF, /* something is wrong with this block */
  149. /* bit fields */
  150. BsAlloc = 1<<0, /* block is in use */
  151. BsCopied = 1<<1, /* block has been copied (usually in preparation for unlink) */
  152. BsVenti = 1<<2, /* block has been stored on Venti */
  153. BsClosed = 1<<3, /* block has been unlinked on disk from active file system */
  154. BsMask = BsAlloc|BsCopied|BsVenti|BsClosed,
  155. };
  156. /*
  157. * block types
  158. * more regular than Venti block types
  159. * bit 3 -> block or data block
  160. * bits 2-0 -> level of block
  161. */
  162. enum {
  163. BtData,
  164. BtDir = 1<<3,
  165. BtLevelMask = 7,
  166. BtMax = 1<<4,
  167. };
  168. /* io states */
  169. enum {
  170. BioEmpty, /* label & data are not valid */
  171. BioLabel, /* label is good */
  172. BioClean, /* data is on the disk */
  173. BioDirty, /* data is not yet on the disk */
  174. BioReading, /* in process of reading data */
  175. BioWriting, /* in process of writing data */
  176. BioReadError, /* error reading: assume disk always handles write errors */
  177. BioVentiError, /* error reading from venti (probably disconnected) */
  178. BioMax
  179. };
  180. struct Label {
  181. uchar type;
  182. uchar state;
  183. u32int tag;
  184. u32int epoch;
  185. u32int epochClose;
  186. };
  187. struct Block {
  188. Cache *c;
  189. int ref;
  190. int nlock;
  191. uintptr pc; /* pc that fetched this block from the cache */
  192. VtLock *lk;
  193. int part;
  194. u32int addr;
  195. uchar score[VtScoreSize]; /* score */
  196. Label l;
  197. uchar *dmap;
  198. uchar *data;
  199. /* the following is private; used by cache */
  200. Block *next; /* doubly linked hash chains */
  201. Block **prev;
  202. u32int heap; /* index in heap table */
  203. u32int used; /* last reference times */
  204. u32int vers; /* version of dirty flag */
  205. BList *uhead; /* blocks to unlink when this block is written */
  206. BList *utail;
  207. /* block ordering for cache -> disk */
  208. BList *prior; /* list of blocks before this one */
  209. Block *ionext;
  210. int iostate;
  211. VtRendez *ioready;
  212. };
  213. /* tree walker, for gc and archiver */
  214. struct WalkPtr
  215. {
  216. uchar *data;
  217. int isEntry;
  218. int n;
  219. int m;
  220. Entry e;
  221. uchar type;
  222. u32int tag;
  223. };
  224. enum
  225. {
  226. DoClose = 1<<0,
  227. DoClre = 1<<1,
  228. DoClri = 1<<2,
  229. DoClrp = 1<<3,
  230. };
  231. struct Fsck
  232. {
  233. /* filled in by caller */
  234. int printblocks;
  235. int useventi;
  236. int flags;
  237. int printdirs;
  238. int printfiles;
  239. int walksnapshots;
  240. int walkfs;
  241. Fs *fs;
  242. int (*print)(char*, ...);
  243. void (*clre)(Fsck*, Block*, int);
  244. void (*clrp)(Fsck*, Block*, int);
  245. void (*close)(Fsck*, Block*, u32int);
  246. void (*clri)(Fsck*, char*, MetaBlock*, int, Block*);
  247. /* used internally */
  248. Cache *cache;
  249. uchar *amap; /* all blocks seen so far */
  250. uchar *emap; /* all blocks seen in this epoch */
  251. uchar *xmap; /* all blocks in this epoch with parents in this epoch */
  252. uchar *errmap; /* blocks with errors */
  253. uchar *smap; /* walked sources */
  254. int nblocks;
  255. int bsize;
  256. int walkdepth;
  257. u32int hint; /* where the next root probably is */
  258. int nseen;
  259. int quantum;
  260. int nclre;
  261. int nclrp;
  262. int nclose;
  263. int nclri;
  264. };
  265. /* disk partitions; keep in sync with partname[] in disk.c */
  266. enum {
  267. PartError,
  268. PartSuper,
  269. PartLabel,
  270. PartData,
  271. PartVenti, /* fake partition */
  272. };
  273. extern vtType[BtMax];