blob: 6058569fe86c3dcf862ddc933e234e721dbdc3e6 [file] [log] [blame]
Joe Thornber3241b1d2011-10-31 20:19:11 +00001/*
2 * Copyright (C) 2011 Red Hat, Inc.
3 *
4 * This file is released under the GPL.
5 */
6
7#include "dm-space-map-common.h"
8#include "dm-transaction-manager.h"
9
10#include <linux/bitops.h>
11#include <linux/device-mapper.h>
12
13#define DM_MSG_PREFIX "space map common"
14
15/*----------------------------------------------------------------*/
16
17/*
18 * Index validator.
19 */
20#define INDEX_CSUM_XOR 160478
21
22static void index_prepare_for_write(struct dm_block_validator *v,
23 struct dm_block *b,
24 size_t block_size)
25{
26 struct disk_metadata_index *mi_le = dm_block_data(b);
27
28 mi_le->blocknr = cpu_to_le64(dm_block_location(b));
29 mi_le->csum = cpu_to_le32(dm_bm_checksum(&mi_le->padding,
30 block_size - sizeof(__le32),
31 INDEX_CSUM_XOR));
32}
33
34static int index_check(struct dm_block_validator *v,
35 struct dm_block *b,
36 size_t block_size)
37{
38 struct disk_metadata_index *mi_le = dm_block_data(b);
39 __le32 csum_disk;
40
41 if (dm_block_location(b) != le64_to_cpu(mi_le->blocknr)) {
Mike Snitzer89ddeb82012-12-21 20:23:34 +000042 DMERR_LIMIT("index_check failed: blocknr %llu != wanted %llu",
43 le64_to_cpu(mi_le->blocknr), dm_block_location(b));
Joe Thornber3241b1d2011-10-31 20:19:11 +000044 return -ENOTBLK;
45 }
46
47 csum_disk = cpu_to_le32(dm_bm_checksum(&mi_le->padding,
48 block_size - sizeof(__le32),
49 INDEX_CSUM_XOR));
50 if (csum_disk != mi_le->csum) {
Mike Snitzer89ddeb82012-12-21 20:23:34 +000051 DMERR_LIMIT("index_check failed: csum %u != wanted %u",
52 le32_to_cpu(csum_disk), le32_to_cpu(mi_le->csum));
Joe Thornber3241b1d2011-10-31 20:19:11 +000053 return -EILSEQ;
54 }
55
56 return 0;
57}
58
59static struct dm_block_validator index_validator = {
60 .name = "index",
61 .prepare_for_write = index_prepare_for_write,
62 .check = index_check
63};
64
65/*----------------------------------------------------------------*/
66
67/*
68 * Bitmap validator
69 */
70#define BITMAP_CSUM_XOR 240779
71
72static void bitmap_prepare_for_write(struct dm_block_validator *v,
73 struct dm_block *b,
74 size_t block_size)
75{
76 struct disk_bitmap_header *disk_header = dm_block_data(b);
77
78 disk_header->blocknr = cpu_to_le64(dm_block_location(b));
79 disk_header->csum = cpu_to_le32(dm_bm_checksum(&disk_header->not_used,
80 block_size - sizeof(__le32),
81 BITMAP_CSUM_XOR));
82}
83
84static int bitmap_check(struct dm_block_validator *v,
85 struct dm_block *b,
86 size_t block_size)
87{
88 struct disk_bitmap_header *disk_header = dm_block_data(b);
89 __le32 csum_disk;
90
91 if (dm_block_location(b) != le64_to_cpu(disk_header->blocknr)) {
Mike Snitzer89ddeb82012-12-21 20:23:34 +000092 DMERR_LIMIT("bitmap check failed: blocknr %llu != wanted %llu",
93 le64_to_cpu(disk_header->blocknr), dm_block_location(b));
Joe Thornber3241b1d2011-10-31 20:19:11 +000094 return -ENOTBLK;
95 }
96
97 csum_disk = cpu_to_le32(dm_bm_checksum(&disk_header->not_used,
98 block_size - sizeof(__le32),
99 BITMAP_CSUM_XOR));
100 if (csum_disk != disk_header->csum) {
Mike Snitzer89ddeb82012-12-21 20:23:34 +0000101 DMERR_LIMIT("bitmap check failed: csum %u != wanted %u",
102 le32_to_cpu(csum_disk), le32_to_cpu(disk_header->csum));
Joe Thornber3241b1d2011-10-31 20:19:11 +0000103 return -EILSEQ;
104 }
105
106 return 0;
107}
108
109static struct dm_block_validator dm_sm_bitmap_validator = {
110 .name = "sm_bitmap",
111 .prepare_for_write = bitmap_prepare_for_write,
112 .check = bitmap_check
113};
114
115/*----------------------------------------------------------------*/
116
117#define ENTRIES_PER_WORD 32
118#define ENTRIES_SHIFT 5
119
120static void *dm_bitmap_data(struct dm_block *b)
121{
122 return dm_block_data(b) + sizeof(struct disk_bitmap_header);
123}
124
125#define WORD_MASK_HIGH 0xAAAAAAAAAAAAAAAAULL
126
127static unsigned bitmap_word_used(void *addr, unsigned b)
128{
129 __le64 *words_le = addr;
130 __le64 *w_le = words_le + (b >> ENTRIES_SHIFT);
131
132 uint64_t bits = le64_to_cpu(*w_le);
133 uint64_t mask = (bits + WORD_MASK_HIGH + 1) & WORD_MASK_HIGH;
134
135 return !(~bits & mask);
136}
137
138static unsigned sm_lookup_bitmap(void *addr, unsigned b)
139{
140 __le64 *words_le = addr;
141 __le64 *w_le = words_le + (b >> ENTRIES_SHIFT);
142 unsigned hi, lo;
143
144 b = (b & (ENTRIES_PER_WORD - 1)) << 1;
145 hi = !!test_bit_le(b, (void *) w_le);
146 lo = !!test_bit_le(b + 1, (void *) w_le);
147 return (hi << 1) | lo;
148}
149
150static void sm_set_bitmap(void *addr, unsigned b, unsigned val)
151{
152 __le64 *words_le = addr;
153 __le64 *w_le = words_le + (b >> ENTRIES_SHIFT);
154
155 b = (b & (ENTRIES_PER_WORD - 1)) << 1;
156
157 if (val & 2)
158 __set_bit_le(b, (void *) w_le);
159 else
160 __clear_bit_le(b, (void *) w_le);
161
162 if (val & 1)
163 __set_bit_le(b + 1, (void *) w_le);
164 else
165 __clear_bit_le(b + 1, (void *) w_le);
166}
167
168static int sm_find_free(void *addr, unsigned begin, unsigned end,
169 unsigned *result)
170{
171 while (begin < end) {
172 if (!(begin & (ENTRIES_PER_WORD - 1)) &&
173 bitmap_word_used(addr, begin)) {
174 begin += ENTRIES_PER_WORD;
175 continue;
176 }
177
178 if (!sm_lookup_bitmap(addr, begin)) {
179 *result = begin;
180 return 0;
181 }
182
183 begin++;
184 }
185
186 return -ENOSPC;
187}
188
189/*----------------------------------------------------------------*/
190
191static int sm_ll_init(struct ll_disk *ll, struct dm_transaction_manager *tm)
192{
193 ll->tm = tm;
194
195 ll->bitmap_info.tm = tm;
196 ll->bitmap_info.levels = 1;
197
198 /*
199 * Because the new bitmap blocks are created via a shadow
200 * operation, the old entry has already had its reference count
201 * decremented and we don't need the btree to do any bookkeeping.
202 */
203 ll->bitmap_info.value_type.size = sizeof(struct disk_index_entry);
204 ll->bitmap_info.value_type.inc = NULL;
205 ll->bitmap_info.value_type.dec = NULL;
206 ll->bitmap_info.value_type.equal = NULL;
207
208 ll->ref_count_info.tm = tm;
209 ll->ref_count_info.levels = 1;
210 ll->ref_count_info.value_type.size = sizeof(uint32_t);
211 ll->ref_count_info.value_type.inc = NULL;
212 ll->ref_count_info.value_type.dec = NULL;
213 ll->ref_count_info.value_type.equal = NULL;
214
215 ll->block_size = dm_bm_block_size(dm_tm_get_bm(tm));
216
217 if (ll->block_size > (1 << 30)) {
218 DMERR("block size too big to hold bitmaps");
219 return -EINVAL;
220 }
221
222 ll->entries_per_block = (ll->block_size - sizeof(struct disk_bitmap_header)) *
223 ENTRIES_PER_BYTE;
224 ll->nr_blocks = 0;
225 ll->bitmap_root = 0;
226 ll->ref_count_root = 0;
Joe Thornberf4b90362012-07-27 15:08:06 +0100227 ll->bitmap_index_changed = false;
Joe Thornber3241b1d2011-10-31 20:19:11 +0000228
229 return 0;
230}
231
232int sm_ll_extend(struct ll_disk *ll, dm_block_t extra_blocks)
233{
234 int r;
235 dm_block_t i, nr_blocks, nr_indexes;
236 unsigned old_blocks, blocks;
237
238 nr_blocks = ll->nr_blocks + extra_blocks;
239 old_blocks = dm_sector_div_up(ll->nr_blocks, ll->entries_per_block);
240 blocks = dm_sector_div_up(nr_blocks, ll->entries_per_block);
241
242 nr_indexes = dm_sector_div_up(nr_blocks, ll->entries_per_block);
243 if (nr_indexes > ll->max_entries(ll)) {
244 DMERR("space map too large");
245 return -EINVAL;
246 }
247
248 for (i = old_blocks; i < blocks; i++) {
249 struct dm_block *b;
250 struct disk_index_entry idx;
251
252 r = dm_tm_new_block(ll->tm, &dm_sm_bitmap_validator, &b);
253 if (r < 0)
254 return r;
255 idx.blocknr = cpu_to_le64(dm_block_location(b));
256
257 r = dm_tm_unlock(ll->tm, b);
258 if (r < 0)
259 return r;
260
261 idx.nr_free = cpu_to_le32(ll->entries_per_block);
262 idx.none_free_before = 0;
263
264 r = ll->save_ie(ll, i, &idx);
265 if (r < 0)
266 return r;
267 }
268
269 ll->nr_blocks = nr_blocks;
270 return 0;
271}
272
273int sm_ll_lookup_bitmap(struct ll_disk *ll, dm_block_t b, uint32_t *result)
274{
275 int r;
276 dm_block_t index = b;
277 struct disk_index_entry ie_disk;
278 struct dm_block *blk;
279
280 b = do_div(index, ll->entries_per_block);
281 r = ll->load_ie(ll, index, &ie_disk);
282 if (r < 0)
283 return r;
284
285 r = dm_tm_read_lock(ll->tm, le64_to_cpu(ie_disk.blocknr),
286 &dm_sm_bitmap_validator, &blk);
287 if (r < 0)
288 return r;
289
290 *result = sm_lookup_bitmap(dm_bitmap_data(blk), b);
291
292 return dm_tm_unlock(ll->tm, blk);
293}
294
Joe Thornberf7220632013-08-09 13:04:56 +0100295static int sm_ll_lookup_big_ref_count(struct ll_disk *ll, dm_block_t b,
296 uint32_t *result)
Joe Thornber3241b1d2011-10-31 20:19:11 +0000297{
298 __le32 le_rc;
Joe Thornberf7220632013-08-09 13:04:56 +0100299 int r;
Joe Thornber3241b1d2011-10-31 20:19:11 +0000300
301 r = dm_btree_lookup(&ll->ref_count_info, ll->ref_count_root, &b, &le_rc);
302 if (r < 0)
303 return r;
304
305 *result = le32_to_cpu(le_rc);
306
307 return r;
308}
309
Joe Thornberf7220632013-08-09 13:04:56 +0100310int sm_ll_lookup(struct ll_disk *ll, dm_block_t b, uint32_t *result)
311{
312 int r = sm_ll_lookup_bitmap(ll, b, result);
313
314 if (r)
315 return r;
316
317 if (*result != 3)
318 return r;
319
320 return sm_ll_lookup_big_ref_count(ll, b, result);
321}
322
Joe Thornber3241b1d2011-10-31 20:19:11 +0000323int sm_ll_find_free_block(struct ll_disk *ll, dm_block_t begin,
324 dm_block_t end, dm_block_t *result)
325{
326 int r;
327 struct disk_index_entry ie_disk;
328 dm_block_t i, index_begin = begin;
329 dm_block_t index_end = dm_sector_div_up(end, ll->entries_per_block);
330
331 /*
332 * FIXME: Use shifts
333 */
334 begin = do_div(index_begin, ll->entries_per_block);
335 end = do_div(end, ll->entries_per_block);
336
337 for (i = index_begin; i < index_end; i++, begin = 0) {
338 struct dm_block *blk;
339 unsigned position;
340 uint32_t bit_end;
341
342 r = ll->load_ie(ll, i, &ie_disk);
343 if (r < 0)
344 return r;
345
346 if (le32_to_cpu(ie_disk.nr_free) == 0)
347 continue;
348
349 r = dm_tm_read_lock(ll->tm, le64_to_cpu(ie_disk.blocknr),
350 &dm_sm_bitmap_validator, &blk);
351 if (r < 0)
352 return r;
353
354 bit_end = (i == index_end - 1) ? end : ll->entries_per_block;
355
356 r = sm_find_free(dm_bitmap_data(blk),
357 max_t(unsigned, begin, le32_to_cpu(ie_disk.none_free_before)),
358 bit_end, &position);
359 if (r == -ENOSPC) {
360 /*
361 * This might happen because we started searching
362 * part way through the bitmap.
363 */
364 dm_tm_unlock(ll->tm, blk);
365 continue;
366
367 } else if (r < 0) {
368 dm_tm_unlock(ll->tm, blk);
369 return r;
370 }
371
372 r = dm_tm_unlock(ll->tm, blk);
373 if (r < 0)
374 return r;
375
376 *result = i * ll->entries_per_block + (dm_block_t) position;
377 return 0;
378 }
379
380 return -ENOSPC;
381}
382
Joe Thornberf7220632013-08-09 13:04:56 +0100383static int sm_ll_mutate(struct ll_disk *ll, dm_block_t b,
384 uint32_t (*mutator)(void *context, uint32_t old),
385 void *context, enum allocation_event *ev)
Joe Thornber3241b1d2011-10-31 20:19:11 +0000386{
387 int r;
Joe Thornberf7220632013-08-09 13:04:56 +0100388 uint32_t bit, old, ref_count;
Joe Thornber3241b1d2011-10-31 20:19:11 +0000389 struct dm_block *nb;
390 dm_block_t index = b;
391 struct disk_index_entry ie_disk;
392 void *bm_le;
393 int inc;
394
395 bit = do_div(index, ll->entries_per_block);
396 r = ll->load_ie(ll, index, &ie_disk);
397 if (r < 0)
398 return r;
399
400 r = dm_tm_shadow_block(ll->tm, le64_to_cpu(ie_disk.blocknr),
401 &dm_sm_bitmap_validator, &nb, &inc);
402 if (r < 0) {
403 DMERR("dm_tm_shadow_block() failed");
404 return r;
405 }
406 ie_disk.blocknr = cpu_to_le64(dm_block_location(nb));
407
408 bm_le = dm_bitmap_data(nb);
409 old = sm_lookup_bitmap(bm_le, bit);
410
Joe Thornberf7220632013-08-09 13:04:56 +0100411 if (old > 2) {
412 r = sm_ll_lookup_big_ref_count(ll, b, &old);
413 if (r < 0)
414 return r;
415 }
416
417 ref_count = mutator(context, old);
418
Joe Thornber3241b1d2011-10-31 20:19:11 +0000419 if (ref_count <= 2) {
420 sm_set_bitmap(bm_le, bit, ref_count);
421
422 r = dm_tm_unlock(ll->tm, nb);
423 if (r < 0)
424 return r;
425
Joe Thornber3241b1d2011-10-31 20:19:11 +0000426 if (old > 2) {
427 r = dm_btree_remove(&ll->ref_count_info,
428 ll->ref_count_root,
429 &b, &ll->ref_count_root);
430 if (r)
431 return r;
432 }
Joe Thornber3241b1d2011-10-31 20:19:11 +0000433
434 } else {
435 __le32 le_rc = cpu_to_le32(ref_count);
436
437 sm_set_bitmap(bm_le, bit, 3);
438 r = dm_tm_unlock(ll->tm, nb);
439 if (r < 0)
440 return r;
441
442 __dm_bless_for_disk(&le_rc);
443 r = dm_btree_insert(&ll->ref_count_info, ll->ref_count_root,
444 &b, &le_rc, &ll->ref_count_root);
445 if (r < 0) {
446 DMERR("ref count insert failed");
447 return r;
448 }
449 }
450
451 if (ref_count && !old) {
452 *ev = SM_ALLOC;
453 ll->nr_allocated++;
Wei Yongjun0bcf0872012-10-12 16:59:47 +0100454 le32_add_cpu(&ie_disk.nr_free, -1);
Joe Thornber3241b1d2011-10-31 20:19:11 +0000455 if (le32_to_cpu(ie_disk.none_free_before) == bit)
456 ie_disk.none_free_before = cpu_to_le32(bit + 1);
457
458 } else if (old && !ref_count) {
459 *ev = SM_FREE;
460 ll->nr_allocated--;
Wei Yongjun0bcf0872012-10-12 16:59:47 +0100461 le32_add_cpu(&ie_disk.nr_free, 1);
Joe Thornber3241b1d2011-10-31 20:19:11 +0000462 ie_disk.none_free_before = cpu_to_le32(min(le32_to_cpu(ie_disk.none_free_before), bit));
463 }
464
465 return ll->save_ie(ll, index, &ie_disk);
466}
467
Joe Thornberf7220632013-08-09 13:04:56 +0100468static uint32_t set_ref_count(void *context, uint32_t old)
469{
470 return *((uint32_t *) context);
471}
472
473int sm_ll_insert(struct ll_disk *ll, dm_block_t b,
474 uint32_t ref_count, enum allocation_event *ev)
475{
476 return sm_ll_mutate(ll, b, set_ref_count, &ref_count, ev);
477}
478
479static uint32_t inc_ref_count(void *context, uint32_t old)
480{
481 return old + 1;
482}
483
Joe Thornber3241b1d2011-10-31 20:19:11 +0000484int sm_ll_inc(struct ll_disk *ll, dm_block_t b, enum allocation_event *ev)
485{
Joe Thornberf7220632013-08-09 13:04:56 +0100486 return sm_ll_mutate(ll, b, inc_ref_count, NULL, ev);
487}
Joe Thornber3241b1d2011-10-31 20:19:11 +0000488
Joe Thornberf7220632013-08-09 13:04:56 +0100489static uint32_t dec_ref_count(void *context, uint32_t old)
490{
491 return old - 1;
Joe Thornber3241b1d2011-10-31 20:19:11 +0000492}
493
494int sm_ll_dec(struct ll_disk *ll, dm_block_t b, enum allocation_event *ev)
495{
Joe Thornberf7220632013-08-09 13:04:56 +0100496 return sm_ll_mutate(ll, b, dec_ref_count, NULL, ev);
Joe Thornber3241b1d2011-10-31 20:19:11 +0000497}
498
499int sm_ll_commit(struct ll_disk *ll)
500{
Joe Thornberf4b90362012-07-27 15:08:06 +0100501 int r = 0;
502
503 if (ll->bitmap_index_changed) {
504 r = ll->commit(ll);
505 if (!r)
506 ll->bitmap_index_changed = false;
507 }
508
509 return r;
Joe Thornber3241b1d2011-10-31 20:19:11 +0000510}
511
512/*----------------------------------------------------------------*/
513
514static int metadata_ll_load_ie(struct ll_disk *ll, dm_block_t index,
515 struct disk_index_entry *ie)
516{
517 memcpy(ie, ll->mi_le.index + index, sizeof(*ie));
518 return 0;
519}
520
521static int metadata_ll_save_ie(struct ll_disk *ll, dm_block_t index,
522 struct disk_index_entry *ie)
523{
Joe Thornberf4b90362012-07-27 15:08:06 +0100524 ll->bitmap_index_changed = true;
Joe Thornber3241b1d2011-10-31 20:19:11 +0000525 memcpy(ll->mi_le.index + index, ie, sizeof(*ie));
526 return 0;
527}
528
529static int metadata_ll_init_index(struct ll_disk *ll)
530{
531 int r;
532 struct dm_block *b;
533
534 r = dm_tm_new_block(ll->tm, &index_validator, &b);
535 if (r < 0)
536 return r;
537
538 memcpy(dm_block_data(b), &ll->mi_le, sizeof(ll->mi_le));
539 ll->bitmap_root = dm_block_location(b);
540
541 return dm_tm_unlock(ll->tm, b);
542}
543
544static int metadata_ll_open(struct ll_disk *ll)
545{
546 int r;
547 struct dm_block *block;
548
549 r = dm_tm_read_lock(ll->tm, ll->bitmap_root,
550 &index_validator, &block);
551 if (r)
552 return r;
553
554 memcpy(&ll->mi_le, dm_block_data(block), sizeof(ll->mi_le));
555 return dm_tm_unlock(ll->tm, block);
556}
557
558static dm_block_t metadata_ll_max_entries(struct ll_disk *ll)
559{
560 return MAX_METADATA_BITMAPS;
561}
562
563static int metadata_ll_commit(struct ll_disk *ll)
564{
565 int r, inc;
566 struct dm_block *b;
567
568 r = dm_tm_shadow_block(ll->tm, ll->bitmap_root, &index_validator, &b, &inc);
569 if (r)
570 return r;
571
572 memcpy(dm_block_data(b), &ll->mi_le, sizeof(ll->mi_le));
573 ll->bitmap_root = dm_block_location(b);
574
575 return dm_tm_unlock(ll->tm, b);
576}
577
578int sm_ll_new_metadata(struct ll_disk *ll, struct dm_transaction_manager *tm)
579{
580 int r;
581
582 r = sm_ll_init(ll, tm);
583 if (r < 0)
584 return r;
585
586 ll->load_ie = metadata_ll_load_ie;
587 ll->save_ie = metadata_ll_save_ie;
588 ll->init_index = metadata_ll_init_index;
589 ll->open_index = metadata_ll_open;
590 ll->max_entries = metadata_ll_max_entries;
591 ll->commit = metadata_ll_commit;
592
593 ll->nr_blocks = 0;
594 ll->nr_allocated = 0;
595
596 r = ll->init_index(ll);
597 if (r < 0)
598 return r;
599
600 r = dm_btree_empty(&ll->ref_count_info, &ll->ref_count_root);
601 if (r < 0)
602 return r;
603
604 return 0;
605}
606
607int sm_ll_open_metadata(struct ll_disk *ll, struct dm_transaction_manager *tm,
608 void *root_le, size_t len)
609{
610 int r;
611 struct disk_sm_root *smr = root_le;
612
613 if (len < sizeof(struct disk_sm_root)) {
614 DMERR("sm_metadata root too small");
615 return -ENOMEM;
616 }
617
618 r = sm_ll_init(ll, tm);
619 if (r < 0)
620 return r;
621
622 ll->load_ie = metadata_ll_load_ie;
623 ll->save_ie = metadata_ll_save_ie;
624 ll->init_index = metadata_ll_init_index;
625 ll->open_index = metadata_ll_open;
626 ll->max_entries = metadata_ll_max_entries;
627 ll->commit = metadata_ll_commit;
628
629 ll->nr_blocks = le64_to_cpu(smr->nr_blocks);
630 ll->nr_allocated = le64_to_cpu(smr->nr_allocated);
631 ll->bitmap_root = le64_to_cpu(smr->bitmap_root);
632 ll->ref_count_root = le64_to_cpu(smr->ref_count_root);
633
634 return ll->open_index(ll);
635}
636
637/*----------------------------------------------------------------*/
638
639static int disk_ll_load_ie(struct ll_disk *ll, dm_block_t index,
640 struct disk_index_entry *ie)
641{
642 return dm_btree_lookup(&ll->bitmap_info, ll->bitmap_root, &index, ie);
643}
644
645static int disk_ll_save_ie(struct ll_disk *ll, dm_block_t index,
646 struct disk_index_entry *ie)
647{
648 __dm_bless_for_disk(ie);
649 return dm_btree_insert(&ll->bitmap_info, ll->bitmap_root,
650 &index, ie, &ll->bitmap_root);
651}
652
653static int disk_ll_init_index(struct ll_disk *ll)
654{
655 return dm_btree_empty(&ll->bitmap_info, &ll->bitmap_root);
656}
657
658static int disk_ll_open(struct ll_disk *ll)
659{
660 /* nothing to do */
661 return 0;
662}
663
664static dm_block_t disk_ll_max_entries(struct ll_disk *ll)
665{
666 return -1ULL;
667}
668
669static int disk_ll_commit(struct ll_disk *ll)
670{
671 return 0;
672}
673
674int sm_ll_new_disk(struct ll_disk *ll, struct dm_transaction_manager *tm)
675{
676 int r;
677
678 r = sm_ll_init(ll, tm);
679 if (r < 0)
680 return r;
681
682 ll->load_ie = disk_ll_load_ie;
683 ll->save_ie = disk_ll_save_ie;
684 ll->init_index = disk_ll_init_index;
685 ll->open_index = disk_ll_open;
686 ll->max_entries = disk_ll_max_entries;
687 ll->commit = disk_ll_commit;
688
689 ll->nr_blocks = 0;
690 ll->nr_allocated = 0;
691
692 r = ll->init_index(ll);
693 if (r < 0)
694 return r;
695
696 r = dm_btree_empty(&ll->ref_count_info, &ll->ref_count_root);
697 if (r < 0)
698 return r;
699
700 return 0;
701}
702
703int sm_ll_open_disk(struct ll_disk *ll, struct dm_transaction_manager *tm,
704 void *root_le, size_t len)
705{
706 int r;
707 struct disk_sm_root *smr = root_le;
708
709 if (len < sizeof(struct disk_sm_root)) {
710 DMERR("sm_metadata root too small");
711 return -ENOMEM;
712 }
713
714 r = sm_ll_init(ll, tm);
715 if (r < 0)
716 return r;
717
718 ll->load_ie = disk_ll_load_ie;
719 ll->save_ie = disk_ll_save_ie;
720 ll->init_index = disk_ll_init_index;
721 ll->open_index = disk_ll_open;
722 ll->max_entries = disk_ll_max_entries;
723 ll->commit = disk_ll_commit;
724
725 ll->nr_blocks = le64_to_cpu(smr->nr_blocks);
726 ll->nr_allocated = le64_to_cpu(smr->nr_allocated);
727 ll->bitmap_root = le64_to_cpu(smr->bitmap_root);
728 ll->ref_count_root = le64_to_cpu(smr->ref_count_root);
729
730 return ll->open_index(ll);
731}
732
733/*----------------------------------------------------------------*/