Linux Audio

Check our new training course

Loading...
v5.4
  1/*
  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)) {
 42		DMERR_LIMIT("index_check failed: blocknr %llu != wanted %llu",
 43			    le64_to_cpu(mi_le->blocknr), dm_block_location(b));
 44		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) {
 51		DMERR_LIMIT("index_check failed: csum %u != wanted %u",
 52			    le32_to_cpu(csum_disk), le32_to_cpu(mi_le->csum));
 53		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 dm_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 dm_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)) {
 92		DMERR_LIMIT("bitmap check failed: blocknr %llu != wanted %llu",
 93			    le64_to_cpu(disk_header->blocknr), dm_block_location(b));
 94		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) {
101		DMERR_LIMIT("bitmap check failed: csum %u != wanted %u",
102			    le32_to_cpu(csum_disk), le32_to_cpu(disk_header->csum));
103		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 = dm_bitmap_prepare_for_write,
112	.check = dm_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 dm_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		    dm_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	memset(ll, 0, sizeof(struct ll_disk));
194
195	ll->tm = tm;
196
197	ll->bitmap_info.tm = tm;
198	ll->bitmap_info.levels = 1;
199
200	/*
201	 * Because the new bitmap blocks are created via a shadow
202	 * operation, the old entry has already had its reference count
203	 * decremented and we don't need the btree to do any bookkeeping.
204	 */
205	ll->bitmap_info.value_type.size = sizeof(struct disk_index_entry);
206	ll->bitmap_info.value_type.inc = NULL;
207	ll->bitmap_info.value_type.dec = NULL;
208	ll->bitmap_info.value_type.equal = NULL;
209
210	ll->ref_count_info.tm = tm;
211	ll->ref_count_info.levels = 1;
212	ll->ref_count_info.value_type.size = sizeof(uint32_t);
213	ll->ref_count_info.value_type.inc = NULL;
214	ll->ref_count_info.value_type.dec = NULL;
215	ll->ref_count_info.value_type.equal = NULL;
216
217	ll->block_size = dm_bm_block_size(dm_tm_get_bm(tm));
218
219	if (ll->block_size > (1 << 30)) {
220		DMERR("block size too big to hold bitmaps");
221		return -EINVAL;
222	}
223
224	ll->entries_per_block = (ll->block_size - sizeof(struct disk_bitmap_header)) *
225		ENTRIES_PER_BYTE;
226	ll->nr_blocks = 0;
227	ll->bitmap_root = 0;
228	ll->ref_count_root = 0;
229	ll->bitmap_index_changed = false;
230
231	return 0;
232}
233
234int sm_ll_extend(struct ll_disk *ll, dm_block_t extra_blocks)
235{
236	int r;
237	dm_block_t i, nr_blocks, nr_indexes;
238	unsigned old_blocks, blocks;
239
240	nr_blocks = ll->nr_blocks + extra_blocks;
241	old_blocks = dm_sector_div_up(ll->nr_blocks, ll->entries_per_block);
242	blocks = dm_sector_div_up(nr_blocks, ll->entries_per_block);
243
244	nr_indexes = dm_sector_div_up(nr_blocks, ll->entries_per_block);
245	if (nr_indexes > ll->max_entries(ll)) {
246		DMERR("space map too large");
247		return -EINVAL;
248	}
249
250	/*
251	 * We need to set this before the dm_tm_new_block() call below.
252	 */
253	ll->nr_blocks = nr_blocks;
254	for (i = old_blocks; i < blocks; i++) {
255		struct dm_block *b;
256		struct disk_index_entry idx;
257
258		r = dm_tm_new_block(ll->tm, &dm_sm_bitmap_validator, &b);
259		if (r < 0)
260			return r;
261
262		idx.blocknr = cpu_to_le64(dm_block_location(b));
263
264		dm_tm_unlock(ll->tm, b);
265
266		idx.nr_free = cpu_to_le32(ll->entries_per_block);
267		idx.none_free_before = 0;
268
269		r = ll->save_ie(ll, i, &idx);
270		if (r < 0)
271			return r;
272	}
273
274	return 0;
275}
276
277int sm_ll_lookup_bitmap(struct ll_disk *ll, dm_block_t b, uint32_t *result)
278{
279	int r;
280	dm_block_t index = b;
281	struct disk_index_entry ie_disk;
282	struct dm_block *blk;
283
284	b = do_div(index, ll->entries_per_block);
285	r = ll->load_ie(ll, index, &ie_disk);
286	if (r < 0)
287		return r;
288
289	r = dm_tm_read_lock(ll->tm, le64_to_cpu(ie_disk.blocknr),
290			    &dm_sm_bitmap_validator, &blk);
291	if (r < 0)
292		return r;
293
294	*result = sm_lookup_bitmap(dm_bitmap_data(blk), b);
295
296	dm_tm_unlock(ll->tm, blk);
297
298	return 0;
299}
300
301static int sm_ll_lookup_big_ref_count(struct ll_disk *ll, dm_block_t b,
302				      uint32_t *result)
303{
304	__le32 le_rc;
305	int r;
306
307	r = dm_btree_lookup(&ll->ref_count_info, ll->ref_count_root, &b, &le_rc);
308	if (r < 0)
309		return r;
310
311	*result = le32_to_cpu(le_rc);
312
313	return r;
314}
315
316int sm_ll_lookup(struct ll_disk *ll, dm_block_t b, uint32_t *result)
317{
318	int r = sm_ll_lookup_bitmap(ll, b, result);
319
320	if (r)
321		return r;
322
323	if (*result != 3)
324		return r;
325
326	return sm_ll_lookup_big_ref_count(ll, b, result);
327}
328
329int sm_ll_find_free_block(struct ll_disk *ll, dm_block_t begin,
330			  dm_block_t end, dm_block_t *result)
331{
332	int r;
333	struct disk_index_entry ie_disk;
334	dm_block_t i, index_begin = begin;
335	dm_block_t index_end = dm_sector_div_up(end, ll->entries_per_block);
336
337	/*
338	 * FIXME: Use shifts
339	 */
340	begin = do_div(index_begin, ll->entries_per_block);
341	end = do_div(end, ll->entries_per_block);
342
343	for (i = index_begin; i < index_end; i++, begin = 0) {
344		struct dm_block *blk;
345		unsigned position;
346		uint32_t bit_end;
347
348		r = ll->load_ie(ll, i, &ie_disk);
349		if (r < 0)
350			return r;
351
352		if (le32_to_cpu(ie_disk.nr_free) == 0)
353			continue;
354
355		r = dm_tm_read_lock(ll->tm, le64_to_cpu(ie_disk.blocknr),
356				    &dm_sm_bitmap_validator, &blk);
357		if (r < 0)
358			return r;
359
360		bit_end = (i == index_end - 1) ?  end : ll->entries_per_block;
361
362		r = sm_find_free(dm_bitmap_data(blk),
363				 max_t(unsigned, begin, le32_to_cpu(ie_disk.none_free_before)),
364				 bit_end, &position);
365		if (r == -ENOSPC) {
366			/*
367			 * This might happen because we started searching
368			 * part way through the bitmap.
369			 */
370			dm_tm_unlock(ll->tm, blk);
371			continue;
 
 
 
 
372		}
373
374		dm_tm_unlock(ll->tm, blk);
375
376		*result = i * ll->entries_per_block + (dm_block_t) position;
377		return 0;
378	}
379
380	return -ENOSPC;
381}
382
383static int sm_ll_mutate(struct ll_disk *ll, dm_block_t b,
384			int (*mutator)(void *context, uint32_t old, uint32_t *new),
385			void *context, enum allocation_event *ev)
386{
387	int r;
388	uint32_t bit, old, ref_count;
389	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
411	if (old > 2) {
412		r = sm_ll_lookup_big_ref_count(ll, b, &old);
413		if (r < 0) {
414			dm_tm_unlock(ll->tm, nb);
415			return r;
416		}
417	}
418
419	r = mutator(context, old, &ref_count);
420	if (r) {
421		dm_tm_unlock(ll->tm, nb);
422		return r;
423	}
424
425	if (ref_count <= 2) {
426		sm_set_bitmap(bm_le, bit, ref_count);
427
428		dm_tm_unlock(ll->tm, nb);
429
430		if (old > 2) {
431			r = dm_btree_remove(&ll->ref_count_info,
432					    ll->ref_count_root,
433					    &b, &ll->ref_count_root);
434			if (r)
435				return r;
436		}
437
438	} else {
439		__le32 le_rc = cpu_to_le32(ref_count);
440
441		sm_set_bitmap(bm_le, bit, 3);
442		dm_tm_unlock(ll->tm, nb);
443
444		__dm_bless_for_disk(&le_rc);
445		r = dm_btree_insert(&ll->ref_count_info, ll->ref_count_root,
446				    &b, &le_rc, &ll->ref_count_root);
447		if (r < 0) {
448			DMERR("ref count insert failed");
449			return r;
450		}
451	}
452
453	if (ref_count && !old) {
454		*ev = SM_ALLOC;
455		ll->nr_allocated++;
456		le32_add_cpu(&ie_disk.nr_free, -1);
457		if (le32_to_cpu(ie_disk.none_free_before) == bit)
458			ie_disk.none_free_before = cpu_to_le32(bit + 1);
459
460	} else if (old && !ref_count) {
461		*ev = SM_FREE;
462		ll->nr_allocated--;
463		le32_add_cpu(&ie_disk.nr_free, 1);
464		ie_disk.none_free_before = cpu_to_le32(min(le32_to_cpu(ie_disk.none_free_before), bit));
465	} else
466		*ev = SM_NONE;
467
468	return ll->save_ie(ll, index, &ie_disk);
469}
470
471static int set_ref_count(void *context, uint32_t old, uint32_t *new)
472{
473	*new = *((uint32_t *) context);
474	return 0;
475}
476
477int sm_ll_insert(struct ll_disk *ll, dm_block_t b,
478		 uint32_t ref_count, enum allocation_event *ev)
479{
480	return sm_ll_mutate(ll, b, set_ref_count, &ref_count, ev);
481}
482
483static int inc_ref_count(void *context, uint32_t old, uint32_t *new)
484{
485	*new = old + 1;
486	return 0;
487}
488
489int sm_ll_inc(struct ll_disk *ll, dm_block_t b, enum allocation_event *ev)
490{
491	return sm_ll_mutate(ll, b, inc_ref_count, NULL, ev);
492}
493
494static int dec_ref_count(void *context, uint32_t old, uint32_t *new)
495{
496	if (!old) {
497		DMERR_LIMIT("unable to decrement a reference count below 0");
498		return -EINVAL;
499	}
500
501	*new = old - 1;
502	return 0;
503}
504
505int sm_ll_dec(struct ll_disk *ll, dm_block_t b, enum allocation_event *ev)
506{
507	return sm_ll_mutate(ll, b, dec_ref_count, NULL, ev);
508}
509
510int sm_ll_commit(struct ll_disk *ll)
511{
512	int r = 0;
513
514	if (ll->bitmap_index_changed) {
515		r = ll->commit(ll);
516		if (!r)
517			ll->bitmap_index_changed = false;
518	}
519
520	return r;
521}
522
523/*----------------------------------------------------------------*/
524
525static int metadata_ll_load_ie(struct ll_disk *ll, dm_block_t index,
526			       struct disk_index_entry *ie)
527{
528	memcpy(ie, ll->mi_le.index + index, sizeof(*ie));
529	return 0;
530}
531
532static int metadata_ll_save_ie(struct ll_disk *ll, dm_block_t index,
533			       struct disk_index_entry *ie)
534{
535	ll->bitmap_index_changed = true;
536	memcpy(ll->mi_le.index + index, ie, sizeof(*ie));
537	return 0;
538}
539
540static int metadata_ll_init_index(struct ll_disk *ll)
541{
542	int r;
543	struct dm_block *b;
544
545	r = dm_tm_new_block(ll->tm, &index_validator, &b);
546	if (r < 0)
547		return r;
548
 
549	ll->bitmap_root = dm_block_location(b);
550
551	dm_tm_unlock(ll->tm, b);
552
553	return 0;
554}
555
556static int metadata_ll_open(struct ll_disk *ll)
557{
558	int r;
559	struct dm_block *block;
560
561	r = dm_tm_read_lock(ll->tm, ll->bitmap_root,
562			    &index_validator, &block);
563	if (r)
564		return r;
565
566	memcpy(&ll->mi_le, dm_block_data(block), sizeof(ll->mi_le));
567	dm_tm_unlock(ll->tm, block);
568
569	return 0;
570}
571
572static dm_block_t metadata_ll_max_entries(struct ll_disk *ll)
573{
574	return MAX_METADATA_BITMAPS;
575}
576
577static int metadata_ll_commit(struct ll_disk *ll)
578{
579	int r, inc;
580	struct dm_block *b;
581
582	r = dm_tm_shadow_block(ll->tm, ll->bitmap_root, &index_validator, &b, &inc);
583	if (r)
584		return r;
585
586	memcpy(dm_block_data(b), &ll->mi_le, sizeof(ll->mi_le));
587	ll->bitmap_root = dm_block_location(b);
588
589	dm_tm_unlock(ll->tm, b);
590
591	return 0;
592}
593
594int sm_ll_new_metadata(struct ll_disk *ll, struct dm_transaction_manager *tm)
595{
596	int r;
597
598	r = sm_ll_init(ll, tm);
599	if (r < 0)
600		return r;
601
602	ll->load_ie = metadata_ll_load_ie;
603	ll->save_ie = metadata_ll_save_ie;
604	ll->init_index = metadata_ll_init_index;
605	ll->open_index = metadata_ll_open;
606	ll->max_entries = metadata_ll_max_entries;
607	ll->commit = metadata_ll_commit;
608
609	ll->nr_blocks = 0;
610	ll->nr_allocated = 0;
611
612	r = ll->init_index(ll);
613	if (r < 0)
614		return r;
615
616	r = dm_btree_empty(&ll->ref_count_info, &ll->ref_count_root);
617	if (r < 0)
618		return r;
619
620	return 0;
621}
622
623int sm_ll_open_metadata(struct ll_disk *ll, struct dm_transaction_manager *tm,
624			void *root_le, size_t len)
625{
626	int r;
627	struct disk_sm_root smr;
628
629	if (len < sizeof(struct disk_sm_root)) {
630		DMERR("sm_metadata root too small");
631		return -ENOMEM;
632	}
633
634	/*
635	 * We don't know the alignment of the root_le buffer, so need to
636	 * copy into a new structure.
637	 */
638	memcpy(&smr, root_le, sizeof(smr));
639
640	r = sm_ll_init(ll, tm);
641	if (r < 0)
642		return r;
643
644	ll->load_ie = metadata_ll_load_ie;
645	ll->save_ie = metadata_ll_save_ie;
646	ll->init_index = metadata_ll_init_index;
647	ll->open_index = metadata_ll_open;
648	ll->max_entries = metadata_ll_max_entries;
649	ll->commit = metadata_ll_commit;
650
651	ll->nr_blocks = le64_to_cpu(smr.nr_blocks);
652	ll->nr_allocated = le64_to_cpu(smr.nr_allocated);
653	ll->bitmap_root = le64_to_cpu(smr.bitmap_root);
654	ll->ref_count_root = le64_to_cpu(smr.ref_count_root);
655
656	return ll->open_index(ll);
657}
658
659/*----------------------------------------------------------------*/
660
661static int disk_ll_load_ie(struct ll_disk *ll, dm_block_t index,
662			   struct disk_index_entry *ie)
663{
664	return dm_btree_lookup(&ll->bitmap_info, ll->bitmap_root, &index, ie);
665}
666
667static int disk_ll_save_ie(struct ll_disk *ll, dm_block_t index,
668			   struct disk_index_entry *ie)
669{
670	__dm_bless_for_disk(ie);
671	return dm_btree_insert(&ll->bitmap_info, ll->bitmap_root,
672			       &index, ie, &ll->bitmap_root);
673}
674
675static int disk_ll_init_index(struct ll_disk *ll)
676{
677	return dm_btree_empty(&ll->bitmap_info, &ll->bitmap_root);
678}
679
680static int disk_ll_open(struct ll_disk *ll)
681{
682	/* nothing to do */
683	return 0;
684}
685
686static dm_block_t disk_ll_max_entries(struct ll_disk *ll)
687{
688	return -1ULL;
689}
690
691static int disk_ll_commit(struct ll_disk *ll)
692{
693	return 0;
694}
695
696int sm_ll_new_disk(struct ll_disk *ll, struct dm_transaction_manager *tm)
697{
698	int r;
699
700	r = sm_ll_init(ll, tm);
701	if (r < 0)
702		return r;
703
704	ll->load_ie = disk_ll_load_ie;
705	ll->save_ie = disk_ll_save_ie;
706	ll->init_index = disk_ll_init_index;
707	ll->open_index = disk_ll_open;
708	ll->max_entries = disk_ll_max_entries;
709	ll->commit = disk_ll_commit;
710
711	ll->nr_blocks = 0;
712	ll->nr_allocated = 0;
713
714	r = ll->init_index(ll);
715	if (r < 0)
716		return r;
717
718	r = dm_btree_empty(&ll->ref_count_info, &ll->ref_count_root);
719	if (r < 0)
720		return r;
721
722	return 0;
723}
724
725int sm_ll_open_disk(struct ll_disk *ll, struct dm_transaction_manager *tm,
726		    void *root_le, size_t len)
727{
728	int r;
729	struct disk_sm_root *smr = root_le;
730
731	if (len < sizeof(struct disk_sm_root)) {
732		DMERR("sm_metadata root too small");
733		return -ENOMEM;
734	}
735
736	r = sm_ll_init(ll, tm);
737	if (r < 0)
738		return r;
739
740	ll->load_ie = disk_ll_load_ie;
741	ll->save_ie = disk_ll_save_ie;
742	ll->init_index = disk_ll_init_index;
743	ll->open_index = disk_ll_open;
744	ll->max_entries = disk_ll_max_entries;
745	ll->commit = disk_ll_commit;
746
747	ll->nr_blocks = le64_to_cpu(smr->nr_blocks);
748	ll->nr_allocated = le64_to_cpu(smr->nr_allocated);
749	ll->bitmap_root = le64_to_cpu(smr->bitmap_root);
750	ll->ref_count_root = le64_to_cpu(smr->ref_count_root);
751
752	return ll->open_index(ll);
753}
754
755/*----------------------------------------------------------------*/
v4.6
  1/*
  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)) {
 42		DMERR_LIMIT("index_check failed: blocknr %llu != wanted %llu",
 43			    le64_to_cpu(mi_le->blocknr), dm_block_location(b));
 44		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) {
 51		DMERR_LIMIT("index_check failed: csum %u != wanted %u",
 52			    le32_to_cpu(csum_disk), le32_to_cpu(mi_le->csum));
 53		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)) {
 92		DMERR_LIMIT("bitmap check failed: blocknr %llu != wanted %llu",
 93			    le64_to_cpu(disk_header->blocknr), dm_block_location(b));
 94		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) {
101		DMERR_LIMIT("bitmap check failed: csum %u != wanted %u",
102			    le32_to_cpu(csum_disk), le32_to_cpu(disk_header->csum));
103		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;
227	ll->bitmap_index_changed = false;
228
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	/*
249	 * We need to set this before the dm_tm_new_block() call below.
250	 */
251	ll->nr_blocks = nr_blocks;
252	for (i = old_blocks; i < blocks; i++) {
253		struct dm_block *b;
254		struct disk_index_entry idx;
255
256		r = dm_tm_new_block(ll->tm, &dm_sm_bitmap_validator, &b);
257		if (r < 0)
258			return r;
259
260		idx.blocknr = cpu_to_le64(dm_block_location(b));
261
262		dm_tm_unlock(ll->tm, b);
263
264		idx.nr_free = cpu_to_le32(ll->entries_per_block);
265		idx.none_free_before = 0;
266
267		r = ll->save_ie(ll, i, &idx);
268		if (r < 0)
269			return r;
270	}
271
272	return 0;
273}
274
275int sm_ll_lookup_bitmap(struct ll_disk *ll, dm_block_t b, uint32_t *result)
276{
277	int r;
278	dm_block_t index = b;
279	struct disk_index_entry ie_disk;
280	struct dm_block *blk;
281
282	b = do_div(index, ll->entries_per_block);
283	r = ll->load_ie(ll, index, &ie_disk);
284	if (r < 0)
285		return r;
286
287	r = dm_tm_read_lock(ll->tm, le64_to_cpu(ie_disk.blocknr),
288			    &dm_sm_bitmap_validator, &blk);
289	if (r < 0)
290		return r;
291
292	*result = sm_lookup_bitmap(dm_bitmap_data(blk), b);
293
294	dm_tm_unlock(ll->tm, blk);
295
296	return 0;
297}
298
299static int sm_ll_lookup_big_ref_count(struct ll_disk *ll, dm_block_t b,
300				      uint32_t *result)
301{
302	__le32 le_rc;
303	int r;
304
305	r = dm_btree_lookup(&ll->ref_count_info, ll->ref_count_root, &b, &le_rc);
306	if (r < 0)
307		return r;
308
309	*result = le32_to_cpu(le_rc);
310
311	return r;
312}
313
314int sm_ll_lookup(struct ll_disk *ll, dm_block_t b, uint32_t *result)
315{
316	int r = sm_ll_lookup_bitmap(ll, b, result);
317
318	if (r)
319		return r;
320
321	if (*result != 3)
322		return r;
323
324	return sm_ll_lookup_big_ref_count(ll, b, result);
325}
326
327int sm_ll_find_free_block(struct ll_disk *ll, dm_block_t begin,
328			  dm_block_t end, dm_block_t *result)
329{
330	int r;
331	struct disk_index_entry ie_disk;
332	dm_block_t i, index_begin = begin;
333	dm_block_t index_end = dm_sector_div_up(end, ll->entries_per_block);
334
335	/*
336	 * FIXME: Use shifts
337	 */
338	begin = do_div(index_begin, ll->entries_per_block);
339	end = do_div(end, ll->entries_per_block);
340
341	for (i = index_begin; i < index_end; i++, begin = 0) {
342		struct dm_block *blk;
343		unsigned position;
344		uint32_t bit_end;
345
346		r = ll->load_ie(ll, i, &ie_disk);
347		if (r < 0)
348			return r;
349
350		if (le32_to_cpu(ie_disk.nr_free) == 0)
351			continue;
352
353		r = dm_tm_read_lock(ll->tm, le64_to_cpu(ie_disk.blocknr),
354				    &dm_sm_bitmap_validator, &blk);
355		if (r < 0)
356			return r;
357
358		bit_end = (i == index_end - 1) ?  end : ll->entries_per_block;
359
360		r = sm_find_free(dm_bitmap_data(blk),
361				 max_t(unsigned, begin, le32_to_cpu(ie_disk.none_free_before)),
362				 bit_end, &position);
363		if (r == -ENOSPC) {
364			/*
365			 * This might happen because we started searching
366			 * part way through the bitmap.
367			 */
368			dm_tm_unlock(ll->tm, blk);
369			continue;
370
371		} else if (r < 0) {
372			dm_tm_unlock(ll->tm, blk);
373			return r;
374		}
375
376		dm_tm_unlock(ll->tm, blk);
377
378		*result = i * ll->entries_per_block + (dm_block_t) position;
379		return 0;
380	}
381
382	return -ENOSPC;
383}
384
385static int sm_ll_mutate(struct ll_disk *ll, dm_block_t b,
386			int (*mutator)(void *context, uint32_t old, uint32_t *new),
387			void *context, enum allocation_event *ev)
388{
389	int r;
390	uint32_t bit, old, ref_count;
391	struct dm_block *nb;
392	dm_block_t index = b;
393	struct disk_index_entry ie_disk;
394	void *bm_le;
395	int inc;
396
397	bit = do_div(index, ll->entries_per_block);
398	r = ll->load_ie(ll, index, &ie_disk);
399	if (r < 0)
400		return r;
401
402	r = dm_tm_shadow_block(ll->tm, le64_to_cpu(ie_disk.blocknr),
403			       &dm_sm_bitmap_validator, &nb, &inc);
404	if (r < 0) {
405		DMERR("dm_tm_shadow_block() failed");
406		return r;
407	}
408	ie_disk.blocknr = cpu_to_le64(dm_block_location(nb));
409
410	bm_le = dm_bitmap_data(nb);
411	old = sm_lookup_bitmap(bm_le, bit);
412
413	if (old > 2) {
414		r = sm_ll_lookup_big_ref_count(ll, b, &old);
415		if (r < 0) {
416			dm_tm_unlock(ll->tm, nb);
417			return r;
418		}
419	}
420
421	r = mutator(context, old, &ref_count);
422	if (r) {
423		dm_tm_unlock(ll->tm, nb);
424		return r;
425	}
426
427	if (ref_count <= 2) {
428		sm_set_bitmap(bm_le, bit, ref_count);
429
430		dm_tm_unlock(ll->tm, nb);
431
432		if (old > 2) {
433			r = dm_btree_remove(&ll->ref_count_info,
434					    ll->ref_count_root,
435					    &b, &ll->ref_count_root);
436			if (r)
437				return r;
438		}
439
440	} else {
441		__le32 le_rc = cpu_to_le32(ref_count);
442
443		sm_set_bitmap(bm_le, bit, 3);
444		dm_tm_unlock(ll->tm, nb);
445
446		__dm_bless_for_disk(&le_rc);
447		r = dm_btree_insert(&ll->ref_count_info, ll->ref_count_root,
448				    &b, &le_rc, &ll->ref_count_root);
449		if (r < 0) {
450			DMERR("ref count insert failed");
451			return r;
452		}
453	}
454
455	if (ref_count && !old) {
456		*ev = SM_ALLOC;
457		ll->nr_allocated++;
458		le32_add_cpu(&ie_disk.nr_free, -1);
459		if (le32_to_cpu(ie_disk.none_free_before) == bit)
460			ie_disk.none_free_before = cpu_to_le32(bit + 1);
461
462	} else if (old && !ref_count) {
463		*ev = SM_FREE;
464		ll->nr_allocated--;
465		le32_add_cpu(&ie_disk.nr_free, 1);
466		ie_disk.none_free_before = cpu_to_le32(min(le32_to_cpu(ie_disk.none_free_before), bit));
467	}
 
468
469	return ll->save_ie(ll, index, &ie_disk);
470}
471
472static int set_ref_count(void *context, uint32_t old, uint32_t *new)
473{
474	*new = *((uint32_t *) context);
475	return 0;
476}
477
478int sm_ll_insert(struct ll_disk *ll, dm_block_t b,
479		 uint32_t ref_count, enum allocation_event *ev)
480{
481	return sm_ll_mutate(ll, b, set_ref_count, &ref_count, ev);
482}
483
484static int inc_ref_count(void *context, uint32_t old, uint32_t *new)
485{
486	*new = old + 1;
487	return 0;
488}
489
490int sm_ll_inc(struct ll_disk *ll, dm_block_t b, enum allocation_event *ev)
491{
492	return sm_ll_mutate(ll, b, inc_ref_count, NULL, ev);
493}
494
495static int dec_ref_count(void *context, uint32_t old, uint32_t *new)
496{
497	if (!old) {
498		DMERR_LIMIT("unable to decrement a reference count below 0");
499		return -EINVAL;
500	}
501
502	*new = old - 1;
503	return 0;
504}
505
506int sm_ll_dec(struct ll_disk *ll, dm_block_t b, enum allocation_event *ev)
507{
508	return sm_ll_mutate(ll, b, dec_ref_count, NULL, ev);
509}
510
511int sm_ll_commit(struct ll_disk *ll)
512{
513	int r = 0;
514
515	if (ll->bitmap_index_changed) {
516		r = ll->commit(ll);
517		if (!r)
518			ll->bitmap_index_changed = false;
519	}
520
521	return r;
522}
523
524/*----------------------------------------------------------------*/
525
526static int metadata_ll_load_ie(struct ll_disk *ll, dm_block_t index,
527			       struct disk_index_entry *ie)
528{
529	memcpy(ie, ll->mi_le.index + index, sizeof(*ie));
530	return 0;
531}
532
533static int metadata_ll_save_ie(struct ll_disk *ll, dm_block_t index,
534			       struct disk_index_entry *ie)
535{
536	ll->bitmap_index_changed = true;
537	memcpy(ll->mi_le.index + index, ie, sizeof(*ie));
538	return 0;
539}
540
541static int metadata_ll_init_index(struct ll_disk *ll)
542{
543	int r;
544	struct dm_block *b;
545
546	r = dm_tm_new_block(ll->tm, &index_validator, &b);
547	if (r < 0)
548		return r;
549
550	memcpy(dm_block_data(b), &ll->mi_le, sizeof(ll->mi_le));
551	ll->bitmap_root = dm_block_location(b);
552
553	dm_tm_unlock(ll->tm, b);
554
555	return 0;
556}
557
558static int metadata_ll_open(struct ll_disk *ll)
559{
560	int r;
561	struct dm_block *block;
562
563	r = dm_tm_read_lock(ll->tm, ll->bitmap_root,
564			    &index_validator, &block);
565	if (r)
566		return r;
567
568	memcpy(&ll->mi_le, dm_block_data(block), sizeof(ll->mi_le));
569	dm_tm_unlock(ll->tm, block);
570
571	return 0;
572}
573
574static dm_block_t metadata_ll_max_entries(struct ll_disk *ll)
575{
576	return MAX_METADATA_BITMAPS;
577}
578
579static int metadata_ll_commit(struct ll_disk *ll)
580{
581	int r, inc;
582	struct dm_block *b;
583
584	r = dm_tm_shadow_block(ll->tm, ll->bitmap_root, &index_validator, &b, &inc);
585	if (r)
586		return r;
587
588	memcpy(dm_block_data(b), &ll->mi_le, sizeof(ll->mi_le));
589	ll->bitmap_root = dm_block_location(b);
590
591	dm_tm_unlock(ll->tm, b);
592
593	return 0;
594}
595
596int sm_ll_new_metadata(struct ll_disk *ll, struct dm_transaction_manager *tm)
597{
598	int r;
599
600	r = sm_ll_init(ll, tm);
601	if (r < 0)
602		return r;
603
604	ll->load_ie = metadata_ll_load_ie;
605	ll->save_ie = metadata_ll_save_ie;
606	ll->init_index = metadata_ll_init_index;
607	ll->open_index = metadata_ll_open;
608	ll->max_entries = metadata_ll_max_entries;
609	ll->commit = metadata_ll_commit;
610
611	ll->nr_blocks = 0;
612	ll->nr_allocated = 0;
613
614	r = ll->init_index(ll);
615	if (r < 0)
616		return r;
617
618	r = dm_btree_empty(&ll->ref_count_info, &ll->ref_count_root);
619	if (r < 0)
620		return r;
621
622	return 0;
623}
624
625int sm_ll_open_metadata(struct ll_disk *ll, struct dm_transaction_manager *tm,
626			void *root_le, size_t len)
627{
628	int r;
629	struct disk_sm_root *smr = root_le;
630
631	if (len < sizeof(struct disk_sm_root)) {
632		DMERR("sm_metadata root too small");
633		return -ENOMEM;
634	}
635
 
 
 
 
 
 
636	r = sm_ll_init(ll, tm);
637	if (r < 0)
638		return r;
639
640	ll->load_ie = metadata_ll_load_ie;
641	ll->save_ie = metadata_ll_save_ie;
642	ll->init_index = metadata_ll_init_index;
643	ll->open_index = metadata_ll_open;
644	ll->max_entries = metadata_ll_max_entries;
645	ll->commit = metadata_ll_commit;
646
647	ll->nr_blocks = le64_to_cpu(smr->nr_blocks);
648	ll->nr_allocated = le64_to_cpu(smr->nr_allocated);
649	ll->bitmap_root = le64_to_cpu(smr->bitmap_root);
650	ll->ref_count_root = le64_to_cpu(smr->ref_count_root);
651
652	return ll->open_index(ll);
653}
654
655/*----------------------------------------------------------------*/
656
657static int disk_ll_load_ie(struct ll_disk *ll, dm_block_t index,
658			   struct disk_index_entry *ie)
659{
660	return dm_btree_lookup(&ll->bitmap_info, ll->bitmap_root, &index, ie);
661}
662
663static int disk_ll_save_ie(struct ll_disk *ll, dm_block_t index,
664			   struct disk_index_entry *ie)
665{
666	__dm_bless_for_disk(ie);
667	return dm_btree_insert(&ll->bitmap_info, ll->bitmap_root,
668			       &index, ie, &ll->bitmap_root);
669}
670
671static int disk_ll_init_index(struct ll_disk *ll)
672{
673	return dm_btree_empty(&ll->bitmap_info, &ll->bitmap_root);
674}
675
676static int disk_ll_open(struct ll_disk *ll)
677{
678	/* nothing to do */
679	return 0;
680}
681
682static dm_block_t disk_ll_max_entries(struct ll_disk *ll)
683{
684	return -1ULL;
685}
686
687static int disk_ll_commit(struct ll_disk *ll)
688{
689	return 0;
690}
691
692int sm_ll_new_disk(struct ll_disk *ll, struct dm_transaction_manager *tm)
693{
694	int r;
695
696	r = sm_ll_init(ll, tm);
697	if (r < 0)
698		return r;
699
700	ll->load_ie = disk_ll_load_ie;
701	ll->save_ie = disk_ll_save_ie;
702	ll->init_index = disk_ll_init_index;
703	ll->open_index = disk_ll_open;
704	ll->max_entries = disk_ll_max_entries;
705	ll->commit = disk_ll_commit;
706
707	ll->nr_blocks = 0;
708	ll->nr_allocated = 0;
709
710	r = ll->init_index(ll);
711	if (r < 0)
712		return r;
713
714	r = dm_btree_empty(&ll->ref_count_info, &ll->ref_count_root);
715	if (r < 0)
716		return r;
717
718	return 0;
719}
720
721int sm_ll_open_disk(struct ll_disk *ll, struct dm_transaction_manager *tm,
722		    void *root_le, size_t len)
723{
724	int r;
725	struct disk_sm_root *smr = root_le;
726
727	if (len < sizeof(struct disk_sm_root)) {
728		DMERR("sm_metadata root too small");
729		return -ENOMEM;
730	}
731
732	r = sm_ll_init(ll, tm);
733	if (r < 0)
734		return r;
735
736	ll->load_ie = disk_ll_load_ie;
737	ll->save_ie = disk_ll_save_ie;
738	ll->init_index = disk_ll_init_index;
739	ll->open_index = disk_ll_open;
740	ll->max_entries = disk_ll_max_entries;
741	ll->commit = disk_ll_commit;
742
743	ll->nr_blocks = le64_to_cpu(smr->nr_blocks);
744	ll->nr_allocated = le64_to_cpu(smr->nr_allocated);
745	ll->bitmap_root = le64_to_cpu(smr->bitmap_root);
746	ll->ref_count_root = le64_to_cpu(smr->ref_count_root);
747
748	return ll->open_index(ll);
749}
750
751/*----------------------------------------------------------------*/