Linux Audio

Check our new training course

Embedded Linux Audio

Check our new training course
with Creative Commons CC-BY-SA
lecture materials

Bootlin logo

Elixir Cross Referencer

Loading...
   1
   2
   3
   4
   5
   6
   7
   8
   9
  10
  11
  12
  13
  14
  15
  16
  17
  18
  19
  20
  21
  22
  23
  24
  25
  26
  27
  28
  29
  30
  31
  32
  33
  34
  35
  36
  37
  38
  39
  40
  41
  42
  43
  44
  45
  46
  47
  48
  49
  50
  51
  52
  53
  54
  55
  56
  57
  58
  59
  60
  61
  62
  63
  64
  65
  66
  67
  68
  69
  70
  71
  72
  73
  74
  75
  76
  77
  78
  79
  80
  81
  82
  83
  84
  85
  86
  87
  88
  89
  90
  91
  92
  93
  94
  95
  96
  97
  98
  99
 100
 101
 102
 103
 104
 105
 106
 107
 108
 109
 110
 111
 112
 113
 114
 115
 116
 117
 118
 119
 120
 121
 122
 123
 124
 125
 126
 127
 128
 129
 130
 131
 132
 133
 134
 135
 136
 137
 138
 139
 140
 141
 142
 143
 144
 145
 146
 147
 148
 149
 150
 151
 152
 153
 154
 155
 156
 157
 158
 159
 160
 161
 162
 163
 164
 165
 166
 167
 168
 169
 170
 171
 172
 173
 174
 175
 176
 177
 178
 179
 180
 181
 182
 183
 184
 185
 186
 187
 188
 189
 190
 191
 192
 193
 194
 195
 196
 197
 198
 199
 200
 201
 202
 203
 204
 205
 206
 207
 208
 209
 210
 211
 212
 213
 214
 215
 216
 217
 218
 219
 220
 221
 222
 223
 224
 225
 226
 227
 228
 229
 230
 231
 232
 233
 234
 235
 236
 237
 238
 239
 240
 241
 242
 243
 244
 245
 246
 247
 248
 249
 250
 251
 252
 253
 254
 255
 256
 257
 258
 259
 260
 261
 262
 263
 264
 265
 266
 267
 268
 269
 270
 271
 272
 273
 274
 275
 276
 277
 278
 279
 280
 281
 282
 283
 284
 285
 286
 287
 288
 289
 290
 291
 292
 293
 294
 295
 296
 297
 298
 299
 300
 301
 302
 303
 304
 305
 306
 307
 308
 309
 310
 311
 312
 313
 314
 315
 316
 317
 318
 319
 320
 321
 322
 323
 324
 325
 326
 327
 328
 329
 330
 331
 332
 333
 334
 335
 336
 337
 338
 339
 340
 341
 342
 343
 344
 345
 346
 347
 348
 349
 350
 351
 352
 353
 354
 355
 356
 357
 358
 359
 360
 361
 362
 363
 364
 365
 366
 367
 368
 369
 370
 371
 372
 373
 374
 375
 376
 377
 378
 379
 380
 381
 382
 383
 384
 385
 386
 387
 388
 389
 390
 391
 392
 393
 394
 395
 396
 397
 398
 399
 400
 401
 402
 403
 404
 405
 406
 407
 408
 409
 410
 411
 412
 413
 414
 415
 416
 417
 418
 419
 420
 421
 422
 423
 424
 425
 426
 427
 428
 429
 430
 431
 432
 433
 434
 435
 436
 437
 438
 439
 440
 441
 442
 443
 444
 445
 446
 447
 448
 449
 450
 451
 452
 453
 454
 455
 456
 457
 458
 459
 460
 461
 462
 463
 464
 465
 466
 467
 468
 469
 470
 471
 472
 473
 474
 475
 476
 477
 478
 479
 480
 481
 482
 483
 484
 485
 486
 487
 488
 489
 490
 491
 492
 493
 494
 495
 496
 497
 498
 499
 500
 501
 502
 503
 504
 505
 506
 507
 508
 509
 510
 511
 512
 513
 514
 515
 516
 517
 518
 519
 520
 521
 522
 523
 524
 525
 526
 527
 528
 529
 530
 531
 532
 533
 534
 535
 536
 537
 538
 539
 540
 541
 542
 543
 544
 545
 546
 547
 548
 549
 550
 551
 552
 553
 554
 555
 556
 557
 558
 559
 560
 561
 562
 563
 564
 565
 566
 567
 568
 569
 570
 571
 572
 573
 574
 575
 576
 577
 578
 579
 580
 581
 582
 583
 584
 585
 586
 587
 588
 589
 590
 591
 592
 593
 594
 595
 596
 597
 598
 599
 600
 601
 602
 603
 604
 605
 606
 607
 608
 609
 610
 611
 612
 613
 614
 615
 616
 617
 618
 619
 620
 621
 622
 623
 624
 625
 626
 627
 628
 629
 630
 631
 632
 633
 634
 635
 636
 637
 638
 639
 640
 641
 642
 643
 644
 645
 646
 647
 648
 649
 650
 651
 652
 653
 654
 655
 656
 657
 658
 659
 660
 661
 662
 663
 664
 665
 666
 667
 668
 669
 670
 671
 672
 673
 674
 675
 676
 677
 678
 679
 680
 681
 682
 683
 684
 685
 686
 687
 688
 689
 690
 691
 692
 693
 694
 695
 696
 697
 698
 699
 700
 701
 702
 703
 704
 705
 706
 707
 708
 709
 710
 711
 712
 713
 714
 715
 716
 717
 718
 719
 720
 721
 722
 723
 724
 725
 726
 727
 728
 729
 730
 731
 732
 733
 734
 735
 736
 737
 738
 739
 740
 741
 742
 743
 744
 745
 746
 747
 748
 749
 750
 751
 752
 753
 754
 755
 756
 757
 758
 759
 760
 761
 762
 763
 764
 765
 766
 767
 768
 769
 770
 771
 772
 773
 774
 775
 776
 777
 778
 779
 780
 781
 782
 783
 784
 785
 786
 787
 788
 789
 790
 791
 792
 793
 794
 795
 796
 797
 798
 799
 800
 801
 802
 803
 804
 805
 806
 807
 808
 809
 810
 811
 812
 813
 814
 815
 816
 817
 818
 819
 820
 821
 822
 823
 824
 825
 826
 827
 828
 829
 830
 831
 832
 833
 834
 835
 836
 837
 838
 839
 840
 841
 842
 843
 844
 845
 846
 847
 848
 849
 850
 851
 852
 853
 854
 855
 856
 857
 858
 859
 860
 861
 862
 863
 864
 865
 866
 867
 868
 869
 870
 871
 872
 873
 874
 875
 876
 877
 878
 879
 880
 881
 882
 883
 884
 885
 886
 887
 888
 889
 890
 891
 892
 893
 894
 895
 896
 897
 898
 899
 900
 901
 902
 903
 904
 905
 906
 907
 908
 909
 910
 911
 912
 913
 914
 915
 916
 917
 918
 919
 920
 921
 922
 923
 924
 925
 926
 927
 928
 929
 930
 931
 932
 933
 934
 935
 936
 937
 938
 939
 940
 941
 942
 943
 944
 945
 946
 947
 948
 949
 950
 951
 952
 953
 954
 955
 956
 957
 958
 959
 960
 961
 962
 963
 964
 965
 966
 967
 968
 969
 970
 971
 972
 973
 974
 975
 976
 977
 978
 979
 980
 981
 982
 983
 984
 985
 986
 987
 988
 989
 990
 991
 992
 993
 994
 995
 996
 997
 998
 999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
// SPDX-License-Identifier: GPL-2.0-only
/*
 * Copyright (C) 2011 Red Hat, Inc.
 *
 * This file is released under the GPL.
 */

#include "dm-space-map-common.h"
#include "dm-transaction-manager.h"
#include "dm-btree-internal.h"
#include "dm-persistent-data-internal.h"

#include <linux/bitops.h>
#include <linux/device-mapper.h>

#define DM_MSG_PREFIX "space map common"

/*----------------------------------------------------------------*/

/*
 * Index validator.
 */
#define INDEX_CSUM_XOR 160478

static void index_prepare_for_write(struct dm_block_validator *v,
				    struct dm_block *b,
				    size_t block_size)
{
	struct disk_metadata_index *mi_le = dm_block_data(b);

	mi_le->blocknr = cpu_to_le64(dm_block_location(b));
	mi_le->csum = cpu_to_le32(dm_bm_checksum(&mi_le->padding,
						 block_size - sizeof(__le32),
						 INDEX_CSUM_XOR));
}

static int index_check(struct dm_block_validator *v,
		       struct dm_block *b,
		       size_t block_size)
{
	struct disk_metadata_index *mi_le = dm_block_data(b);
	__le32 csum_disk;

	if (dm_block_location(b) != le64_to_cpu(mi_le->blocknr)) {
		DMERR_LIMIT("%s failed: blocknr %llu != wanted %llu", __func__,
			    le64_to_cpu(mi_le->blocknr), dm_block_location(b));
		return -ENOTBLK;
	}

	csum_disk = cpu_to_le32(dm_bm_checksum(&mi_le->padding,
					       block_size - sizeof(__le32),
					       INDEX_CSUM_XOR));
	if (csum_disk != mi_le->csum) {
		DMERR_LIMIT("i%s failed: csum %u != wanted %u", __func__,
			    le32_to_cpu(csum_disk), le32_to_cpu(mi_le->csum));
		return -EILSEQ;
	}

	return 0;
}

static struct dm_block_validator index_validator = {
	.name = "index",
	.prepare_for_write = index_prepare_for_write,
	.check = index_check
};

/*----------------------------------------------------------------*/

/*
 * Bitmap validator
 */
#define BITMAP_CSUM_XOR 240779

static void dm_bitmap_prepare_for_write(struct dm_block_validator *v,
					struct dm_block *b,
					size_t block_size)
{
	struct disk_bitmap_header *disk_header = dm_block_data(b);

	disk_header->blocknr = cpu_to_le64(dm_block_location(b));
	disk_header->csum = cpu_to_le32(dm_bm_checksum(&disk_header->not_used,
						       block_size - sizeof(__le32),
						       BITMAP_CSUM_XOR));
}

static int dm_bitmap_check(struct dm_block_validator *v,
			   struct dm_block *b,
			   size_t block_size)
{
	struct disk_bitmap_header *disk_header = dm_block_data(b);
	__le32 csum_disk;

	if (dm_block_location(b) != le64_to_cpu(disk_header->blocknr)) {
		DMERR_LIMIT("bitmap check failed: blocknr %llu != wanted %llu",
			    le64_to_cpu(disk_header->blocknr), dm_block_location(b));
		return -ENOTBLK;
	}

	csum_disk = cpu_to_le32(dm_bm_checksum(&disk_header->not_used,
					       block_size - sizeof(__le32),
					       BITMAP_CSUM_XOR));
	if (csum_disk != disk_header->csum) {
		DMERR_LIMIT("bitmap check failed: csum %u != wanted %u",
			    le32_to_cpu(csum_disk), le32_to_cpu(disk_header->csum));
		return -EILSEQ;
	}

	return 0;
}

static struct dm_block_validator dm_sm_bitmap_validator = {
	.name = "sm_bitmap",
	.prepare_for_write = dm_bitmap_prepare_for_write,
	.check = dm_bitmap_check,
};

/*----------------------------------------------------------------*/

#define ENTRIES_PER_WORD 32
#define ENTRIES_SHIFT	5

static void *dm_bitmap_data(struct dm_block *b)
{
	return dm_block_data(b) + sizeof(struct disk_bitmap_header);
}

#define WORD_MASK_HIGH 0xAAAAAAAAAAAAAAAAULL

static unsigned int dm_bitmap_word_used(void *addr, unsigned int b)
{
	__le64 *words_le = addr;
	__le64 *w_le = words_le + (b >> ENTRIES_SHIFT);

	uint64_t bits = le64_to_cpu(*w_le);
	uint64_t mask = (bits + WORD_MASK_HIGH + 1) & WORD_MASK_HIGH;

	return !(~bits & mask);
}

static unsigned int sm_lookup_bitmap(void *addr, unsigned int b)
{
	__le64 *words_le = addr;
	__le64 *w_le = words_le + (b >> ENTRIES_SHIFT);
	unsigned int hi, lo;

	b = (b & (ENTRIES_PER_WORD - 1)) << 1;
	hi = !!test_bit_le(b, (void *) w_le);
	lo = !!test_bit_le(b + 1, (void *) w_le);
	return (hi << 1) | lo;
}

static void sm_set_bitmap(void *addr, unsigned int b, unsigned int val)
{
	__le64 *words_le = addr;
	__le64 *w_le = words_le + (b >> ENTRIES_SHIFT);

	b = (b & (ENTRIES_PER_WORD - 1)) << 1;

	if (val & 2)
		__set_bit_le(b, (void *) w_le);
	else
		__clear_bit_le(b, (void *) w_le);

	if (val & 1)
		__set_bit_le(b + 1, (void *) w_le);
	else
		__clear_bit_le(b + 1, (void *) w_le);
}

static int sm_find_free(void *addr, unsigned int begin, unsigned int end,
			unsigned int *result)
{
	while (begin < end) {
		if (!(begin & (ENTRIES_PER_WORD - 1)) &&
		    dm_bitmap_word_used(addr, begin)) {
			begin += ENTRIES_PER_WORD;
			continue;
		}

		if (!sm_lookup_bitmap(addr, begin)) {
			*result = begin;
			return 0;
		}

		begin++;
	}

	return -ENOSPC;
}

/*----------------------------------------------------------------*/

static int sm_ll_init(struct ll_disk *ll, struct dm_transaction_manager *tm)
{
	memset(ll, 0, sizeof(struct ll_disk));

	ll->tm = tm;

	ll->bitmap_info.tm = tm;
	ll->bitmap_info.levels = 1;

	/*
	 * Because the new bitmap blocks are created via a shadow
	 * operation, the old entry has already had its reference count
	 * decremented and we don't need the btree to do any bookkeeping.
	 */
	ll->bitmap_info.value_type.size = sizeof(struct disk_index_entry);
	ll->bitmap_info.value_type.inc = NULL;
	ll->bitmap_info.value_type.dec = NULL;
	ll->bitmap_info.value_type.equal = NULL;

	ll->ref_count_info.tm = tm;
	ll->ref_count_info.levels = 1;
	ll->ref_count_info.value_type.size = sizeof(uint32_t);
	ll->ref_count_info.value_type.inc = NULL;
	ll->ref_count_info.value_type.dec = NULL;
	ll->ref_count_info.value_type.equal = NULL;

	ll->block_size = dm_bm_block_size(dm_tm_get_bm(tm));

	if (ll->block_size > (1 << 30)) {
		DMERR("block size too big to hold bitmaps");
		return -EINVAL;
	}

	ll->entries_per_block = (ll->block_size - sizeof(struct disk_bitmap_header)) *
		ENTRIES_PER_BYTE;
	ll->nr_blocks = 0;
	ll->bitmap_root = 0;
	ll->ref_count_root = 0;
	ll->bitmap_index_changed = false;

	return 0;
}

int sm_ll_extend(struct ll_disk *ll, dm_block_t extra_blocks)
{
	int r;
	dm_block_t i, nr_blocks, nr_indexes;
	unsigned int old_blocks, blocks;

	nr_blocks = ll->nr_blocks + extra_blocks;
	old_blocks = dm_sector_div_up(ll->nr_blocks, ll->entries_per_block);
	blocks = dm_sector_div_up(nr_blocks, ll->entries_per_block);

	nr_indexes = dm_sector_div_up(nr_blocks, ll->entries_per_block);
	if (nr_indexes > ll->max_entries(ll)) {
		DMERR("space map too large");
		return -EINVAL;
	}

	/*
	 * We need to set this before the dm_tm_new_block() call below.
	 */
	ll->nr_blocks = nr_blocks;
	for (i = old_blocks; i < blocks; i++) {
		struct dm_block *b;
		struct disk_index_entry idx;

		r = dm_tm_new_block(ll->tm, &dm_sm_bitmap_validator, &b);
		if (r < 0)
			return r;

		idx.blocknr = cpu_to_le64(dm_block_location(b));

		dm_tm_unlock(ll->tm, b);

		idx.nr_free = cpu_to_le32(ll->entries_per_block);
		idx.none_free_before = 0;

		r = ll->save_ie(ll, i, &idx);
		if (r < 0)
			return r;
	}

	return 0;
}

int sm_ll_lookup_bitmap(struct ll_disk *ll, dm_block_t b, uint32_t *result)
{
	int r;
	dm_block_t index = b;
	struct disk_index_entry ie_disk;
	struct dm_block *blk;

	if (b >= ll->nr_blocks) {
		DMERR_LIMIT("metadata block out of bounds");
		return -EINVAL;
	}

	b = do_div(index, ll->entries_per_block);
	r = ll->load_ie(ll, index, &ie_disk);
	if (r < 0)
		return r;

	r = dm_tm_read_lock(ll->tm, le64_to_cpu(ie_disk.blocknr),
			    &dm_sm_bitmap_validator, &blk);
	if (r < 0)
		return r;

	*result = sm_lookup_bitmap(dm_bitmap_data(blk), b);

	dm_tm_unlock(ll->tm, blk);

	return 0;
}

static int sm_ll_lookup_big_ref_count(struct ll_disk *ll, dm_block_t b,
				      uint32_t *result)
{
	__le32 le_rc;
	int r;

	r = dm_btree_lookup(&ll->ref_count_info, ll->ref_count_root, &b, &le_rc);
	if (r < 0)
		return r;

	*result = le32_to_cpu(le_rc);

	return r;
}

int sm_ll_lookup(struct ll_disk *ll, dm_block_t b, uint32_t *result)
{
	int r = sm_ll_lookup_bitmap(ll, b, result);

	if (r)
		return r;

	if (*result != 3)
		return r;

	return sm_ll_lookup_big_ref_count(ll, b, result);
}

int sm_ll_find_free_block(struct ll_disk *ll, dm_block_t begin,
			  dm_block_t end, dm_block_t *result)
{
	int r;
	struct disk_index_entry ie_disk;
	dm_block_t i, index_begin = begin;
	dm_block_t index_end = dm_sector_div_up(end, ll->entries_per_block);

	/*
	 * FIXME: Use shifts
	 */
	begin = do_div(index_begin, ll->entries_per_block);
	end = do_div(end, ll->entries_per_block);
	if (end == 0)
		end = ll->entries_per_block;

	for (i = index_begin; i < index_end; i++, begin = 0) {
		struct dm_block *blk;
		unsigned int position;
		uint32_t bit_end;

		r = ll->load_ie(ll, i, &ie_disk);
		if (r < 0)
			return r;

		if (le32_to_cpu(ie_disk.nr_free) == 0)
			continue;

		r = dm_tm_read_lock(ll->tm, le64_to_cpu(ie_disk.blocknr),
				    &dm_sm_bitmap_validator, &blk);
		if (r < 0)
			return r;

		bit_end = (i == index_end - 1) ?  end : ll->entries_per_block;

		r = sm_find_free(dm_bitmap_data(blk),
				 max_t(unsigned int, begin, le32_to_cpu(ie_disk.none_free_before)),
				 bit_end, &position);
		if (r == -ENOSPC) {
			/*
			 * This might happen because we started searching
			 * part way through the bitmap.
			 */
			dm_tm_unlock(ll->tm, blk);
			continue;
		}

		dm_tm_unlock(ll->tm, blk);

		*result = i * ll->entries_per_block + (dm_block_t) position;
		return 0;
	}

	return -ENOSPC;
}

int sm_ll_find_common_free_block(struct ll_disk *old_ll, struct ll_disk *new_ll,
				 dm_block_t begin, dm_block_t end, dm_block_t *b)
{
	int r;
	uint32_t count;

	do {
		r = sm_ll_find_free_block(new_ll, begin, new_ll->nr_blocks, b);
		if (r)
			break;

		/* double check this block wasn't used in the old transaction */
		if (*b >= old_ll->nr_blocks)
			count = 0;
		else {
			r = sm_ll_lookup(old_ll, *b, &count);
			if (r)
				break;

			if (count)
				begin = *b + 1;
		}
	} while (count);

	return r;
}

/*----------------------------------------------------------------*/

int sm_ll_insert(struct ll_disk *ll, dm_block_t b,
		 uint32_t ref_count, int32_t *nr_allocations)
{
	int r;
	uint32_t bit, old;
	struct dm_block *nb;
	dm_block_t index = b;
	struct disk_index_entry ie_disk;
	void *bm_le;
	int inc;

	bit = do_div(index, ll->entries_per_block);
	r = ll->load_ie(ll, index, &ie_disk);
	if (r < 0)
		return r;

	r = dm_tm_shadow_block(ll->tm, le64_to_cpu(ie_disk.blocknr),
			       &dm_sm_bitmap_validator, &nb, &inc);
	if (r < 0) {
		DMERR("dm_tm_shadow_block() failed");
		return r;
	}
	ie_disk.blocknr = cpu_to_le64(dm_block_location(nb));
	bm_le = dm_bitmap_data(nb);

	old = sm_lookup_bitmap(bm_le, bit);
	if (old > 2) {
		r = sm_ll_lookup_big_ref_count(ll, b, &old);
		if (r < 0) {
			dm_tm_unlock(ll->tm, nb);
			return r;
		}
	}

	if (r) {
		dm_tm_unlock(ll->tm, nb);
		return r;
	}

	if (ref_count <= 2) {
		sm_set_bitmap(bm_le, bit, ref_count);
		dm_tm_unlock(ll->tm, nb);

		if (old > 2) {
			r = dm_btree_remove(&ll->ref_count_info,
					    ll->ref_count_root,
					    &b, &ll->ref_count_root);
			if (r)
				return r;
		}

	} else {
		__le32 le_rc = cpu_to_le32(ref_count);

		sm_set_bitmap(bm_le, bit, 3);
		dm_tm_unlock(ll->tm, nb);

		__dm_bless_for_disk(&le_rc);
		r = dm_btree_insert(&ll->ref_count_info, ll->ref_count_root,
				    &b, &le_rc, &ll->ref_count_root);
		if (r < 0) {
			DMERR("ref count insert failed");
			return r;
		}
	}

	if (ref_count && !old) {
		*nr_allocations = 1;
		ll->nr_allocated++;
		le32_add_cpu(&ie_disk.nr_free, -1);
		if (le32_to_cpu(ie_disk.none_free_before) == bit)
			ie_disk.none_free_before = cpu_to_le32(bit + 1);

	} else if (old && !ref_count) {
		*nr_allocations = -1;
		ll->nr_allocated--;
		le32_add_cpu(&ie_disk.nr_free, 1);
		ie_disk.none_free_before = cpu_to_le32(min(le32_to_cpu(ie_disk.none_free_before), bit));
	} else
		*nr_allocations = 0;

	return ll->save_ie(ll, index, &ie_disk);
}

/*----------------------------------------------------------------*/

/*
 * Holds useful intermediate results for the range based inc and dec
 * operations.
 */
struct inc_context {
	struct disk_index_entry ie_disk;
	struct dm_block *bitmap_block;
	void *bitmap;

	struct dm_block *overflow_leaf;
};

static inline void init_inc_context(struct inc_context *ic)
{
	ic->bitmap_block = NULL;
	ic->bitmap = NULL;
	ic->overflow_leaf = NULL;
}

static inline void exit_inc_context(struct ll_disk *ll, struct inc_context *ic)
{
	if (ic->bitmap_block)
		dm_tm_unlock(ll->tm, ic->bitmap_block);
	if (ic->overflow_leaf)
		dm_tm_unlock(ll->tm, ic->overflow_leaf);
}

static inline void reset_inc_context(struct ll_disk *ll, struct inc_context *ic)
{
	exit_inc_context(ll, ic);
	init_inc_context(ic);
}

/*
 * Confirms a btree node contains a particular key at an index.
 */
static bool contains_key(struct btree_node *n, uint64_t key, int index)
{
	return index >= 0 &&
		index < le32_to_cpu(n->header.nr_entries) &&
		le64_to_cpu(n->keys[index]) == key;
}

static int __sm_ll_inc_overflow(struct ll_disk *ll, dm_block_t b, struct inc_context *ic)
{
	int r;
	int index;
	struct btree_node *n;
	__le32 *v_ptr;
	uint32_t rc;

	/*
	 * bitmap_block needs to be unlocked because getting the
	 * overflow_leaf may need to allocate, and thus use the space map.
	 */
	reset_inc_context(ll, ic);

	r = btree_get_overwrite_leaf(&ll->ref_count_info, ll->ref_count_root,
				     b, &index, &ll->ref_count_root, &ic->overflow_leaf);
	if (r < 0)
		return r;

	n = dm_block_data(ic->overflow_leaf);

	if (!contains_key(n, b, index)) {
		DMERR("overflow btree is missing an entry");
		return -EINVAL;
	}

	v_ptr = value_ptr(n, index);
	rc = le32_to_cpu(*v_ptr) + 1;
	*v_ptr = cpu_to_le32(rc);

	return 0;
}

static int sm_ll_inc_overflow(struct ll_disk *ll, dm_block_t b, struct inc_context *ic)
{
	int index;
	struct btree_node *n;
	__le32 *v_ptr;
	uint32_t rc;

	/*
	 * Do we already have the correct overflow leaf?
	 */
	if (ic->overflow_leaf) {
		n = dm_block_data(ic->overflow_leaf);
		index = lower_bound(n, b);
		if (contains_key(n, b, index)) {
			v_ptr = value_ptr(n, index);
			rc = le32_to_cpu(*v_ptr) + 1;
			*v_ptr = cpu_to_le32(rc);

			return 0;
		}
	}

	return __sm_ll_inc_overflow(ll, b, ic);
}

static inline int shadow_bitmap(struct ll_disk *ll, struct inc_context *ic)
{
	int r, inc;

	r = dm_tm_shadow_block(ll->tm, le64_to_cpu(ic->ie_disk.blocknr),
			       &dm_sm_bitmap_validator, &ic->bitmap_block, &inc);
	if (r < 0) {
		DMERR("dm_tm_shadow_block() failed");
		return r;
	}
	ic->ie_disk.blocknr = cpu_to_le64(dm_block_location(ic->bitmap_block));
	ic->bitmap = dm_bitmap_data(ic->bitmap_block);
	return 0;
}

/*
 * Once shadow_bitmap has been called, which always happens at the start of inc/dec,
 * we can reopen the bitmap with a simple write lock, rather than re calling
 * dm_tm_shadow_block().
 */
static inline int ensure_bitmap(struct ll_disk *ll, struct inc_context *ic)
{
	if (!ic->bitmap_block) {
		int r = dm_bm_write_lock(dm_tm_get_bm(ll->tm), le64_to_cpu(ic->ie_disk.blocknr),
					 &dm_sm_bitmap_validator, &ic->bitmap_block);
		if (r) {
			DMERR("unable to re-get write lock for bitmap");
			return r;
		}
		ic->bitmap = dm_bitmap_data(ic->bitmap_block);
	}

	return 0;
}

/*
 * Loops round incrementing entries in a single bitmap.
 */
static inline int sm_ll_inc_bitmap(struct ll_disk *ll, dm_block_t b,
				   uint32_t bit, uint32_t bit_end,
				   int32_t *nr_allocations, dm_block_t *new_b,
				   struct inc_context *ic)
{
	int r;
	__le32 le_rc;
	uint32_t old;

	for (; bit != bit_end; bit++, b++) {
		/*
		 * We only need to drop the bitmap if we need to find a new btree
		 * leaf for the overflow.  So if it was dropped last iteration,
		 * we now re-get it.
		 */
		r = ensure_bitmap(ll, ic);
		if (r)
			return r;

		old = sm_lookup_bitmap(ic->bitmap, bit);
		switch (old) {
		case 0:
			/* inc bitmap, adjust nr_allocated */
			sm_set_bitmap(ic->bitmap, bit, 1);
			(*nr_allocations)++;
			ll->nr_allocated++;
			le32_add_cpu(&ic->ie_disk.nr_free, -1);
			if (le32_to_cpu(ic->ie_disk.none_free_before) == bit)
				ic->ie_disk.none_free_before = cpu_to_le32(bit + 1);
			break;

		case 1:
			/* inc bitmap */
			sm_set_bitmap(ic->bitmap, bit, 2);
			break;

		case 2:
			/* inc bitmap and insert into overflow */
			sm_set_bitmap(ic->bitmap, bit, 3);
			reset_inc_context(ll, ic);

			le_rc = cpu_to_le32(3);
			__dm_bless_for_disk(&le_rc);
			r = dm_btree_insert(&ll->ref_count_info, ll->ref_count_root,
					    &b, &le_rc, &ll->ref_count_root);
			if (r < 0) {
				DMERR("ref count insert failed");
				return r;
			}
			break;

		default:
			/*
			 * inc within the overflow tree only.
			 */
			r = sm_ll_inc_overflow(ll, b, ic);
			if (r < 0)
				return r;
		}
	}

	*new_b = b;
	return 0;
}

/*
 * Finds a bitmap that contains entries in the block range, and increments
 * them.
 */
static int __sm_ll_inc(struct ll_disk *ll, dm_block_t b, dm_block_t e,
		       int32_t *nr_allocations, dm_block_t *new_b)
{
	int r;
	struct inc_context ic;
	uint32_t bit, bit_end;
	dm_block_t index = b;

	init_inc_context(&ic);

	bit = do_div(index, ll->entries_per_block);
	r = ll->load_ie(ll, index, &ic.ie_disk);
	if (r < 0)
		return r;

	r = shadow_bitmap(ll, &ic);
	if (r)
		return r;

	bit_end = min(bit + (e - b), (dm_block_t) ll->entries_per_block);
	r = sm_ll_inc_bitmap(ll, b, bit, bit_end, nr_allocations, new_b, &ic);

	exit_inc_context(ll, &ic);

	if (r)
		return r;

	return ll->save_ie(ll, index, &ic.ie_disk);
}

int sm_ll_inc(struct ll_disk *ll, dm_block_t b, dm_block_t e,
	      int32_t *nr_allocations)
{
	*nr_allocations = 0;
	while (b != e) {
		int r = __sm_ll_inc(ll, b, e, nr_allocations, &b);

		if (r)
			return r;
	}

	return 0;
}

/*----------------------------------------------------------------*/

static int __sm_ll_del_overflow(struct ll_disk *ll, dm_block_t b,
				struct inc_context *ic)
{
	reset_inc_context(ll, ic);
	return dm_btree_remove(&ll->ref_count_info, ll->ref_count_root,
			       &b, &ll->ref_count_root);
}

static int __sm_ll_dec_overflow(struct ll_disk *ll, dm_block_t b,
				struct inc_context *ic, uint32_t *old_rc)
{
	int r;
	int index = -1;
	struct btree_node *n;
	__le32 *v_ptr;
	uint32_t rc;

	reset_inc_context(ll, ic);
	r = btree_get_overwrite_leaf(&ll->ref_count_info, ll->ref_count_root,
				     b, &index, &ll->ref_count_root, &ic->overflow_leaf);
	if (r < 0)
		return r;

	n = dm_block_data(ic->overflow_leaf);

	if (!contains_key(n, b, index)) {
		DMERR("overflow btree is missing an entry");
		return -EINVAL;
	}

	v_ptr = value_ptr(n, index);
	rc = le32_to_cpu(*v_ptr);
	*old_rc = rc;

	if (rc == 3)
		return __sm_ll_del_overflow(ll, b, ic);

	rc--;
	*v_ptr = cpu_to_le32(rc);
	return 0;
}

static int sm_ll_dec_overflow(struct ll_disk *ll, dm_block_t b,
			      struct inc_context *ic, uint32_t *old_rc)
{
	/*
	 * Do we already have the correct overflow leaf?
	 */
	if (ic->overflow_leaf) {
		int index;
		struct btree_node *n;
		__le32 *v_ptr;
		uint32_t rc;

		n = dm_block_data(ic->overflow_leaf);
		index = lower_bound(n, b);
		if (contains_key(n, b, index)) {
			v_ptr = value_ptr(n, index);
			rc = le32_to_cpu(*v_ptr);
			*old_rc = rc;

			if (rc > 3) {
				rc--;
				*v_ptr = cpu_to_le32(rc);
				return 0;
			} else {
				return __sm_ll_del_overflow(ll, b, ic);
			}

		}
	}

	return __sm_ll_dec_overflow(ll, b, ic, old_rc);
}

/*
 * Loops round incrementing entries in a single bitmap.
 */
static inline int sm_ll_dec_bitmap(struct ll_disk *ll, dm_block_t b,
				   uint32_t bit, uint32_t bit_end,
				   struct inc_context *ic,
				   int32_t *nr_allocations, dm_block_t *new_b)
{
	int r;
	uint32_t old;

	for (; bit != bit_end; bit++, b++) {
		/*
		 * We only need to drop the bitmap if we need to find a new btree
		 * leaf for the overflow.  So if it was dropped last iteration,
		 * we now re-get it.
		 */
		r = ensure_bitmap(ll, ic);
		if (r)
			return r;

		old = sm_lookup_bitmap(ic->bitmap, bit);
		switch (old) {
		case 0:
			DMERR("unable to decrement block");
			return -EINVAL;

		case 1:
			/* dec bitmap */
			sm_set_bitmap(ic->bitmap, bit, 0);
			(*nr_allocations)--;
			ll->nr_allocated--;
			le32_add_cpu(&ic->ie_disk.nr_free, 1);
			ic->ie_disk.none_free_before =
				cpu_to_le32(min(le32_to_cpu(ic->ie_disk.none_free_before), bit));
			break;

		case 2:
			/* dec bitmap and insert into overflow */
			sm_set_bitmap(ic->bitmap, bit, 1);
			break;

		case 3:
			r = sm_ll_dec_overflow(ll, b, ic, &old);
			if (r < 0)
				return r;

			if (old == 3) {
				r = ensure_bitmap(ll, ic);
				if (r)
					return r;

				sm_set_bitmap(ic->bitmap, bit, 2);
			}
			break;
		}
	}

	*new_b = b;
	return 0;
}

static int __sm_ll_dec(struct ll_disk *ll, dm_block_t b, dm_block_t e,
		       int32_t *nr_allocations, dm_block_t *new_b)
{
	int r;
	uint32_t bit, bit_end;
	struct inc_context ic;
	dm_block_t index = b;

	init_inc_context(&ic);

	bit = do_div(index, ll->entries_per_block);
	r = ll->load_ie(ll, index, &ic.ie_disk);
	if (r < 0)
		return r;

	r = shadow_bitmap(ll, &ic);
	if (r)
		return r;

	bit_end = min(bit + (e - b), (dm_block_t) ll->entries_per_block);
	r = sm_ll_dec_bitmap(ll, b, bit, bit_end, &ic, nr_allocations, new_b);
	exit_inc_context(ll, &ic);

	if (r)
		return r;

	return ll->save_ie(ll, index, &ic.ie_disk);
}

int sm_ll_dec(struct ll_disk *ll, dm_block_t b, dm_block_t e,
	      int32_t *nr_allocations)
{
	*nr_allocations = 0;
	while (b != e) {
		int r = __sm_ll_dec(ll, b, e, nr_allocations, &b);

		if (r)
			return r;
	}

	return 0;
}

/*----------------------------------------------------------------*/

int sm_ll_commit(struct ll_disk *ll)
{
	int r = 0;

	if (ll->bitmap_index_changed) {
		r = ll->commit(ll);
		if (!r)
			ll->bitmap_index_changed = false;
	}

	return r;
}

/*----------------------------------------------------------------*/

static int metadata_ll_load_ie(struct ll_disk *ll, dm_block_t index,
			       struct disk_index_entry *ie)
{
	memcpy(ie, ll->mi_le.index + index, sizeof(*ie));
	return 0;
}

static int metadata_ll_save_ie(struct ll_disk *ll, dm_block_t index,
			       struct disk_index_entry *ie)
{
	ll->bitmap_index_changed = true;
	memcpy(ll->mi_le.index + index, ie, sizeof(*ie));
	return 0;
}

static int metadata_ll_init_index(struct ll_disk *ll)
{
	int r;
	struct dm_block *b;

	r = dm_tm_new_block(ll->tm, &index_validator, &b);
	if (r < 0)
		return r;

	ll->bitmap_root = dm_block_location(b);

	dm_tm_unlock(ll->tm, b);

	return 0;
}

static int metadata_ll_open(struct ll_disk *ll)
{
	int r;
	struct dm_block *block;

	r = dm_tm_read_lock(ll->tm, ll->bitmap_root,
			    &index_validator, &block);
	if (r)
		return r;

	memcpy(&ll->mi_le, dm_block_data(block), sizeof(ll->mi_le));
	dm_tm_unlock(ll->tm, block);

	return 0;
}

static dm_block_t metadata_ll_max_entries(struct ll_disk *ll)
{
	return MAX_METADATA_BITMAPS;
}

static int metadata_ll_commit(struct ll_disk *ll)
{
	int r, inc;
	struct dm_block *b;

	r = dm_tm_shadow_block(ll->tm, ll->bitmap_root, &index_validator, &b, &inc);
	if (r)
		return r;

	memcpy(dm_block_data(b), &ll->mi_le, sizeof(ll->mi_le));
	ll->bitmap_root = dm_block_location(b);

	dm_tm_unlock(ll->tm, b);

	return 0;
}

int sm_ll_new_metadata(struct ll_disk *ll, struct dm_transaction_manager *tm)
{
	int r;

	r = sm_ll_init(ll, tm);
	if (r < 0)
		return r;

	ll->load_ie = metadata_ll_load_ie;
	ll->save_ie = metadata_ll_save_ie;
	ll->init_index = metadata_ll_init_index;
	ll->open_index = metadata_ll_open;
	ll->max_entries = metadata_ll_max_entries;
	ll->commit = metadata_ll_commit;

	ll->nr_blocks = 0;
	ll->nr_allocated = 0;

	r = ll->init_index(ll);
	if (r < 0)
		return r;

	r = dm_btree_empty(&ll->ref_count_info, &ll->ref_count_root);
	if (r < 0)
		return r;

	return 0;
}

int sm_ll_open_metadata(struct ll_disk *ll, struct dm_transaction_manager *tm,
			void *root_le, size_t len)
{
	int r;
	struct disk_sm_root smr;

	if (len < sizeof(struct disk_sm_root)) {
		DMERR("sm_metadata root too small");
		return -ENOMEM;
	}

	/*
	 * We don't know the alignment of the root_le buffer, so need to
	 * copy into a new structure.
	 */
	memcpy(&smr, root_le, sizeof(smr));

	r = sm_ll_init(ll, tm);
	if (r < 0)
		return r;

	ll->load_ie = metadata_ll_load_ie;
	ll->save_ie = metadata_ll_save_ie;
	ll->init_index = metadata_ll_init_index;
	ll->open_index = metadata_ll_open;
	ll->max_entries = metadata_ll_max_entries;
	ll->commit = metadata_ll_commit;

	ll->nr_blocks = le64_to_cpu(smr.nr_blocks);
	ll->nr_allocated = le64_to_cpu(smr.nr_allocated);
	ll->bitmap_root = le64_to_cpu(smr.bitmap_root);
	ll->ref_count_root = le64_to_cpu(smr.ref_count_root);

	return ll->open_index(ll);
}

/*----------------------------------------------------------------*/

static inline int ie_cache_writeback(struct ll_disk *ll, struct ie_cache *iec)
{
	iec->dirty = false;
	__dm_bless_for_disk(iec->ie);
	return dm_btree_insert(&ll->bitmap_info, ll->bitmap_root,
			       &iec->index, &iec->ie, &ll->bitmap_root);
}

static inline unsigned int hash_index(dm_block_t index)
{
	return dm_hash_block(index, IE_CACHE_MASK);
}

static int disk_ll_load_ie(struct ll_disk *ll, dm_block_t index,
			   struct disk_index_entry *ie)
{
	int r;
	unsigned int h = hash_index(index);
	struct ie_cache *iec = ll->ie_cache + h;

	if (iec->valid) {
		if (iec->index == index) {
			memcpy(ie, &iec->ie, sizeof(*ie));
			return 0;
		}

		if (iec->dirty) {
			r = ie_cache_writeback(ll, iec);
			if (r)
				return r;
		}
	}

	r = dm_btree_lookup(&ll->bitmap_info, ll->bitmap_root, &index, ie);
	if (!r) {
		iec->valid = true;
		iec->dirty = false;
		iec->index = index;
		memcpy(&iec->ie, ie, sizeof(*ie));
	}

	return r;
}

static int disk_ll_save_ie(struct ll_disk *ll, dm_block_t index,
			   struct disk_index_entry *ie)
{
	int r;
	unsigned int h = hash_index(index);
	struct ie_cache *iec = ll->ie_cache + h;

	ll->bitmap_index_changed = true;
	if (iec->valid) {
		if (iec->index == index) {
			memcpy(&iec->ie, ie, sizeof(*ie));
			iec->dirty = true;
			return 0;
		}

		if (iec->dirty) {
			r = ie_cache_writeback(ll, iec);
			if (r)
				return r;
		}
	}

	iec->valid = true;
	iec->dirty = true;
	iec->index = index;
	memcpy(&iec->ie, ie, sizeof(*ie));
	return 0;
}

static int disk_ll_init_index(struct ll_disk *ll)
{
	unsigned int i;

	for (i = 0; i < IE_CACHE_SIZE; i++) {
		struct ie_cache *iec = ll->ie_cache + i;

		iec->valid = false;
		iec->dirty = false;
	}
	return dm_btree_empty(&ll->bitmap_info, &ll->bitmap_root);
}

static int disk_ll_open(struct ll_disk *ll)
{
	return 0;
}

static dm_block_t disk_ll_max_entries(struct ll_disk *ll)
{
	return -1ULL;
}

static int disk_ll_commit(struct ll_disk *ll)
{
	int r = 0;
	unsigned int i;

	for (i = 0; i < IE_CACHE_SIZE; i++) {
		struct ie_cache *iec = ll->ie_cache + i;

		if (iec->valid && iec->dirty)
			r = ie_cache_writeback(ll, iec);
	}

	return r;
}

int sm_ll_new_disk(struct ll_disk *ll, struct dm_transaction_manager *tm)
{
	int r;

	r = sm_ll_init(ll, tm);
	if (r < 0)
		return r;

	ll->load_ie = disk_ll_load_ie;
	ll->save_ie = disk_ll_save_ie;
	ll->init_index = disk_ll_init_index;
	ll->open_index = disk_ll_open;
	ll->max_entries = disk_ll_max_entries;
	ll->commit = disk_ll_commit;

	ll->nr_blocks = 0;
	ll->nr_allocated = 0;

	r = ll->init_index(ll);
	if (r < 0)
		return r;

	r = dm_btree_empty(&ll->ref_count_info, &ll->ref_count_root);
	if (r < 0)
		return r;

	return 0;
}

int sm_ll_open_disk(struct ll_disk *ll, struct dm_transaction_manager *tm,
		    void *root_le, size_t len)
{
	int r;
	struct disk_sm_root *smr = root_le;

	if (len < sizeof(struct disk_sm_root)) {
		DMERR("sm_metadata root too small");
		return -ENOMEM;
	}

	r = sm_ll_init(ll, tm);
	if (r < 0)
		return r;

	ll->load_ie = disk_ll_load_ie;
	ll->save_ie = disk_ll_save_ie;
	ll->init_index = disk_ll_init_index;
	ll->open_index = disk_ll_open;
	ll->max_entries = disk_ll_max_entries;
	ll->commit = disk_ll_commit;

	ll->nr_blocks = le64_to_cpu(smr->nr_blocks);
	ll->nr_allocated = le64_to_cpu(smr->nr_allocated);
	ll->bitmap_root = le64_to_cpu(smr->bitmap_root);
	ll->ref_count_root = le64_to_cpu(smr->ref_count_root);

	return ll->open_index(ll);
}

/*----------------------------------------------------------------*/