dm-space-map-metadata.c 18 KB

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