dm-space-map-disk.c 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280
  1. /*
  2. * Copyright (C) 2011 Red Hat, Inc.
  3. *
  4. * This file is released under the GPL.
  5. */
  6. #include "dm-space-map-common.h"
  7. #include "dm-space-map-disk.h"
  8. #include "dm-space-map.h"
  9. #include "dm-transaction-manager.h"
  10. #include <linux/list.h>
  11. #include <linux/slab.h>
  12. #include <linux/export.h>
  13. #include <linux/device-mapper.h>
  14. #define DM_MSG_PREFIX "space map disk"
  15. /*----------------------------------------------------------------*/
  16. /*
  17. * Space map interface.
  18. */
  19. struct sm_disk {
  20. struct dm_space_map sm;
  21. struct ll_disk ll;
  22. struct ll_disk old_ll;
  23. dm_block_t begin;
  24. dm_block_t nr_allocated_this_transaction;
  25. };
  26. static void sm_disk_destroy(struct dm_space_map *sm)
  27. {
  28. struct sm_disk *smd = container_of(sm, struct sm_disk, sm);
  29. kfree(smd);
  30. }
  31. static int sm_disk_extend(struct dm_space_map *sm, dm_block_t extra_blocks)
  32. {
  33. struct sm_disk *smd = container_of(sm, struct sm_disk, sm);
  34. return sm_ll_extend(&smd->ll, extra_blocks);
  35. }
  36. static int sm_disk_get_nr_blocks(struct dm_space_map *sm, dm_block_t *count)
  37. {
  38. struct sm_disk *smd = container_of(sm, struct sm_disk, sm);
  39. *count = smd->old_ll.nr_blocks;
  40. return 0;
  41. }
  42. static int sm_disk_get_nr_free(struct dm_space_map *sm, dm_block_t *count)
  43. {
  44. struct sm_disk *smd = container_of(sm, struct sm_disk, sm);
  45. *count = (smd->old_ll.nr_blocks - smd->old_ll.nr_allocated) - smd->nr_allocated_this_transaction;
  46. return 0;
  47. }
  48. static int sm_disk_get_count(struct dm_space_map *sm, dm_block_t b,
  49. uint32_t *result)
  50. {
  51. struct sm_disk *smd = container_of(sm, struct sm_disk, sm);
  52. return sm_ll_lookup(&smd->ll, b, result);
  53. }
  54. static int sm_disk_count_is_more_than_one(struct dm_space_map *sm, dm_block_t b,
  55. int *result)
  56. {
  57. int r;
  58. uint32_t count;
  59. r = sm_disk_get_count(sm, b, &count);
  60. if (r)
  61. return r;
  62. *result = count > 1;
  63. return 0;
  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. int32_t nr_allocations;
  70. struct sm_disk *smd = container_of(sm, struct sm_disk, sm);
  71. r = sm_ll_insert(&smd->ll, b, count, &nr_allocations);
  72. if (!r) {
  73. smd->nr_allocated_this_transaction += nr_allocations;
  74. }
  75. return r;
  76. }
  77. static int sm_disk_inc_blocks(struct dm_space_map *sm, dm_block_t b, dm_block_t e)
  78. {
  79. int r;
  80. int32_t nr_allocations;
  81. struct sm_disk *smd = container_of(sm, struct sm_disk, sm);
  82. r = sm_ll_inc(&smd->ll, b, e, &nr_allocations);
  83. if (!r)
  84. smd->nr_allocated_this_transaction += nr_allocations;
  85. return r;
  86. }
  87. static int sm_disk_dec_blocks(struct dm_space_map *sm, dm_block_t b, dm_block_t e)
  88. {
  89. int r;
  90. int32_t nr_allocations;
  91. struct sm_disk *smd = container_of(sm, struct sm_disk, sm);
  92. r = sm_ll_dec(&smd->ll, b, e, &nr_allocations);
  93. if (!r)
  94. smd->nr_allocated_this_transaction += nr_allocations;
  95. return r;
  96. }
  97. static int sm_disk_new_block(struct dm_space_map *sm, dm_block_t *b)
  98. {
  99. int r;
  100. int32_t nr_allocations;
  101. struct sm_disk *smd = container_of(sm, struct sm_disk, sm);
  102. /*
  103. * Any block we allocate has to be free in both the old and current ll.
  104. */
  105. r = sm_ll_find_common_free_block(&smd->old_ll, &smd->ll, smd->begin, smd->ll.nr_blocks, b);
  106. if (r == -ENOSPC) {
  107. /*
  108. * There's no free block between smd->begin and the end of the metadata device.
  109. * We search before smd->begin in case something has been freed.
  110. */
  111. r = sm_ll_find_common_free_block(&smd->old_ll, &smd->ll, 0, smd->begin, b);
  112. }
  113. if (r)
  114. return r;
  115. smd->begin = *b + 1;
  116. r = sm_ll_inc(&smd->ll, *b, *b + 1, &nr_allocations);
  117. if (!r) {
  118. smd->nr_allocated_this_transaction += nr_allocations;
  119. }
  120. return r;
  121. }
  122. static int sm_disk_commit(struct dm_space_map *sm)
  123. {
  124. int r;
  125. struct sm_disk *smd = container_of(sm, struct sm_disk, sm);
  126. r = sm_ll_commit(&smd->ll);
  127. if (r)
  128. return r;
  129. memcpy(&smd->old_ll, &smd->ll, sizeof(smd->old_ll));
  130. smd->nr_allocated_this_transaction = 0;
  131. return 0;
  132. }
  133. static int sm_disk_root_size(struct dm_space_map *sm, size_t *result)
  134. {
  135. *result = sizeof(struct disk_sm_root);
  136. return 0;
  137. }
  138. static int sm_disk_copy_root(struct dm_space_map *sm, void *where_le, size_t max)
  139. {
  140. struct sm_disk *smd = container_of(sm, struct sm_disk, sm);
  141. struct disk_sm_root root_le;
  142. root_le.nr_blocks = cpu_to_le64(smd->ll.nr_blocks);
  143. root_le.nr_allocated = cpu_to_le64(smd->ll.nr_allocated);
  144. root_le.bitmap_root = cpu_to_le64(smd->ll.bitmap_root);
  145. root_le.ref_count_root = cpu_to_le64(smd->ll.ref_count_root);
  146. if (max < sizeof(root_le))
  147. return -ENOSPC;
  148. memcpy(where_le, &root_le, sizeof(root_le));
  149. return 0;
  150. }
  151. /*----------------------------------------------------------------*/
  152. static struct dm_space_map ops = {
  153. .destroy = sm_disk_destroy,
  154. .extend = sm_disk_extend,
  155. .get_nr_blocks = sm_disk_get_nr_blocks,
  156. .get_nr_free = sm_disk_get_nr_free,
  157. .get_count = sm_disk_get_count,
  158. .count_is_more_than_one = sm_disk_count_is_more_than_one,
  159. .set_count = sm_disk_set_count,
  160. .inc_blocks = sm_disk_inc_blocks,
  161. .dec_blocks = sm_disk_dec_blocks,
  162. .new_block = sm_disk_new_block,
  163. .commit = sm_disk_commit,
  164. .root_size = sm_disk_root_size,
  165. .copy_root = sm_disk_copy_root,
  166. .register_threshold_callback = NULL
  167. };
  168. struct dm_space_map *dm_sm_disk_create(struct dm_transaction_manager *tm,
  169. dm_block_t nr_blocks)
  170. {
  171. int r;
  172. struct sm_disk *smd;
  173. smd = kmalloc(sizeof(*smd), GFP_KERNEL);
  174. if (!smd)
  175. return ERR_PTR(-ENOMEM);
  176. smd->begin = 0;
  177. smd->nr_allocated_this_transaction = 0;
  178. memcpy(&smd->sm, &ops, sizeof(smd->sm));
  179. r = sm_ll_new_disk(&smd->ll, tm);
  180. if (r)
  181. goto bad;
  182. r = sm_ll_extend(&smd->ll, nr_blocks);
  183. if (r)
  184. goto bad;
  185. r = sm_disk_commit(&smd->sm);
  186. if (r)
  187. goto bad;
  188. return &smd->sm;
  189. bad:
  190. kfree(smd);
  191. return ERR_PTR(r);
  192. }
  193. EXPORT_SYMBOL_GPL(dm_sm_disk_create);
  194. struct dm_space_map *dm_sm_disk_open(struct dm_transaction_manager *tm,
  195. void *root_le, size_t len)
  196. {
  197. int r;
  198. struct sm_disk *smd;
  199. smd = kmalloc(sizeof(*smd), GFP_KERNEL);
  200. if (!smd)
  201. return ERR_PTR(-ENOMEM);
  202. smd->begin = 0;
  203. smd->nr_allocated_this_transaction = 0;
  204. memcpy(&smd->sm, &ops, sizeof(smd->sm));
  205. r = sm_ll_open_disk(&smd->ll, tm, root_le, len);
  206. if (r)
  207. goto bad;
  208. r = sm_disk_commit(&smd->sm);
  209. if (r)
  210. goto bad;
  211. return &smd->sm;
  212. bad:
  213. kfree(smd);
  214. return ERR_PTR(r);
  215. }
  216. EXPORT_SYMBOL_GPL(dm_sm_disk_open);
  217. /*----------------------------------------------------------------*/