dm-space-map-common.c 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785
  1. /*
  2. * Copyright (C) 2011 Red Hat, Inc.
  3. *
  4. * This file is released under the GPL.
  5. */
  6. #include "dm-space-map-common.h"
  7. #include "dm-transaction-manager.h"
  8. #include <linux/bitops.h>
  9. #include <linux/device-mapper.h>
  10. #define DM_MSG_PREFIX "space map common"
  11. /*----------------------------------------------------------------*/
  12. /*
  13. * Index validator.
  14. */
  15. #define INDEX_CSUM_XOR 160478
  16. static void index_prepare_for_write(struct dm_block_validator *v,
  17. struct dm_block *b,
  18. size_t block_size)
  19. {
  20. struct disk_metadata_index *mi_le = dm_block_data(b);
  21. mi_le->blocknr = cpu_to_le64(dm_block_location(b));
  22. mi_le->csum = cpu_to_le32(dm_bm_checksum(&mi_le->padding,
  23. block_size - sizeof(__le32),
  24. INDEX_CSUM_XOR));
  25. }
  26. static int index_check(struct dm_block_validator *v,
  27. struct dm_block *b,
  28. size_t block_size)
  29. {
  30. struct disk_metadata_index *mi_le = dm_block_data(b);
  31. __le32 csum_disk;
  32. if (dm_block_location(b) != le64_to_cpu(mi_le->blocknr)) {
  33. DMERR_LIMIT("index_check failed: blocknr %llu != wanted %llu",
  34. le64_to_cpu(mi_le->blocknr), dm_block_location(b));
  35. return -ENOTBLK;
  36. }
  37. csum_disk = cpu_to_le32(dm_bm_checksum(&mi_le->padding,
  38. block_size - sizeof(__le32),
  39. INDEX_CSUM_XOR));
  40. if (csum_disk != mi_le->csum) {
  41. DMERR_LIMIT("index_check failed: csum %u != wanted %u",
  42. le32_to_cpu(csum_disk), le32_to_cpu(mi_le->csum));
  43. return -EILSEQ;
  44. }
  45. return 0;
  46. }
  47. static struct dm_block_validator index_validator = {
  48. .name = "index",
  49. .prepare_for_write = index_prepare_for_write,
  50. .check = index_check
  51. };
  52. /*----------------------------------------------------------------*/
  53. /*
  54. * Bitmap validator
  55. */
  56. #define BITMAP_CSUM_XOR 240779
  57. static void dm_bitmap_prepare_for_write(struct dm_block_validator *v,
  58. struct dm_block *b,
  59. size_t block_size)
  60. {
  61. struct disk_bitmap_header *disk_header = dm_block_data(b);
  62. disk_header->blocknr = cpu_to_le64(dm_block_location(b));
  63. disk_header->csum = cpu_to_le32(dm_bm_checksum(&disk_header->not_used,
  64. block_size - sizeof(__le32),
  65. BITMAP_CSUM_XOR));
  66. }
  67. static int dm_bitmap_check(struct dm_block_validator *v,
  68. struct dm_block *b,
  69. size_t block_size)
  70. {
  71. struct disk_bitmap_header *disk_header = dm_block_data(b);
  72. __le32 csum_disk;
  73. if (dm_block_location(b) != le64_to_cpu(disk_header->blocknr)) {
  74. DMERR_LIMIT("bitmap check failed: blocknr %llu != wanted %llu",
  75. le64_to_cpu(disk_header->blocknr), dm_block_location(b));
  76. return -ENOTBLK;
  77. }
  78. csum_disk = cpu_to_le32(dm_bm_checksum(&disk_header->not_used,
  79. block_size - sizeof(__le32),
  80. BITMAP_CSUM_XOR));
  81. if (csum_disk != disk_header->csum) {
  82. DMERR_LIMIT("bitmap check failed: csum %u != wanted %u",
  83. le32_to_cpu(csum_disk), le32_to_cpu(disk_header->csum));
  84. return -EILSEQ;
  85. }
  86. return 0;
  87. }
  88. static struct dm_block_validator dm_sm_bitmap_validator = {
  89. .name = "sm_bitmap",
  90. .prepare_for_write = dm_bitmap_prepare_for_write,
  91. .check = dm_bitmap_check,
  92. };
  93. /*----------------------------------------------------------------*/
  94. #define ENTRIES_PER_WORD 32
  95. #define ENTRIES_SHIFT 5
  96. static void *dm_bitmap_data(struct dm_block *b)
  97. {
  98. return dm_block_data(b) + sizeof(struct disk_bitmap_header);
  99. }
  100. #define WORD_MASK_HIGH 0xAAAAAAAAAAAAAAAAULL
  101. static unsigned dm_bitmap_word_used(void *addr, unsigned b)
  102. {
  103. __le64 *words_le = addr;
  104. __le64 *w_le = words_le + (b >> ENTRIES_SHIFT);
  105. uint64_t bits = le64_to_cpu(*w_le);
  106. uint64_t mask = (bits + WORD_MASK_HIGH + 1) & WORD_MASK_HIGH;
  107. return !(~bits & mask);
  108. }
  109. static unsigned sm_lookup_bitmap(void *addr, unsigned b)
  110. {
  111. __le64 *words_le = addr;
  112. __le64 *w_le = words_le + (b >> ENTRIES_SHIFT);
  113. unsigned hi, lo;
  114. b = (b & (ENTRIES_PER_WORD - 1)) << 1;
  115. hi = !!test_bit_le(b, (void *) w_le);
  116. lo = !!test_bit_le(b + 1, (void *) w_le);
  117. return (hi << 1) | lo;
  118. }
  119. static void sm_set_bitmap(void *addr, unsigned b, unsigned val)
  120. {
  121. __le64 *words_le = addr;
  122. __le64 *w_le = words_le + (b >> ENTRIES_SHIFT);
  123. b = (b & (ENTRIES_PER_WORD - 1)) << 1;
  124. if (val & 2)
  125. __set_bit_le(b, (void *) w_le);
  126. else
  127. __clear_bit_le(b, (void *) w_le);
  128. if (val & 1)
  129. __set_bit_le(b + 1, (void *) w_le);
  130. else
  131. __clear_bit_le(b + 1, (void *) w_le);
  132. }
  133. static int sm_find_free(void *addr, unsigned begin, unsigned end,
  134. unsigned *result)
  135. {
  136. while (begin < end) {
  137. if (!(begin & (ENTRIES_PER_WORD - 1)) &&
  138. dm_bitmap_word_used(addr, begin)) {
  139. begin += ENTRIES_PER_WORD;
  140. continue;
  141. }
  142. if (!sm_lookup_bitmap(addr, begin)) {
  143. *result = begin;
  144. return 0;
  145. }
  146. begin++;
  147. }
  148. return -ENOSPC;
  149. }
  150. /*----------------------------------------------------------------*/
  151. static int sm_ll_init(struct ll_disk *ll, struct dm_transaction_manager *tm)
  152. {
  153. memset(ll, 0, sizeof(struct ll_disk));
  154. ll->tm = tm;
  155. ll->bitmap_info.tm = tm;
  156. ll->bitmap_info.levels = 1;
  157. /*
  158. * Because the new bitmap blocks are created via a shadow
  159. * operation, the old entry has already had its reference count
  160. * decremented and we don't need the btree to do any bookkeeping.
  161. */
  162. ll->bitmap_info.value_type.size = sizeof(struct disk_index_entry);
  163. ll->bitmap_info.value_type.inc = NULL;
  164. ll->bitmap_info.value_type.dec = NULL;
  165. ll->bitmap_info.value_type.equal = NULL;
  166. ll->ref_count_info.tm = tm;
  167. ll->ref_count_info.levels = 1;
  168. ll->ref_count_info.value_type.size = sizeof(uint32_t);
  169. ll->ref_count_info.value_type.inc = NULL;
  170. ll->ref_count_info.value_type.dec = NULL;
  171. ll->ref_count_info.value_type.equal = NULL;
  172. ll->block_size = dm_bm_block_size(dm_tm_get_bm(tm));
  173. if (ll->block_size > (1 << 30)) {
  174. DMERR("block size too big to hold bitmaps");
  175. return -EINVAL;
  176. }
  177. ll->entries_per_block = (ll->block_size - sizeof(struct disk_bitmap_header)) *
  178. ENTRIES_PER_BYTE;
  179. ll->nr_blocks = 0;
  180. ll->bitmap_root = 0;
  181. ll->ref_count_root = 0;
  182. ll->bitmap_index_changed = false;
  183. return 0;
  184. }
  185. int sm_ll_extend(struct ll_disk *ll, dm_block_t extra_blocks)
  186. {
  187. int r;
  188. dm_block_t i, nr_blocks, nr_indexes;
  189. unsigned old_blocks, blocks;
  190. nr_blocks = ll->nr_blocks + extra_blocks;
  191. old_blocks = dm_sector_div_up(ll->nr_blocks, ll->entries_per_block);
  192. blocks = dm_sector_div_up(nr_blocks, ll->entries_per_block);
  193. nr_indexes = dm_sector_div_up(nr_blocks, ll->entries_per_block);
  194. if (nr_indexes > ll->max_entries(ll)) {
  195. DMERR("space map too large");
  196. return -EINVAL;
  197. }
  198. /*
  199. * We need to set this before the dm_tm_new_block() call below.
  200. */
  201. ll->nr_blocks = nr_blocks;
  202. for (i = old_blocks; i < blocks; i++) {
  203. struct dm_block *b;
  204. struct disk_index_entry idx;
  205. r = dm_tm_new_block(ll->tm, &dm_sm_bitmap_validator, &b);
  206. if (r < 0)
  207. return r;
  208. idx.blocknr = cpu_to_le64(dm_block_location(b));
  209. dm_tm_unlock(ll->tm, b);
  210. idx.nr_free = cpu_to_le32(ll->entries_per_block);
  211. idx.none_free_before = 0;
  212. r = ll->save_ie(ll, i, &idx);
  213. if (r < 0)
  214. return r;
  215. }
  216. return 0;
  217. }
  218. int sm_ll_lookup_bitmap(struct ll_disk *ll, dm_block_t b, uint32_t *result)
  219. {
  220. int r;
  221. dm_block_t index = b;
  222. struct disk_index_entry ie_disk;
  223. struct dm_block *blk;
  224. b = do_div(index, ll->entries_per_block);
  225. r = ll->load_ie(ll, index, &ie_disk);
  226. if (r < 0)
  227. return r;
  228. r = dm_tm_read_lock(ll->tm, le64_to_cpu(ie_disk.blocknr),
  229. &dm_sm_bitmap_validator, &blk);
  230. if (r < 0)
  231. return r;
  232. *result = sm_lookup_bitmap(dm_bitmap_data(blk), b);
  233. dm_tm_unlock(ll->tm, blk);
  234. return 0;
  235. }
  236. static int sm_ll_lookup_big_ref_count(struct ll_disk *ll, dm_block_t b,
  237. uint32_t *result)
  238. {
  239. __le32 le_rc;
  240. int r;
  241. r = dm_btree_lookup(&ll->ref_count_info, ll->ref_count_root, &b, &le_rc);
  242. if (r < 0)
  243. return r;
  244. *result = le32_to_cpu(le_rc);
  245. return r;
  246. }
  247. int sm_ll_lookup(struct ll_disk *ll, dm_block_t b, uint32_t *result)
  248. {
  249. int r = sm_ll_lookup_bitmap(ll, b, result);
  250. if (r)
  251. return r;
  252. if (*result != 3)
  253. return r;
  254. return sm_ll_lookup_big_ref_count(ll, b, result);
  255. }
  256. int sm_ll_find_free_block(struct ll_disk *ll, dm_block_t begin,
  257. dm_block_t end, dm_block_t *result)
  258. {
  259. int r;
  260. struct disk_index_entry ie_disk;
  261. dm_block_t i, index_begin = begin;
  262. dm_block_t index_end = dm_sector_div_up(end, ll->entries_per_block);
  263. /*
  264. * FIXME: Use shifts
  265. */
  266. begin = do_div(index_begin, ll->entries_per_block);
  267. end = do_div(end, ll->entries_per_block);
  268. if (end == 0)
  269. end = ll->entries_per_block;
  270. for (i = index_begin; i < index_end; i++, begin = 0) {
  271. struct dm_block *blk;
  272. unsigned position;
  273. uint32_t bit_end;
  274. r = ll->load_ie(ll, i, &ie_disk);
  275. if (r < 0)
  276. return r;
  277. if (le32_to_cpu(ie_disk.nr_free) == 0)
  278. continue;
  279. r = dm_tm_read_lock(ll->tm, le64_to_cpu(ie_disk.blocknr),
  280. &dm_sm_bitmap_validator, &blk);
  281. if (r < 0)
  282. return r;
  283. bit_end = (i == index_end - 1) ? end : ll->entries_per_block;
  284. r = sm_find_free(dm_bitmap_data(blk),
  285. max_t(unsigned, begin, le32_to_cpu(ie_disk.none_free_before)),
  286. bit_end, &position);
  287. if (r == -ENOSPC) {
  288. /*
  289. * This might happen because we started searching
  290. * part way through the bitmap.
  291. */
  292. dm_tm_unlock(ll->tm, blk);
  293. continue;
  294. }
  295. dm_tm_unlock(ll->tm, blk);
  296. *result = i * ll->entries_per_block + (dm_block_t) position;
  297. return 0;
  298. }
  299. return -ENOSPC;
  300. }
  301. int sm_ll_find_common_free_block(struct ll_disk *old_ll, struct ll_disk *new_ll,
  302. dm_block_t begin, dm_block_t end, dm_block_t *b)
  303. {
  304. int r;
  305. uint32_t count;
  306. do {
  307. r = sm_ll_find_free_block(new_ll, begin, new_ll->nr_blocks, b);
  308. if (r)
  309. break;
  310. /* double check this block wasn't used in the old transaction */
  311. if (*b >= old_ll->nr_blocks)
  312. count = 0;
  313. else {
  314. r = sm_ll_lookup(old_ll, *b, &count);
  315. if (r)
  316. break;
  317. if (count)
  318. begin = *b + 1;
  319. }
  320. } while (count);
  321. return r;
  322. }
  323. static int sm_ll_mutate(struct ll_disk *ll, dm_block_t b,
  324. int (*mutator)(void *context, uint32_t old, uint32_t *new),
  325. void *context, enum allocation_event *ev)
  326. {
  327. int r;
  328. uint32_t bit, old, ref_count;
  329. struct dm_block *nb;
  330. dm_block_t index = b;
  331. struct disk_index_entry ie_disk;
  332. void *bm_le;
  333. int inc;
  334. bit = do_div(index, ll->entries_per_block);
  335. r = ll->load_ie(ll, index, &ie_disk);
  336. if (r < 0)
  337. return r;
  338. r = dm_tm_shadow_block(ll->tm, le64_to_cpu(ie_disk.blocknr),
  339. &dm_sm_bitmap_validator, &nb, &inc);
  340. if (r < 0) {
  341. DMERR("dm_tm_shadow_block() failed");
  342. return r;
  343. }
  344. ie_disk.blocknr = cpu_to_le64(dm_block_location(nb));
  345. bm_le = dm_bitmap_data(nb);
  346. old = sm_lookup_bitmap(bm_le, bit);
  347. if (old > 2) {
  348. r = sm_ll_lookup_big_ref_count(ll, b, &old);
  349. if (r < 0) {
  350. dm_tm_unlock(ll->tm, nb);
  351. return r;
  352. }
  353. }
  354. r = mutator(context, old, &ref_count);
  355. if (r) {
  356. dm_tm_unlock(ll->tm, nb);
  357. return r;
  358. }
  359. if (ref_count <= 2) {
  360. sm_set_bitmap(bm_le, bit, ref_count);
  361. dm_tm_unlock(ll->tm, nb);
  362. if (old > 2) {
  363. r = dm_btree_remove(&ll->ref_count_info,
  364. ll->ref_count_root,
  365. &b, &ll->ref_count_root);
  366. if (r)
  367. return r;
  368. }
  369. } else {
  370. __le32 le_rc = cpu_to_le32(ref_count);
  371. sm_set_bitmap(bm_le, bit, 3);
  372. dm_tm_unlock(ll->tm, nb);
  373. __dm_bless_for_disk(&le_rc);
  374. r = dm_btree_insert(&ll->ref_count_info, ll->ref_count_root,
  375. &b, &le_rc, &ll->ref_count_root);
  376. if (r < 0) {
  377. DMERR("ref count insert failed");
  378. return r;
  379. }
  380. }
  381. if (ref_count && !old) {
  382. *ev = SM_ALLOC;
  383. ll->nr_allocated++;
  384. le32_add_cpu(&ie_disk.nr_free, -1);
  385. if (le32_to_cpu(ie_disk.none_free_before) == bit)
  386. ie_disk.none_free_before = cpu_to_le32(bit + 1);
  387. } else if (old && !ref_count) {
  388. *ev = SM_FREE;
  389. ll->nr_allocated--;
  390. le32_add_cpu(&ie_disk.nr_free, 1);
  391. ie_disk.none_free_before = cpu_to_le32(min(le32_to_cpu(ie_disk.none_free_before), bit));
  392. } else
  393. *ev = SM_NONE;
  394. return ll->save_ie(ll, index, &ie_disk);
  395. }
  396. static int set_ref_count(void *context, uint32_t old, uint32_t *new)
  397. {
  398. *new = *((uint32_t *) context);
  399. return 0;
  400. }
  401. int sm_ll_insert(struct ll_disk *ll, dm_block_t b,
  402. uint32_t ref_count, enum allocation_event *ev)
  403. {
  404. return sm_ll_mutate(ll, b, set_ref_count, &ref_count, ev);
  405. }
  406. static int inc_ref_count(void *context, uint32_t old, uint32_t *new)
  407. {
  408. *new = old + 1;
  409. return 0;
  410. }
  411. int sm_ll_inc(struct ll_disk *ll, dm_block_t b, enum allocation_event *ev)
  412. {
  413. return sm_ll_mutate(ll, b, inc_ref_count, NULL, ev);
  414. }
  415. static int dec_ref_count(void *context, uint32_t old, uint32_t *new)
  416. {
  417. if (!old) {
  418. DMERR_LIMIT("unable to decrement a reference count below 0");
  419. return -EINVAL;
  420. }
  421. *new = old - 1;
  422. return 0;
  423. }
  424. int sm_ll_dec(struct ll_disk *ll, dm_block_t b, enum allocation_event *ev)
  425. {
  426. return sm_ll_mutate(ll, b, dec_ref_count, NULL, ev);
  427. }
  428. int sm_ll_commit(struct ll_disk *ll)
  429. {
  430. int r = 0;
  431. if (ll->bitmap_index_changed) {
  432. r = ll->commit(ll);
  433. if (!r)
  434. ll->bitmap_index_changed = false;
  435. }
  436. return r;
  437. }
  438. /*----------------------------------------------------------------*/
  439. static int metadata_ll_load_ie(struct ll_disk *ll, dm_block_t index,
  440. struct disk_index_entry *ie)
  441. {
  442. memcpy(ie, ll->mi_le.index + index, sizeof(*ie));
  443. return 0;
  444. }
  445. static int metadata_ll_save_ie(struct ll_disk *ll, dm_block_t index,
  446. struct disk_index_entry *ie)
  447. {
  448. ll->bitmap_index_changed = true;
  449. memcpy(ll->mi_le.index + index, ie, sizeof(*ie));
  450. return 0;
  451. }
  452. static int metadata_ll_init_index(struct ll_disk *ll)
  453. {
  454. int r;
  455. struct dm_block *b;
  456. r = dm_tm_new_block(ll->tm, &index_validator, &b);
  457. if (r < 0)
  458. return r;
  459. ll->bitmap_root = dm_block_location(b);
  460. dm_tm_unlock(ll->tm, b);
  461. return 0;
  462. }
  463. static int metadata_ll_open(struct ll_disk *ll)
  464. {
  465. int r;
  466. struct dm_block *block;
  467. r = dm_tm_read_lock(ll->tm, ll->bitmap_root,
  468. &index_validator, &block);
  469. if (r)
  470. return r;
  471. memcpy(&ll->mi_le, dm_block_data(block), sizeof(ll->mi_le));
  472. dm_tm_unlock(ll->tm, block);
  473. return 0;
  474. }
  475. static dm_block_t metadata_ll_max_entries(struct ll_disk *ll)
  476. {
  477. return MAX_METADATA_BITMAPS;
  478. }
  479. static int metadata_ll_commit(struct ll_disk *ll)
  480. {
  481. int r, inc;
  482. struct dm_block *b;
  483. r = dm_tm_shadow_block(ll->tm, ll->bitmap_root, &index_validator, &b, &inc);
  484. if (r)
  485. return r;
  486. memcpy(dm_block_data(b), &ll->mi_le, sizeof(ll->mi_le));
  487. ll->bitmap_root = dm_block_location(b);
  488. dm_tm_unlock(ll->tm, b);
  489. return 0;
  490. }
  491. int sm_ll_new_metadata(struct ll_disk *ll, struct dm_transaction_manager *tm)
  492. {
  493. int r;
  494. r = sm_ll_init(ll, tm);
  495. if (r < 0)
  496. return r;
  497. ll->load_ie = metadata_ll_load_ie;
  498. ll->save_ie = metadata_ll_save_ie;
  499. ll->init_index = metadata_ll_init_index;
  500. ll->open_index = metadata_ll_open;
  501. ll->max_entries = metadata_ll_max_entries;
  502. ll->commit = metadata_ll_commit;
  503. ll->nr_blocks = 0;
  504. ll->nr_allocated = 0;
  505. r = ll->init_index(ll);
  506. if (r < 0)
  507. return r;
  508. r = dm_btree_empty(&ll->ref_count_info, &ll->ref_count_root);
  509. if (r < 0)
  510. return r;
  511. return 0;
  512. }
  513. int sm_ll_open_metadata(struct ll_disk *ll, struct dm_transaction_manager *tm,
  514. void *root_le, size_t len)
  515. {
  516. int r;
  517. struct disk_sm_root smr;
  518. if (len < sizeof(struct disk_sm_root)) {
  519. DMERR("sm_metadata root too small");
  520. return -ENOMEM;
  521. }
  522. /*
  523. * We don't know the alignment of the root_le buffer, so need to
  524. * copy into a new structure.
  525. */
  526. memcpy(&smr, root_le, sizeof(smr));
  527. r = sm_ll_init(ll, tm);
  528. if (r < 0)
  529. return r;
  530. ll->load_ie = metadata_ll_load_ie;
  531. ll->save_ie = metadata_ll_save_ie;
  532. ll->init_index = metadata_ll_init_index;
  533. ll->open_index = metadata_ll_open;
  534. ll->max_entries = metadata_ll_max_entries;
  535. ll->commit = metadata_ll_commit;
  536. ll->nr_blocks = le64_to_cpu(smr.nr_blocks);
  537. ll->nr_allocated = le64_to_cpu(smr.nr_allocated);
  538. ll->bitmap_root = le64_to_cpu(smr.bitmap_root);
  539. ll->ref_count_root = le64_to_cpu(smr.ref_count_root);
  540. return ll->open_index(ll);
  541. }
  542. /*----------------------------------------------------------------*/
  543. static int disk_ll_load_ie(struct ll_disk *ll, dm_block_t index,
  544. struct disk_index_entry *ie)
  545. {
  546. return dm_btree_lookup(&ll->bitmap_info, ll->bitmap_root, &index, ie);
  547. }
  548. static int disk_ll_save_ie(struct ll_disk *ll, dm_block_t index,
  549. struct disk_index_entry *ie)
  550. {
  551. __dm_bless_for_disk(ie);
  552. return dm_btree_insert(&ll->bitmap_info, ll->bitmap_root,
  553. &index, ie, &ll->bitmap_root);
  554. }
  555. static int disk_ll_init_index(struct ll_disk *ll)
  556. {
  557. return dm_btree_empty(&ll->bitmap_info, &ll->bitmap_root);
  558. }
  559. static int disk_ll_open(struct ll_disk *ll)
  560. {
  561. /* nothing to do */
  562. return 0;
  563. }
  564. static dm_block_t disk_ll_max_entries(struct ll_disk *ll)
  565. {
  566. return -1ULL;
  567. }
  568. static int disk_ll_commit(struct ll_disk *ll)
  569. {
  570. return 0;
  571. }
  572. int sm_ll_new_disk(struct ll_disk *ll, struct dm_transaction_manager *tm)
  573. {
  574. int r;
  575. r = sm_ll_init(ll, tm);
  576. if (r < 0)
  577. return r;
  578. ll->load_ie = disk_ll_load_ie;
  579. ll->save_ie = disk_ll_save_ie;
  580. ll->init_index = disk_ll_init_index;
  581. ll->open_index = disk_ll_open;
  582. ll->max_entries = disk_ll_max_entries;
  583. ll->commit = disk_ll_commit;
  584. ll->nr_blocks = 0;
  585. ll->nr_allocated = 0;
  586. r = ll->init_index(ll);
  587. if (r < 0)
  588. return r;
  589. r = dm_btree_empty(&ll->ref_count_info, &ll->ref_count_root);
  590. if (r < 0)
  591. return r;
  592. return 0;
  593. }
  594. int sm_ll_open_disk(struct ll_disk *ll, struct dm_transaction_manager *tm,
  595. void *root_le, size_t len)
  596. {
  597. int r;
  598. struct disk_sm_root *smr = root_le;
  599. if (len < sizeof(struct disk_sm_root)) {
  600. DMERR("sm_metadata root too small");
  601. return -ENOMEM;
  602. }
  603. r = sm_ll_init(ll, tm);
  604. if (r < 0)
  605. return r;
  606. ll->load_ie = disk_ll_load_ie;
  607. ll->save_ie = disk_ll_save_ie;
  608. ll->init_index = disk_ll_init_index;
  609. ll->open_index = disk_ll_open;
  610. ll->max_entries = disk_ll_max_entries;
  611. ll->commit = disk_ll_commit;
  612. ll->nr_blocks = le64_to_cpu(smr->nr_blocks);
  613. ll->nr_allocated = le64_to_cpu(smr->nr_allocated);
  614. ll->bitmap_root = le64_to_cpu(smr->bitmap_root);
  615. ll->ref_count_root = le64_to_cpu(smr->ref_count_root);
  616. return ll->open_index(ll);
  617. }
  618. /*----------------------------------------------------------------*/