dm-space-map-disk.c 6.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281
  1. // SPDX-License-Identifier: GPL-2.0-only
  2. /*
  3. * Copyright (C) 2011 Red Hat, Inc.
  4. *
  5. * This file is released under the GPL.
  6. */
  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. *result = count > 1;
  64. return 0;
  65. }
  66. static int sm_disk_set_count(struct dm_space_map *sm, dm_block_t b,
  67. uint32_t count)
  68. {
  69. int r;
  70. int32_t nr_allocations;
  71. struct sm_disk *smd = container_of(sm, struct sm_disk, sm);
  72. r = sm_ll_insert(&smd->ll, b, count, &nr_allocations);
  73. if (!r)
  74. smd->nr_allocated_this_transaction += nr_allocations;
  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. if (r)
  113. return r;
  114. smd->begin = *b + 1;
  115. r = sm_ll_inc(&smd->ll, *b, *b + 1, &nr_allocations);
  116. if (!r)
  117. smd->nr_allocated_this_transaction += nr_allocations;
  118. return r;
  119. }
  120. static int sm_disk_commit(struct dm_space_map *sm)
  121. {
  122. int r;
  123. struct sm_disk *smd = container_of(sm, struct sm_disk, sm);
  124. r = sm_ll_commit(&smd->ll);
  125. if (r)
  126. return r;
  127. memcpy(&smd->old_ll, &smd->ll, sizeof(smd->old_ll));
  128. smd->nr_allocated_this_transaction = 0;
  129. return 0;
  130. }
  131. static int sm_disk_root_size(struct dm_space_map *sm, size_t *result)
  132. {
  133. *result = sizeof(struct disk_sm_root);
  134. return 0;
  135. }
  136. static int sm_disk_copy_root(struct dm_space_map *sm, void *where_le, size_t max)
  137. {
  138. struct sm_disk *smd = container_of(sm, struct sm_disk, sm);
  139. struct disk_sm_root root_le;
  140. root_le.nr_blocks = cpu_to_le64(smd->ll.nr_blocks);
  141. root_le.nr_allocated = cpu_to_le64(smd->ll.nr_allocated);
  142. root_le.bitmap_root = cpu_to_le64(smd->ll.bitmap_root);
  143. root_le.ref_count_root = cpu_to_le64(smd->ll.ref_count_root);
  144. if (max < sizeof(root_le))
  145. return -ENOSPC;
  146. memcpy(where_le, &root_le, sizeof(root_le));
  147. return 0;
  148. }
  149. /*----------------------------------------------------------------*/
  150. static struct dm_space_map ops = {
  151. .destroy = sm_disk_destroy,
  152. .extend = sm_disk_extend,
  153. .get_nr_blocks = sm_disk_get_nr_blocks,
  154. .get_nr_free = sm_disk_get_nr_free,
  155. .get_count = sm_disk_get_count,
  156. .count_is_more_than_one = sm_disk_count_is_more_than_one,
  157. .set_count = sm_disk_set_count,
  158. .inc_blocks = sm_disk_inc_blocks,
  159. .dec_blocks = sm_disk_dec_blocks,
  160. .new_block = sm_disk_new_block,
  161. .commit = sm_disk_commit,
  162. .root_size = sm_disk_root_size,
  163. .copy_root = sm_disk_copy_root,
  164. .register_threshold_callback = NULL
  165. };
  166. struct dm_space_map *dm_sm_disk_create(struct dm_transaction_manager *tm,
  167. dm_block_t nr_blocks)
  168. {
  169. int r;
  170. struct sm_disk *smd;
  171. smd = kmalloc(sizeof(*smd), GFP_KERNEL);
  172. if (!smd)
  173. return ERR_PTR(-ENOMEM);
  174. smd->begin = 0;
  175. smd->nr_allocated_this_transaction = 0;
  176. memcpy(&smd->sm, &ops, sizeof(smd->sm));
  177. r = sm_ll_new_disk(&smd->ll, tm);
  178. if (r)
  179. goto bad;
  180. r = sm_ll_extend(&smd->ll, nr_blocks);
  181. if (r)
  182. goto bad;
  183. r = sm_disk_commit(&smd->sm);
  184. if (r)
  185. goto bad;
  186. return &smd->sm;
  187. bad:
  188. kfree(smd);
  189. return ERR_PTR(r);
  190. }
  191. EXPORT_SYMBOL_GPL(dm_sm_disk_create);
  192. struct dm_space_map *dm_sm_disk_open(struct dm_transaction_manager *tm,
  193. void *root_le, size_t len)
  194. {
  195. int r;
  196. struct sm_disk *smd;
  197. smd = kmalloc(sizeof(*smd), GFP_KERNEL);
  198. if (!smd)
  199. return ERR_PTR(-ENOMEM);
  200. smd->begin = 0;
  201. smd->nr_allocated_this_transaction = 0;
  202. memcpy(&smd->sm, &ops, sizeof(smd->sm));
  203. r = sm_ll_open_disk(&smd->ll, tm, root_le, len);
  204. if (r)
  205. goto bad;
  206. r = sm_disk_commit(&smd->sm);
  207. if (r)
  208. goto bad;
  209. return &smd->sm;
  210. bad:
  211. kfree(smd);
  212. return ERR_PTR(r);
  213. }
  214. EXPORT_SYMBOL_GPL(dm_sm_disk_open);
  215. /*----------------------------------------------------------------*/