Lines Matching refs:cmd
273 static inline int superblock_read_lock(struct dm_clone_metadata *cmd, in superblock_read_lock() argument
276 return dm_bm_read_lock(cmd->bm, SUPERBLOCK_LOCATION, &sb_validator, sblock); in superblock_read_lock()
279 static inline int superblock_write_lock_zero(struct dm_clone_metadata *cmd, in superblock_write_lock_zero() argument
282 return dm_bm_write_lock_zero(cmd->bm, SUPERBLOCK_LOCATION, &sb_validator, sblock); in superblock_write_lock_zero()
285 static int __copy_sm_root(struct dm_clone_metadata *cmd) in __copy_sm_root() argument
290 r = dm_sm_root_size(cmd->sm, &root_size); in __copy_sm_root()
294 return dm_sm_copy_root(cmd->sm, &cmd->metadata_space_map_root, root_size); in __copy_sm_root()
298 static void __prepare_superblock(struct dm_clone_metadata *cmd, in __prepare_superblock() argument
310 memcpy(&sb->metadata_space_map_root, &cmd->metadata_space_map_root, in __prepare_superblock()
311 sizeof(cmd->metadata_space_map_root)); in __prepare_superblock()
313 sb->region_size = cpu_to_le64(cmd->region_size); in __prepare_superblock()
314 sb->target_size = cpu_to_le64(cmd->target_size); in __prepare_superblock()
315 sb->bitset_root = cpu_to_le64(cmd->bitset_root); in __prepare_superblock()
318 static int __open_metadata(struct dm_clone_metadata *cmd) in __open_metadata() argument
324 r = superblock_read_lock(cmd, &sblock); in __open_metadata()
334 if (cmd->region_size != le64_to_cpu(sb->region_size) || in __open_metadata()
335 cmd->target_size != le64_to_cpu(sb->target_size)) { in __open_metadata()
341 r = dm_tm_open_with_sm(cmd->bm, SUPERBLOCK_LOCATION, in __open_metadata()
344 &cmd->tm, &cmd->sm); in __open_metadata()
351 dm_disk_bitset_init(cmd->tm, &cmd->bitset_info); in __open_metadata()
352 cmd->bitset_root = le64_to_cpu(sb->bitset_root); in __open_metadata()
360 static int __format_metadata(struct dm_clone_metadata *cmd) in __format_metadata() argument
366 r = dm_tm_create_with_sm(cmd->bm, SUPERBLOCK_LOCATION, &cmd->tm, &cmd->sm); in __format_metadata()
372 dm_disk_bitset_init(cmd->tm, &cmd->bitset_info); in __format_metadata()
374 r = dm_bitset_empty(&cmd->bitset_info, &cmd->bitset_root); in __format_metadata()
380 r = dm_bitset_resize(&cmd->bitset_info, cmd->bitset_root, 0, in __format_metadata()
381 cmd->nr_regions, false, &cmd->bitset_root); in __format_metadata()
383 DMERR("Failed to resize on-disk bitset to %lu entries", cmd->nr_regions); in __format_metadata()
388 r = dm_tm_pre_commit(cmd->tm); in __format_metadata()
394 r = __copy_sm_root(cmd); in __format_metadata()
400 r = superblock_write_lock_zero(cmd, &sblock); in __format_metadata()
407 __prepare_superblock(cmd, sb); in __format_metadata()
408 r = dm_tm_commit(cmd->tm, sblock); in __format_metadata()
417 dm_sm_destroy(cmd->sm); in __format_metadata()
418 dm_tm_destroy(cmd->tm); in __format_metadata()
423 static int __open_or_format_metadata(struct dm_clone_metadata *cmd, bool may_format_device) in __open_or_format_metadata() argument
428 r = __superblock_all_zeroes(cmd->bm, &formatted); in __open_or_format_metadata()
433 return may_format_device ? __format_metadata(cmd) : -EPERM; in __open_or_format_metadata()
435 return __open_metadata(cmd); in __open_or_format_metadata()
438 static int __create_persistent_data_structures(struct dm_clone_metadata *cmd, in __create_persistent_data_structures() argument
444 cmd->bm = dm_block_manager_create(cmd->bdev, in __create_persistent_data_structures()
447 if (IS_ERR(cmd->bm)) { in __create_persistent_data_structures()
449 return PTR_ERR(cmd->bm); in __create_persistent_data_structures()
452 r = __open_or_format_metadata(cmd, may_format_device); in __create_persistent_data_structures()
454 dm_block_manager_destroy(cmd->bm); in __create_persistent_data_structures()
459 static void __destroy_persistent_data_structures(struct dm_clone_metadata *cmd) in __destroy_persistent_data_structures() argument
461 dm_sm_destroy(cmd->sm); in __destroy_persistent_data_structures()
462 dm_tm_destroy(cmd->tm); in __destroy_persistent_data_structures()
463 dm_block_manager_destroy(cmd->bm); in __destroy_persistent_data_structures()
492 static int dirty_map_init(struct dm_clone_metadata *cmd) in dirty_map_init() argument
494 if (__dirty_map_init(&cmd->dmap[0], cmd->nr_words, cmd->nr_regions)) { in dirty_map_init()
499 if (__dirty_map_init(&cmd->dmap[1], cmd->nr_words, cmd->nr_regions)) { in dirty_map_init()
501 __dirty_map_exit(&cmd->dmap[0]); in dirty_map_init()
505 cmd->current_dmap = &cmd->dmap[0]; in dirty_map_init()
506 cmd->committing_dmap = NULL; in dirty_map_init()
511 static void dirty_map_exit(struct dm_clone_metadata *cmd) in dirty_map_exit() argument
513 __dirty_map_exit(&cmd->dmap[0]); in dirty_map_exit()
514 __dirty_map_exit(&cmd->dmap[1]); in dirty_map_exit()
517 static int __load_bitset_in_core(struct dm_clone_metadata *cmd) in __load_bitset_in_core() argument
524 r = dm_bitset_flush(&cmd->bitset_info, cmd->bitset_root, &cmd->bitset_root); in __load_bitset_in_core()
528 r = dm_bitset_cursor_begin(&cmd->bitset_info, cmd->bitset_root, cmd->nr_regions, &c); in __load_bitset_in_core()
534 __set_bit(i, cmd->region_map); in __load_bitset_in_core()
536 __clear_bit(i, cmd->region_map); in __load_bitset_in_core()
538 if (i >= (cmd->nr_regions - 1)) in __load_bitset_in_core()
557 struct dm_clone_metadata *cmd; in dm_clone_metadata_open() local
559 cmd = kzalloc(sizeof(*cmd), GFP_KERNEL); in dm_clone_metadata_open()
560 if (!cmd) { in dm_clone_metadata_open()
565 cmd->bdev = bdev; in dm_clone_metadata_open()
566 cmd->target_size = target_size; in dm_clone_metadata_open()
567 cmd->region_size = region_size; in dm_clone_metadata_open()
568 cmd->nr_regions = dm_sector_div_up(cmd->target_size, cmd->region_size); in dm_clone_metadata_open()
569 cmd->nr_words = BITS_TO_LONGS(cmd->nr_regions); in dm_clone_metadata_open()
571 init_rwsem(&cmd->lock); in dm_clone_metadata_open()
572 spin_lock_init(&cmd->bitmap_lock); in dm_clone_metadata_open()
573 cmd->read_only = 0; in dm_clone_metadata_open()
574 cmd->fail_io = false; in dm_clone_metadata_open()
575 cmd->hydration_done = false; in dm_clone_metadata_open()
577 cmd->region_map = kvmalloc(bitmap_size(cmd->nr_regions), GFP_KERNEL); in dm_clone_metadata_open()
578 if (!cmd->region_map) { in dm_clone_metadata_open()
584 r = __create_persistent_data_structures(cmd, true); in dm_clone_metadata_open()
588 r = __load_bitset_in_core(cmd); in dm_clone_metadata_open()
594 r = dirty_map_init(cmd); in dm_clone_metadata_open()
598 if (bitmap_full(cmd->region_map, cmd->nr_regions)) in dm_clone_metadata_open()
599 cmd->hydration_done = true; in dm_clone_metadata_open()
601 return cmd; in dm_clone_metadata_open()
604 __destroy_persistent_data_structures(cmd); in dm_clone_metadata_open()
607 kvfree(cmd->region_map); in dm_clone_metadata_open()
610 kfree(cmd); in dm_clone_metadata_open()
615 void dm_clone_metadata_close(struct dm_clone_metadata *cmd) in dm_clone_metadata_close() argument
617 if (!cmd->fail_io) in dm_clone_metadata_close()
618 __destroy_persistent_data_structures(cmd); in dm_clone_metadata_close()
620 dirty_map_exit(cmd); in dm_clone_metadata_close()
621 kvfree(cmd->region_map); in dm_clone_metadata_close()
622 kfree(cmd); in dm_clone_metadata_close()
625 bool dm_clone_is_hydration_done(struct dm_clone_metadata *cmd) in dm_clone_is_hydration_done() argument
627 return cmd->hydration_done; in dm_clone_is_hydration_done()
630 bool dm_clone_is_region_hydrated(struct dm_clone_metadata *cmd, unsigned long region_nr) in dm_clone_is_region_hydrated() argument
632 return dm_clone_is_hydration_done(cmd) || test_bit(region_nr, cmd->region_map); in dm_clone_is_region_hydrated()
635 bool dm_clone_is_range_hydrated(struct dm_clone_metadata *cmd, in dm_clone_is_range_hydrated() argument
640 if (dm_clone_is_hydration_done(cmd)) in dm_clone_is_range_hydrated()
643 bit = find_next_zero_bit(cmd->region_map, cmd->nr_regions, start); in dm_clone_is_range_hydrated()
648 unsigned int dm_clone_nr_of_hydrated_regions(struct dm_clone_metadata *cmd) in dm_clone_nr_of_hydrated_regions() argument
650 return bitmap_weight(cmd->region_map, cmd->nr_regions); in dm_clone_nr_of_hydrated_regions()
653 unsigned long dm_clone_find_next_unhydrated_region(struct dm_clone_metadata *cmd, in dm_clone_find_next_unhydrated_region() argument
656 return find_next_zero_bit(cmd->region_map, cmd->nr_regions, start); in dm_clone_find_next_unhydrated_region()
659 static int __update_metadata_word(struct dm_clone_metadata *cmd, in __update_metadata_word() argument
665 unsigned long max_index = min(cmd->nr_regions, (word + 1) * BITS_PER_LONG); in __update_metadata_word()
669 r = dm_bitset_set_bit(&cmd->bitset_info, cmd->bitset_root, in __update_metadata_word()
670 index, &cmd->bitset_root); in __update_metadata_word()
683 static int __metadata_commit(struct dm_clone_metadata *cmd) in __metadata_commit() argument
690 r = dm_bitset_flush(&cmd->bitset_info, cmd->bitset_root, &cmd->bitset_root); in __metadata_commit()
697 r = dm_tm_pre_commit(cmd->tm); in __metadata_commit()
704 r = __copy_sm_root(cmd); in __metadata_commit()
711 r = superblock_write_lock_zero(cmd, &sblock); in __metadata_commit()
719 __prepare_superblock(cmd, sb); in __metadata_commit()
722 r = dm_tm_commit(cmd->tm, sblock); in __metadata_commit()
731 if (bitmap_full(cmd->region_map, cmd->nr_regions)) in __metadata_commit()
732 cmd->hydration_done = true; in __metadata_commit()
737 static int __flush_dmap(struct dm_clone_metadata *cmd, struct dirty_map *dmap) in __flush_dmap() argument
744 word = find_next_bit(dmap->dirty_words, cmd->nr_words, word); in __flush_dmap()
746 if (word == cmd->nr_words) in __flush_dmap()
749 r = __update_metadata_word(cmd, dmap->dirty_regions, word); in __flush_dmap()
756 } while (word < cmd->nr_words); in __flush_dmap()
758 r = __metadata_commit(cmd); in __flush_dmap()
764 spin_lock_irq(&cmd->bitmap_lock); in __flush_dmap()
766 spin_unlock_irq(&cmd->bitmap_lock); in __flush_dmap()
771 int dm_clone_metadata_pre_commit(struct dm_clone_metadata *cmd) in dm_clone_metadata_pre_commit() argument
776 down_write(&cmd->lock); in dm_clone_metadata_pre_commit()
778 if (cmd->fail_io || dm_bm_is_read_only(cmd->bm)) { in dm_clone_metadata_pre_commit()
784 dmap = cmd->current_dmap; in dm_clone_metadata_pre_commit()
787 next_dmap = (dmap == &cmd->dmap[0]) ? &cmd->dmap[1] : &cmd->dmap[0]; in dm_clone_metadata_pre_commit()
793 if (WARN_ON(next_dmap->changed || cmd->committing_dmap)) { in dm_clone_metadata_pre_commit()
799 spin_lock_irq(&cmd->bitmap_lock); in dm_clone_metadata_pre_commit()
800 cmd->current_dmap = next_dmap; in dm_clone_metadata_pre_commit()
801 spin_unlock_irq(&cmd->bitmap_lock); in dm_clone_metadata_pre_commit()
804 cmd->committing_dmap = dmap; in dm_clone_metadata_pre_commit()
806 up_write(&cmd->lock); in dm_clone_metadata_pre_commit()
811 int dm_clone_metadata_commit(struct dm_clone_metadata *cmd) in dm_clone_metadata_commit() argument
815 down_write(&cmd->lock); in dm_clone_metadata_commit()
817 if (cmd->fail_io || dm_bm_is_read_only(cmd->bm)) in dm_clone_metadata_commit()
820 if (WARN_ON(!cmd->committing_dmap)) { in dm_clone_metadata_commit()
825 r = __flush_dmap(cmd, cmd->committing_dmap); in dm_clone_metadata_commit()
828 cmd->committing_dmap = NULL; in dm_clone_metadata_commit()
831 up_write(&cmd->lock); in dm_clone_metadata_commit()
836 int dm_clone_set_region_hydrated(struct dm_clone_metadata *cmd, unsigned long region_nr) in dm_clone_set_region_hydrated() argument
842 if (unlikely(region_nr >= cmd->nr_regions)) { in dm_clone_set_region_hydrated()
844 region_nr, cmd->nr_regions); in dm_clone_set_region_hydrated()
850 spin_lock_irqsave(&cmd->bitmap_lock, flags); in dm_clone_set_region_hydrated()
852 if (cmd->read_only) { in dm_clone_set_region_hydrated()
857 dmap = cmd->current_dmap; in dm_clone_set_region_hydrated()
861 __set_bit(region_nr, cmd->region_map); in dm_clone_set_region_hydrated()
865 spin_unlock_irqrestore(&cmd->bitmap_lock, flags); in dm_clone_set_region_hydrated()
870 int dm_clone_cond_set_range(struct dm_clone_metadata *cmd, unsigned long start, in dm_clone_cond_set_range() argument
877 if (unlikely(start >= cmd->nr_regions || (start + nr_regions) < start || in dm_clone_cond_set_range()
878 (start + nr_regions) > cmd->nr_regions)) { in dm_clone_cond_set_range()
880 start, nr_regions, cmd->nr_regions); in dm_clone_cond_set_range()
884 spin_lock_irq(&cmd->bitmap_lock); in dm_clone_cond_set_range()
886 if (cmd->read_only) { in dm_clone_cond_set_range()
891 dmap = cmd->current_dmap; in dm_clone_cond_set_range()
893 if (!test_bit(region_nr, cmd->region_map)) { in dm_clone_cond_set_range()
897 __set_bit(region_nr, cmd->region_map); in dm_clone_cond_set_range()
902 spin_unlock_irq(&cmd->bitmap_lock); in dm_clone_cond_set_range()
917 int dm_clone_reload_in_core_bitset(struct dm_clone_metadata *cmd) in dm_clone_reload_in_core_bitset() argument
921 down_write(&cmd->lock); in dm_clone_reload_in_core_bitset()
923 if (cmd->fail_io) in dm_clone_reload_in_core_bitset()
926 r = __load_bitset_in_core(cmd); in dm_clone_reload_in_core_bitset()
928 up_write(&cmd->lock); in dm_clone_reload_in_core_bitset()
933 bool dm_clone_changed_this_transaction(struct dm_clone_metadata *cmd) in dm_clone_changed_this_transaction() argument
938 spin_lock_irqsave(&cmd->bitmap_lock, flags); in dm_clone_changed_this_transaction()
939 r = cmd->dmap[0].changed || cmd->dmap[1].changed; in dm_clone_changed_this_transaction()
940 spin_unlock_irqrestore(&cmd->bitmap_lock, flags); in dm_clone_changed_this_transaction()
945 int dm_clone_metadata_abort(struct dm_clone_metadata *cmd) in dm_clone_metadata_abort() argument
949 down_write(&cmd->lock); in dm_clone_metadata_abort()
951 if (cmd->fail_io || dm_bm_is_read_only(cmd->bm)) in dm_clone_metadata_abort()
954 __destroy_persistent_data_structures(cmd); in dm_clone_metadata_abort()
956 r = __create_persistent_data_structures(cmd, false); in dm_clone_metadata_abort()
959 cmd->fail_io = true; in dm_clone_metadata_abort()
962 up_write(&cmd->lock); in dm_clone_metadata_abort()
967 void dm_clone_metadata_set_read_only(struct dm_clone_metadata *cmd) in dm_clone_metadata_set_read_only() argument
969 down_write(&cmd->lock); in dm_clone_metadata_set_read_only()
971 spin_lock_irq(&cmd->bitmap_lock); in dm_clone_metadata_set_read_only()
972 cmd->read_only = 1; in dm_clone_metadata_set_read_only()
973 spin_unlock_irq(&cmd->bitmap_lock); in dm_clone_metadata_set_read_only()
975 if (!cmd->fail_io) in dm_clone_metadata_set_read_only()
976 dm_bm_set_read_only(cmd->bm); in dm_clone_metadata_set_read_only()
978 up_write(&cmd->lock); in dm_clone_metadata_set_read_only()
981 void dm_clone_metadata_set_read_write(struct dm_clone_metadata *cmd) in dm_clone_metadata_set_read_write() argument
983 down_write(&cmd->lock); in dm_clone_metadata_set_read_write()
985 spin_lock_irq(&cmd->bitmap_lock); in dm_clone_metadata_set_read_write()
986 cmd->read_only = 0; in dm_clone_metadata_set_read_write()
987 spin_unlock_irq(&cmd->bitmap_lock); in dm_clone_metadata_set_read_write()
989 if (!cmd->fail_io) in dm_clone_metadata_set_read_write()
990 dm_bm_set_read_write(cmd->bm); in dm_clone_metadata_set_read_write()
992 up_write(&cmd->lock); in dm_clone_metadata_set_read_write()
995 int dm_clone_get_free_metadata_block_count(struct dm_clone_metadata *cmd, in dm_clone_get_free_metadata_block_count() argument
1000 down_read(&cmd->lock); in dm_clone_get_free_metadata_block_count()
1002 if (!cmd->fail_io) in dm_clone_get_free_metadata_block_count()
1003 r = dm_sm_get_nr_free(cmd->sm, result); in dm_clone_get_free_metadata_block_count()
1005 up_read(&cmd->lock); in dm_clone_get_free_metadata_block_count()
1010 int dm_clone_get_metadata_dev_size(struct dm_clone_metadata *cmd, in dm_clone_get_metadata_dev_size() argument
1015 down_read(&cmd->lock); in dm_clone_get_metadata_dev_size()
1017 if (!cmd->fail_io) in dm_clone_get_metadata_dev_size()
1018 r = dm_sm_get_nr_blocks(cmd->sm, result); in dm_clone_get_metadata_dev_size()
1020 up_read(&cmd->lock); in dm_clone_get_metadata_dev_size()