dm-space-map-metadata.c 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622
  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_get_nr_blocks(struct dm_space_map *sm, dm_block_t *count)
  117. {
  118. struct sm_metadata *smm = container_of(sm, struct sm_metadata, sm);
  119. *count = smm->ll.nr_blocks;
  120. return 0;
  121. }
  122. static int sm_metadata_get_nr_free(struct dm_space_map *sm, dm_block_t *count)
  123. {
  124. struct sm_metadata *smm = container_of(sm, struct sm_metadata, sm);
  125. *count = smm->old_ll.nr_blocks - smm->old_ll.nr_allocated -
  126. smm->allocated_this_transaction;
  127. return 0;
  128. }
  129. static int sm_metadata_get_count(struct dm_space_map *sm, dm_block_t b,
  130. uint32_t *result)
  131. {
  132. int r, i;
  133. struct sm_metadata *smm = container_of(sm, struct sm_metadata, sm);
  134. unsigned adjustment = 0;
  135. /*
  136. * We may have some uncommitted adjustments to add. This list
  137. * should always be really short.
  138. */
  139. for (i = 0; i < smm->nr_uncommitted; i++) {
  140. struct block_op *op = smm->uncommitted + i;
  141. if (op->block != b)
  142. continue;
  143. switch (op->type) {
  144. case BOP_INC:
  145. adjustment++;
  146. break;
  147. case BOP_DEC:
  148. adjustment--;
  149. break;
  150. }
  151. }
  152. r = sm_ll_lookup(&smm->ll, b, result);
  153. if (r)
  154. return r;
  155. *result += adjustment;
  156. return 0;
  157. }
  158. static int sm_metadata_count_is_more_than_one(struct dm_space_map *sm,
  159. dm_block_t b, int *result)
  160. {
  161. int r, i, adjustment = 0;
  162. struct sm_metadata *smm = container_of(sm, struct sm_metadata, sm);
  163. uint32_t rc;
  164. /*
  165. * We may have some uncommitted adjustments to add. This list
  166. * should always be really short.
  167. */
  168. for (i = 0; i < smm->nr_uncommitted; i++) {
  169. struct block_op *op = smm->uncommitted + i;
  170. if (op->block != b)
  171. continue;
  172. switch (op->type) {
  173. case BOP_INC:
  174. adjustment++;
  175. break;
  176. case BOP_DEC:
  177. adjustment--;
  178. break;
  179. }
  180. }
  181. if (adjustment > 1) {
  182. *result = 1;
  183. return 0;
  184. }
  185. r = sm_ll_lookup_bitmap(&smm->ll, b, &rc);
  186. if (r)
  187. return r;
  188. if (rc == 3)
  189. /*
  190. * We err on the side of caution, and always return true.
  191. */
  192. *result = 1;
  193. else
  194. *result = rc + adjustment > 1;
  195. return 0;
  196. }
  197. static int sm_metadata_set_count(struct dm_space_map *sm, dm_block_t b,
  198. uint32_t count)
  199. {
  200. int r, r2;
  201. enum allocation_event ev;
  202. struct sm_metadata *smm = container_of(sm, struct sm_metadata, sm);
  203. if (smm->recursion_count) {
  204. DMERR("cannot recurse set_count()");
  205. return -EINVAL;
  206. }
  207. in(smm);
  208. r = sm_ll_insert(&smm->ll, b, count, &ev);
  209. r2 = out(smm);
  210. return combine_errors(r, r2);
  211. }
  212. static int sm_metadata_inc_block(struct dm_space_map *sm, dm_block_t b)
  213. {
  214. int r, r2 = 0;
  215. enum allocation_event ev;
  216. struct sm_metadata *smm = container_of(sm, struct sm_metadata, sm);
  217. if (recursing(smm))
  218. r = add_bop(smm, BOP_INC, b);
  219. else {
  220. in(smm);
  221. r = sm_ll_inc(&smm->ll, b, &ev);
  222. r2 = out(smm);
  223. }
  224. return combine_errors(r, r2);
  225. }
  226. static int sm_metadata_dec_block(struct dm_space_map *sm, dm_block_t b)
  227. {
  228. int r, r2 = 0;
  229. enum allocation_event ev;
  230. struct sm_metadata *smm = container_of(sm, struct sm_metadata, sm);
  231. if (recursing(smm))
  232. r = add_bop(smm, BOP_DEC, b);
  233. else {
  234. in(smm);
  235. r = sm_ll_dec(&smm->ll, b, &ev);
  236. r2 = out(smm);
  237. }
  238. return combine_errors(r, r2);
  239. }
  240. static int sm_metadata_new_block_(struct dm_space_map *sm, dm_block_t *b)
  241. {
  242. int r, r2 = 0;
  243. enum allocation_event ev;
  244. struct sm_metadata *smm = container_of(sm, struct sm_metadata, sm);
  245. r = sm_ll_find_free_block(&smm->old_ll, smm->begin, smm->old_ll.nr_blocks, b);
  246. if (r)
  247. return r;
  248. smm->begin = *b + 1;
  249. if (recursing(smm))
  250. r = add_bop(smm, BOP_INC, *b);
  251. else {
  252. in(smm);
  253. r = sm_ll_inc(&smm->ll, *b, &ev);
  254. r2 = out(smm);
  255. }
  256. if (!r)
  257. smm->allocated_this_transaction++;
  258. return combine_errors(r, r2);
  259. }
  260. static int sm_metadata_new_block(struct dm_space_map *sm, dm_block_t *b)
  261. {
  262. int r = sm_metadata_new_block_(sm, b);
  263. if (r)
  264. DMERR("unable to allocate new metadata block");
  265. return r;
  266. }
  267. static int sm_metadata_commit(struct dm_space_map *sm)
  268. {
  269. int r;
  270. struct sm_metadata *smm = container_of(sm, struct sm_metadata, sm);
  271. r = sm_ll_commit(&smm->ll);
  272. if (r)
  273. return r;
  274. memcpy(&smm->old_ll, &smm->ll, sizeof(smm->old_ll));
  275. smm->begin = 0;
  276. smm->allocated_this_transaction = 0;
  277. return 0;
  278. }
  279. static int sm_metadata_root_size(struct dm_space_map *sm, size_t *result)
  280. {
  281. *result = sizeof(struct disk_sm_root);
  282. return 0;
  283. }
  284. static int sm_metadata_copy_root(struct dm_space_map *sm, void *where_le, size_t max)
  285. {
  286. struct sm_metadata *smm = container_of(sm, struct sm_metadata, sm);
  287. struct disk_sm_root root_le;
  288. root_le.nr_blocks = cpu_to_le64(smm->ll.nr_blocks);
  289. root_le.nr_allocated = cpu_to_le64(smm->ll.nr_allocated);
  290. root_le.bitmap_root = cpu_to_le64(smm->ll.bitmap_root);
  291. root_le.ref_count_root = cpu_to_le64(smm->ll.ref_count_root);
  292. if (max < sizeof(root_le))
  293. return -ENOSPC;
  294. memcpy(where_le, &root_le, sizeof(root_le));
  295. return 0;
  296. }
  297. static int sm_metadata_extend(struct dm_space_map *sm, dm_block_t extra_blocks);
  298. static struct dm_space_map ops = {
  299. .destroy = sm_metadata_destroy,
  300. .extend = sm_metadata_extend,
  301. .get_nr_blocks = sm_metadata_get_nr_blocks,
  302. .get_nr_free = sm_metadata_get_nr_free,
  303. .get_count = sm_metadata_get_count,
  304. .count_is_more_than_one = sm_metadata_count_is_more_than_one,
  305. .set_count = sm_metadata_set_count,
  306. .inc_block = sm_metadata_inc_block,
  307. .dec_block = sm_metadata_dec_block,
  308. .new_block = sm_metadata_new_block,
  309. .commit = sm_metadata_commit,
  310. .root_size = sm_metadata_root_size,
  311. .copy_root = sm_metadata_copy_root
  312. };
  313. /*----------------------------------------------------------------*/
  314. /*
  315. * When a new space map is created that manages its own space. We use
  316. * this tiny bootstrap allocator.
  317. */
  318. static void sm_bootstrap_destroy(struct dm_space_map *sm)
  319. {
  320. }
  321. static int sm_bootstrap_extend(struct dm_space_map *sm, dm_block_t extra_blocks)
  322. {
  323. DMERR("bootstrap doesn't support extend");
  324. return -EINVAL;
  325. }
  326. static int sm_bootstrap_get_nr_blocks(struct dm_space_map *sm, dm_block_t *count)
  327. {
  328. struct sm_metadata *smm = container_of(sm, struct sm_metadata, sm);
  329. return smm->ll.nr_blocks;
  330. }
  331. static int sm_bootstrap_get_nr_free(struct dm_space_map *sm, dm_block_t *count)
  332. {
  333. struct sm_metadata *smm = container_of(sm, struct sm_metadata, sm);
  334. *count = smm->ll.nr_blocks - smm->begin;
  335. return 0;
  336. }
  337. static int sm_bootstrap_get_count(struct dm_space_map *sm, dm_block_t b,
  338. uint32_t *result)
  339. {
  340. struct sm_metadata *smm = container_of(sm, struct sm_metadata, sm);
  341. return b < smm->begin ? 1 : 0;
  342. }
  343. static int sm_bootstrap_count_is_more_than_one(struct dm_space_map *sm,
  344. dm_block_t b, int *result)
  345. {
  346. *result = 0;
  347. return 0;
  348. }
  349. static int sm_bootstrap_set_count(struct dm_space_map *sm, dm_block_t b,
  350. uint32_t count)
  351. {
  352. DMERR("bootstrap doesn't support set_count");
  353. return -EINVAL;
  354. }
  355. static int sm_bootstrap_new_block(struct dm_space_map *sm, dm_block_t *b)
  356. {
  357. struct sm_metadata *smm = container_of(sm, struct sm_metadata, sm);
  358. /*
  359. * We know the entire device is unused.
  360. */
  361. if (smm->begin == smm->ll.nr_blocks)
  362. return -ENOSPC;
  363. *b = smm->begin++;
  364. return 0;
  365. }
  366. static int sm_bootstrap_inc_block(struct dm_space_map *sm, dm_block_t b)
  367. {
  368. struct sm_metadata *smm = container_of(sm, struct sm_metadata, sm);
  369. return add_bop(smm, BOP_INC, b);
  370. }
  371. static int sm_bootstrap_dec_block(struct dm_space_map *sm, dm_block_t b)
  372. {
  373. struct sm_metadata *smm = container_of(sm, struct sm_metadata, sm);
  374. return add_bop(smm, BOP_DEC, b);
  375. }
  376. static int sm_bootstrap_commit(struct dm_space_map *sm)
  377. {
  378. return 0;
  379. }
  380. static int sm_bootstrap_root_size(struct dm_space_map *sm, size_t *result)
  381. {
  382. DMERR("bootstrap doesn't support root_size");
  383. return -EINVAL;
  384. }
  385. static int sm_bootstrap_copy_root(struct dm_space_map *sm, void *where,
  386. size_t max)
  387. {
  388. DMERR("bootstrap doesn't support copy_root");
  389. return -EINVAL;
  390. }
  391. static struct dm_space_map bootstrap_ops = {
  392. .destroy = sm_bootstrap_destroy,
  393. .extend = sm_bootstrap_extend,
  394. .get_nr_blocks = sm_bootstrap_get_nr_blocks,
  395. .get_nr_free = sm_bootstrap_get_nr_free,
  396. .get_count = sm_bootstrap_get_count,
  397. .count_is_more_than_one = sm_bootstrap_count_is_more_than_one,
  398. .set_count = sm_bootstrap_set_count,
  399. .inc_block = sm_bootstrap_inc_block,
  400. .dec_block = sm_bootstrap_dec_block,
  401. .new_block = sm_bootstrap_new_block,
  402. .commit = sm_bootstrap_commit,
  403. .root_size = sm_bootstrap_root_size,
  404. .copy_root = sm_bootstrap_copy_root
  405. };
  406. /*----------------------------------------------------------------*/
  407. static int sm_metadata_extend(struct dm_space_map *sm, dm_block_t extra_blocks)
  408. {
  409. int r, i;
  410. enum allocation_event ev;
  411. struct sm_metadata *smm = container_of(sm, struct sm_metadata, sm);
  412. dm_block_t old_len = smm->ll.nr_blocks;
  413. /*
  414. * Flick into a mode where all blocks get allocated in the new area.
  415. */
  416. smm->begin = old_len;
  417. memcpy(&smm->sm, &bootstrap_ops, sizeof(smm->sm));
  418. /*
  419. * Extend.
  420. */
  421. r = sm_ll_extend(&smm->ll, extra_blocks);
  422. /*
  423. * Switch back to normal behaviour.
  424. */
  425. memcpy(&smm->sm, &ops, sizeof(smm->sm));
  426. for (i = old_len; !r && i < smm->begin; i++)
  427. r = sm_ll_inc(&smm->ll, i, &ev);
  428. return r;
  429. }
  430. /*----------------------------------------------------------------*/
  431. struct dm_space_map *dm_sm_metadata_init(void)
  432. {
  433. struct sm_metadata *smm;
  434. smm = kmalloc(sizeof(*smm), GFP_KERNEL);
  435. if (!smm)
  436. return ERR_PTR(-ENOMEM);
  437. memcpy(&smm->sm, &ops, sizeof(smm->sm));
  438. return &smm->sm;
  439. }
  440. int dm_sm_metadata_create(struct dm_space_map *sm,
  441. struct dm_transaction_manager *tm,
  442. dm_block_t nr_blocks,
  443. dm_block_t superblock)
  444. {
  445. int r;
  446. dm_block_t i;
  447. enum allocation_event ev;
  448. struct sm_metadata *smm = container_of(sm, struct sm_metadata, sm);
  449. smm->begin = superblock + 1;
  450. smm->recursion_count = 0;
  451. smm->allocated_this_transaction = 0;
  452. smm->nr_uncommitted = 0;
  453. memcpy(&smm->sm, &bootstrap_ops, sizeof(smm->sm));
  454. r = sm_ll_new_metadata(&smm->ll, tm);
  455. if (r)
  456. return r;
  457. r = sm_ll_extend(&smm->ll, nr_blocks);
  458. if (r)
  459. return r;
  460. memcpy(&smm->sm, &ops, sizeof(smm->sm));
  461. /*
  462. * Now we need to update the newly created data structures with the
  463. * allocated blocks that they were built from.
  464. */
  465. for (i = superblock; !r && i < smm->begin; i++)
  466. r = sm_ll_inc(&smm->ll, i, &ev);
  467. if (r)
  468. return r;
  469. return sm_metadata_commit(sm);
  470. }
  471. int dm_sm_metadata_open(struct dm_space_map *sm,
  472. struct dm_transaction_manager *tm,
  473. void *root_le, size_t len)
  474. {
  475. int r;
  476. struct sm_metadata *smm = container_of(sm, struct sm_metadata, sm);
  477. r = sm_ll_open_metadata(&smm->ll, tm, root_le, len);
  478. if (r)
  479. return r;
  480. smm->begin = 0;
  481. smm->recursion_count = 0;
  482. smm->allocated_this_transaction = 0;
  483. smm->nr_uncommitted = 0;
  484. memcpy(&smm->old_ll, &smm->ll, sizeof(smm->old_ll));
  485. return 0;
  486. }