dm-space-map-metadata.c 17 KB

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