dm-cache-metadata.c 42 KB

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