dm-space-map-metadata.c 17 KB

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