Lines Matching refs:cmd

202 static int superblock_read_lock(struct dm_cache_metadata *cmd,
205 return dm_bm_read_lock(cmd->bm, CACHE_SUPERBLOCK_LOCATION,
209 static int superblock_lock_zero(struct dm_cache_metadata *cmd,
212 return dm_bm_write_lock_zero(cmd->bm, CACHE_SUPERBLOCK_LOCATION,
216 static int superblock_lock(struct dm_cache_metadata *cmd,
219 return dm_bm_write_lock(cmd->bm, CACHE_SUPERBLOCK_LOCATION,
252 static void __setup_mapping_info(struct dm_cache_metadata *cmd)
261 dm_array_info_init(&cmd->info, cmd->tm, &vt);
263 if (cmd->policy_hint_size) {
265 dm_array_info_init(&cmd->hint_info, cmd->tm, &vt);
269 static int __save_sm_root(struct dm_cache_metadata *cmd)
274 r = dm_sm_root_size(cmd->metadata_sm, &metadata_len);
278 return dm_sm_copy_root(cmd->metadata_sm, &cmd->metadata_space_map_root,
282 static void __copy_sm_root(struct dm_cache_metadata *cmd,
286 &cmd->metadata_space_map_root,
287 sizeof(cmd->metadata_space_map_root));
290 static int __write_initial_superblock(struct dm_cache_metadata *cmd)
295 sector_t bdev_size = i_size_read(cmd->bdev->bd_inode) >> SECTOR_SHIFT;
301 r = dm_tm_pre_commit(cmd->tm);
309 r = __save_sm_root(cmd);
313 r = superblock_lock_zero(cmd, &sblock);
326 __copy_sm_root(cmd, disk_super);
328 disk_super->mapping_root = cpu_to_le64(cmd->root);
329 disk_super->hint_root = cpu_to_le64(cmd->hint_root);
330 disk_super->discard_root = cpu_to_le64(cmd->discard_root);
331 disk_super->discard_block_size = cpu_to_le64(cmd->discard_block_size);
332 disk_super->discard_nr_blocks = cpu_to_le64(from_oblock(cmd->discard_nr_blocks));
334 disk_super->data_block_size = cpu_to_le32(cmd->data_block_size);
342 return dm_tm_commit(cmd->tm, sblock);
345 static int __format_metadata(struct dm_cache_metadata *cmd)
349 r = dm_tm_create_with_sm(cmd->bm, CACHE_SUPERBLOCK_LOCATION,
350 &cmd->tm, &cmd->metadata_sm);
356 __setup_mapping_info(cmd);
358 r = dm_array_empty(&cmd->info, &cmd->root);
362 dm_disk_bitset_init(cmd->tm, &cmd->discard_info);
364 r = dm_bitset_empty(&cmd->discard_info, &cmd->discard_root);
368 cmd->discard_block_size = 0;
369 cmd->discard_nr_blocks = 0;
371 r = __write_initial_superblock(cmd);
375 cmd->clean_when_opened = true;
379 dm_tm_destroy(cmd->tm);
380 dm_sm_destroy(cmd->metadata_sm);
386 struct dm_cache_metadata *cmd)
400 if (get_disk_ro(cmd->bdev->bd_disk))
413 static int __open_metadata(struct dm_cache_metadata *cmd)
420 r = superblock_read_lock(cmd, &sblock);
429 if (le32_to_cpu(disk_super->data_block_size) != cmd->data_block_size) {
432 (unsigned long long)cmd->data_block_size);
437 r = __check_incompat_features(disk_super, cmd);
441 r = dm_tm_open_with_sm(cmd->bm, CACHE_SUPERBLOCK_LOCATION,
444 &cmd->tm, &cmd->metadata_sm);
450 __setup_mapping_info(cmd);
451 dm_disk_bitset_init(cmd->tm, &cmd->discard_info);
453 cmd->clean_when_opened = test_bit(CLEAN_SHUTDOWN, &sb_flags);
461 static int __open_or_format_metadata(struct dm_cache_metadata *cmd,
467 r = __superblock_all_zeroes(cmd->bm, &unformatted);
472 return format_device ? __format_metadata(cmd) : -EPERM;
474 return __open_metadata(cmd);
477 static int __create_persistent_data_objects(struct dm_cache_metadata *cmd,
481 cmd->bm = dm_block_manager_create(cmd->bdev, DM_CACHE_METADATA_BLOCK_SIZE << SECTOR_SHIFT,
484 if (IS_ERR(cmd->bm)) {
486 return PTR_ERR(cmd->bm);
489 r = __open_or_format_metadata(cmd, may_format_device);
491 dm_block_manager_destroy(cmd->bm);
496 static void __destroy_persistent_data_objects(struct dm_cache_metadata *cmd)
498 dm_sm_destroy(cmd->metadata_sm);
499 dm_tm_destroy(cmd->tm);
500 dm_block_manager_destroy(cmd->bm);
524 static void read_superblock_fields(struct dm_cache_metadata *cmd,
527 cmd->root = le64_to_cpu(disk_super->mapping_root);
528 cmd->hint_root = le64_to_cpu(disk_super->hint_root);
529 cmd->discard_root = le64_to_cpu(disk_super->discard_root);
530 cmd->discard_block_size = le64_to_cpu(disk_super->discard_block_size);
531 cmd->discard_nr_blocks = to_oblock(le64_to_cpu(disk_super->discard_nr_blocks));
532 cmd->data_block_size = le32_to_cpu(disk_super->data_block_size);
533 cmd->cache_blocks = to_cblock(le32_to_cpu(disk_super->cache_blocks));
534 strncpy(cmd->policy_name, disk_super->policy_name, sizeof(cmd->policy_name));
535 cmd->policy_version[0] = le32_to_cpu(disk_super->policy_version[0]);
536 cmd->policy_version[1] = le32_to_cpu(disk_super->policy_version[1]);
537 cmd->policy_version[2] = le32_to_cpu(disk_super->policy_version[2]);
538 cmd->policy_hint_size = le32_to_cpu(disk_super->policy_hint_size);
540 cmd->stats.read_hits = le32_to_cpu(disk_super->read_hits);
541 cmd->stats.read_misses = le32_to_cpu(disk_super->read_misses);
542 cmd->stats.write_hits = le32_to_cpu(disk_super->write_hits);
543 cmd->stats.write_misses = le32_to_cpu(disk_super->write_misses);
545 cmd->changed = false;
551 static int __begin_transaction_flags(struct dm_cache_metadata *cmd,
558 r = superblock_lock(cmd, &sblock);
564 read_superblock_fields(cmd, disk_super);
567 return dm_bm_flush(cmd->bm);
570 static int __begin_transaction(struct dm_cache_metadata *cmd)
580 r = superblock_read_lock(cmd, &sblock);
585 read_superblock_fields(cmd, disk_super);
591 static int __commit_transaction(struct dm_cache_metadata *cmd,
603 r = dm_bitset_flush(&cmd->discard_info, cmd->discard_root,
604 &cmd->discard_root);
608 r = dm_tm_pre_commit(cmd->tm);
612 r = __save_sm_root(cmd);
616 r = superblock_lock(cmd, &sblock);
625 disk_super->mapping_root = cpu_to_le64(cmd->root);
626 disk_super->hint_root = cpu_to_le64(cmd->hint_root);
627 disk_super->discard_root = cpu_to_le64(cmd->discard_root);
628 disk_super->discard_block_size = cpu_to_le64(cmd->discard_block_size);
629 disk_super->discard_nr_blocks = cpu_to_le64(from_oblock(cmd->discard_nr_blocks));
630 disk_super->cache_blocks = cpu_to_le32(from_cblock(cmd->cache_blocks));
631 strncpy(disk_super->policy_name, cmd->policy_name, sizeof(disk_super->policy_name));
632 disk_super->policy_version[0] = cpu_to_le32(cmd->policy_version[0]);
633 disk_super->policy_version[1] = cpu_to_le32(cmd->policy_version[1]);
634 disk_super->policy_version[2] = cpu_to_le32(cmd->policy_version[2]);
636 disk_super->read_hits = cpu_to_le32(cmd->stats.read_hits);
637 disk_super->read_misses = cpu_to_le32(cmd->stats.read_misses);
638 disk_super->write_hits = cpu_to_le32(cmd->stats.write_hits);
639 disk_super->write_misses = cpu_to_le32(cmd->stats.write_misses);
640 __copy_sm_root(cmd, disk_super);
642 return dm_tm_commit(cmd->tm, sblock);
678 struct dm_cache_metadata *cmd;
680 cmd = kzalloc(sizeof(*cmd), GFP_KERNEL);
681 if (!cmd) {
686 init_rwsem(&cmd->root_lock);
687 cmd->bdev = bdev;
688 cmd->data_block_size = data_block_size;
689 cmd->cache_blocks = 0;
690 cmd->policy_hint_size = policy_hint_size;
691 cmd->changed = true;
693 r = __create_persistent_data_objects(cmd, may_format_device);
695 kfree(cmd);
699 r = __begin_transaction_flags(cmd, clear_clean_shutdown);
701 dm_cache_metadata_close(cmd);
705 return cmd;
708 void dm_cache_metadata_close(struct dm_cache_metadata *cmd)
710 __destroy_persistent_data_objects(cmd);
711 kfree(cmd);
717 static int block_unmapped_or_clean(struct dm_cache_metadata *cmd, dm_cblock_t b,
725 r = dm_array_get_value(&cmd->info, cmd->root, from_cblock(b), &value);
737 static int blocks_are_unmapped_or_clean(struct dm_cache_metadata *cmd,
745 r = block_unmapped_or_clean(cmd, begin, result);
761 int dm_cache_resize(struct dm_cache_metadata *cmd, dm_cblock_t new_cache_size)
767 down_write(&cmd->root_lock);
770 if (from_cblock(new_cache_size) < from_cblock(cmd->cache_blocks)) {
771 r = blocks_are_unmapped_or_clean(cmd, new_cache_size, cmd->cache_blocks, &clean);
785 r = dm_array_resize(&cmd->info, cmd->root, from_cblock(cmd->cache_blocks),
787 &null_mapping, &cmd->root);
789 cmd->cache_blocks = new_cache_size;
790 cmd->changed = true;
793 up_write(&cmd->root_lock);
798 int dm_cache_discard_bitset_resize(struct dm_cache_metadata *cmd,
804 down_write(&cmd->root_lock);
805 r = dm_bitset_resize(&cmd->discard_info,
806 cmd->discard_root,
807 from_oblock(cmd->discard_nr_blocks),
809 false, &cmd->discard_root);
811 cmd->discard_block_size = discard_block_size;
812 cmd->discard_nr_blocks = new_nr_entries;
815 cmd->changed = true;
816 up_write(&cmd->root_lock);
821 static int __set_discard(struct dm_cache_metadata *cmd, dm_oblock_t b)
823 return dm_bitset_set_bit(&cmd->discard_info, cmd->discard_root,
824 from_oblock(b), &cmd->discard_root);
827 static int __clear_discard(struct dm_cache_metadata *cmd, dm_oblock_t b)
829 return dm_bitset_clear_bit(&cmd->discard_info, cmd->discard_root,
830 from_oblock(b), &cmd->discard_root);
833 static int __is_discarded(struct dm_cache_metadata *cmd, dm_oblock_t b,
836 return dm_bitset_test_bit(&cmd->discard_info, cmd->discard_root,
837 from_oblock(b), &cmd->discard_root,
841 static int __discard(struct dm_cache_metadata *cmd,
846 r = (discard ? __set_discard : __clear_discard)(cmd, dblock);
850 cmd->changed = true;
854 int dm_cache_set_discard(struct dm_cache_metadata *cmd,
859 down_write(&cmd->root_lock);
860 r = __discard(cmd, dblock, discard);
861 up_write(&cmd->root_lock);
866 static int __load_discards(struct dm_cache_metadata *cmd,
873 for (b = 0; b < from_oblock(cmd->discard_nr_blocks); b++) {
876 if (cmd->clean_when_opened) {
877 r = __is_discarded(cmd, dblock, &discard);
883 r = fn(context, cmd->discard_block_size, dblock, discard);
891 int dm_cache_load_discards(struct dm_cache_metadata *cmd,
896 down_read(&cmd->root_lock);
897 r = __load_discards(cmd, fn, context);
898 up_read(&cmd->root_lock);
903 dm_cblock_t dm_cache_size(struct dm_cache_metadata *cmd)
907 down_read(&cmd->root_lock);
908 r = cmd->cache_blocks;
909 up_read(&cmd->root_lock);
914 static int __remove(struct dm_cache_metadata *cmd, dm_cblock_t cblock)
920 r = dm_array_set_value(&cmd->info, cmd->root, from_cblock(cblock),
921 &value, &cmd->root);
925 cmd->changed = true;
929 int dm_cache_remove_mapping(struct dm_cache_metadata *cmd, dm_cblock_t cblock)
933 down_write(&cmd->root_lock);
934 r = __remove(cmd, cblock);
935 up_write(&cmd->root_lock);
940 static int __insert(struct dm_cache_metadata *cmd,
947 r = dm_array_set_value(&cmd->info, cmd->root, from_cblock(cblock),
948 &value, &cmd->root);
952 cmd->changed = true;
956 int dm_cache_insert_mapping(struct dm_cache_metadata *cmd,
961 down_write(&cmd->root_lock);
962 r = __insert(cmd, cblock, oblock);
963 up_write(&cmd->root_lock);
972 struct dm_cache_metadata *cmd;
977 static bool policy_unchanged(struct dm_cache_metadata *cmd,
987 if (strncmp(cmd->policy_name, policy_name, sizeof(cmd->policy_name)))
993 if (cmd->policy_version[0] != policy_version[0])
999 if (cmd->policy_hint_size != policy_hint_size)
1005 static bool hints_array_initialized(struct dm_cache_metadata *cmd)
1007 return cmd->hint_root && cmd->policy_hint_size;
1010 static bool hints_array_available(struct dm_cache_metadata *cmd,
1013 return cmd->clean_when_opened && policy_unchanged(cmd, policy) &&
1014 hints_array_initialized(cmd);
1026 struct dm_cache_metadata *cmd = thunk->cmd;
1033 r = dm_array_get_value(&cmd->hint_info, cmd->hint_root,
1047 static int __load_mappings(struct dm_cache_metadata *cmd,
1056 thunk.cmd = cmd;
1057 thunk.respect_dirty_flags = cmd->clean_when_opened;
1058 thunk.hints_valid = hints_array_available(cmd, policy);
1060 return dm_array_walk(&cmd->info, cmd->root, __load_mapping, &thunk);
1063 int dm_cache_load_mappings(struct dm_cache_metadata *cmd,
1069 down_read(&cmd->root_lock);
1070 r = __load_mappings(cmd, policy, fn, context);
1071 up_read(&cmd->root_lock);
1089 static int __dump_mappings(struct dm_cache_metadata *cmd)
1091 return dm_array_walk(&cmd->info, cmd->root, __dump_mapping, NULL);
1094 void dm_cache_dump(struct dm_cache_metadata *cmd)
1096 down_read(&cmd->root_lock);
1097 __dump_mappings(cmd);
1098 up_read(&cmd->root_lock);
1101 int dm_cache_changed_this_transaction(struct dm_cache_metadata *cmd)
1105 down_read(&cmd->root_lock);
1106 r = cmd->changed;
1107 up_read(&cmd->root_lock);
1112 static int __dirty(struct dm_cache_metadata *cmd, dm_cblock_t cblock, bool dirty)
1119 r = dm_array_get_value(&cmd->info, cmd->root, from_cblock(cblock), &value);
1132 r = dm_array_set_value(&cmd->info, cmd->root, from_cblock(cblock),
1133 &value, &cmd->root);
1137 cmd->changed = true;
1142 int dm_cache_set_dirty(struct dm_cache_metadata *cmd,
1147 down_write(&cmd->root_lock);
1148 r = __dirty(cmd, cblock, dirty);
1149 up_write(&cmd->root_lock);
1154 void dm_cache_metadata_get_stats(struct dm_cache_metadata *cmd,
1157 down_read(&cmd->root_lock);
1158 *stats = cmd->stats;
1159 up_read(&cmd->root_lock);
1162 void dm_cache_metadata_set_stats(struct dm_cache_metadata *cmd,
1165 down_write(&cmd->root_lock);
1166 cmd->stats = *stats;
1167 up_write(&cmd->root_lock);
1170 int dm_cache_commit(struct dm_cache_metadata *cmd, bool clean_shutdown)
1176 down_write(&cmd->root_lock);
1177 r = __commit_transaction(cmd, mutator);
1181 r = __begin_transaction(cmd);
1184 up_write(&cmd->root_lock);
1188 int dm_cache_get_free_metadata_block_count(struct dm_cache_metadata *cmd,
1193 down_read(&cmd->root_lock);
1194 r = dm_sm_get_nr_free(cmd->metadata_sm, result);
1195 up_read(&cmd->root_lock);
1200 int dm_cache_get_metadata_dev_size(struct dm_cache_metadata *cmd,
1205 down_read(&cmd->root_lock);
1206 r = dm_sm_get_nr_blocks(cmd->metadata_sm, result);
1207 up_read(&cmd->root_lock);
1214 static int begin_hints(struct dm_cache_metadata *cmd, struct dm_cache_policy *policy)
1223 (strlen(policy_name) > sizeof(cmd->policy_name) - 1))
1226 if (!policy_unchanged(cmd, policy)) {
1227 strncpy(cmd->policy_name, policy_name, sizeof(cmd->policy_name));
1228 memcpy(cmd->policy_version, policy_version, sizeof(cmd->policy_version));
1233 cmd->policy_hint_size = hint_size;
1235 if (cmd->hint_root) {
1236 r = dm_array_del(&cmd->hint_info, cmd->hint_root);
1241 r = dm_array_empty(&cmd->hint_info, &cmd->hint_root);
1247 r = dm_array_resize(&cmd->hint_info, cmd->hint_root, 0,
1248 from_cblock(cmd->cache_blocks),
1249 &value, &cmd->hint_root);
1259 struct dm_cache_metadata *cmd = context;
1265 r = dm_array_set_value(&cmd->hint_info, cmd->hint_root,
1266 from_cblock(cblock), &value, &cmd->hint_root);
1267 cmd->changed = true;
1272 static int write_hints(struct dm_cache_metadata *cmd, struct dm_cache_policy *policy)
1276 r = begin_hints(cmd, policy);
1282 return policy_walk_mappings(policy, save_hint, cmd);
1285 int dm_cache_write_hints(struct dm_cache_metadata *cmd, struct dm_cache_policy *policy)
1289 down_write(&cmd->root_lock);
1290 r = write_hints(cmd, policy);
1291 up_write(&cmd->root_lock);
1296 int dm_cache_metadata_all_clean(struct dm_cache_metadata *cmd, bool *result)
1298 return blocks_are_unmapped_or_clean(cmd, 0, cmd->cache_blocks, result);