bcache.c 7.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348
  1. /*
  2. * Copyright (c) 2007 Travis Geiselbrecht
  3. *
  4. * Permission is hereby granted, free of charge, to any person obtaining
  5. * a copy of this software and associated documentation files
  6. * (the "Software"), to deal in the Software without restriction,
  7. * including without limitation the rights to use, copy, modify, merge,
  8. * publish, distribute, sublicense, and/or sell copies of the Software,
  9. * and to permit persons to whom the Software is furnished to do so,
  10. * subject to the following conditions:
  11. *
  12. * The above copyright notice and this permission notice shall be
  13. * included in all copies or substantial portions of the Software.
  14. *
  15. * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
  16. * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
  17. * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
  18. * IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY
  19. * CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
  20. * TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
  21. * SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
  22. */
  23. #include <list.h>
  24. #include <stdlib.h>
  25. #include <string.h>
  26. #include <sys/types.h>
  27. #include <debug.h>
  28. #include <lib/bcache.h>
  29. #include <lib/bio.h>
  30. #define LOCAL_TRACE 0
  31. struct bcache_block {
  32. struct list_node node;
  33. bnum_t blocknum;
  34. int ref_count;
  35. bool is_dirty;
  36. void *ptr;
  37. };
  38. struct bcache_stats {
  39. uint32_t hits;
  40. uint32_t depth;
  41. uint32_t misses;
  42. uint32_t reads;
  43. uint32_t writes;
  44. };
  45. struct bcache {
  46. bdev_t *dev;
  47. size_t block_size;
  48. int count;
  49. struct bcache_stats stats;
  50. struct list_node free_list;
  51. struct list_node lru_list;
  52. struct bcache_block *blocks;
  53. };
  54. bcache_t bcache_create(bdev_t *dev, size_t block_size, int block_count)
  55. {
  56. struct bcache *cache;
  57. cache = malloc(sizeof(struct bcache));
  58. cache->dev = dev;
  59. cache->block_size = block_size;
  60. cache->count = block_count;
  61. memset(&cache->stats, 0, sizeof(cache->stats));
  62. list_initialize(&cache->free_list);
  63. list_initialize(&cache->lru_list);
  64. cache->blocks = malloc(sizeof(struct bcache_block) * block_count);
  65. int i;
  66. for (i=0; i < block_count; i++) {
  67. cache->blocks[i].ref_count = 0;
  68. cache->blocks[i].is_dirty = false;
  69. cache->blocks[i].ptr = malloc(block_size);
  70. // add to the free list
  71. list_add_head(&cache->free_list, &cache->blocks[i].node);
  72. }
  73. return (bcache_t)cache;
  74. }
  75. static int flush_block(struct bcache *cache, struct bcache_block *block)
  76. {
  77. int rc;
  78. rc = bio_write(cache->dev, block->ptr,
  79. (off_t)block->blocknum * cache->block_size,
  80. cache->block_size);
  81. if (rc < 0)
  82. goto exit;
  83. block->is_dirty = false;
  84. cache->stats.writes++;
  85. rc = 0;
  86. exit:
  87. return (rc);
  88. }
  89. void bcache_destroy(bcache_t _cache)
  90. {
  91. struct bcache *cache = _cache;
  92. int i;
  93. for (i=0; i < cache->count; i++) {
  94. DEBUG_ASSERT(cache->blocks[i].ref_count == 0);
  95. if (cache->blocks[i].is_dirty)
  96. printf("warning: freeing dirty block %u\n",
  97. cache->blocks[i].blocknum);
  98. free(cache->blocks[i].ptr);
  99. }
  100. free(cache);
  101. }
  102. /* find a block if it's already present */
  103. static struct bcache_block *find_block(struct bcache *cache, uint blocknum)
  104. {
  105. uint32_t depth = 0;
  106. struct bcache_block *block;
  107. LTRACEF("num %u\n", blocknum);
  108. block = NULL;
  109. list_for_every_entry(&cache->lru_list, block, struct bcache_block, node) {
  110. LTRACEF("looking at entry %p, num %u\n", block, block->blocknum);
  111. depth++;
  112. if (block->blocknum == blocknum) {
  113. list_delete(&block->node);
  114. list_add_tail(&cache->lru_list, &block->node);
  115. cache->stats.hits++;
  116. cache->stats.depth += depth;
  117. return block;
  118. }
  119. }
  120. cache->stats.misses++;
  121. return NULL;
  122. }
  123. /* allocate a new block */
  124. static struct bcache_block *alloc_block(struct bcache *cache)
  125. {
  126. int err;
  127. struct bcache_block *block;
  128. /* pop one off the free list if it's present */
  129. block = list_remove_head_type(&cache->free_list, struct bcache_block, node);
  130. if (block) {
  131. block->ref_count = 0;
  132. list_add_tail(&cache->lru_list, &block->node);
  133. LTRACEF("found block %p on free list\n", block);
  134. return block;
  135. }
  136. /* walk the lru, looking for a free block */
  137. list_for_every_entry(&cache->lru_list, block, struct bcache_block, node) {
  138. LTRACEF("looking at %p, num %u\n", block, block->blocknum);
  139. if (block->ref_count == 0) {
  140. if (block->is_dirty) {
  141. err = flush_block(cache, block);
  142. if (err)
  143. return NULL;
  144. }
  145. // add it to the tail of the lru
  146. list_delete(&block->node);
  147. list_add_tail(&cache->lru_list, &block->node);
  148. return block;
  149. }
  150. }
  151. return NULL;
  152. }
  153. static struct bcache_block *find_or_fill_block(struct bcache *cache, uint blocknum)
  154. {
  155. int err;
  156. LTRACEF("block %u\n", blocknum);
  157. /* see if it's already in the cache */
  158. struct bcache_block *block = find_block(cache, blocknum);
  159. if (block == NULL) {
  160. LTRACEF("wasn't allocated\n");
  161. /* allocate a new block and fill it */
  162. block = alloc_block(cache);
  163. DEBUG_ASSERT(block);
  164. LTRACEF("wasn't allocated, new block %p\n", block);
  165. block->blocknum = blocknum;
  166. err = bio_read(cache->dev, block->ptr, (off_t)blocknum * cache->block_size, cache->block_size);
  167. if (err < 0) {
  168. /* free the block, return an error */
  169. list_add_tail(&cache->free_list, &block->node);
  170. return NULL;
  171. }
  172. cache->stats.reads++;
  173. }
  174. DEBUG_ASSERT(block->blocknum == blocknum);
  175. return block;
  176. }
  177. int bcache_read_block(bcache_t _cache, void *buf, uint blocknum)
  178. {
  179. struct bcache *cache = _cache;
  180. LTRACEF("buf %p, blocknum %u\n", buf, blocknum);
  181. struct bcache_block *block = find_or_fill_block(cache, blocknum);
  182. if (block == NULL) {
  183. /* error */
  184. return -1;
  185. }
  186. memcpy(buf, block->ptr, cache->block_size);
  187. return 0;
  188. }
  189. int bcache_get_block(bcache_t _cache, void **ptr, uint blocknum)
  190. {
  191. struct bcache *cache = _cache;
  192. LTRACEF("ptr %p, blocknum %u\n", ptr, blocknum);
  193. DEBUG_ASSERT(ptr);
  194. struct bcache_block *block = find_or_fill_block(cache, blocknum);
  195. if (block == NULL) {
  196. /* error */
  197. return -1;
  198. }
  199. /* increment the ref count to keep it from being freed */
  200. block->ref_count++;
  201. *ptr = block->ptr;
  202. return 0;
  203. }
  204. int bcache_put_block(bcache_t _cache, uint blocknum)
  205. {
  206. struct bcache *cache = _cache;
  207. LTRACEF("blocknum %u\n", blocknum);
  208. struct bcache_block *block = find_block(cache, blocknum);
  209. /* be pretty hard on the caller for now */
  210. DEBUG_ASSERT(block);
  211. DEBUG_ASSERT(block->ref_count > 0);
  212. block->ref_count--;
  213. return 0;
  214. }
  215. int bcache_mark_block_dirty(bcache_t priv, uint blocknum)
  216. {
  217. int err;
  218. struct bcache *cache = priv;
  219. struct bcache_block *block;
  220. block = find_block(cache, blocknum);
  221. if (!block) {
  222. err = -1;
  223. goto exit;
  224. }
  225. block->is_dirty = true;
  226. err = 0;
  227. exit:
  228. return (err);
  229. }
  230. int bcache_zero_block(bcache_t priv, uint blocknum)
  231. {
  232. int err;
  233. struct bcache *cache = priv;
  234. struct bcache_block *block;
  235. block = find_block(cache, blocknum);
  236. if (!block) {
  237. block = alloc_block(cache);
  238. if (!block) {
  239. err = -1;
  240. goto exit;
  241. }
  242. block->blocknum = blocknum;
  243. }
  244. memset(block->ptr, 0, cache->block_size);
  245. block->is_dirty = true;
  246. err = 0;
  247. exit:
  248. return (err);
  249. }
  250. int bcache_flush(bcache_t priv)
  251. {
  252. int err;
  253. struct bcache *cache = priv;
  254. struct bcache_block *block;
  255. list_for_every_entry(&cache->lru_list, block, struct bcache_block, node) {
  256. if (block->is_dirty) {
  257. err = flush_block(cache, block);
  258. if (err)
  259. goto exit;
  260. }
  261. }
  262. err = 0;
  263. exit:
  264. return (err);
  265. }
  266. void bcache_dump(bcache_t priv, const char *name)
  267. {
  268. uint32_t finds;
  269. struct bcache *cache = priv;
  270. finds = cache->stats.hits + cache->stats.misses;
  271. printf("%s: hits=%u(%u%%) depth=%u misses=%u(%u%%) reads=%u writes=%u\n",
  272. name,
  273. cache->stats.hits,
  274. finds ? (cache->stats.hits * 100) / finds : 0,
  275. cache->stats.hits ? cache->stats.depth / cache->stats.hits : 0,
  276. cache->stats.misses,
  277. finds ? (cache->stats.misses * 100) / finds : 0,
  278. cache->stats.reads,
  279. cache->stats.writes);
  280. }