dm-cache-metadata.c 36 KB

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