dm-space-map-common.c 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751
  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 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 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 = bitmap_prepare_for_write,
  91. .check = 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 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. 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. ll->tm = tm;
  154. ll->bitmap_info.tm = tm;
  155. ll->bitmap_info.levels = 1;
  156. /*
  157. * Because the new bitmap blocks are created via a shadow
  158. * operation, the old entry has already had its reference count
  159. * decremented and we don't need the btree to do any bookkeeping.
  160. */
  161. ll->bitmap_info.value_type.size = sizeof(struct disk_index_entry);
  162. ll->bitmap_info.value_type.inc = NULL;
  163. ll->bitmap_info.value_type.dec = NULL;
  164. ll->bitmap_info.value_type.equal = NULL;
  165. ll->ref_count_info.tm = tm;
  166. ll->ref_count_info.levels = 1;
  167. ll->ref_count_info.value_type.size = sizeof(uint32_t);
  168. ll->ref_count_info.value_type.inc = NULL;
  169. ll->ref_count_info.value_type.dec = NULL;
  170. ll->ref_count_info.value_type.equal = NULL;
  171. ll->block_size = dm_bm_block_size(dm_tm_get_bm(tm));
  172. if (ll->block_size > (1 << 30)) {
  173. DMERR("block size too big to hold bitmaps");
  174. return -EINVAL;
  175. }
  176. ll->entries_per_block = (ll->block_size - sizeof(struct disk_bitmap_header)) *
  177. ENTRIES_PER_BYTE;
  178. ll->nr_blocks = 0;
  179. ll->bitmap_root = 0;
  180. ll->ref_count_root = 0;
  181. ll->bitmap_index_changed = false;
  182. return 0;
  183. }
  184. int sm_ll_extend(struct ll_disk *ll, dm_block_t extra_blocks)
  185. {
  186. int r;
  187. dm_block_t i, nr_blocks, nr_indexes;
  188. unsigned old_blocks, blocks;
  189. nr_blocks = ll->nr_blocks + extra_blocks;
  190. old_blocks = dm_sector_div_up(ll->nr_blocks, ll->entries_per_block);
  191. blocks = dm_sector_div_up(nr_blocks, ll->entries_per_block);
  192. nr_indexes = dm_sector_div_up(nr_blocks, ll->entries_per_block);
  193. if (nr_indexes > ll->max_entries(ll)) {
  194. DMERR("space map too large");
  195. return -EINVAL;
  196. }
  197. /*
  198. * We need to set this before the dm_tm_new_block() call below.
  199. */
  200. ll->nr_blocks = nr_blocks;
  201. for (i = old_blocks; i < blocks; i++) {
  202. struct dm_block *b;
  203. struct disk_index_entry idx;
  204. r = dm_tm_new_block(ll->tm, &dm_sm_bitmap_validator, &b);
  205. if (r < 0)
  206. return r;
  207. idx.blocknr = cpu_to_le64(dm_block_location(b));
  208. dm_tm_unlock(ll->tm, b);
  209. idx.nr_free = cpu_to_le32(ll->entries_per_block);
  210. idx.none_free_before = 0;
  211. r = ll->save_ie(ll, i, &idx);
  212. if (r < 0)
  213. return r;
  214. }
  215. return 0;
  216. }
  217. int sm_ll_lookup_bitmap(struct ll_disk *ll, dm_block_t b, uint32_t *result)
  218. {
  219. int r;
  220. dm_block_t index = b;
  221. struct disk_index_entry ie_disk;
  222. struct dm_block *blk;
  223. b = do_div(index, ll->entries_per_block);
  224. r = ll->load_ie(ll, index, &ie_disk);
  225. if (r < 0)
  226. return r;
  227. r = dm_tm_read_lock(ll->tm, le64_to_cpu(ie_disk.blocknr),
  228. &dm_sm_bitmap_validator, &blk);
  229. if (r < 0)
  230. return r;
  231. *result = sm_lookup_bitmap(dm_bitmap_data(blk), b);
  232. dm_tm_unlock(ll->tm, blk);
  233. return 0;
  234. }
  235. static int sm_ll_lookup_big_ref_count(struct ll_disk *ll, dm_block_t b,
  236. uint32_t *result)
  237. {
  238. __le32 le_rc;
  239. int r;
  240. r = dm_btree_lookup(&ll->ref_count_info, ll->ref_count_root, &b, &le_rc);
  241. if (r < 0)
  242. return r;
  243. *result = le32_to_cpu(le_rc);
  244. return r;
  245. }
  246. int sm_ll_lookup(struct ll_disk *ll, dm_block_t b, uint32_t *result)
  247. {
  248. int r = sm_ll_lookup_bitmap(ll, b, result);
  249. if (r)
  250. return r;
  251. if (*result != 3)
  252. return r;
  253. return sm_ll_lookup_big_ref_count(ll, b, result);
  254. }
  255. int sm_ll_find_free_block(struct ll_disk *ll, dm_block_t begin,
  256. dm_block_t end, dm_block_t *result)
  257. {
  258. int r;
  259. struct disk_index_entry ie_disk;
  260. dm_block_t i, index_begin = begin;
  261. dm_block_t index_end = dm_sector_div_up(end, ll->entries_per_block);
  262. /*
  263. * FIXME: Use shifts
  264. */
  265. begin = do_div(index_begin, ll->entries_per_block);
  266. end = do_div(end, ll->entries_per_block);
  267. for (i = index_begin; i < index_end; i++, begin = 0) {
  268. struct dm_block *blk;
  269. unsigned position;
  270. uint32_t bit_end;
  271. r = ll->load_ie(ll, i, &ie_disk);
  272. if (r < 0)
  273. return r;
  274. if (le32_to_cpu(ie_disk.nr_free) == 0)
  275. continue;
  276. r = dm_tm_read_lock(ll->tm, le64_to_cpu(ie_disk.blocknr),
  277. &dm_sm_bitmap_validator, &blk);
  278. if (r < 0)
  279. return r;
  280. bit_end = (i == index_end - 1) ? end : ll->entries_per_block;
  281. r = sm_find_free(dm_bitmap_data(blk),
  282. max_t(unsigned, begin, le32_to_cpu(ie_disk.none_free_before)),
  283. bit_end, &position);
  284. if (r == -ENOSPC) {
  285. /*
  286. * This might happen because we started searching
  287. * part way through the bitmap.
  288. */
  289. dm_tm_unlock(ll->tm, blk);
  290. continue;
  291. } else if (r < 0) {
  292. dm_tm_unlock(ll->tm, blk);
  293. return r;
  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. static int sm_ll_mutate(struct ll_disk *ll, dm_block_t b,
  302. int (*mutator)(void *context, uint32_t old, uint32_t *new),
  303. void *context, enum allocation_event *ev)
  304. {
  305. int r;
  306. uint32_t bit, old, ref_count;
  307. struct dm_block *nb;
  308. dm_block_t index = b;
  309. struct disk_index_entry ie_disk;
  310. void *bm_le;
  311. int inc;
  312. bit = do_div(index, ll->entries_per_block);
  313. r = ll->load_ie(ll, index, &ie_disk);
  314. if (r < 0)
  315. return r;
  316. r = dm_tm_shadow_block(ll->tm, le64_to_cpu(ie_disk.blocknr),
  317. &dm_sm_bitmap_validator, &nb, &inc);
  318. if (r < 0) {
  319. DMERR("dm_tm_shadow_block() failed");
  320. return r;
  321. }
  322. ie_disk.blocknr = cpu_to_le64(dm_block_location(nb));
  323. bm_le = dm_bitmap_data(nb);
  324. old = sm_lookup_bitmap(bm_le, bit);
  325. if (old > 2) {
  326. r = sm_ll_lookup_big_ref_count(ll, b, &old);
  327. if (r < 0) {
  328. dm_tm_unlock(ll->tm, nb);
  329. return r;
  330. }
  331. }
  332. r = mutator(context, old, &ref_count);
  333. if (r) {
  334. dm_tm_unlock(ll->tm, nb);
  335. return r;
  336. }
  337. if (ref_count <= 2) {
  338. sm_set_bitmap(bm_le, bit, ref_count);
  339. dm_tm_unlock(ll->tm, nb);
  340. if (old > 2) {
  341. r = dm_btree_remove(&ll->ref_count_info,
  342. ll->ref_count_root,
  343. &b, &ll->ref_count_root);
  344. if (r)
  345. return r;
  346. }
  347. } else {
  348. __le32 le_rc = cpu_to_le32(ref_count);
  349. sm_set_bitmap(bm_le, bit, 3);
  350. dm_tm_unlock(ll->tm, nb);
  351. __dm_bless_for_disk(&le_rc);
  352. r = dm_btree_insert(&ll->ref_count_info, ll->ref_count_root,
  353. &b, &le_rc, &ll->ref_count_root);
  354. if (r < 0) {
  355. DMERR("ref count insert failed");
  356. return r;
  357. }
  358. }
  359. if (ref_count && !old) {
  360. *ev = SM_ALLOC;
  361. ll->nr_allocated++;
  362. le32_add_cpu(&ie_disk.nr_free, -1);
  363. if (le32_to_cpu(ie_disk.none_free_before) == bit)
  364. ie_disk.none_free_before = cpu_to_le32(bit + 1);
  365. } else if (old && !ref_count) {
  366. *ev = SM_FREE;
  367. ll->nr_allocated--;
  368. le32_add_cpu(&ie_disk.nr_free, 1);
  369. ie_disk.none_free_before = cpu_to_le32(min(le32_to_cpu(ie_disk.none_free_before), bit));
  370. }
  371. return ll->save_ie(ll, index, &ie_disk);
  372. }
  373. static int set_ref_count(void *context, uint32_t old, uint32_t *new)
  374. {
  375. *new = *((uint32_t *) context);
  376. return 0;
  377. }
  378. int sm_ll_insert(struct ll_disk *ll, dm_block_t b,
  379. uint32_t ref_count, enum allocation_event *ev)
  380. {
  381. return sm_ll_mutate(ll, b, set_ref_count, &ref_count, ev);
  382. }
  383. static int inc_ref_count(void *context, uint32_t old, uint32_t *new)
  384. {
  385. *new = old + 1;
  386. return 0;
  387. }
  388. int sm_ll_inc(struct ll_disk *ll, dm_block_t b, enum allocation_event *ev)
  389. {
  390. return sm_ll_mutate(ll, b, inc_ref_count, NULL, ev);
  391. }
  392. static int dec_ref_count(void *context, uint32_t old, uint32_t *new)
  393. {
  394. if (!old) {
  395. DMERR_LIMIT("unable to decrement a reference count below 0");
  396. return -EINVAL;
  397. }
  398. *new = old - 1;
  399. return 0;
  400. }
  401. int sm_ll_dec(struct ll_disk *ll, dm_block_t b, enum allocation_event *ev)
  402. {
  403. return sm_ll_mutate(ll, b, dec_ref_count, NULL, ev);
  404. }
  405. int sm_ll_commit(struct ll_disk *ll)
  406. {
  407. int r = 0;
  408. if (ll->bitmap_index_changed) {
  409. r = ll->commit(ll);
  410. if (!r)
  411. ll->bitmap_index_changed = false;
  412. }
  413. return r;
  414. }
  415. /*----------------------------------------------------------------*/
  416. static int metadata_ll_load_ie(struct ll_disk *ll, dm_block_t index,
  417. struct disk_index_entry *ie)
  418. {
  419. memcpy(ie, ll->mi_le.index + index, sizeof(*ie));
  420. return 0;
  421. }
  422. static int metadata_ll_save_ie(struct ll_disk *ll, dm_block_t index,
  423. struct disk_index_entry *ie)
  424. {
  425. ll->bitmap_index_changed = true;
  426. memcpy(ll->mi_le.index + index, ie, sizeof(*ie));
  427. return 0;
  428. }
  429. static int metadata_ll_init_index(struct ll_disk *ll)
  430. {
  431. int r;
  432. struct dm_block *b;
  433. r = dm_tm_new_block(ll->tm, &index_validator, &b);
  434. if (r < 0)
  435. return r;
  436. memcpy(dm_block_data(b), &ll->mi_le, sizeof(ll->mi_le));
  437. ll->bitmap_root = dm_block_location(b);
  438. dm_tm_unlock(ll->tm, b);
  439. return 0;
  440. }
  441. static int metadata_ll_open(struct ll_disk *ll)
  442. {
  443. int r;
  444. struct dm_block *block;
  445. r = dm_tm_read_lock(ll->tm, ll->bitmap_root,
  446. &index_validator, &block);
  447. if (r)
  448. return r;
  449. memcpy(&ll->mi_le, dm_block_data(block), sizeof(ll->mi_le));
  450. dm_tm_unlock(ll->tm, block);
  451. return 0;
  452. }
  453. static dm_block_t metadata_ll_max_entries(struct ll_disk *ll)
  454. {
  455. return MAX_METADATA_BITMAPS;
  456. }
  457. static int metadata_ll_commit(struct ll_disk *ll)
  458. {
  459. int r, inc;
  460. struct dm_block *b;
  461. r = dm_tm_shadow_block(ll->tm, ll->bitmap_root, &index_validator, &b, &inc);
  462. if (r)
  463. return r;
  464. memcpy(dm_block_data(b), &ll->mi_le, sizeof(ll->mi_le));
  465. ll->bitmap_root = dm_block_location(b);
  466. dm_tm_unlock(ll->tm, b);
  467. return 0;
  468. }
  469. int sm_ll_new_metadata(struct ll_disk *ll, struct dm_transaction_manager *tm)
  470. {
  471. int r;
  472. r = sm_ll_init(ll, tm);
  473. if (r < 0)
  474. return r;
  475. ll->load_ie = metadata_ll_load_ie;
  476. ll->save_ie = metadata_ll_save_ie;
  477. ll->init_index = metadata_ll_init_index;
  478. ll->open_index = metadata_ll_open;
  479. ll->max_entries = metadata_ll_max_entries;
  480. ll->commit = metadata_ll_commit;
  481. ll->nr_blocks = 0;
  482. ll->nr_allocated = 0;
  483. r = ll->init_index(ll);
  484. if (r < 0)
  485. return r;
  486. r = dm_btree_empty(&ll->ref_count_info, &ll->ref_count_root);
  487. if (r < 0)
  488. return r;
  489. return 0;
  490. }
  491. int sm_ll_open_metadata(struct ll_disk *ll, struct dm_transaction_manager *tm,
  492. void *root_le, size_t len)
  493. {
  494. int r;
  495. struct disk_sm_root *smr = root_le;
  496. if (len < sizeof(struct disk_sm_root)) {
  497. DMERR("sm_metadata root too small");
  498. return -ENOMEM;
  499. }
  500. r = sm_ll_init(ll, tm);
  501. if (r < 0)
  502. return r;
  503. ll->load_ie = metadata_ll_load_ie;
  504. ll->save_ie = metadata_ll_save_ie;
  505. ll->init_index = metadata_ll_init_index;
  506. ll->open_index = metadata_ll_open;
  507. ll->max_entries = metadata_ll_max_entries;
  508. ll->commit = metadata_ll_commit;
  509. ll->nr_blocks = le64_to_cpu(smr->nr_blocks);
  510. ll->nr_allocated = le64_to_cpu(smr->nr_allocated);
  511. ll->bitmap_root = le64_to_cpu(smr->bitmap_root);
  512. ll->ref_count_root = le64_to_cpu(smr->ref_count_root);
  513. return ll->open_index(ll);
  514. }
  515. /*----------------------------------------------------------------*/
  516. static int disk_ll_load_ie(struct ll_disk *ll, dm_block_t index,
  517. struct disk_index_entry *ie)
  518. {
  519. return dm_btree_lookup(&ll->bitmap_info, ll->bitmap_root, &index, ie);
  520. }
  521. static int disk_ll_save_ie(struct ll_disk *ll, dm_block_t index,
  522. struct disk_index_entry *ie)
  523. {
  524. __dm_bless_for_disk(ie);
  525. return dm_btree_insert(&ll->bitmap_info, ll->bitmap_root,
  526. &index, ie, &ll->bitmap_root);
  527. }
  528. static int disk_ll_init_index(struct ll_disk *ll)
  529. {
  530. return dm_btree_empty(&ll->bitmap_info, &ll->bitmap_root);
  531. }
  532. static int disk_ll_open(struct ll_disk *ll)
  533. {
  534. /* nothing to do */
  535. return 0;
  536. }
  537. static dm_block_t disk_ll_max_entries(struct ll_disk *ll)
  538. {
  539. return -1ULL;
  540. }
  541. static int disk_ll_commit(struct ll_disk *ll)
  542. {
  543. return 0;
  544. }
  545. int sm_ll_new_disk(struct ll_disk *ll, struct dm_transaction_manager *tm)
  546. {
  547. int r;
  548. r = sm_ll_init(ll, tm);
  549. if (r < 0)
  550. return r;
  551. ll->load_ie = disk_ll_load_ie;
  552. ll->save_ie = disk_ll_save_ie;
  553. ll->init_index = disk_ll_init_index;
  554. ll->open_index = disk_ll_open;
  555. ll->max_entries = disk_ll_max_entries;
  556. ll->commit = disk_ll_commit;
  557. ll->nr_blocks = 0;
  558. ll->nr_allocated = 0;
  559. r = ll->init_index(ll);
  560. if (r < 0)
  561. return r;
  562. r = dm_btree_empty(&ll->ref_count_info, &ll->ref_count_root);
  563. if (r < 0)
  564. return r;
  565. return 0;
  566. }
  567. int sm_ll_open_disk(struct ll_disk *ll, struct dm_transaction_manager *tm,
  568. void *root_le, size_t len)
  569. {
  570. int r;
  571. struct disk_sm_root *smr = root_le;
  572. if (len < sizeof(struct disk_sm_root)) {
  573. DMERR("sm_metadata root too small");
  574. return -ENOMEM;
  575. }
  576. r = sm_ll_init(ll, tm);
  577. if (r < 0)
  578. return r;
  579. ll->load_ie = disk_ll_load_ie;
  580. ll->save_ie = disk_ll_save_ie;
  581. ll->init_index = disk_ll_init_index;
  582. ll->open_index = disk_ll_open;
  583. ll->max_entries = disk_ll_max_entries;
  584. ll->commit = disk_ll_commit;
  585. ll->nr_blocks = le64_to_cpu(smr->nr_blocks);
  586. ll->nr_allocated = le64_to_cpu(smr->nr_allocated);
  587. ll->bitmap_root = le64_to_cpu(smr->bitmap_root);
  588. ll->ref_count_root = le64_to_cpu(smr->ref_count_root);
  589. return ll->open_index(ll);
  590. }
  591. /*----------------------------------------------------------------*/