dm-space-map-disk.c 7.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335
  1. /*
  2. * Copyright (C) 2011 Red Hat, Inc.
  3. *
  4. * This file is released under the GPL.
  5. */
  6. #include "dm-space-map-checker.h"
  7. #include "dm-space-map-common.h"
  8. #include "dm-space-map-disk.h"
  9. #include "dm-space-map.h"
  10. #include "dm-transaction-manager.h"
  11. #include <linux/list.h>
  12. #include <linux/slab.h>
  13. #include <linux/export.h>
  14. #include <linux/device-mapper.h>
  15. #define DM_MSG_PREFIX "space map disk"
  16. /*----------------------------------------------------------------*/
  17. /*
  18. * Space map interface.
  19. */
  20. struct sm_disk {
  21. struct dm_space_map sm;
  22. struct ll_disk ll;
  23. struct ll_disk old_ll;
  24. dm_block_t begin;
  25. dm_block_t nr_allocated_this_transaction;
  26. };
  27. static void sm_disk_destroy(struct dm_space_map *sm)
  28. {
  29. struct sm_disk *smd = container_of(sm, struct sm_disk, sm);
  30. kfree(smd);
  31. }
  32. static int sm_disk_extend(struct dm_space_map *sm, dm_block_t extra_blocks)
  33. {
  34. struct sm_disk *smd = container_of(sm, struct sm_disk, sm);
  35. return sm_ll_extend(&smd->ll, extra_blocks);
  36. }
  37. static int sm_disk_get_nr_blocks(struct dm_space_map *sm, dm_block_t *count)
  38. {
  39. struct sm_disk *smd = container_of(sm, struct sm_disk, sm);
  40. *count = smd->old_ll.nr_blocks;
  41. return 0;
  42. }
  43. static int sm_disk_get_nr_free(struct dm_space_map *sm, dm_block_t *count)
  44. {
  45. struct sm_disk *smd = container_of(sm, struct sm_disk, sm);
  46. *count = (smd->old_ll.nr_blocks - smd->old_ll.nr_allocated) - smd->nr_allocated_this_transaction;
  47. return 0;
  48. }
  49. static int sm_disk_get_count(struct dm_space_map *sm, dm_block_t b,
  50. uint32_t *result)
  51. {
  52. struct sm_disk *smd = container_of(sm, struct sm_disk, sm);
  53. return sm_ll_lookup(&smd->ll, b, result);
  54. }
  55. static int sm_disk_count_is_more_than_one(struct dm_space_map *sm, dm_block_t b,
  56. int *result)
  57. {
  58. int r;
  59. uint32_t count;
  60. r = sm_disk_get_count(sm, b, &count);
  61. if (r)
  62. return r;
  63. return count > 1;
  64. }
  65. static int sm_disk_set_count(struct dm_space_map *sm, dm_block_t b,
  66. uint32_t count)
  67. {
  68. int r;
  69. uint32_t old_count;
  70. enum allocation_event ev;
  71. struct sm_disk *smd = container_of(sm, struct sm_disk, sm);
  72. r = sm_ll_insert(&smd->ll, b, count, &ev);
  73. if (!r) {
  74. switch (ev) {
  75. case SM_NONE:
  76. break;
  77. case SM_ALLOC:
  78. /*
  79. * This _must_ be free in the prior transaction
  80. * otherwise we've lost atomicity.
  81. */
  82. smd->nr_allocated_this_transaction++;
  83. break;
  84. case SM_FREE:
  85. /*
  86. * It's only free if it's also free in the last
  87. * transaction.
  88. */
  89. r = sm_ll_lookup(&smd->old_ll, b, &old_count);
  90. if (r)
  91. return r;
  92. if (!old_count)
  93. smd->nr_allocated_this_transaction--;
  94. break;
  95. }
  96. }
  97. return r;
  98. }
  99. static int sm_disk_inc_block(struct dm_space_map *sm, dm_block_t b)
  100. {
  101. int r;
  102. enum allocation_event ev;
  103. struct sm_disk *smd = container_of(sm, struct sm_disk, sm);
  104. r = sm_ll_inc(&smd->ll, b, &ev);
  105. if (!r && (ev == SM_ALLOC))
  106. /*
  107. * This _must_ be free in the prior transaction
  108. * otherwise we've lost atomicity.
  109. */
  110. smd->nr_allocated_this_transaction++;
  111. return r;
  112. }
  113. static int sm_disk_dec_block(struct dm_space_map *sm, dm_block_t b)
  114. {
  115. int r;
  116. uint32_t old_count;
  117. enum allocation_event ev;
  118. struct sm_disk *smd = container_of(sm, struct sm_disk, sm);
  119. r = sm_ll_dec(&smd->ll, b, &ev);
  120. if (!r && (ev == SM_FREE)) {
  121. /*
  122. * It's only free if it's also free in the last
  123. * transaction.
  124. */
  125. r = sm_ll_lookup(&smd->old_ll, b, &old_count);
  126. if (r)
  127. return r;
  128. if (!old_count)
  129. smd->nr_allocated_this_transaction--;
  130. }
  131. return r;
  132. }
  133. static int sm_disk_new_block(struct dm_space_map *sm, dm_block_t *b)
  134. {
  135. int r;
  136. enum allocation_event ev;
  137. struct sm_disk *smd = container_of(sm, struct sm_disk, sm);
  138. /* FIXME: we should loop round a couple of times */
  139. r = sm_ll_find_free_block(&smd->old_ll, smd->begin, smd->old_ll.nr_blocks, b);
  140. if (r)
  141. return r;
  142. smd->begin = *b + 1;
  143. r = sm_ll_inc(&smd->ll, *b, &ev);
  144. if (!r) {
  145. BUG_ON(ev != SM_ALLOC);
  146. smd->nr_allocated_this_transaction++;
  147. }
  148. return r;
  149. }
  150. static int sm_disk_commit(struct dm_space_map *sm)
  151. {
  152. int r;
  153. dm_block_t nr_free;
  154. struct sm_disk *smd = container_of(sm, struct sm_disk, sm);
  155. r = sm_disk_get_nr_free(sm, &nr_free);
  156. if (r)
  157. return r;
  158. r = sm_ll_commit(&smd->ll);
  159. if (r)
  160. return r;
  161. memcpy(&smd->old_ll, &smd->ll, sizeof(smd->old_ll));
  162. smd->begin = 0;
  163. smd->nr_allocated_this_transaction = 0;
  164. r = sm_disk_get_nr_free(sm, &nr_free);
  165. if (r)
  166. return r;
  167. return 0;
  168. }
  169. static int sm_disk_root_size(struct dm_space_map *sm, size_t *result)
  170. {
  171. *result = sizeof(struct disk_sm_root);
  172. return 0;
  173. }
  174. static int sm_disk_copy_root(struct dm_space_map *sm, void *where_le, size_t max)
  175. {
  176. struct sm_disk *smd = container_of(sm, struct sm_disk, sm);
  177. struct disk_sm_root root_le;
  178. root_le.nr_blocks = cpu_to_le64(smd->ll.nr_blocks);
  179. root_le.nr_allocated = cpu_to_le64(smd->ll.nr_allocated);
  180. root_le.bitmap_root = cpu_to_le64(smd->ll.bitmap_root);
  181. root_le.ref_count_root = cpu_to_le64(smd->ll.ref_count_root);
  182. if (max < sizeof(root_le))
  183. return -ENOSPC;
  184. memcpy(where_le, &root_le, sizeof(root_le));
  185. return 0;
  186. }
  187. /*----------------------------------------------------------------*/
  188. static struct dm_space_map ops = {
  189. .destroy = sm_disk_destroy,
  190. .extend = sm_disk_extend,
  191. .get_nr_blocks = sm_disk_get_nr_blocks,
  192. .get_nr_free = sm_disk_get_nr_free,
  193. .get_count = sm_disk_get_count,
  194. .count_is_more_than_one = sm_disk_count_is_more_than_one,
  195. .set_count = sm_disk_set_count,
  196. .inc_block = sm_disk_inc_block,
  197. .dec_block = sm_disk_dec_block,
  198. .new_block = sm_disk_new_block,
  199. .commit = sm_disk_commit,
  200. .root_size = sm_disk_root_size,
  201. .copy_root = sm_disk_copy_root
  202. };
  203. static struct dm_space_map *dm_sm_disk_create_real(
  204. struct dm_transaction_manager *tm,
  205. dm_block_t nr_blocks)
  206. {
  207. int r;
  208. struct sm_disk *smd;
  209. smd = kmalloc(sizeof(*smd), GFP_KERNEL);
  210. if (!smd)
  211. return ERR_PTR(-ENOMEM);
  212. smd->begin = 0;
  213. smd->nr_allocated_this_transaction = 0;
  214. memcpy(&smd->sm, &ops, sizeof(smd->sm));
  215. r = sm_ll_new_disk(&smd->ll, tm);
  216. if (r)
  217. goto bad;
  218. r = sm_ll_extend(&smd->ll, nr_blocks);
  219. if (r)
  220. goto bad;
  221. r = sm_disk_commit(&smd->sm);
  222. if (r)
  223. goto bad;
  224. return &smd->sm;
  225. bad:
  226. kfree(smd);
  227. return ERR_PTR(r);
  228. }
  229. struct dm_space_map *dm_sm_disk_create(struct dm_transaction_manager *tm,
  230. dm_block_t nr_blocks)
  231. {
  232. struct dm_space_map *sm = dm_sm_disk_create_real(tm, nr_blocks);
  233. return dm_sm_checker_create_fresh(sm);
  234. }
  235. EXPORT_SYMBOL_GPL(dm_sm_disk_create);
  236. static struct dm_space_map *dm_sm_disk_open_real(
  237. struct dm_transaction_manager *tm,
  238. void *root_le, size_t len)
  239. {
  240. int r;
  241. struct sm_disk *smd;
  242. smd = kmalloc(sizeof(*smd), GFP_KERNEL);
  243. if (!smd)
  244. return ERR_PTR(-ENOMEM);
  245. smd->begin = 0;
  246. smd->nr_allocated_this_transaction = 0;
  247. memcpy(&smd->sm, &ops, sizeof(smd->sm));
  248. r = sm_ll_open_disk(&smd->ll, tm, root_le, len);
  249. if (r)
  250. goto bad;
  251. r = sm_disk_commit(&smd->sm);
  252. if (r)
  253. goto bad;
  254. return &smd->sm;
  255. bad:
  256. kfree(smd);
  257. return ERR_PTR(r);
  258. }
  259. struct dm_space_map *dm_sm_disk_open(struct dm_transaction_manager *tm,
  260. void *root_le, size_t len)
  261. {
  262. return dm_sm_checker_create(
  263. dm_sm_disk_open_real(tm, root_le, len));
  264. }
  265. EXPORT_SYMBOL_GPL(dm_sm_disk_open);
  266. /*----------------------------------------------------------------*/