dm-space-map-metadata.c 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597
  1. /*
  2. * Copyright (C) 2011 Red Hat, Inc.
  3. *
  4. * This file is released under the GPL.
  5. */
  6. #include "dm-space-map.h"
  7. #include "dm-space-map-common.h"
  8. #include "dm-space-map-metadata.h"
  9. #include <linux/list.h>
  10. #include <linux/slab.h>
  11. #include <linux/device-mapper.h>
  12. #define DM_MSG_PREFIX "space map metadata"
  13. /*----------------------------------------------------------------*/
  14. /*
  15. * Space map interface.
  16. *
  17. * The low level disk format is written using the standard btree and
  18. * transaction manager. This means that performing disk operations may
  19. * cause us to recurse into the space map in order to allocate new blocks.
  20. * For this reason we have a pool of pre-allocated blocks large enough to
  21. * service any metadata_ll_disk operation.
  22. */
  23. /*
  24. * FIXME: we should calculate this based on the size of the device.
  25. * Only the metadata space map needs this functionality.
  26. */
  27. #define MAX_RECURSIVE_ALLOCATIONS 1024
  28. enum block_op_type {
  29. BOP_INC,
  30. BOP_DEC
  31. };
  32. struct block_op {
  33. enum block_op_type type;
  34. dm_block_t block;
  35. };
  36. struct sm_metadata {
  37. struct dm_space_map sm;
  38. struct ll_disk ll;
  39. struct ll_disk old_ll;
  40. dm_block_t begin;
  41. unsigned recursion_count;
  42. unsigned allocated_this_transaction;
  43. unsigned nr_uncommitted;
  44. struct block_op uncommitted[MAX_RECURSIVE_ALLOCATIONS];
  45. };
  46. static int add_bop(struct sm_metadata *smm, enum block_op_type type, dm_block_t b)
  47. {
  48. struct block_op *op;
  49. if (smm->nr_uncommitted == MAX_RECURSIVE_ALLOCATIONS) {
  50. DMERR("too many recursive allocations");
  51. return -ENOMEM;
  52. }
  53. op = smm->uncommitted + smm->nr_uncommitted++;
  54. op->type = type;
  55. op->block = b;
  56. return 0;
  57. }
  58. static int commit_bop(struct sm_metadata *smm, struct block_op *op)
  59. {
  60. int r = 0;
  61. enum allocation_event ev;
  62. switch (op->type) {
  63. case BOP_INC:
  64. r = sm_ll_inc(&smm->ll, op->block, &ev);
  65. break;
  66. case BOP_DEC:
  67. r = sm_ll_dec(&smm->ll, op->block, &ev);
  68. break;
  69. }
  70. return r;
  71. }
  72. static void in(struct sm_metadata *smm)
  73. {
  74. smm->recursion_count++;
  75. }
  76. static int out(struct sm_metadata *smm)
  77. {
  78. int r = 0;
  79. /*
  80. * If we're not recursing then very bad things are happening.
  81. */
  82. if (!smm->recursion_count) {
  83. DMERR("lost track of recursion depth");
  84. return -ENOMEM;
  85. }
  86. if (smm->recursion_count == 1 && smm->nr_uncommitted) {
  87. while (smm->nr_uncommitted && !r) {
  88. smm->nr_uncommitted--;
  89. r = commit_bop(smm, smm->uncommitted +
  90. smm->nr_uncommitted);
  91. if (r)
  92. break;
  93. }
  94. }
  95. smm->recursion_count--;
  96. return r;
  97. }
  98. /*
  99. * When using the out() function above, we often want to combine an error
  100. * code for the operation run in the recursive context with that from
  101. * out().
  102. */
  103. static int combine_errors(int r1, int r2)
  104. {
  105. return r1 ? r1 : r2;
  106. }
  107. static int recursing(struct sm_metadata *smm)
  108. {
  109. return smm->recursion_count;
  110. }
  111. static void sm_metadata_destroy(struct dm_space_map *sm)
  112. {
  113. struct sm_metadata *smm = container_of(sm, struct sm_metadata, sm);
  114. kfree(smm);
  115. }
  116. static int sm_metadata_extend(struct dm_space_map *sm, dm_block_t extra_blocks)
  117. {
  118. DMERR("doesn't support extend");
  119. return -EINVAL;
  120. }
  121. static int sm_metadata_get_nr_blocks(struct dm_space_map *sm, dm_block_t *count)
  122. {
  123. struct sm_metadata *smm = container_of(sm, struct sm_metadata, sm);
  124. *count = smm->ll.nr_blocks;
  125. return 0;
  126. }
  127. static int sm_metadata_get_nr_free(struct dm_space_map *sm, dm_block_t *count)
  128. {
  129. struct sm_metadata *smm = container_of(sm, struct sm_metadata, sm);
  130. *count = smm->old_ll.nr_blocks - smm->old_ll.nr_allocated -
  131. smm->allocated_this_transaction;
  132. return 0;
  133. }
  134. static int sm_metadata_get_count(struct dm_space_map *sm, dm_block_t b,
  135. uint32_t *result)
  136. {
  137. int r, i;
  138. struct sm_metadata *smm = container_of(sm, struct sm_metadata, sm);
  139. unsigned adjustment = 0;
  140. /*
  141. * We may have some uncommitted adjustments to add. This list
  142. * should always be really short.
  143. */
  144. for (i = 0; i < smm->nr_uncommitted; i++) {
  145. struct block_op *op = smm->uncommitted + i;
  146. if (op->block != b)
  147. continue;
  148. switch (op->type) {
  149. case BOP_INC:
  150. adjustment++;
  151. break;
  152. case BOP_DEC:
  153. adjustment--;
  154. break;
  155. }
  156. }
  157. r = sm_ll_lookup(&smm->ll, b, result);
  158. if (r)
  159. return r;
  160. *result += adjustment;
  161. return 0;
  162. }
  163. static int sm_metadata_count_is_more_than_one(struct dm_space_map *sm,
  164. dm_block_t b, int *result)
  165. {
  166. int r, i, adjustment = 0;
  167. struct sm_metadata *smm = container_of(sm, struct sm_metadata, sm);
  168. uint32_t rc;
  169. /*
  170. * We may have some uncommitted adjustments to add. This list
  171. * should always be really short.
  172. */
  173. for (i = 0; i < smm->nr_uncommitted; i++) {
  174. struct block_op *op = smm->uncommitted + i;
  175. if (op->block != b)
  176. continue;
  177. switch (op->type) {
  178. case BOP_INC:
  179. adjustment++;
  180. break;
  181. case BOP_DEC:
  182. adjustment--;
  183. break;
  184. }
  185. }
  186. if (adjustment > 1) {
  187. *result = 1;
  188. return 0;
  189. }
  190. r = sm_ll_lookup_bitmap(&smm->ll, b, &rc);
  191. if (r)
  192. return r;
  193. if (rc == 3)
  194. /*
  195. * We err on the side of caution, and always return true.
  196. */
  197. *result = 1;
  198. else
  199. *result = rc + adjustment > 1;
  200. return 0;
  201. }
  202. static int sm_metadata_set_count(struct dm_space_map *sm, dm_block_t b,
  203. uint32_t count)
  204. {
  205. int r, r2;
  206. enum allocation_event ev;
  207. struct sm_metadata *smm = container_of(sm, struct sm_metadata, sm);
  208. if (smm->recursion_count) {
  209. DMERR("cannot recurse set_count()");
  210. return -EINVAL;
  211. }
  212. in(smm);
  213. r = sm_ll_insert(&smm->ll, b, count, &ev);
  214. r2 = out(smm);
  215. return combine_errors(r, r2);
  216. }
  217. static int sm_metadata_inc_block(struct dm_space_map *sm, dm_block_t b)
  218. {
  219. int r, r2 = 0;
  220. enum allocation_event ev;
  221. struct sm_metadata *smm = container_of(sm, struct sm_metadata, sm);
  222. if (recursing(smm))
  223. r = add_bop(smm, BOP_INC, b);
  224. else {
  225. in(smm);
  226. r = sm_ll_inc(&smm->ll, b, &ev);
  227. r2 = out(smm);
  228. }
  229. return combine_errors(r, r2);
  230. }
  231. static int sm_metadata_dec_block(struct dm_space_map *sm, dm_block_t b)
  232. {
  233. int r, r2 = 0;
  234. enum allocation_event ev;
  235. struct sm_metadata *smm = container_of(sm, struct sm_metadata, sm);
  236. if (recursing(smm))
  237. r = add_bop(smm, BOP_DEC, b);
  238. else {
  239. in(smm);
  240. r = sm_ll_dec(&smm->ll, b, &ev);
  241. r2 = out(smm);
  242. }
  243. return combine_errors(r, r2);
  244. }
  245. static int sm_metadata_new_block_(struct dm_space_map *sm, dm_block_t *b)
  246. {
  247. int r, r2 = 0;
  248. enum allocation_event ev;
  249. struct sm_metadata *smm = container_of(sm, struct sm_metadata, sm);
  250. r = sm_ll_find_free_block(&smm->old_ll, smm->begin, smm->old_ll.nr_blocks, b);
  251. if (r)
  252. return r;
  253. smm->begin = *b + 1;
  254. if (recursing(smm))
  255. r = add_bop(smm, BOP_INC, *b);
  256. else {
  257. in(smm);
  258. r = sm_ll_inc(&smm->ll, *b, &ev);
  259. r2 = out(smm);
  260. }
  261. if (!r)
  262. smm->allocated_this_transaction++;
  263. return combine_errors(r, r2);
  264. }
  265. static int sm_metadata_new_block(struct dm_space_map *sm, dm_block_t *b)
  266. {
  267. int r = sm_metadata_new_block_(sm, b);
  268. if (r)
  269. DMERR("out of metadata space");
  270. return r;
  271. }
  272. static int sm_metadata_commit(struct dm_space_map *sm)
  273. {
  274. int r;
  275. struct sm_metadata *smm = container_of(sm, struct sm_metadata, sm);
  276. r = sm_ll_commit(&smm->ll);
  277. if (r)
  278. return r;
  279. memcpy(&smm->old_ll, &smm->ll, sizeof(smm->old_ll));
  280. smm->begin = 0;
  281. smm->allocated_this_transaction = 0;
  282. return 0;
  283. }
  284. static int sm_metadata_root_size(struct dm_space_map *sm, size_t *result)
  285. {
  286. *result = sizeof(struct disk_sm_root);
  287. return 0;
  288. }
  289. static int sm_metadata_copy_root(struct dm_space_map *sm, void *where_le, size_t max)
  290. {
  291. struct sm_metadata *smm = container_of(sm, struct sm_metadata, sm);
  292. struct disk_sm_root root_le;
  293. root_le.nr_blocks = cpu_to_le64(smm->ll.nr_blocks);
  294. root_le.nr_allocated = cpu_to_le64(smm->ll.nr_allocated);
  295. root_le.bitmap_root = cpu_to_le64(smm->ll.bitmap_root);
  296. root_le.ref_count_root = cpu_to_le64(smm->ll.ref_count_root);
  297. if (max < sizeof(root_le))
  298. return -ENOSPC;
  299. memcpy(where_le, &root_le, sizeof(root_le));
  300. return 0;
  301. }
  302. static struct dm_space_map ops = {
  303. .destroy = sm_metadata_destroy,
  304. .extend = sm_metadata_extend,
  305. .get_nr_blocks = sm_metadata_get_nr_blocks,
  306. .get_nr_free = sm_metadata_get_nr_free,
  307. .get_count = sm_metadata_get_count,
  308. .count_is_more_than_one = sm_metadata_count_is_more_than_one,
  309. .set_count = sm_metadata_set_count,
  310. .inc_block = sm_metadata_inc_block,
  311. .dec_block = sm_metadata_dec_block,
  312. .new_block = sm_metadata_new_block,
  313. .commit = sm_metadata_commit,
  314. .root_size = sm_metadata_root_size,
  315. .copy_root = sm_metadata_copy_root
  316. };
  317. /*----------------------------------------------------------------*/
  318. /*
  319. * When a new space map is created that manages its own space. We use
  320. * this tiny bootstrap allocator.
  321. */
  322. static void sm_bootstrap_destroy(struct dm_space_map *sm)
  323. {
  324. }
  325. static int sm_bootstrap_extend(struct dm_space_map *sm, dm_block_t extra_blocks)
  326. {
  327. DMERR("boostrap doesn't support extend");
  328. return -EINVAL;
  329. }
  330. static int sm_bootstrap_get_nr_blocks(struct dm_space_map *sm, dm_block_t *count)
  331. {
  332. struct sm_metadata *smm = container_of(sm, struct sm_metadata, sm);
  333. return smm->ll.nr_blocks;
  334. }
  335. static int sm_bootstrap_get_nr_free(struct dm_space_map *sm, dm_block_t *count)
  336. {
  337. struct sm_metadata *smm = container_of(sm, struct sm_metadata, sm);
  338. *count = smm->ll.nr_blocks - smm->begin;
  339. return 0;
  340. }
  341. static int sm_bootstrap_get_count(struct dm_space_map *sm, dm_block_t b,
  342. uint32_t *result)
  343. {
  344. struct sm_metadata *smm = container_of(sm, struct sm_metadata, sm);
  345. return b < smm->begin ? 1 : 0;
  346. }
  347. static int sm_bootstrap_count_is_more_than_one(struct dm_space_map *sm,
  348. dm_block_t b, int *result)
  349. {
  350. *result = 0;
  351. return 0;
  352. }
  353. static int sm_bootstrap_set_count(struct dm_space_map *sm, dm_block_t b,
  354. uint32_t count)
  355. {
  356. DMERR("boostrap doesn't support set_count");
  357. return -EINVAL;
  358. }
  359. static int sm_bootstrap_new_block(struct dm_space_map *sm, dm_block_t *b)
  360. {
  361. struct sm_metadata *smm = container_of(sm, struct sm_metadata, sm);
  362. /*
  363. * We know the entire device is unused.
  364. */
  365. if (smm->begin == smm->ll.nr_blocks)
  366. return -ENOSPC;
  367. *b = smm->begin++;
  368. return 0;
  369. }
  370. static int sm_bootstrap_inc_block(struct dm_space_map *sm, dm_block_t b)
  371. {
  372. struct sm_metadata *smm = container_of(sm, struct sm_metadata, sm);
  373. return add_bop(smm, BOP_INC, b);
  374. }
  375. static int sm_bootstrap_dec_block(struct dm_space_map *sm, dm_block_t b)
  376. {
  377. struct sm_metadata *smm = container_of(sm, struct sm_metadata, sm);
  378. return add_bop(smm, BOP_DEC, b);
  379. }
  380. static int sm_bootstrap_commit(struct dm_space_map *sm)
  381. {
  382. return 0;
  383. }
  384. static int sm_bootstrap_root_size(struct dm_space_map *sm, size_t *result)
  385. {
  386. DMERR("boostrap doesn't support root_size");
  387. return -EINVAL;
  388. }
  389. static int sm_bootstrap_copy_root(struct dm_space_map *sm, void *where,
  390. size_t max)
  391. {
  392. DMERR("boostrap doesn't support copy_root");
  393. return -EINVAL;
  394. }
  395. static struct dm_space_map bootstrap_ops = {
  396. .destroy = sm_bootstrap_destroy,
  397. .extend = sm_bootstrap_extend,
  398. .get_nr_blocks = sm_bootstrap_get_nr_blocks,
  399. .get_nr_free = sm_bootstrap_get_nr_free,
  400. .get_count = sm_bootstrap_get_count,
  401. .count_is_more_than_one = sm_bootstrap_count_is_more_than_one,
  402. .set_count = sm_bootstrap_set_count,
  403. .inc_block = sm_bootstrap_inc_block,
  404. .dec_block = sm_bootstrap_dec_block,
  405. .new_block = sm_bootstrap_new_block,
  406. .commit = sm_bootstrap_commit,
  407. .root_size = sm_bootstrap_root_size,
  408. .copy_root = sm_bootstrap_copy_root
  409. };
  410. /*----------------------------------------------------------------*/
  411. struct dm_space_map *dm_sm_metadata_init(void)
  412. {
  413. struct sm_metadata *smm;
  414. smm = kmalloc(sizeof(*smm), GFP_KERNEL);
  415. if (!smm)
  416. return ERR_PTR(-ENOMEM);
  417. memcpy(&smm->sm, &ops, sizeof(smm->sm));
  418. return &smm->sm;
  419. }
  420. int dm_sm_metadata_create(struct dm_space_map *sm,
  421. struct dm_transaction_manager *tm,
  422. dm_block_t nr_blocks,
  423. dm_block_t superblock)
  424. {
  425. int r;
  426. dm_block_t i;
  427. enum allocation_event ev;
  428. struct sm_metadata *smm = container_of(sm, struct sm_metadata, sm);
  429. smm->begin = superblock + 1;
  430. smm->recursion_count = 0;
  431. smm->allocated_this_transaction = 0;
  432. smm->nr_uncommitted = 0;
  433. memcpy(&smm->sm, &bootstrap_ops, sizeof(smm->sm));
  434. r = sm_ll_new_metadata(&smm->ll, tm);
  435. if (r)
  436. return r;
  437. r = sm_ll_extend(&smm->ll, nr_blocks);
  438. if (r)
  439. return r;
  440. memcpy(&smm->sm, &ops, sizeof(smm->sm));
  441. /*
  442. * Now we need to update the newly created data structures with the
  443. * allocated blocks that they were built from.
  444. */
  445. for (i = superblock; !r && i < smm->begin; i++)
  446. r = sm_ll_inc(&smm->ll, i, &ev);
  447. if (r)
  448. return r;
  449. return sm_metadata_commit(sm);
  450. }
  451. int dm_sm_metadata_open(struct dm_space_map *sm,
  452. struct dm_transaction_manager *tm,
  453. void *root_le, size_t len)
  454. {
  455. int r;
  456. struct sm_metadata *smm = container_of(sm, struct sm_metadata, sm);
  457. r = sm_ll_open_metadata(&smm->ll, tm, root_le, len);
  458. if (r)
  459. return r;
  460. smm->begin = 0;
  461. smm->recursion_count = 0;
  462. smm->allocated_this_transaction = 0;
  463. smm->nr_uncommitted = 0;
  464. memcpy(&smm->old_ll, &smm->ll, sizeof(smm->old_ll));
  465. return 0;
  466. }