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()
497 static int dirty_map_init(struct dm_clone_metadata *cmd) in dirty_map_init() argument
499 if (__dirty_map_init(&cmd->dmap[0], cmd->nr_words, cmd->nr_regions)) { in dirty_map_init()
504 if (__dirty_map_init(&cmd->dmap[1], cmd->nr_words, cmd->nr_regions)) { in dirty_map_init()
506 __dirty_map_exit(&cmd->dmap[0]); in dirty_map_init()
510 cmd->current_dmap = &cmd->dmap[0]; in dirty_map_init()
511 cmd->committing_dmap = NULL; in dirty_map_init()
516 static void dirty_map_exit(struct dm_clone_metadata *cmd) in dirty_map_exit() argument
518 __dirty_map_exit(&cmd->dmap[0]); in dirty_map_exit()
519 __dirty_map_exit(&cmd->dmap[1]); in dirty_map_exit()
522 static int __load_bitset_in_core(struct dm_clone_metadata *cmd) in __load_bitset_in_core() argument
529 r = dm_bitset_flush(&cmd->bitset_info, cmd->bitset_root, &cmd->bitset_root); in __load_bitset_in_core()
533 r = dm_bitset_cursor_begin(&cmd->bitset_info, cmd->bitset_root, cmd->nr_regions, &c); in __load_bitset_in_core()
539 __set_bit(i, cmd->region_map); in __load_bitset_in_core()
541 __clear_bit(i, cmd->region_map); in __load_bitset_in_core()
543 if (i >= (cmd->nr_regions - 1)) in __load_bitset_in_core()
562 struct dm_clone_metadata *cmd; in dm_clone_metadata_open() local
564 cmd = kzalloc(sizeof(*cmd), GFP_KERNEL); in dm_clone_metadata_open()
565 if (!cmd) { in dm_clone_metadata_open()
570 cmd->bdev = bdev; in dm_clone_metadata_open()
571 cmd->target_size = target_size; in dm_clone_metadata_open()
572 cmd->region_size = region_size; in dm_clone_metadata_open()
573 cmd->nr_regions = dm_sector_div_up(cmd->target_size, cmd->region_size); in dm_clone_metadata_open()
574 cmd->nr_words = BITS_TO_LONGS(cmd->nr_regions); in dm_clone_metadata_open()
576 init_rwsem(&cmd->lock); in dm_clone_metadata_open()
577 spin_lock_init(&cmd->bitmap_lock); in dm_clone_metadata_open()
578 cmd->read_only = 0; in dm_clone_metadata_open()
579 cmd->fail_io = false; in dm_clone_metadata_open()
580 cmd->hydration_done = false; in dm_clone_metadata_open()
582 cmd->region_map = kvmalloc(bitmap_size(cmd->nr_regions), GFP_KERNEL); in dm_clone_metadata_open()
583 if (!cmd->region_map) { in dm_clone_metadata_open()
589 r = __create_persistent_data_structures(cmd, true); in dm_clone_metadata_open()
593 r = __load_bitset_in_core(cmd); in dm_clone_metadata_open()
599 r = dirty_map_init(cmd); in dm_clone_metadata_open()
603 if (bitmap_full(cmd->region_map, cmd->nr_regions)) in dm_clone_metadata_open()
604 cmd->hydration_done = true; in dm_clone_metadata_open()
606 return cmd; in dm_clone_metadata_open()
609 __destroy_persistent_data_structures(cmd); in dm_clone_metadata_open()
612 kvfree(cmd->region_map); in dm_clone_metadata_open()
615 kfree(cmd); in dm_clone_metadata_open()
620 void dm_clone_metadata_close(struct dm_clone_metadata *cmd) in dm_clone_metadata_close() argument
622 if (!cmd->fail_io) in dm_clone_metadata_close()
623 __destroy_persistent_data_structures(cmd); in dm_clone_metadata_close()
625 dirty_map_exit(cmd); in dm_clone_metadata_close()
626 kvfree(cmd->region_map); in dm_clone_metadata_close()
627 kfree(cmd); in dm_clone_metadata_close()
630 bool dm_clone_is_hydration_done(struct dm_clone_metadata *cmd) in dm_clone_is_hydration_done() argument
632 return cmd->hydration_done; in dm_clone_is_hydration_done()
635 bool dm_clone_is_region_hydrated(struct dm_clone_metadata *cmd, unsigned long region_nr) in dm_clone_is_region_hydrated() argument
637 return dm_clone_is_hydration_done(cmd) || test_bit(region_nr, cmd->region_map); in dm_clone_is_region_hydrated()
640 bool dm_clone_is_range_hydrated(struct dm_clone_metadata *cmd, in dm_clone_is_range_hydrated() argument
645 if (dm_clone_is_hydration_done(cmd)) in dm_clone_is_range_hydrated()
648 bit = find_next_zero_bit(cmd->region_map, cmd->nr_regions, start); in dm_clone_is_range_hydrated()
653 unsigned int dm_clone_nr_of_hydrated_regions(struct dm_clone_metadata *cmd) in dm_clone_nr_of_hydrated_regions() argument
655 return bitmap_weight(cmd->region_map, cmd->nr_regions); in dm_clone_nr_of_hydrated_regions()
658 unsigned long dm_clone_find_next_unhydrated_region(struct dm_clone_metadata *cmd, in dm_clone_find_next_unhydrated_region() argument
661 return find_next_zero_bit(cmd->region_map, cmd->nr_regions, start); in dm_clone_find_next_unhydrated_region()
664 static int __update_metadata_word(struct dm_clone_metadata *cmd, in __update_metadata_word() argument
670 unsigned long max_index = min(cmd->nr_regions, (word + 1) * BITS_PER_LONG); in __update_metadata_word()
674 r = dm_bitset_set_bit(&cmd->bitset_info, cmd->bitset_root, in __update_metadata_word()
675 index, &cmd->bitset_root); in __update_metadata_word()
688 static int __metadata_commit(struct dm_clone_metadata *cmd) in __metadata_commit() argument
695 r = dm_bitset_flush(&cmd->bitset_info, cmd->bitset_root, &cmd->bitset_root); in __metadata_commit()
702 r = dm_tm_pre_commit(cmd->tm); in __metadata_commit()
709 r = __copy_sm_root(cmd); in __metadata_commit()
716 r = superblock_write_lock_zero(cmd, &sblock); in __metadata_commit()
724 __prepare_superblock(cmd, sb); in __metadata_commit()
727 r = dm_tm_commit(cmd->tm, sblock); in __metadata_commit()
736 if (bitmap_full(cmd->region_map, cmd->nr_regions)) in __metadata_commit()
737 cmd->hydration_done = true; in __metadata_commit()
742 static int __flush_dmap(struct dm_clone_metadata *cmd, struct dirty_map *dmap) in __flush_dmap() argument
749 word = find_next_bit(dmap->dirty_words, cmd->nr_words, word); in __flush_dmap()
751 if (word == cmd->nr_words) in __flush_dmap()
754 r = __update_metadata_word(cmd, dmap->dirty_regions, word); in __flush_dmap()
761 } while (word < cmd->nr_words); in __flush_dmap()
763 r = __metadata_commit(cmd); in __flush_dmap()
769 spin_lock_irq(&cmd->bitmap_lock); in __flush_dmap()
771 spin_unlock_irq(&cmd->bitmap_lock); in __flush_dmap()
776 int dm_clone_metadata_pre_commit(struct dm_clone_metadata *cmd) in dm_clone_metadata_pre_commit() argument
781 down_write(&cmd->lock); in dm_clone_metadata_pre_commit()
783 if (cmd->fail_io || dm_bm_is_read_only(cmd->bm)) { in dm_clone_metadata_pre_commit()
789 dmap = cmd->current_dmap; in dm_clone_metadata_pre_commit()
792 next_dmap = (dmap == &cmd->dmap[0]) ? &cmd->dmap[1] : &cmd->dmap[0]; in dm_clone_metadata_pre_commit()
798 if (WARN_ON(next_dmap->changed || cmd->committing_dmap)) { in dm_clone_metadata_pre_commit()
804 spin_lock_irq(&cmd->bitmap_lock); in dm_clone_metadata_pre_commit()
805 cmd->current_dmap = next_dmap; in dm_clone_metadata_pre_commit()
806 spin_unlock_irq(&cmd->bitmap_lock); in dm_clone_metadata_pre_commit()
809 cmd->committing_dmap = dmap; in dm_clone_metadata_pre_commit()
811 up_write(&cmd->lock); in dm_clone_metadata_pre_commit()
816 int dm_clone_metadata_commit(struct dm_clone_metadata *cmd) in dm_clone_metadata_commit() argument
820 down_write(&cmd->lock); in dm_clone_metadata_commit()
822 if (cmd->fail_io || dm_bm_is_read_only(cmd->bm)) in dm_clone_metadata_commit()
825 if (WARN_ON(!cmd->committing_dmap)) { in dm_clone_metadata_commit()
830 r = __flush_dmap(cmd, cmd->committing_dmap); in dm_clone_metadata_commit()
833 cmd->committing_dmap = NULL; in dm_clone_metadata_commit()
836 up_write(&cmd->lock); in dm_clone_metadata_commit()
841 int dm_clone_set_region_hydrated(struct dm_clone_metadata *cmd, unsigned long region_nr) in dm_clone_set_region_hydrated() argument
847 if (unlikely(region_nr >= cmd->nr_regions)) { in dm_clone_set_region_hydrated()
849 region_nr, cmd->nr_regions); in dm_clone_set_region_hydrated()
855 spin_lock_irqsave(&cmd->bitmap_lock, flags); in dm_clone_set_region_hydrated()
857 if (cmd->read_only) { in dm_clone_set_region_hydrated()
862 dmap = cmd->current_dmap; in dm_clone_set_region_hydrated()
866 __set_bit(region_nr, cmd->region_map); in dm_clone_set_region_hydrated()
870 spin_unlock_irqrestore(&cmd->bitmap_lock, flags); in dm_clone_set_region_hydrated()
875 int dm_clone_cond_set_range(struct dm_clone_metadata *cmd, unsigned long start, in dm_clone_cond_set_range() argument
882 if (unlikely(start >= cmd->nr_regions || (start + nr_regions) < start || in dm_clone_cond_set_range()
883 (start + nr_regions) > cmd->nr_regions)) { in dm_clone_cond_set_range()
885 start, nr_regions, cmd->nr_regions); in dm_clone_cond_set_range()
889 spin_lock_irq(&cmd->bitmap_lock); in dm_clone_cond_set_range()
891 if (cmd->read_only) { in dm_clone_cond_set_range()
896 dmap = cmd->current_dmap; in dm_clone_cond_set_range()
898 if (!test_bit(region_nr, cmd->region_map)) { in dm_clone_cond_set_range()
902 __set_bit(region_nr, cmd->region_map); in dm_clone_cond_set_range()
907 spin_unlock_irq(&cmd->bitmap_lock); in dm_clone_cond_set_range()
922 int dm_clone_reload_in_core_bitset(struct dm_clone_metadata *cmd) in dm_clone_reload_in_core_bitset() argument
926 down_write(&cmd->lock); in dm_clone_reload_in_core_bitset()
928 if (cmd->fail_io) in dm_clone_reload_in_core_bitset()
931 r = __load_bitset_in_core(cmd); in dm_clone_reload_in_core_bitset()
933 up_write(&cmd->lock); in dm_clone_reload_in_core_bitset()
938 bool dm_clone_changed_this_transaction(struct dm_clone_metadata *cmd) in dm_clone_changed_this_transaction() argument
943 spin_lock_irqsave(&cmd->bitmap_lock, flags); in dm_clone_changed_this_transaction()
944 r = cmd->dmap[0].changed || cmd->dmap[1].changed; in dm_clone_changed_this_transaction()
945 spin_unlock_irqrestore(&cmd->bitmap_lock, flags); in dm_clone_changed_this_transaction()
950 int dm_clone_metadata_abort(struct dm_clone_metadata *cmd) in dm_clone_metadata_abort() argument
954 down_write(&cmd->lock); in dm_clone_metadata_abort()
956 if (cmd->fail_io || dm_bm_is_read_only(cmd->bm)) in dm_clone_metadata_abort()
959 __destroy_persistent_data_structures(cmd); in dm_clone_metadata_abort()
961 r = __create_persistent_data_structures(cmd, false); in dm_clone_metadata_abort()
964 cmd->fail_io = true; in dm_clone_metadata_abort()
967 up_write(&cmd->lock); in dm_clone_metadata_abort()
972 void dm_clone_metadata_set_read_only(struct dm_clone_metadata *cmd) in dm_clone_metadata_set_read_only() argument
974 down_write(&cmd->lock); in dm_clone_metadata_set_read_only()
976 spin_lock_irq(&cmd->bitmap_lock); in dm_clone_metadata_set_read_only()
977 cmd->read_only = 1; in dm_clone_metadata_set_read_only()
978 spin_unlock_irq(&cmd->bitmap_lock); in dm_clone_metadata_set_read_only()
980 if (!cmd->fail_io) in dm_clone_metadata_set_read_only()
981 dm_bm_set_read_only(cmd->bm); in dm_clone_metadata_set_read_only()
983 up_write(&cmd->lock); in dm_clone_metadata_set_read_only()
986 void dm_clone_metadata_set_read_write(struct dm_clone_metadata *cmd) in dm_clone_metadata_set_read_write() argument
988 down_write(&cmd->lock); in dm_clone_metadata_set_read_write()
990 spin_lock_irq(&cmd->bitmap_lock); in dm_clone_metadata_set_read_write()
991 cmd->read_only = 0; in dm_clone_metadata_set_read_write()
992 spin_unlock_irq(&cmd->bitmap_lock); in dm_clone_metadata_set_read_write()
994 if (!cmd->fail_io) in dm_clone_metadata_set_read_write()
995 dm_bm_set_read_write(cmd->bm); in dm_clone_metadata_set_read_write()
997 up_write(&cmd->lock); in dm_clone_metadata_set_read_write()
1000 int dm_clone_get_free_metadata_block_count(struct dm_clone_metadata *cmd, in dm_clone_get_free_metadata_block_count() argument
1005 down_read(&cmd->lock); in dm_clone_get_free_metadata_block_count()
1007 if (!cmd->fail_io) in dm_clone_get_free_metadata_block_count()
1008 r = dm_sm_get_nr_free(cmd->sm, result); in dm_clone_get_free_metadata_block_count()
1010 up_read(&cmd->lock); in dm_clone_get_free_metadata_block_count()
1015 int dm_clone_get_metadata_dev_size(struct dm_clone_metadata *cmd, in dm_clone_get_metadata_dev_size() argument
1020 down_read(&cmd->lock); in dm_clone_get_metadata_dev_size()
1022 if (!cmd->fail_io) in dm_clone_get_metadata_dev_size()
1023 r = dm_sm_get_nr_blocks(cmd->sm, result); in dm_clone_get_metadata_dev_size()
1025 up_read(&cmd->lock); in dm_clone_get_metadata_dev_size()