dm-space-map-metadata.c 18 KB

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