dm-space-map-metadata.c 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818
  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 bop_ring_buffer {
  78. unsigned begin;
  79. unsigned end;
  80. struct block_op bops[MAX_RECURSIVE_ALLOCATIONS + 1];
  81. };
  82. static void brb_init(struct bop_ring_buffer *brb)
  83. {
  84. brb->begin = 0;
  85. brb->end = 0;
  86. }
  87. static bool brb_empty(struct bop_ring_buffer *brb)
  88. {
  89. return brb->begin == brb->end;
  90. }
  91. static unsigned brb_next(struct bop_ring_buffer *brb, unsigned old)
  92. {
  93. unsigned r = old + 1;
  94. return (r >= (sizeof(brb->bops) / sizeof(*brb->bops))) ? 0 : r;
  95. }
  96. static int brb_push(struct bop_ring_buffer *brb,
  97. enum block_op_type type, dm_block_t b)
  98. {
  99. struct block_op *bop;
  100. unsigned next = brb_next(brb, brb->end);
  101. /*
  102. * We don't allow the last bop to be filled, this way we can
  103. * differentiate between full and empty.
  104. */
  105. if (next == brb->begin)
  106. return -ENOMEM;
  107. bop = brb->bops + brb->end;
  108. bop->type = type;
  109. bop->block = b;
  110. brb->end = next;
  111. return 0;
  112. }
  113. static int brb_pop(struct bop_ring_buffer *brb, struct block_op *result)
  114. {
  115. struct block_op *bop;
  116. if (brb_empty(brb))
  117. return -ENODATA;
  118. bop = brb->bops + brb->begin;
  119. result->type = bop->type;
  120. result->block = bop->block;
  121. brb->begin = brb_next(brb, brb->begin);
  122. return 0;
  123. }
  124. /*----------------------------------------------------------------*/
  125. struct sm_metadata {
  126. struct dm_space_map sm;
  127. struct ll_disk ll;
  128. struct ll_disk old_ll;
  129. dm_block_t begin;
  130. unsigned recursion_count;
  131. unsigned allocated_this_transaction;
  132. struct bop_ring_buffer uncommitted;
  133. struct threshold threshold;
  134. };
  135. static int add_bop(struct sm_metadata *smm, enum block_op_type type, dm_block_t b)
  136. {
  137. int r = brb_push(&smm->uncommitted, type, b);
  138. if (r) {
  139. DMERR("too many recursive allocations");
  140. return -ENOMEM;
  141. }
  142. return 0;
  143. }
  144. static int commit_bop(struct sm_metadata *smm, struct block_op *op)
  145. {
  146. int r = 0;
  147. enum allocation_event ev;
  148. switch (op->type) {
  149. case BOP_INC:
  150. r = sm_ll_inc(&smm->ll, op->block, &ev);
  151. break;
  152. case BOP_DEC:
  153. r = sm_ll_dec(&smm->ll, op->block, &ev);
  154. break;
  155. }
  156. return r;
  157. }
  158. static void in(struct sm_metadata *smm)
  159. {
  160. smm->recursion_count++;
  161. }
  162. static int apply_bops(struct sm_metadata *smm)
  163. {
  164. int r = 0;
  165. while (!brb_empty(&smm->uncommitted)) {
  166. struct block_op bop;
  167. r = brb_pop(&smm->uncommitted, &bop);
  168. if (r) {
  169. DMERR("bug in bop ring buffer");
  170. break;
  171. }
  172. r = commit_bop(smm, &bop);
  173. if (r)
  174. break;
  175. }
  176. return r;
  177. }
  178. static int out(struct sm_metadata *smm)
  179. {
  180. int r = 0;
  181. /*
  182. * If we're not recursing then very bad things are happening.
  183. */
  184. if (!smm->recursion_count) {
  185. DMERR("lost track of recursion depth");
  186. return -ENOMEM;
  187. }
  188. if (smm->recursion_count == 1)
  189. apply_bops(smm);
  190. smm->recursion_count--;
  191. return r;
  192. }
  193. /*
  194. * When using the out() function above, we often want to combine an error
  195. * code for the operation run in the recursive context with that from
  196. * out().
  197. */
  198. static int combine_errors(int r1, int r2)
  199. {
  200. return r1 ? r1 : r2;
  201. }
  202. static int recursing(struct sm_metadata *smm)
  203. {
  204. return smm->recursion_count;
  205. }
  206. static void sm_metadata_destroy(struct dm_space_map *sm)
  207. {
  208. struct sm_metadata *smm = container_of(sm, struct sm_metadata, sm);
  209. kfree(smm);
  210. }
  211. static int sm_metadata_get_nr_blocks(struct dm_space_map *sm, dm_block_t *count)
  212. {
  213. struct sm_metadata *smm = container_of(sm, struct sm_metadata, sm);
  214. *count = smm->ll.nr_blocks;
  215. return 0;
  216. }
  217. static int sm_metadata_get_nr_free(struct dm_space_map *sm, dm_block_t *count)
  218. {
  219. struct sm_metadata *smm = container_of(sm, struct sm_metadata, sm);
  220. *count = smm->old_ll.nr_blocks - smm->old_ll.nr_allocated -
  221. smm->allocated_this_transaction;
  222. return 0;
  223. }
  224. static int sm_metadata_get_count(struct dm_space_map *sm, dm_block_t b,
  225. uint32_t *result)
  226. {
  227. int r;
  228. unsigned i;
  229. struct sm_metadata *smm = container_of(sm, struct sm_metadata, sm);
  230. unsigned adjustment = 0;
  231. /*
  232. * We may have some uncommitted adjustments to add. This list
  233. * should always be really short.
  234. */
  235. for (i = smm->uncommitted.begin;
  236. i != smm->uncommitted.end;
  237. i = brb_next(&smm->uncommitted, i)) {
  238. struct block_op *op = smm->uncommitted.bops + i;
  239. if (op->block != b)
  240. continue;
  241. switch (op->type) {
  242. case BOP_INC:
  243. adjustment++;
  244. break;
  245. case BOP_DEC:
  246. adjustment--;
  247. break;
  248. }
  249. }
  250. r = sm_ll_lookup(&smm->ll, b, result);
  251. if (r)
  252. return r;
  253. *result += adjustment;
  254. return 0;
  255. }
  256. static int sm_metadata_count_is_more_than_one(struct dm_space_map *sm,
  257. dm_block_t b, int *result)
  258. {
  259. int r, adjustment = 0;
  260. unsigned i;
  261. struct sm_metadata *smm = container_of(sm, struct sm_metadata, sm);
  262. uint32_t rc;
  263. /*
  264. * We may have some uncommitted adjustments to add. This list
  265. * should always be really short.
  266. */
  267. for (i = smm->uncommitted.begin;
  268. i != smm->uncommitted.end;
  269. i = brb_next(&smm->uncommitted, i)) {
  270. struct block_op *op = smm->uncommitted.bops + i;
  271. if (op->block != b)
  272. continue;
  273. switch (op->type) {
  274. case BOP_INC:
  275. adjustment++;
  276. break;
  277. case BOP_DEC:
  278. adjustment--;
  279. break;
  280. }
  281. }
  282. if (adjustment > 1) {
  283. *result = 1;
  284. return 0;
  285. }
  286. r = sm_ll_lookup_bitmap(&smm->ll, b, &rc);
  287. if (r)
  288. return r;
  289. if (rc == 3)
  290. /*
  291. * We err on the side of caution, and always return true.
  292. */
  293. *result = 1;
  294. else
  295. *result = rc + adjustment > 1;
  296. return 0;
  297. }
  298. static int sm_metadata_set_count(struct dm_space_map *sm, dm_block_t b,
  299. uint32_t count)
  300. {
  301. int r, r2;
  302. enum allocation_event ev;
  303. struct sm_metadata *smm = container_of(sm, struct sm_metadata, sm);
  304. if (smm->recursion_count) {
  305. DMERR("cannot recurse set_count()");
  306. return -EINVAL;
  307. }
  308. in(smm);
  309. r = sm_ll_insert(&smm->ll, b, count, &ev);
  310. r2 = out(smm);
  311. return combine_errors(r, r2);
  312. }
  313. static int sm_metadata_inc_block(struct dm_space_map *sm, dm_block_t b)
  314. {
  315. int r, r2 = 0;
  316. enum allocation_event ev;
  317. struct sm_metadata *smm = container_of(sm, struct sm_metadata, sm);
  318. if (recursing(smm))
  319. r = add_bop(smm, BOP_INC, b);
  320. else {
  321. in(smm);
  322. r = sm_ll_inc(&smm->ll, b, &ev);
  323. r2 = out(smm);
  324. }
  325. return combine_errors(r, r2);
  326. }
  327. static int sm_metadata_dec_block(struct dm_space_map *sm, dm_block_t b)
  328. {
  329. int r, r2 = 0;
  330. enum allocation_event ev;
  331. struct sm_metadata *smm = container_of(sm, struct sm_metadata, sm);
  332. if (recursing(smm))
  333. r = add_bop(smm, BOP_DEC, b);
  334. else {
  335. in(smm);
  336. r = sm_ll_dec(&smm->ll, b, &ev);
  337. r2 = out(smm);
  338. }
  339. return combine_errors(r, r2);
  340. }
  341. static int sm_metadata_new_block_(struct dm_space_map *sm, dm_block_t *b)
  342. {
  343. int r, r2 = 0;
  344. enum allocation_event ev;
  345. struct sm_metadata *smm = container_of(sm, struct sm_metadata, sm);
  346. r = sm_ll_find_free_block(&smm->old_ll, smm->begin, smm->old_ll.nr_blocks, b);
  347. if (r)
  348. return r;
  349. smm->begin = *b + 1;
  350. if (recursing(smm))
  351. r = add_bop(smm, BOP_INC, *b);
  352. else {
  353. in(smm);
  354. r = sm_ll_inc(&smm->ll, *b, &ev);
  355. r2 = out(smm);
  356. }
  357. if (!r)
  358. smm->allocated_this_transaction++;
  359. return combine_errors(r, r2);
  360. }
  361. static int sm_metadata_new_block(struct dm_space_map *sm, dm_block_t *b)
  362. {
  363. dm_block_t count;
  364. struct sm_metadata *smm = container_of(sm, struct sm_metadata, sm);
  365. int r = sm_metadata_new_block_(sm, b);
  366. if (r) {
  367. DMERR_LIMIT("unable to allocate new metadata block");
  368. return r;
  369. }
  370. r = sm_metadata_get_nr_free(sm, &count);
  371. if (r) {
  372. DMERR_LIMIT("couldn't get free block count");
  373. return r;
  374. }
  375. check_threshold(&smm->threshold, count);
  376. return r;
  377. }
  378. static int sm_metadata_commit(struct dm_space_map *sm)
  379. {
  380. int r;
  381. struct sm_metadata *smm = container_of(sm, struct sm_metadata, sm);
  382. r = sm_ll_commit(&smm->ll);
  383. if (r)
  384. return r;
  385. memcpy(&smm->old_ll, &smm->ll, sizeof(smm->old_ll));
  386. smm->begin = 0;
  387. smm->allocated_this_transaction = 0;
  388. return 0;
  389. }
  390. static int sm_metadata_register_threshold_callback(struct dm_space_map *sm,
  391. dm_block_t threshold,
  392. dm_sm_threshold_fn fn,
  393. void *context)
  394. {
  395. struct sm_metadata *smm = container_of(sm, struct sm_metadata, sm);
  396. set_threshold(&smm->threshold, threshold, fn, context);
  397. return 0;
  398. }
  399. static int sm_metadata_root_size(struct dm_space_map *sm, size_t *result)
  400. {
  401. *result = sizeof(struct disk_sm_root);
  402. return 0;
  403. }
  404. static int sm_metadata_copy_root(struct dm_space_map *sm, void *where_le, size_t max)
  405. {
  406. struct sm_metadata *smm = container_of(sm, struct sm_metadata, sm);
  407. struct disk_sm_root root_le;
  408. root_le.nr_blocks = cpu_to_le64(smm->ll.nr_blocks);
  409. root_le.nr_allocated = cpu_to_le64(smm->ll.nr_allocated);
  410. root_le.bitmap_root = cpu_to_le64(smm->ll.bitmap_root);
  411. root_le.ref_count_root = cpu_to_le64(smm->ll.ref_count_root);
  412. if (max < sizeof(root_le))
  413. return -ENOSPC;
  414. memcpy(where_le, &root_le, sizeof(root_le));
  415. return 0;
  416. }
  417. static int sm_metadata_extend(struct dm_space_map *sm, dm_block_t extra_blocks);
  418. static struct dm_space_map ops = {
  419. .destroy = sm_metadata_destroy,
  420. .extend = sm_metadata_extend,
  421. .get_nr_blocks = sm_metadata_get_nr_blocks,
  422. .get_nr_free = sm_metadata_get_nr_free,
  423. .get_count = sm_metadata_get_count,
  424. .count_is_more_than_one = sm_metadata_count_is_more_than_one,
  425. .set_count = sm_metadata_set_count,
  426. .inc_block = sm_metadata_inc_block,
  427. .dec_block = sm_metadata_dec_block,
  428. .new_block = sm_metadata_new_block,
  429. .commit = sm_metadata_commit,
  430. .root_size = sm_metadata_root_size,
  431. .copy_root = sm_metadata_copy_root,
  432. .register_threshold_callback = sm_metadata_register_threshold_callback
  433. };
  434. /*----------------------------------------------------------------*/
  435. /*
  436. * When a new space map is created that manages its own space. We use
  437. * this tiny bootstrap allocator.
  438. */
  439. static void sm_bootstrap_destroy(struct dm_space_map *sm)
  440. {
  441. }
  442. static int sm_bootstrap_extend(struct dm_space_map *sm, dm_block_t extra_blocks)
  443. {
  444. DMERR("bootstrap doesn't support extend");
  445. return -EINVAL;
  446. }
  447. static int sm_bootstrap_get_nr_blocks(struct dm_space_map *sm, dm_block_t *count)
  448. {
  449. struct sm_metadata *smm = container_of(sm, struct sm_metadata, sm);
  450. *count = smm->ll.nr_blocks;
  451. return 0;
  452. }
  453. static int sm_bootstrap_get_nr_free(struct dm_space_map *sm, dm_block_t *count)
  454. {
  455. struct sm_metadata *smm = container_of(sm, struct sm_metadata, sm);
  456. *count = smm->ll.nr_blocks - smm->begin;
  457. return 0;
  458. }
  459. static int sm_bootstrap_get_count(struct dm_space_map *sm, dm_block_t b,
  460. uint32_t *result)
  461. {
  462. struct sm_metadata *smm = container_of(sm, struct sm_metadata, sm);
  463. *result = (b < smm->begin) ? 1 : 0;
  464. return 0;
  465. }
  466. static int sm_bootstrap_count_is_more_than_one(struct dm_space_map *sm,
  467. dm_block_t b, int *result)
  468. {
  469. *result = 0;
  470. return 0;
  471. }
  472. static int sm_bootstrap_set_count(struct dm_space_map *sm, dm_block_t b,
  473. uint32_t count)
  474. {
  475. DMERR("bootstrap doesn't support set_count");
  476. return -EINVAL;
  477. }
  478. static int sm_bootstrap_new_block(struct dm_space_map *sm, dm_block_t *b)
  479. {
  480. struct sm_metadata *smm = container_of(sm, struct sm_metadata, sm);
  481. /*
  482. * We know the entire device is unused.
  483. */
  484. if (smm->begin == smm->ll.nr_blocks)
  485. return -ENOSPC;
  486. *b = smm->begin++;
  487. return 0;
  488. }
  489. static int sm_bootstrap_inc_block(struct dm_space_map *sm, dm_block_t b)
  490. {
  491. struct sm_metadata *smm = container_of(sm, struct sm_metadata, sm);
  492. return add_bop(smm, BOP_INC, b);
  493. }
  494. static int sm_bootstrap_dec_block(struct dm_space_map *sm, dm_block_t b)
  495. {
  496. struct sm_metadata *smm = container_of(sm, struct sm_metadata, sm);
  497. return add_bop(smm, BOP_DEC, b);
  498. }
  499. static int sm_bootstrap_commit(struct dm_space_map *sm)
  500. {
  501. return 0;
  502. }
  503. static int sm_bootstrap_root_size(struct dm_space_map *sm, size_t *result)
  504. {
  505. DMERR("bootstrap doesn't support root_size");
  506. return -EINVAL;
  507. }
  508. static int sm_bootstrap_copy_root(struct dm_space_map *sm, void *where,
  509. size_t max)
  510. {
  511. DMERR("bootstrap doesn't support copy_root");
  512. return -EINVAL;
  513. }
  514. static struct dm_space_map bootstrap_ops = {
  515. .destroy = sm_bootstrap_destroy,
  516. .extend = sm_bootstrap_extend,
  517. .get_nr_blocks = sm_bootstrap_get_nr_blocks,
  518. .get_nr_free = sm_bootstrap_get_nr_free,
  519. .get_count = sm_bootstrap_get_count,
  520. .count_is_more_than_one = sm_bootstrap_count_is_more_than_one,
  521. .set_count = sm_bootstrap_set_count,
  522. .inc_block = sm_bootstrap_inc_block,
  523. .dec_block = sm_bootstrap_dec_block,
  524. .new_block = sm_bootstrap_new_block,
  525. .commit = sm_bootstrap_commit,
  526. .root_size = sm_bootstrap_root_size,
  527. .copy_root = sm_bootstrap_copy_root,
  528. .register_threshold_callback = NULL
  529. };
  530. /*----------------------------------------------------------------*/
  531. static int sm_metadata_extend(struct dm_space_map *sm, dm_block_t extra_blocks)
  532. {
  533. int r, i;
  534. enum allocation_event ev;
  535. struct sm_metadata *smm = container_of(sm, struct sm_metadata, sm);
  536. dm_block_t old_len = smm->ll.nr_blocks;
  537. /*
  538. * Flick into a mode where all blocks get allocated in the new area.
  539. */
  540. smm->begin = old_len;
  541. memcpy(sm, &bootstrap_ops, sizeof(*sm));
  542. /*
  543. * Extend.
  544. */
  545. r = sm_ll_extend(&smm->ll, extra_blocks);
  546. if (r)
  547. goto out;
  548. /*
  549. * We repeatedly increment then commit until the commit doesn't
  550. * allocate any new blocks.
  551. */
  552. do {
  553. for (i = old_len; !r && i < smm->begin; i++) {
  554. r = sm_ll_inc(&smm->ll, i, &ev);
  555. if (r)
  556. goto out;
  557. }
  558. old_len = smm->begin;
  559. r = apply_bops(smm);
  560. if (r) {
  561. DMERR("%s: apply_bops failed", __func__);
  562. goto out;
  563. }
  564. r = sm_ll_commit(&smm->ll);
  565. if (r)
  566. goto out;
  567. } while (old_len != smm->begin);
  568. out:
  569. /*
  570. * Switch back to normal behaviour.
  571. */
  572. memcpy(sm, &ops, sizeof(*sm));
  573. return r;
  574. }
  575. /*----------------------------------------------------------------*/
  576. struct dm_space_map *dm_sm_metadata_init(void)
  577. {
  578. struct sm_metadata *smm;
  579. smm = kmalloc(sizeof(*smm), GFP_KERNEL);
  580. if (!smm)
  581. return ERR_PTR(-ENOMEM);
  582. memcpy(&smm->sm, &ops, sizeof(smm->sm));
  583. return &smm->sm;
  584. }
  585. int dm_sm_metadata_create(struct dm_space_map *sm,
  586. struct dm_transaction_manager *tm,
  587. dm_block_t nr_blocks,
  588. dm_block_t superblock)
  589. {
  590. int r;
  591. dm_block_t i;
  592. enum allocation_event ev;
  593. struct sm_metadata *smm = container_of(sm, struct sm_metadata, sm);
  594. smm->begin = superblock + 1;
  595. smm->recursion_count = 0;
  596. smm->allocated_this_transaction = 0;
  597. brb_init(&smm->uncommitted);
  598. threshold_init(&smm->threshold);
  599. memcpy(&smm->sm, &bootstrap_ops, sizeof(smm->sm));
  600. r = sm_ll_new_metadata(&smm->ll, tm);
  601. if (r)
  602. return r;
  603. if (nr_blocks > DM_SM_METADATA_MAX_BLOCKS)
  604. nr_blocks = DM_SM_METADATA_MAX_BLOCKS;
  605. r = sm_ll_extend(&smm->ll, nr_blocks);
  606. if (r)
  607. return r;
  608. memcpy(&smm->sm, &ops, sizeof(smm->sm));
  609. /*
  610. * Now we need to update the newly created data structures with the
  611. * allocated blocks that they were built from.
  612. */
  613. for (i = superblock; !r && i < smm->begin; i++)
  614. r = sm_ll_inc(&smm->ll, i, &ev);
  615. if (r)
  616. return r;
  617. r = apply_bops(smm);
  618. if (r) {
  619. DMERR("%s: apply_bops failed", __func__);
  620. return r;
  621. }
  622. return sm_metadata_commit(sm);
  623. }
  624. int dm_sm_metadata_open(struct dm_space_map *sm,
  625. struct dm_transaction_manager *tm,
  626. void *root_le, size_t len)
  627. {
  628. int r;
  629. struct sm_metadata *smm = container_of(sm, struct sm_metadata, sm);
  630. r = sm_ll_open_metadata(&smm->ll, tm, root_le, len);
  631. if (r)
  632. return r;
  633. smm->begin = 0;
  634. smm->recursion_count = 0;
  635. smm->allocated_this_transaction = 0;
  636. brb_init(&smm->uncommitted);
  637. threshold_init(&smm->threshold);
  638. memcpy(&smm->old_ll, &smm->ll, sizeof(smm->old_ll));
  639. return 0;
  640. }