disk.c 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364
  1. #include <u.h>
  2. #include <libc.h>
  3. #include "cformat.h"
  4. #include "lru.h"
  5. #include "bcache.h"
  6. #include "disk.h"
  7. int icformat(Disk*, ulong);
  8. /*
  9. * read in the disk structures, return -1 if the format
  10. * is inconsistent.
  11. */
  12. int
  13. dinit(Disk *d, int f, int psize)
  14. {
  15. Dir *dir;
  16. char buf[1024];
  17. Dalloc *ba;
  18. uvlong length;
  19. ulong i;
  20. Bbuf *b;
  21. /*
  22. * get disk size
  23. */
  24. dir = dirfstat(f);
  25. if(dir == nil){
  26. perror("dinit: stat");
  27. return -1;
  28. }
  29. length = dir->length;
  30. free(dir);
  31. /*
  32. * read first physical block to get logical block size, number of inodes,
  33. * and number of allocation blocks
  34. */
  35. if(seek(f, 0, 0) < 0){
  36. perror("dinit: seek");
  37. return -1;
  38. }
  39. if(read(f, buf, sizeof(buf)) != sizeof(buf)){
  40. perror("dinit: read");
  41. return -1;
  42. }
  43. ba = (Dalloc*)buf;
  44. if(ba->bsize <= 0){
  45. fprint(2, "dinit: bsize 0x%lux<= 0\n", ba->bsize);
  46. return -1;
  47. }
  48. if((ba->bsize % psize) != 0){
  49. fprint(2, "dinit: logical bsize (%lud) not multiple of physical (%ud)\n",
  50. ba->bsize, psize);
  51. return -1;
  52. }
  53. d->bsize = ba->bsize;
  54. d->nb = length/d->bsize;
  55. d->b2b = (d->bsize - sizeof(Dahdr))*8;
  56. d->nab = (d->nb+d->b2b-1)/d->b2b;
  57. d->p2b = d->bsize/sizeof(Dptr);
  58. strncpy(d->name, ba->name, sizeof(d->name));
  59. /*
  60. * check allocation blocks for consistency
  61. */
  62. if(bcinit(d, f, d->bsize) < 0){
  63. fprint(2, "dinit: couldn't init block cache\n");
  64. return -1;
  65. }
  66. for(i = 0; i < d->nab; i++){
  67. b = bcread(d, i);
  68. if(b == 0){
  69. perror("dinit: read");
  70. return -1;
  71. }
  72. ba = (Dalloc*)b->data;
  73. if(ba->magic != Amagic){
  74. fprint(2, "dinit: bad magic in alloc block %uld\n", i);
  75. return -1;
  76. }
  77. if(d->bsize != ba->bsize){
  78. fprint(2, "dinit: bad bsize in alloc block %uld\n", i);
  79. return -1;
  80. }
  81. if(d->nab != ba->nab){
  82. fprint(2, "dinit: bad nab in alloc block %uld\n", i);
  83. return -1;
  84. }
  85. if(strncmp(d->name, ba->name, sizeof(d->name))){
  86. fprint(2, "dinit: bad name in alloc block %uld\n", i);
  87. return -1;
  88. }
  89. }
  90. return 0;
  91. }
  92. /*
  93. * format the disk as a cache
  94. */
  95. int
  96. dformat(Disk *d, int f, char *name, ulong bsize, ulong psize)
  97. {
  98. int i;
  99. Dir *dir;
  100. uvlong length;
  101. Bbuf *b;
  102. Dalloc *ba;
  103. Dptr dptr;
  104. fprint(2, "formatting disk\n");
  105. /*
  106. * calculate basic numbers
  107. */
  108. dir = dirfstat(f);
  109. if(dir == nil)
  110. return -1;
  111. length = dir->length;
  112. d->bsize = bsize;
  113. if((d->bsize % psize) != 0){
  114. fprint(2, "cfs: logical bsize not multiple of physical\n");
  115. return -1;
  116. }
  117. d->nb = length/d->bsize;
  118. d->b2b = (d->bsize - sizeof(Dahdr))*8;
  119. d->nab = (d->nb+d->b2b-1)/d->b2b;
  120. d->p2b = d->bsize/sizeof(Dptr);
  121. /*
  122. * init allocation blocks
  123. */
  124. if(bcinit(d, f, d->bsize) < 0)
  125. return -1;
  126. for(i = 0; i < d->nab; i++){
  127. b = bcalloc(d, i);
  128. if(b == 0){
  129. perror("cfs: bcalloc");
  130. return -1;
  131. }
  132. memset(b->data, 0, d->bsize);
  133. ba = (Dalloc*)b->data;
  134. ba->magic = Amagic;
  135. ba->bsize = d->bsize;
  136. ba->nab = d->nab;
  137. strncpy(ba->name, name, sizeof(ba->name));
  138. bcmark(d, b);
  139. }
  140. /*
  141. * allocate allocation blocks
  142. */
  143. for(i = 0; i < d->nab; i++)
  144. if(dalloc(d, &dptr) == Notabno){
  145. fprint(2, "can't allocate allocation blocks\n");
  146. return -1;
  147. }
  148. return bcsync(d);
  149. }
  150. /*
  151. * allocate a block from a bit vector page
  152. *
  153. * a return value of Notabno means no blocks left
  154. */
  155. static ulong
  156. _balloc(Dalloc *ba, ulong max)
  157. {
  158. ulong *p;
  159. ulong *e;
  160. ulong i; /* bit position in long */
  161. ulong m; /* 1<<i */
  162. ulong v; /* old value of long */
  163. int len; /* number of valid words */
  164. /*
  165. * find a word with a 0 bit
  166. */
  167. len = (max+BtoUL-1)/BtoUL;
  168. for(p = ba->bits, e = p + len; p < e; p++)
  169. if(*p != 0xFFFFFFFF)
  170. break;
  171. if(p == e)
  172. return Notabno;
  173. /*
  174. * find the first 0 bit
  175. */
  176. v = *p;
  177. for(m = 1, i = 0; i<BtoUL; i++, m<<=1)
  178. if((m|v) != v)
  179. break;
  180. /*
  181. * calculate block number
  182. */
  183. i += (p - ba->bits)*BtoUL;
  184. if(i >= max)
  185. return Notabno;
  186. /*
  187. * set bit to 1
  188. */
  189. *p = v | m;
  190. return i;
  191. }
  192. /*
  193. * allocate a block
  194. *
  195. * return Notabno if none left
  196. */
  197. ulong
  198. dalloc(Disk *d, Dptr *p)
  199. {
  200. ulong bno;
  201. Bbuf *b;
  202. Dalloc *ba;
  203. ulong rv;
  204. ulong max;
  205. max = d->nb;
  206. for(bno = 0; bno < d->nab; bno++){
  207. b = bcread(d, bno);
  208. ba = (Dalloc*)b->data;
  209. rv = _balloc(ba, max > d->b2b ? d->b2b : max);
  210. if(rv != Notabno){
  211. rv = bno*d->b2b + rv;
  212. if(p){
  213. p->start = p->end = 0;
  214. p->bno = rv;
  215. }
  216. bcmark(d, b);
  217. return rv;
  218. }
  219. max -= d->b2b;
  220. }
  221. if(p)
  222. p->bno = Notabno;
  223. return Notabno;
  224. }
  225. /*
  226. * allocate a block of pointers
  227. */
  228. ulong
  229. dpalloc(Disk *d, Dptr *p)
  230. {
  231. Bbuf *b;
  232. Dptr *sp;
  233. Dptr *ep;
  234. if(dalloc(d, p) == Notabno)
  235. return Notabno;
  236. /*
  237. * allocate the page and invalidate all the
  238. * pointers
  239. */
  240. b = bcalloc(d, p->bno);
  241. if(b == 0)
  242. return -1;
  243. sp = (Dptr*)b->data;
  244. for(ep = sp + d->p2b; sp < ep; sp++){
  245. sp->bno = Notabno;
  246. sp->start = sp->end = 0;
  247. }
  248. p->bno |= Indbno;
  249. p->start = 0;
  250. p->end = d->bsize;
  251. /*
  252. * mark the page as dirty
  253. */
  254. bcmark(d, b);
  255. return 0;
  256. }
  257. /*
  258. * free a block
  259. */
  260. int
  261. _bfree(Disk *d, ulong i)
  262. {
  263. ulong *p;
  264. ulong m;
  265. Bbuf *b;
  266. Dalloc *ba;
  267. ulong bno;
  268. /*
  269. * get correct allocation block
  270. */
  271. bno = i/d->b2b;
  272. if(bno >= d->nab)
  273. return -1;
  274. b = bcread(d, bno);
  275. if(b == 0)
  276. return -1;
  277. ba = (Dalloc*)b->data;
  278. /*
  279. * change bit
  280. */
  281. i -= bno*d->b2b;
  282. p = ba->bits + (i/BtoUL);
  283. m = 1<<(i%BtoUL);
  284. *p &= ~m;
  285. bcmark(d, b);
  286. return 0;
  287. }
  288. /*
  289. * free a block (or blocks)
  290. */
  291. int
  292. dfree(Disk *d, Dptr *dp)
  293. {
  294. Dptr *sp;
  295. Dptr *ep;
  296. Bbuf *b;
  297. ulong bno;
  298. bno = dp->bno;
  299. dp->bno = Notabno;
  300. /*
  301. * nothing to free
  302. */
  303. if(bno == Notabno)
  304. return 0;
  305. /*
  306. * direct pointer
  307. */
  308. if((bno & Indbno) == 0)
  309. return _bfree(d, bno);
  310. /*
  311. * first indirect page
  312. */
  313. bno &= ~Indbno;
  314. _bfree(d, bno);
  315. /*
  316. * then all the pages it points to
  317. *
  318. * DANGER: this algorithm may fail is their are more
  319. * allocation blocks than block buffers
  320. */
  321. b = bcread(d, bno);
  322. if(b == 0)
  323. return -1;
  324. sp = (Dptr*)b->data;
  325. for(ep = sp + d->p2b; sp < ep; sp++)
  326. if(dfree(d, sp) < 0)
  327. return -1;
  328. return 0;
  329. }