dm-space-map-metadata.c 15 KB

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