dm-cache-metadata.c 35 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485
  1. /*
  2. * Copyright (C) 2012 Red Hat, Inc.
  3. *
  4. * This file is released under the GPL.
  5. */
  6. #include "dm-cache-metadata.h"
  7. #include "persistent-data/dm-array.h"
  8. #include "persistent-data/dm-bitset.h"
  9. #include "persistent-data/dm-space-map.h"
  10. #include "persistent-data/dm-space-map-disk.h"
  11. #include "persistent-data/dm-transaction-manager.h"
  12. #include <linux/device-mapper.h>
  13. /*----------------------------------------------------------------*/
  14. #define DM_MSG_PREFIX "cache metadata"
  15. #define CACHE_SUPERBLOCK_MAGIC 06142003
  16. #define CACHE_SUPERBLOCK_LOCATION 0
  17. /*
  18. * defines a range of metadata versions that this module can handle.
  19. */
  20. #define MIN_CACHE_VERSION 1
  21. #define MAX_CACHE_VERSION 1
  22. #define CACHE_METADATA_CACHE_SIZE 64
  23. /*
  24. * 3 for btree insert +
  25. * 2 for btree lookup used within space map
  26. */
  27. #define CACHE_MAX_CONCURRENT_LOCKS 5
  28. #define SPACE_MAP_ROOT_SIZE 128
  29. enum superblock_flag_bits {
  30. /* for spotting crashes that would invalidate the dirty bitset */
  31. CLEAN_SHUTDOWN,
  32. /* metadata must be checked using the tools */
  33. NEEDS_CHECK,
  34. };
  35. /*
  36. * Each mapping from cache block -> origin block carries a set of flags.
  37. */
  38. enum mapping_bits {
  39. /*
  40. * A valid mapping. Because we're using an array we clear this
  41. * flag for an non existant mapping.
  42. */
  43. M_VALID = 1,
  44. /*
  45. * The data on the cache is different from that on the origin.
  46. */
  47. M_DIRTY = 2
  48. };
  49. struct cache_disk_superblock {
  50. __le32 csum;
  51. __le32 flags;
  52. __le64 blocknr;
  53. __u8 uuid[16];
  54. __le64 magic;
  55. __le32 version;
  56. __u8 policy_name[CACHE_POLICY_NAME_SIZE];
  57. __le32 policy_hint_size;
  58. __u8 metadata_space_map_root[SPACE_MAP_ROOT_SIZE];
  59. __le64 mapping_root;
  60. __le64 hint_root;
  61. __le64 discard_root;
  62. __le64 discard_block_size;
  63. __le64 discard_nr_blocks;
  64. __le32 data_block_size;
  65. __le32 metadata_block_size;
  66. __le32 cache_blocks;
  67. __le32 compat_flags;
  68. __le32 compat_ro_flags;
  69. __le32 incompat_flags;
  70. __le32 read_hits;
  71. __le32 read_misses;
  72. __le32 write_hits;
  73. __le32 write_misses;
  74. __le32 policy_version[CACHE_POLICY_VERSION_SIZE];
  75. } __packed;
  76. struct dm_cache_metadata {
  77. atomic_t ref_count;
  78. struct list_head list;
  79. struct block_device *bdev;
  80. struct dm_block_manager *bm;
  81. struct dm_space_map *metadata_sm;
  82. struct dm_transaction_manager *tm;
  83. struct dm_array_info info;
  84. struct dm_array_info hint_info;
  85. struct dm_disk_bitset discard_info;
  86. struct rw_semaphore root_lock;
  87. unsigned long flags;
  88. dm_block_t root;
  89. dm_block_t hint_root;
  90. dm_block_t discard_root;
  91. sector_t discard_block_size;
  92. dm_dblock_t discard_nr_blocks;
  93. sector_t data_block_size;
  94. dm_cblock_t cache_blocks;
  95. bool changed:1;
  96. bool clean_when_opened:1;
  97. char policy_name[CACHE_POLICY_NAME_SIZE];
  98. unsigned policy_version[CACHE_POLICY_VERSION_SIZE];
  99. size_t policy_hint_size;
  100. struct dm_cache_statistics stats;
  101. /*
  102. * Reading the space map root can fail, so we read it into this
  103. * buffer before the superblock is locked and updated.
  104. */
  105. __u8 metadata_space_map_root[SPACE_MAP_ROOT_SIZE];
  106. /*
  107. * Set if a transaction has to be aborted but the attempt to roll
  108. * back to the previous (good) transaction failed. The only
  109. * metadata operation permissible in this state is the closing of
  110. * the device.
  111. */
  112. bool fail_io:1;
  113. };
  114. /*-------------------------------------------------------------------
  115. * superblock validator
  116. *-----------------------------------------------------------------*/
  117. #define SUPERBLOCK_CSUM_XOR 9031977
  118. static void sb_prepare_for_write(struct dm_block_validator *v,
  119. struct dm_block *b,
  120. size_t sb_block_size)
  121. {
  122. struct cache_disk_superblock *disk_super = dm_block_data(b);
  123. disk_super->blocknr = cpu_to_le64(dm_block_location(b));
  124. disk_super->csum = cpu_to_le32(dm_bm_checksum(&disk_super->flags,
  125. sb_block_size - sizeof(__le32),
  126. SUPERBLOCK_CSUM_XOR));
  127. }
  128. static int check_metadata_version(struct cache_disk_superblock *disk_super)
  129. {
  130. uint32_t metadata_version = le32_to_cpu(disk_super->version);
  131. if (metadata_version < MIN_CACHE_VERSION || metadata_version > MAX_CACHE_VERSION) {
  132. DMERR("Cache metadata version %u found, but only versions between %u and %u supported.",
  133. metadata_version, MIN_CACHE_VERSION, MAX_CACHE_VERSION);
  134. return -EINVAL;
  135. }
  136. return 0;
  137. }
  138. static int sb_check(struct dm_block_validator *v,
  139. struct dm_block *b,
  140. size_t sb_block_size)
  141. {
  142. struct cache_disk_superblock *disk_super = dm_block_data(b);
  143. __le32 csum_le;
  144. if (dm_block_location(b) != le64_to_cpu(disk_super->blocknr)) {
  145. DMERR("sb_check failed: blocknr %llu: wanted %llu",
  146. le64_to_cpu(disk_super->blocknr),
  147. (unsigned long long)dm_block_location(b));
  148. return -ENOTBLK;
  149. }
  150. if (le64_to_cpu(disk_super->magic) != CACHE_SUPERBLOCK_MAGIC) {
  151. DMERR("sb_check failed: magic %llu: wanted %llu",
  152. le64_to_cpu(disk_super->magic),
  153. (unsigned long long)CACHE_SUPERBLOCK_MAGIC);
  154. return -EILSEQ;
  155. }
  156. csum_le = cpu_to_le32(dm_bm_checksum(&disk_super->flags,
  157. sb_block_size - sizeof(__le32),
  158. SUPERBLOCK_CSUM_XOR));
  159. if (csum_le != disk_super->csum) {
  160. DMERR("sb_check failed: csum %u: wanted %u",
  161. le32_to_cpu(csum_le), le32_to_cpu(disk_super->csum));
  162. return -EILSEQ;
  163. }
  164. return check_metadata_version(disk_super);
  165. }
  166. static struct dm_block_validator sb_validator = {
  167. .name = "superblock",
  168. .prepare_for_write = sb_prepare_for_write,
  169. .check = sb_check
  170. };
  171. /*----------------------------------------------------------------*/
  172. static int superblock_read_lock(struct dm_cache_metadata *cmd,
  173. struct dm_block **sblock)
  174. {
  175. return dm_bm_read_lock(cmd->bm, CACHE_SUPERBLOCK_LOCATION,
  176. &sb_validator, sblock);
  177. }
  178. static int superblock_lock_zero(struct dm_cache_metadata *cmd,
  179. struct dm_block **sblock)
  180. {
  181. return dm_bm_write_lock_zero(cmd->bm, CACHE_SUPERBLOCK_LOCATION,
  182. &sb_validator, sblock);
  183. }
  184. static int superblock_lock(struct dm_cache_metadata *cmd,
  185. struct dm_block **sblock)
  186. {
  187. return dm_bm_write_lock(cmd->bm, CACHE_SUPERBLOCK_LOCATION,
  188. &sb_validator, sblock);
  189. }
  190. /*----------------------------------------------------------------*/
  191. static int __superblock_all_zeroes(struct dm_block_manager *bm, bool *result)
  192. {
  193. int r;
  194. unsigned i;
  195. struct dm_block *b;
  196. __le64 *data_le, zero = cpu_to_le64(0);
  197. unsigned sb_block_size = dm_bm_block_size(bm) / sizeof(__le64);
  198. /*
  199. * We can't use a validator here - it may be all zeroes.
  200. */
  201. r = dm_bm_read_lock(bm, CACHE_SUPERBLOCK_LOCATION, NULL, &b);
  202. if (r)
  203. return r;
  204. data_le = dm_block_data(b);
  205. *result = true;
  206. for (i = 0; i < sb_block_size; i++) {
  207. if (data_le[i] != zero) {
  208. *result = false;
  209. break;
  210. }
  211. }
  212. return dm_bm_unlock(b);
  213. }
  214. static void __setup_mapping_info(struct dm_cache_metadata *cmd)
  215. {
  216. struct dm_btree_value_type vt;
  217. vt.context = NULL;
  218. vt.size = sizeof(__le64);
  219. vt.inc = NULL;
  220. vt.dec = NULL;
  221. vt.equal = NULL;
  222. dm_array_info_init(&cmd->info, cmd->tm, &vt);
  223. if (cmd->policy_hint_size) {
  224. vt.size = sizeof(__le32);
  225. dm_array_info_init(&cmd->hint_info, cmd->tm, &vt);
  226. }
  227. }
  228. static int __save_sm_root(struct dm_cache_metadata *cmd)
  229. {
  230. int r;
  231. size_t metadata_len;
  232. r = dm_sm_root_size(cmd->metadata_sm, &metadata_len);
  233. if (r < 0)
  234. return r;
  235. return dm_sm_copy_root(cmd->metadata_sm, &cmd->metadata_space_map_root,
  236. metadata_len);
  237. }
  238. static void __copy_sm_root(struct dm_cache_metadata *cmd,
  239. struct cache_disk_superblock *disk_super)
  240. {
  241. memcpy(&disk_super->metadata_space_map_root,
  242. &cmd->metadata_space_map_root,
  243. sizeof(cmd->metadata_space_map_root));
  244. }
  245. static int __write_initial_superblock(struct dm_cache_metadata *cmd)
  246. {
  247. int r;
  248. struct dm_block *sblock;
  249. struct cache_disk_superblock *disk_super;
  250. sector_t bdev_size = i_size_read(cmd->bdev->bd_inode) >> SECTOR_SHIFT;
  251. /* FIXME: see if we can lose the max sectors limit */
  252. if (bdev_size > DM_CACHE_METADATA_MAX_SECTORS)
  253. bdev_size = DM_CACHE_METADATA_MAX_SECTORS;
  254. r = dm_tm_pre_commit(cmd->tm);
  255. if (r < 0)
  256. return r;
  257. /*
  258. * dm_sm_copy_root() can fail. So we need to do it before we start
  259. * updating the superblock.
  260. */
  261. r = __save_sm_root(cmd);
  262. if (r)
  263. return r;
  264. r = superblock_lock_zero(cmd, &sblock);
  265. if (r)
  266. return r;
  267. disk_super = dm_block_data(sblock);
  268. disk_super->flags = 0;
  269. memset(disk_super->uuid, 0, sizeof(disk_super->uuid));
  270. disk_super->magic = cpu_to_le64(CACHE_SUPERBLOCK_MAGIC);
  271. disk_super->version = cpu_to_le32(MAX_CACHE_VERSION);
  272. memset(disk_super->policy_name, 0, sizeof(disk_super->policy_name));
  273. memset(disk_super->policy_version, 0, sizeof(disk_super->policy_version));
  274. disk_super->policy_hint_size = 0;
  275. __copy_sm_root(cmd, disk_super);
  276. disk_super->mapping_root = cpu_to_le64(cmd->root);
  277. disk_super->hint_root = cpu_to_le64(cmd->hint_root);
  278. disk_super->discard_root = cpu_to_le64(cmd->discard_root);
  279. disk_super->discard_block_size = cpu_to_le64(cmd->discard_block_size);
  280. disk_super->discard_nr_blocks = cpu_to_le64(from_dblock(cmd->discard_nr_blocks));
  281. disk_super->metadata_block_size = cpu_to_le32(DM_CACHE_METADATA_BLOCK_SIZE);
  282. disk_super->data_block_size = cpu_to_le32(cmd->data_block_size);
  283. disk_super->cache_blocks = cpu_to_le32(0);
  284. disk_super->read_hits = cpu_to_le32(0);
  285. disk_super->read_misses = cpu_to_le32(0);
  286. disk_super->write_hits = cpu_to_le32(0);
  287. disk_super->write_misses = cpu_to_le32(0);
  288. return dm_tm_commit(cmd->tm, sblock);
  289. }
  290. static int __format_metadata(struct dm_cache_metadata *cmd)
  291. {
  292. int r;
  293. r = dm_tm_create_with_sm(cmd->bm, CACHE_SUPERBLOCK_LOCATION,
  294. &cmd->tm, &cmd->metadata_sm);
  295. if (r < 0) {
  296. DMERR("tm_create_with_sm failed");
  297. return r;
  298. }
  299. __setup_mapping_info(cmd);
  300. r = dm_array_empty(&cmd->info, &cmd->root);
  301. if (r < 0)
  302. goto bad;
  303. dm_disk_bitset_init(cmd->tm, &cmd->discard_info);
  304. r = dm_bitset_empty(&cmd->discard_info, &cmd->discard_root);
  305. if (r < 0)
  306. goto bad;
  307. cmd->discard_block_size = 0;
  308. cmd->discard_nr_blocks = 0;
  309. r = __write_initial_superblock(cmd);
  310. if (r)
  311. goto bad;
  312. cmd->clean_when_opened = true;
  313. return 0;
  314. bad:
  315. dm_tm_destroy(cmd->tm);
  316. dm_sm_destroy(cmd->metadata_sm);
  317. return r;
  318. }
  319. static int __check_incompat_features(struct cache_disk_superblock *disk_super,
  320. struct dm_cache_metadata *cmd)
  321. {
  322. uint32_t features;
  323. features = le32_to_cpu(disk_super->incompat_flags) & ~DM_CACHE_FEATURE_INCOMPAT_SUPP;
  324. if (features) {
  325. DMERR("could not access metadata due to unsupported optional features (%lx).",
  326. (unsigned long)features);
  327. return -EINVAL;
  328. }
  329. /*
  330. * Check for read-only metadata to skip the following RDWR checks.
  331. */
  332. if (get_disk_ro(cmd->bdev->bd_disk))
  333. return 0;
  334. features = le32_to_cpu(disk_super->compat_ro_flags) & ~DM_CACHE_FEATURE_COMPAT_RO_SUPP;
  335. if (features) {
  336. DMERR("could not access metadata RDWR due to unsupported optional features (%lx).",
  337. (unsigned long)features);
  338. return -EINVAL;
  339. }
  340. return 0;
  341. }
  342. static int __open_metadata(struct dm_cache_metadata *cmd)
  343. {
  344. int r;
  345. struct dm_block *sblock;
  346. struct cache_disk_superblock *disk_super;
  347. unsigned long sb_flags;
  348. r = superblock_read_lock(cmd, &sblock);
  349. if (r < 0) {
  350. DMERR("couldn't read lock superblock");
  351. return r;
  352. }
  353. disk_super = dm_block_data(sblock);
  354. /* Verify the data block size hasn't changed */
  355. if (le32_to_cpu(disk_super->data_block_size) != cmd->data_block_size) {
  356. DMERR("changing the data block size (from %u to %llu) is not supported",
  357. le32_to_cpu(disk_super->data_block_size),
  358. (unsigned long long)cmd->data_block_size);
  359. r = -EINVAL;
  360. goto bad;
  361. }
  362. r = __check_incompat_features(disk_super, cmd);
  363. if (r < 0)
  364. goto bad;
  365. r = dm_tm_open_with_sm(cmd->bm, CACHE_SUPERBLOCK_LOCATION,
  366. disk_super->metadata_space_map_root,
  367. sizeof(disk_super->metadata_space_map_root),
  368. &cmd->tm, &cmd->metadata_sm);
  369. if (r < 0) {
  370. DMERR("tm_open_with_sm failed");
  371. goto bad;
  372. }
  373. __setup_mapping_info(cmd);
  374. dm_disk_bitset_init(cmd->tm, &cmd->discard_info);
  375. sb_flags = le32_to_cpu(disk_super->flags);
  376. cmd->clean_when_opened = test_bit(CLEAN_SHUTDOWN, &sb_flags);
  377. return dm_bm_unlock(sblock);
  378. bad:
  379. dm_bm_unlock(sblock);
  380. return r;
  381. }
  382. static int __open_or_format_metadata(struct dm_cache_metadata *cmd,
  383. bool format_device)
  384. {
  385. int r;
  386. bool unformatted = false;
  387. r = __superblock_all_zeroes(cmd->bm, &unformatted);
  388. if (r)
  389. return r;
  390. if (unformatted)
  391. return format_device ? __format_metadata(cmd) : -EPERM;
  392. return __open_metadata(cmd);
  393. }
  394. static int __create_persistent_data_objects(struct dm_cache_metadata *cmd,
  395. bool may_format_device)
  396. {
  397. int r;
  398. cmd->bm = dm_block_manager_create(cmd->bdev, DM_CACHE_METADATA_BLOCK_SIZE << SECTOR_SHIFT,
  399. CACHE_METADATA_CACHE_SIZE,
  400. CACHE_MAX_CONCURRENT_LOCKS);
  401. if (IS_ERR(cmd->bm)) {
  402. DMERR("could not create block manager");
  403. return PTR_ERR(cmd->bm);
  404. }
  405. r = __open_or_format_metadata(cmd, may_format_device);
  406. if (r)
  407. dm_block_manager_destroy(cmd->bm);
  408. return r;
  409. }
  410. static void __destroy_persistent_data_objects(struct dm_cache_metadata *cmd)
  411. {
  412. dm_sm_destroy(cmd->metadata_sm);
  413. dm_tm_destroy(cmd->tm);
  414. dm_block_manager_destroy(cmd->bm);
  415. }
  416. typedef unsigned long (*flags_mutator)(unsigned long);
  417. static void update_flags(struct cache_disk_superblock *disk_super,
  418. flags_mutator mutator)
  419. {
  420. uint32_t sb_flags = mutator(le32_to_cpu(disk_super->flags));
  421. disk_super->flags = cpu_to_le32(sb_flags);
  422. }
  423. static unsigned long set_clean_shutdown(unsigned long flags)
  424. {
  425. set_bit(CLEAN_SHUTDOWN, &flags);
  426. return flags;
  427. }
  428. static unsigned long clear_clean_shutdown(unsigned long flags)
  429. {
  430. clear_bit(CLEAN_SHUTDOWN, &flags);
  431. return flags;
  432. }
  433. static void read_superblock_fields(struct dm_cache_metadata *cmd,
  434. struct cache_disk_superblock *disk_super)
  435. {
  436. cmd->flags = le32_to_cpu(disk_super->flags);
  437. cmd->root = le64_to_cpu(disk_super->mapping_root);
  438. cmd->hint_root = le64_to_cpu(disk_super->hint_root);
  439. cmd->discard_root = le64_to_cpu(disk_super->discard_root);
  440. cmd->discard_block_size = le64_to_cpu(disk_super->discard_block_size);
  441. cmd->discard_nr_blocks = to_dblock(le64_to_cpu(disk_super->discard_nr_blocks));
  442. cmd->data_block_size = le32_to_cpu(disk_super->data_block_size);
  443. cmd->cache_blocks = to_cblock(le32_to_cpu(disk_super->cache_blocks));
  444. strncpy(cmd->policy_name, disk_super->policy_name, sizeof(cmd->policy_name));
  445. cmd->policy_version[0] = le32_to_cpu(disk_super->policy_version[0]);
  446. cmd->policy_version[1] = le32_to_cpu(disk_super->policy_version[1]);
  447. cmd->policy_version[2] = le32_to_cpu(disk_super->policy_version[2]);
  448. cmd->policy_hint_size = le32_to_cpu(disk_super->policy_hint_size);
  449. cmd->stats.read_hits = le32_to_cpu(disk_super->read_hits);
  450. cmd->stats.read_misses = le32_to_cpu(disk_super->read_misses);
  451. cmd->stats.write_hits = le32_to_cpu(disk_super->write_hits);
  452. cmd->stats.write_misses = le32_to_cpu(disk_super->write_misses);
  453. cmd->changed = false;
  454. }
  455. /*
  456. * The mutator updates the superblock flags.
  457. */
  458. static int __begin_transaction_flags(struct dm_cache_metadata *cmd,
  459. flags_mutator mutator)
  460. {
  461. int r;
  462. struct cache_disk_superblock *disk_super;
  463. struct dm_block *sblock;
  464. r = superblock_lock(cmd, &sblock);
  465. if (r)
  466. return r;
  467. disk_super = dm_block_data(sblock);
  468. update_flags(disk_super, mutator);
  469. read_superblock_fields(cmd, disk_super);
  470. dm_bm_unlock(sblock);
  471. return dm_bm_flush(cmd->bm);
  472. }
  473. static int __begin_transaction(struct dm_cache_metadata *cmd)
  474. {
  475. int r;
  476. struct cache_disk_superblock *disk_super;
  477. struct dm_block *sblock;
  478. /*
  479. * We re-read the superblock every time. Shouldn't need to do this
  480. * really.
  481. */
  482. r = superblock_read_lock(cmd, &sblock);
  483. if (r)
  484. return r;
  485. disk_super = dm_block_data(sblock);
  486. read_superblock_fields(cmd, disk_super);
  487. dm_bm_unlock(sblock);
  488. return 0;
  489. }
  490. static int __commit_transaction(struct dm_cache_metadata *cmd,
  491. flags_mutator mutator)
  492. {
  493. int r;
  494. struct cache_disk_superblock *disk_super;
  495. struct dm_block *sblock;
  496. /*
  497. * We need to know if the cache_disk_superblock exceeds a 512-byte sector.
  498. */
  499. BUILD_BUG_ON(sizeof(struct cache_disk_superblock) > 512);
  500. r = dm_bitset_flush(&cmd->discard_info, cmd->discard_root,
  501. &cmd->discard_root);
  502. if (r)
  503. return r;
  504. r = dm_tm_pre_commit(cmd->tm);
  505. if (r < 0)
  506. return r;
  507. r = __save_sm_root(cmd);
  508. if (r)
  509. return r;
  510. r = superblock_lock(cmd, &sblock);
  511. if (r)
  512. return r;
  513. disk_super = dm_block_data(sblock);
  514. if (mutator)
  515. update_flags(disk_super, mutator);
  516. disk_super->flags = cpu_to_le32(cmd->flags);
  517. disk_super->mapping_root = cpu_to_le64(cmd->root);
  518. disk_super->hint_root = cpu_to_le64(cmd->hint_root);
  519. disk_super->discard_root = cpu_to_le64(cmd->discard_root);
  520. disk_super->discard_block_size = cpu_to_le64(cmd->discard_block_size);
  521. disk_super->discard_nr_blocks = cpu_to_le64(from_dblock(cmd->discard_nr_blocks));
  522. disk_super->cache_blocks = cpu_to_le32(from_cblock(cmd->cache_blocks));
  523. strncpy(disk_super->policy_name, cmd->policy_name, sizeof(disk_super->policy_name));
  524. disk_super->policy_version[0] = cpu_to_le32(cmd->policy_version[0]);
  525. disk_super->policy_version[1] = cpu_to_le32(cmd->policy_version[1]);
  526. disk_super->policy_version[2] = cpu_to_le32(cmd->policy_version[2]);
  527. disk_super->read_hits = cpu_to_le32(cmd->stats.read_hits);
  528. disk_super->read_misses = cpu_to_le32(cmd->stats.read_misses);
  529. disk_super->write_hits = cpu_to_le32(cmd->stats.write_hits);
  530. disk_super->write_misses = cpu_to_le32(cmd->stats.write_misses);
  531. __copy_sm_root(cmd, disk_super);
  532. return dm_tm_commit(cmd->tm, sblock);
  533. }
  534. /*----------------------------------------------------------------*/
  535. /*
  536. * The mappings are held in a dm-array that has 64-bit values stored in
  537. * little-endian format. The index is the cblock, the high 48bits of the
  538. * value are the oblock and the low 16 bit the flags.
  539. */
  540. #define FLAGS_MASK ((1 << 16) - 1)
  541. static __le64 pack_value(dm_oblock_t block, unsigned flags)
  542. {
  543. uint64_t value = from_oblock(block);
  544. value <<= 16;
  545. value = value | (flags & FLAGS_MASK);
  546. return cpu_to_le64(value);
  547. }
  548. static void unpack_value(__le64 value_le, dm_oblock_t *block, unsigned *flags)
  549. {
  550. uint64_t value = le64_to_cpu(value_le);
  551. uint64_t b = value >> 16;
  552. *block = to_oblock(b);
  553. *flags = value & FLAGS_MASK;
  554. }
  555. /*----------------------------------------------------------------*/
  556. static struct dm_cache_metadata *metadata_open(struct block_device *bdev,
  557. sector_t data_block_size,
  558. bool may_format_device,
  559. size_t policy_hint_size)
  560. {
  561. int r;
  562. struct dm_cache_metadata *cmd;
  563. cmd = kzalloc(sizeof(*cmd), GFP_KERNEL);
  564. if (!cmd) {
  565. DMERR("could not allocate metadata struct");
  566. return ERR_PTR(-ENOMEM);
  567. }
  568. atomic_set(&cmd->ref_count, 1);
  569. init_rwsem(&cmd->root_lock);
  570. cmd->bdev = bdev;
  571. cmd->data_block_size = data_block_size;
  572. cmd->cache_blocks = 0;
  573. cmd->policy_hint_size = policy_hint_size;
  574. cmd->changed = true;
  575. cmd->fail_io = false;
  576. r = __create_persistent_data_objects(cmd, may_format_device);
  577. if (r) {
  578. kfree(cmd);
  579. return ERR_PTR(r);
  580. }
  581. r = __begin_transaction_flags(cmd, clear_clean_shutdown);
  582. if (r < 0) {
  583. dm_cache_metadata_close(cmd);
  584. return ERR_PTR(r);
  585. }
  586. return cmd;
  587. }
  588. /*
  589. * We keep a little list of ref counted metadata objects to prevent two
  590. * different target instances creating separate bufio instances. This is
  591. * an issue if a table is reloaded before the suspend.
  592. */
  593. static DEFINE_MUTEX(table_lock);
  594. static LIST_HEAD(table);
  595. static struct dm_cache_metadata *lookup(struct block_device *bdev)
  596. {
  597. struct dm_cache_metadata *cmd;
  598. list_for_each_entry(cmd, &table, list)
  599. if (cmd->bdev == bdev) {
  600. atomic_inc(&cmd->ref_count);
  601. return cmd;
  602. }
  603. return NULL;
  604. }
  605. static struct dm_cache_metadata *lookup_or_open(struct block_device *bdev,
  606. sector_t data_block_size,
  607. bool may_format_device,
  608. size_t policy_hint_size)
  609. {
  610. struct dm_cache_metadata *cmd, *cmd2;
  611. mutex_lock(&table_lock);
  612. cmd = lookup(bdev);
  613. mutex_unlock(&table_lock);
  614. if (cmd)
  615. return cmd;
  616. cmd = metadata_open(bdev, data_block_size, may_format_device, policy_hint_size);
  617. if (!IS_ERR(cmd)) {
  618. mutex_lock(&table_lock);
  619. cmd2 = lookup(bdev);
  620. if (cmd2) {
  621. mutex_unlock(&table_lock);
  622. __destroy_persistent_data_objects(cmd);
  623. kfree(cmd);
  624. return cmd2;
  625. }
  626. list_add(&cmd->list, &table);
  627. mutex_unlock(&table_lock);
  628. }
  629. return cmd;
  630. }
  631. static bool same_params(struct dm_cache_metadata *cmd, sector_t data_block_size)
  632. {
  633. if (cmd->data_block_size != data_block_size) {
  634. DMERR("data_block_size (%llu) different from that in metadata (%llu)\n",
  635. (unsigned long long) data_block_size,
  636. (unsigned long long) cmd->data_block_size);
  637. return false;
  638. }
  639. return true;
  640. }
  641. struct dm_cache_metadata *dm_cache_metadata_open(struct block_device *bdev,
  642. sector_t data_block_size,
  643. bool may_format_device,
  644. size_t policy_hint_size)
  645. {
  646. struct dm_cache_metadata *cmd = lookup_or_open(bdev, data_block_size,
  647. may_format_device, policy_hint_size);
  648. if (!IS_ERR(cmd) && !same_params(cmd, data_block_size)) {
  649. dm_cache_metadata_close(cmd);
  650. return ERR_PTR(-EINVAL);
  651. }
  652. return cmd;
  653. }
  654. void dm_cache_metadata_close(struct dm_cache_metadata *cmd)
  655. {
  656. if (atomic_dec_and_test(&cmd->ref_count)) {
  657. mutex_lock(&table_lock);
  658. list_del(&cmd->list);
  659. mutex_unlock(&table_lock);
  660. if (!cmd->fail_io)
  661. __destroy_persistent_data_objects(cmd);
  662. kfree(cmd);
  663. }
  664. }
  665. /*
  666. * Checks that the given cache block is either unmapped or clean.
  667. */
  668. static int block_unmapped_or_clean(struct dm_cache_metadata *cmd, dm_cblock_t b,
  669. bool *result)
  670. {
  671. int r;
  672. __le64 value;
  673. dm_oblock_t ob;
  674. unsigned flags;
  675. r = dm_array_get_value(&cmd->info, cmd->root, from_cblock(b), &value);
  676. if (r) {
  677. DMERR("block_unmapped_or_clean failed");
  678. return r;
  679. }
  680. unpack_value(value, &ob, &flags);
  681. *result = !((flags & M_VALID) && (flags & M_DIRTY));
  682. return 0;
  683. }
  684. static int blocks_are_unmapped_or_clean(struct dm_cache_metadata *cmd,
  685. dm_cblock_t begin, dm_cblock_t end,
  686. bool *result)
  687. {
  688. int r;
  689. *result = true;
  690. while (begin != end) {
  691. r = block_unmapped_or_clean(cmd, begin, result);
  692. if (r)
  693. return r;
  694. if (!*result) {
  695. DMERR("cache block %llu is dirty",
  696. (unsigned long long) from_cblock(begin));
  697. return 0;
  698. }
  699. begin = to_cblock(from_cblock(begin) + 1);
  700. }
  701. return 0;
  702. }
  703. #define WRITE_LOCK(cmd) \
  704. if (cmd->fail_io || dm_bm_is_read_only(cmd->bm)) \
  705. return -EINVAL; \
  706. down_write(&cmd->root_lock)
  707. #define WRITE_LOCK_VOID(cmd) \
  708. if (cmd->fail_io || dm_bm_is_read_only(cmd->bm)) \
  709. return; \
  710. down_write(&cmd->root_lock)
  711. #define WRITE_UNLOCK(cmd) \
  712. up_write(&cmd->root_lock)
  713. int dm_cache_resize(struct dm_cache_metadata *cmd, dm_cblock_t new_cache_size)
  714. {
  715. int r;
  716. bool clean;
  717. __le64 null_mapping = pack_value(0, 0);
  718. WRITE_LOCK(cmd);
  719. __dm_bless_for_disk(&null_mapping);
  720. if (from_cblock(new_cache_size) < from_cblock(cmd->cache_blocks)) {
  721. r = blocks_are_unmapped_or_clean(cmd, new_cache_size, cmd->cache_blocks, &clean);
  722. if (r) {
  723. __dm_unbless_for_disk(&null_mapping);
  724. goto out;
  725. }
  726. if (!clean) {
  727. DMERR("unable to shrink cache due to dirty blocks");
  728. r = -EINVAL;
  729. __dm_unbless_for_disk(&null_mapping);
  730. goto out;
  731. }
  732. }
  733. r = dm_array_resize(&cmd->info, cmd->root, from_cblock(cmd->cache_blocks),
  734. from_cblock(new_cache_size),
  735. &null_mapping, &cmd->root);
  736. if (!r)
  737. cmd->cache_blocks = new_cache_size;
  738. cmd->changed = true;
  739. out:
  740. WRITE_UNLOCK(cmd);
  741. return r;
  742. }
  743. int dm_cache_discard_bitset_resize(struct dm_cache_metadata *cmd,
  744. sector_t discard_block_size,
  745. dm_dblock_t new_nr_entries)
  746. {
  747. int r;
  748. WRITE_LOCK(cmd);
  749. r = dm_bitset_resize(&cmd->discard_info,
  750. cmd->discard_root,
  751. from_dblock(cmd->discard_nr_blocks),
  752. from_dblock(new_nr_entries),
  753. false, &cmd->discard_root);
  754. if (!r) {
  755. cmd->discard_block_size = discard_block_size;
  756. cmd->discard_nr_blocks = new_nr_entries;
  757. }
  758. cmd->changed = true;
  759. WRITE_UNLOCK(cmd);
  760. return r;
  761. }
  762. static int __set_discard(struct dm_cache_metadata *cmd, dm_dblock_t b)
  763. {
  764. return dm_bitset_set_bit(&cmd->discard_info, cmd->discard_root,
  765. from_dblock(b), &cmd->discard_root);
  766. }
  767. static int __clear_discard(struct dm_cache_metadata *cmd, dm_dblock_t b)
  768. {
  769. return dm_bitset_clear_bit(&cmd->discard_info, cmd->discard_root,
  770. from_dblock(b), &cmd->discard_root);
  771. }
  772. static int __is_discarded(struct dm_cache_metadata *cmd, dm_dblock_t b,
  773. bool *is_discarded)
  774. {
  775. return dm_bitset_test_bit(&cmd->discard_info, cmd->discard_root,
  776. from_dblock(b), &cmd->discard_root,
  777. is_discarded);
  778. }
  779. static int __discard(struct dm_cache_metadata *cmd,
  780. dm_dblock_t dblock, bool discard)
  781. {
  782. int r;
  783. r = (discard ? __set_discard : __clear_discard)(cmd, dblock);
  784. if (r)
  785. return r;
  786. cmd->changed = true;
  787. return 0;
  788. }
  789. int dm_cache_set_discard(struct dm_cache_metadata *cmd,
  790. dm_dblock_t dblock, bool discard)
  791. {
  792. int r;
  793. WRITE_LOCK(cmd);
  794. r = __discard(cmd, dblock, discard);
  795. WRITE_UNLOCK(cmd);
  796. return r;
  797. }
  798. static int __load_discards(struct dm_cache_metadata *cmd,
  799. load_discard_fn fn, void *context)
  800. {
  801. int r = 0;
  802. dm_block_t b;
  803. bool discard;
  804. for (b = 0; b < from_dblock(cmd->discard_nr_blocks); b++) {
  805. dm_dblock_t dblock = to_dblock(b);
  806. if (cmd->clean_when_opened) {
  807. r = __is_discarded(cmd, dblock, &discard);
  808. if (r)
  809. return r;
  810. } else
  811. discard = false;
  812. r = fn(context, cmd->discard_block_size, dblock, discard);
  813. if (r)
  814. break;
  815. }
  816. return r;
  817. }
  818. int dm_cache_load_discards(struct dm_cache_metadata *cmd,
  819. load_discard_fn fn, void *context)
  820. {
  821. int r;
  822. down_read(&cmd->root_lock);
  823. r = __load_discards(cmd, fn, context);
  824. up_read(&cmd->root_lock);
  825. return r;
  826. }
  827. dm_cblock_t dm_cache_size(struct dm_cache_metadata *cmd)
  828. {
  829. dm_cblock_t r;
  830. down_read(&cmd->root_lock);
  831. r = cmd->cache_blocks;
  832. up_read(&cmd->root_lock);
  833. return r;
  834. }
  835. static int __remove(struct dm_cache_metadata *cmd, dm_cblock_t cblock)
  836. {
  837. int r;
  838. __le64 value = pack_value(0, 0);
  839. __dm_bless_for_disk(&value);
  840. r = dm_array_set_value(&cmd->info, cmd->root, from_cblock(cblock),
  841. &value, &cmd->root);
  842. if (r)
  843. return r;
  844. cmd->changed = true;
  845. return 0;
  846. }
  847. int dm_cache_remove_mapping(struct dm_cache_metadata *cmd, dm_cblock_t cblock)
  848. {
  849. int r;
  850. WRITE_LOCK(cmd);
  851. r = __remove(cmd, cblock);
  852. WRITE_UNLOCK(cmd);
  853. return r;
  854. }
  855. static int __insert(struct dm_cache_metadata *cmd,
  856. dm_cblock_t cblock, dm_oblock_t oblock)
  857. {
  858. int r;
  859. __le64 value = pack_value(oblock, M_VALID);
  860. __dm_bless_for_disk(&value);
  861. r = dm_array_set_value(&cmd->info, cmd->root, from_cblock(cblock),
  862. &value, &cmd->root);
  863. if (r)
  864. return r;
  865. cmd->changed = true;
  866. return 0;
  867. }
  868. int dm_cache_insert_mapping(struct dm_cache_metadata *cmd,
  869. dm_cblock_t cblock, dm_oblock_t oblock)
  870. {
  871. int r;
  872. WRITE_LOCK(cmd);
  873. r = __insert(cmd, cblock, oblock);
  874. WRITE_UNLOCK(cmd);
  875. return r;
  876. }
  877. struct thunk {
  878. load_mapping_fn fn;
  879. void *context;
  880. struct dm_cache_metadata *cmd;
  881. bool respect_dirty_flags;
  882. bool hints_valid;
  883. };
  884. static bool policy_unchanged(struct dm_cache_metadata *cmd,
  885. struct dm_cache_policy *policy)
  886. {
  887. const char *policy_name = dm_cache_policy_get_name(policy);
  888. const unsigned *policy_version = dm_cache_policy_get_version(policy);
  889. size_t policy_hint_size = dm_cache_policy_get_hint_size(policy);
  890. /*
  891. * Ensure policy names match.
  892. */
  893. if (strncmp(cmd->policy_name, policy_name, sizeof(cmd->policy_name)))
  894. return false;
  895. /*
  896. * Ensure policy major versions match.
  897. */
  898. if (cmd->policy_version[0] != policy_version[0])
  899. return false;
  900. /*
  901. * Ensure policy hint sizes match.
  902. */
  903. if (cmd->policy_hint_size != policy_hint_size)
  904. return false;
  905. return true;
  906. }
  907. static bool hints_array_initialized(struct dm_cache_metadata *cmd)
  908. {
  909. return cmd->hint_root && cmd->policy_hint_size;
  910. }
  911. static bool hints_array_available(struct dm_cache_metadata *cmd,
  912. struct dm_cache_policy *policy)
  913. {
  914. return cmd->clean_when_opened && policy_unchanged(cmd, policy) &&
  915. hints_array_initialized(cmd);
  916. }
  917. static int __load_mapping(void *context, uint64_t cblock, void *leaf)
  918. {
  919. int r = 0;
  920. bool dirty;
  921. __le64 value;
  922. __le32 hint_value = 0;
  923. dm_oblock_t oblock;
  924. unsigned flags;
  925. struct thunk *thunk = context;
  926. struct dm_cache_metadata *cmd = thunk->cmd;
  927. memcpy(&value, leaf, sizeof(value));
  928. unpack_value(value, &oblock, &flags);
  929. if (flags & M_VALID) {
  930. if (thunk->hints_valid) {
  931. r = dm_array_get_value(&cmd->hint_info, cmd->hint_root,
  932. cblock, &hint_value);
  933. if (r && r != -ENODATA)
  934. return r;
  935. }
  936. dirty = thunk->respect_dirty_flags ? (flags & M_DIRTY) : true;
  937. r = thunk->fn(thunk->context, oblock, to_cblock(cblock),
  938. dirty, le32_to_cpu(hint_value), thunk->hints_valid);
  939. }
  940. return r;
  941. }
  942. static int __load_mappings(struct dm_cache_metadata *cmd,
  943. struct dm_cache_policy *policy,
  944. load_mapping_fn fn, void *context)
  945. {
  946. struct thunk thunk;
  947. thunk.fn = fn;
  948. thunk.context = context;
  949. thunk.cmd = cmd;
  950. thunk.respect_dirty_flags = cmd->clean_when_opened;
  951. thunk.hints_valid = hints_array_available(cmd, policy);
  952. return dm_array_walk(&cmd->info, cmd->root, __load_mapping, &thunk);
  953. }
  954. int dm_cache_load_mappings(struct dm_cache_metadata *cmd,
  955. struct dm_cache_policy *policy,
  956. load_mapping_fn fn, void *context)
  957. {
  958. int r;
  959. down_read(&cmd->root_lock);
  960. r = __load_mappings(cmd, policy, fn, context);
  961. up_read(&cmd->root_lock);
  962. return r;
  963. }
  964. static int __dump_mapping(void *context, uint64_t cblock, void *leaf)
  965. {
  966. int r = 0;
  967. __le64 value;
  968. dm_oblock_t oblock;
  969. unsigned flags;
  970. memcpy(&value, leaf, sizeof(value));
  971. unpack_value(value, &oblock, &flags);
  972. return r;
  973. }
  974. static int __dump_mappings(struct dm_cache_metadata *cmd)
  975. {
  976. return dm_array_walk(&cmd->info, cmd->root, __dump_mapping, NULL);
  977. }
  978. void dm_cache_dump(struct dm_cache_metadata *cmd)
  979. {
  980. down_read(&cmd->root_lock);
  981. __dump_mappings(cmd);
  982. up_read(&cmd->root_lock);
  983. }
  984. int dm_cache_changed_this_transaction(struct dm_cache_metadata *cmd)
  985. {
  986. int r;
  987. down_read(&cmd->root_lock);
  988. r = cmd->changed;
  989. up_read(&cmd->root_lock);
  990. return r;
  991. }
  992. static int __dirty(struct dm_cache_metadata *cmd, dm_cblock_t cblock, bool dirty)
  993. {
  994. int r;
  995. unsigned flags;
  996. dm_oblock_t oblock;
  997. __le64 value;
  998. r = dm_array_get_value(&cmd->info, cmd->root, from_cblock(cblock), &value);
  999. if (r)
  1000. return r;
  1001. unpack_value(value, &oblock, &flags);
  1002. if (((flags & M_DIRTY) && dirty) || (!(flags & M_DIRTY) && !dirty))
  1003. /* nothing to be done */
  1004. return 0;
  1005. value = pack_value(oblock, (flags & ~M_DIRTY) | (dirty ? M_DIRTY : 0));
  1006. __dm_bless_for_disk(&value);
  1007. r = dm_array_set_value(&cmd->info, cmd->root, from_cblock(cblock),
  1008. &value, &cmd->root);
  1009. if (r)
  1010. return r;
  1011. cmd->changed = true;
  1012. return 0;
  1013. }
  1014. int dm_cache_set_dirty(struct dm_cache_metadata *cmd,
  1015. dm_cblock_t cblock, bool dirty)
  1016. {
  1017. int r;
  1018. WRITE_LOCK(cmd);
  1019. r = __dirty(cmd, cblock, dirty);
  1020. WRITE_UNLOCK(cmd);
  1021. return r;
  1022. }
  1023. void dm_cache_metadata_get_stats(struct dm_cache_metadata *cmd,
  1024. struct dm_cache_statistics *stats)
  1025. {
  1026. down_read(&cmd->root_lock);
  1027. *stats = cmd->stats;
  1028. up_read(&cmd->root_lock);
  1029. }
  1030. void dm_cache_metadata_set_stats(struct dm_cache_metadata *cmd,
  1031. struct dm_cache_statistics *stats)
  1032. {
  1033. WRITE_LOCK_VOID(cmd);
  1034. cmd->stats = *stats;
  1035. WRITE_UNLOCK(cmd);
  1036. }
  1037. int dm_cache_commit(struct dm_cache_metadata *cmd, bool clean_shutdown)
  1038. {
  1039. int r;
  1040. flags_mutator mutator = (clean_shutdown ? set_clean_shutdown :
  1041. clear_clean_shutdown);
  1042. WRITE_LOCK(cmd);
  1043. r = __commit_transaction(cmd, mutator);
  1044. if (r)
  1045. goto out;
  1046. r = __begin_transaction(cmd);
  1047. out:
  1048. WRITE_UNLOCK(cmd);
  1049. return r;
  1050. }
  1051. int dm_cache_get_free_metadata_block_count(struct dm_cache_metadata *cmd,
  1052. dm_block_t *result)
  1053. {
  1054. int r = -EINVAL;
  1055. down_read(&cmd->root_lock);
  1056. r = dm_sm_get_nr_free(cmd->metadata_sm, result);
  1057. up_read(&cmd->root_lock);
  1058. return r;
  1059. }
  1060. int dm_cache_get_metadata_dev_size(struct dm_cache_metadata *cmd,
  1061. dm_block_t *result)
  1062. {
  1063. int r = -EINVAL;
  1064. down_read(&cmd->root_lock);
  1065. r = dm_sm_get_nr_blocks(cmd->metadata_sm, result);
  1066. up_read(&cmd->root_lock);
  1067. return r;
  1068. }
  1069. /*----------------------------------------------------------------*/
  1070. static int begin_hints(struct dm_cache_metadata *cmd, struct dm_cache_policy *policy)
  1071. {
  1072. int r;
  1073. __le32 value;
  1074. size_t hint_size;
  1075. const char *policy_name = dm_cache_policy_get_name(policy);
  1076. const unsigned *policy_version = dm_cache_policy_get_version(policy);
  1077. if (!policy_name[0] ||
  1078. (strlen(policy_name) > sizeof(cmd->policy_name) - 1))
  1079. return -EINVAL;
  1080. if (!policy_unchanged(cmd, policy)) {
  1081. strncpy(cmd->policy_name, policy_name, sizeof(cmd->policy_name));
  1082. memcpy(cmd->policy_version, policy_version, sizeof(cmd->policy_version));
  1083. hint_size = dm_cache_policy_get_hint_size(policy);
  1084. if (!hint_size)
  1085. return 0; /* short-circuit hints initialization */
  1086. cmd->policy_hint_size = hint_size;
  1087. if (cmd->hint_root) {
  1088. r = dm_array_del(&cmd->hint_info, cmd->hint_root);
  1089. if (r)
  1090. return r;
  1091. }
  1092. r = dm_array_empty(&cmd->hint_info, &cmd->hint_root);
  1093. if (r)
  1094. return r;
  1095. value = cpu_to_le32(0);
  1096. __dm_bless_for_disk(&value);
  1097. r = dm_array_resize(&cmd->hint_info, cmd->hint_root, 0,
  1098. from_cblock(cmd->cache_blocks),
  1099. &value, &cmd->hint_root);
  1100. if (r)
  1101. return r;
  1102. }
  1103. return 0;
  1104. }
  1105. static int save_hint(void *context, dm_cblock_t cblock, dm_oblock_t oblock, uint32_t hint)
  1106. {
  1107. struct dm_cache_metadata *cmd = context;
  1108. __le32 value = cpu_to_le32(hint);
  1109. int r;
  1110. __dm_bless_for_disk(&value);
  1111. r = dm_array_set_value(&cmd->hint_info, cmd->hint_root,
  1112. from_cblock(cblock), &value, &cmd->hint_root);
  1113. cmd->changed = true;
  1114. return r;
  1115. }
  1116. static int write_hints(struct dm_cache_metadata *cmd, struct dm_cache_policy *policy)
  1117. {
  1118. int r;
  1119. r = begin_hints(cmd, policy);
  1120. if (r) {
  1121. DMERR("begin_hints failed");
  1122. return r;
  1123. }
  1124. return policy_walk_mappings(policy, save_hint, cmd);
  1125. }
  1126. int dm_cache_write_hints(struct dm_cache_metadata *cmd, struct dm_cache_policy *policy)
  1127. {
  1128. int r;
  1129. WRITE_LOCK(cmd);
  1130. r = write_hints(cmd, policy);
  1131. WRITE_UNLOCK(cmd);
  1132. return r;
  1133. }
  1134. int dm_cache_metadata_all_clean(struct dm_cache_metadata *cmd, bool *result)
  1135. {
  1136. return blocks_are_unmapped_or_clean(cmd, 0, cmd->cache_blocks, result);
  1137. }
  1138. void dm_cache_metadata_set_read_only(struct dm_cache_metadata *cmd)
  1139. {
  1140. WRITE_LOCK_VOID(cmd);
  1141. dm_bm_set_read_only(cmd->bm);
  1142. WRITE_UNLOCK(cmd);
  1143. }
  1144. void dm_cache_metadata_set_read_write(struct dm_cache_metadata *cmd)
  1145. {
  1146. WRITE_LOCK_VOID(cmd);
  1147. dm_bm_set_read_write(cmd->bm);
  1148. WRITE_UNLOCK(cmd);
  1149. }
  1150. int dm_cache_metadata_set_needs_check(struct dm_cache_metadata *cmd)
  1151. {
  1152. int r;
  1153. struct dm_block *sblock;
  1154. struct cache_disk_superblock *disk_super;
  1155. /*
  1156. * We ignore fail_io for this function.
  1157. */
  1158. down_write(&cmd->root_lock);
  1159. set_bit(NEEDS_CHECK, &cmd->flags);
  1160. r = superblock_lock(cmd, &sblock);
  1161. if (r) {
  1162. DMERR("couldn't read superblock");
  1163. goto out;
  1164. }
  1165. disk_super = dm_block_data(sblock);
  1166. disk_super->flags = cpu_to_le32(cmd->flags);
  1167. dm_bm_unlock(sblock);
  1168. out:
  1169. up_write(&cmd->root_lock);
  1170. return r;
  1171. }
  1172. bool dm_cache_metadata_needs_check(struct dm_cache_metadata *cmd)
  1173. {
  1174. bool needs_check;
  1175. down_read(&cmd->root_lock);
  1176. needs_check = !!test_bit(NEEDS_CHECK, &cmd->flags);
  1177. up_read(&cmd->root_lock);
  1178. return needs_check;
  1179. }
  1180. int dm_cache_metadata_abort(struct dm_cache_metadata *cmd)
  1181. {
  1182. int r;
  1183. WRITE_LOCK(cmd);
  1184. __destroy_persistent_data_objects(cmd);
  1185. r = __create_persistent_data_objects(cmd, false);
  1186. if (r)
  1187. cmd->fail_io = true;
  1188. WRITE_UNLOCK(cmd);
  1189. return r;
  1190. }