dm-era-target.c 38 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731
  1. #include "dm.h"
  2. #include "persistent-data/dm-transaction-manager.h"
  3. #include "persistent-data/dm-bitset.h"
  4. #include "persistent-data/dm-space-map.h"
  5. #include <linux/dm-io.h>
  6. #include <linux/dm-kcopyd.h>
  7. #include <linux/init.h>
  8. #include <linux/mempool.h>
  9. #include <linux/module.h>
  10. #include <linux/slab.h>
  11. #include <linux/vmalloc.h>
  12. #define DM_MSG_PREFIX "era"
  13. #define SUPERBLOCK_LOCATION 0
  14. #define SUPERBLOCK_MAGIC 2126579579
  15. #define SUPERBLOCK_CSUM_XOR 146538381
  16. #define MIN_ERA_VERSION 1
  17. #define MAX_ERA_VERSION 1
  18. #define INVALID_WRITESET_ROOT SUPERBLOCK_LOCATION
  19. #define MIN_BLOCK_SIZE 8
  20. /*----------------------------------------------------------------
  21. * Writeset
  22. *--------------------------------------------------------------*/
  23. struct writeset_metadata {
  24. uint32_t nr_bits;
  25. dm_block_t root;
  26. };
  27. struct writeset {
  28. struct writeset_metadata md;
  29. /*
  30. * An in core copy of the bits to save constantly doing look ups on
  31. * disk.
  32. */
  33. unsigned long *bits;
  34. };
  35. /*
  36. * This does not free off the on disk bitset as this will normally be done
  37. * after digesting into the era array.
  38. */
  39. static void writeset_free(struct writeset *ws)
  40. {
  41. vfree(ws->bits);
  42. }
  43. static int setup_on_disk_bitset(struct dm_disk_bitset *info,
  44. unsigned nr_bits, dm_block_t *root)
  45. {
  46. int r;
  47. r = dm_bitset_empty(info, root);
  48. if (r)
  49. return r;
  50. return dm_bitset_resize(info, *root, 0, nr_bits, false, root);
  51. }
  52. static size_t bitset_size(unsigned nr_bits)
  53. {
  54. return sizeof(unsigned long) * dm_div_up(nr_bits, BITS_PER_LONG);
  55. }
  56. /*
  57. * Allocates memory for the in core bitset.
  58. */
  59. static int writeset_alloc(struct writeset *ws, dm_block_t nr_blocks)
  60. {
  61. ws->md.nr_bits = nr_blocks;
  62. ws->md.root = INVALID_WRITESET_ROOT;
  63. ws->bits = vzalloc(bitset_size(nr_blocks));
  64. if (!ws->bits) {
  65. DMERR("%s: couldn't allocate in memory bitset", __func__);
  66. return -ENOMEM;
  67. }
  68. return 0;
  69. }
  70. /*
  71. * Wipes the in-core bitset, and creates a new on disk bitset.
  72. */
  73. static int writeset_init(struct dm_disk_bitset *info, struct writeset *ws)
  74. {
  75. int r;
  76. memset(ws->bits, 0, bitset_size(ws->md.nr_bits));
  77. r = setup_on_disk_bitset(info, ws->md.nr_bits, &ws->md.root);
  78. if (r) {
  79. DMERR("%s: setup_on_disk_bitset failed", __func__);
  80. return r;
  81. }
  82. return 0;
  83. }
  84. static bool writeset_marked(struct writeset *ws, dm_block_t block)
  85. {
  86. return test_bit(block, ws->bits);
  87. }
  88. static int writeset_marked_on_disk(struct dm_disk_bitset *info,
  89. struct writeset_metadata *m, dm_block_t block,
  90. bool *result)
  91. {
  92. dm_block_t old = m->root;
  93. /*
  94. * The bitset was flushed when it was archived, so we know there'll
  95. * be no change to the root.
  96. */
  97. int r = dm_bitset_test_bit(info, m->root, block, &m->root, result);
  98. if (r) {
  99. DMERR("%s: dm_bitset_test_bit failed", __func__);
  100. return r;
  101. }
  102. BUG_ON(m->root != old);
  103. return r;
  104. }
  105. /*
  106. * Returns < 0 on error, 0 if the bit wasn't previously set, 1 if it was.
  107. */
  108. static int writeset_test_and_set(struct dm_disk_bitset *info,
  109. struct writeset *ws, uint32_t block)
  110. {
  111. int r;
  112. if (!test_and_set_bit(block, ws->bits)) {
  113. r = dm_bitset_set_bit(info, ws->md.root, block, &ws->md.root);
  114. if (r) {
  115. /* FIXME: fail mode */
  116. return r;
  117. }
  118. return 0;
  119. }
  120. return 1;
  121. }
  122. /*----------------------------------------------------------------
  123. * On disk metadata layout
  124. *--------------------------------------------------------------*/
  125. #define SPACE_MAP_ROOT_SIZE 128
  126. #define UUID_LEN 16
  127. struct writeset_disk {
  128. __le32 nr_bits;
  129. __le64 root;
  130. } __packed;
  131. struct superblock_disk {
  132. __le32 csum;
  133. __le32 flags;
  134. __le64 blocknr;
  135. __u8 uuid[UUID_LEN];
  136. __le64 magic;
  137. __le32 version;
  138. __u8 metadata_space_map_root[SPACE_MAP_ROOT_SIZE];
  139. __le32 data_block_size;
  140. __le32 metadata_block_size;
  141. __le32 nr_blocks;
  142. __le32 current_era;
  143. struct writeset_disk current_writeset;
  144. /*
  145. * Only these two fields are valid within the metadata snapshot.
  146. */
  147. __le64 writeset_tree_root;
  148. __le64 era_array_root;
  149. __le64 metadata_snap;
  150. } __packed;
  151. /*----------------------------------------------------------------
  152. * Superblock validation
  153. *--------------------------------------------------------------*/
  154. static void sb_prepare_for_write(struct dm_block_validator *v,
  155. struct dm_block *b,
  156. size_t sb_block_size)
  157. {
  158. struct superblock_disk *disk = dm_block_data(b);
  159. disk->blocknr = cpu_to_le64(dm_block_location(b));
  160. disk->csum = cpu_to_le32(dm_bm_checksum(&disk->flags,
  161. sb_block_size - sizeof(__le32),
  162. SUPERBLOCK_CSUM_XOR));
  163. }
  164. static int check_metadata_version(struct superblock_disk *disk)
  165. {
  166. uint32_t metadata_version = le32_to_cpu(disk->version);
  167. if (metadata_version < MIN_ERA_VERSION || metadata_version > MAX_ERA_VERSION) {
  168. DMERR("Era metadata version %u found, but only versions between %u and %u supported.",
  169. metadata_version, MIN_ERA_VERSION, MAX_ERA_VERSION);
  170. return -EINVAL;
  171. }
  172. return 0;
  173. }
  174. static int sb_check(struct dm_block_validator *v,
  175. struct dm_block *b,
  176. size_t sb_block_size)
  177. {
  178. struct superblock_disk *disk = dm_block_data(b);
  179. __le32 csum_le;
  180. if (dm_block_location(b) != le64_to_cpu(disk->blocknr)) {
  181. DMERR("sb_check failed: blocknr %llu: wanted %llu",
  182. le64_to_cpu(disk->blocknr),
  183. (unsigned long long)dm_block_location(b));
  184. return -ENOTBLK;
  185. }
  186. if (le64_to_cpu(disk->magic) != SUPERBLOCK_MAGIC) {
  187. DMERR("sb_check failed: magic %llu: wanted %llu",
  188. le64_to_cpu(disk->magic),
  189. (unsigned long long) SUPERBLOCK_MAGIC);
  190. return -EILSEQ;
  191. }
  192. csum_le = cpu_to_le32(dm_bm_checksum(&disk->flags,
  193. sb_block_size - sizeof(__le32),
  194. SUPERBLOCK_CSUM_XOR));
  195. if (csum_le != disk->csum) {
  196. DMERR("sb_check failed: csum %u: wanted %u",
  197. le32_to_cpu(csum_le), le32_to_cpu(disk->csum));
  198. return -EILSEQ;
  199. }
  200. return check_metadata_version(disk);
  201. }
  202. static struct dm_block_validator sb_validator = {
  203. .name = "superblock",
  204. .prepare_for_write = sb_prepare_for_write,
  205. .check = sb_check
  206. };
  207. /*----------------------------------------------------------------
  208. * Low level metadata handling
  209. *--------------------------------------------------------------*/
  210. #define DM_ERA_METADATA_BLOCK_SIZE 4096
  211. #define DM_ERA_METADATA_CACHE_SIZE 64
  212. #define ERA_MAX_CONCURRENT_LOCKS 5
  213. struct era_metadata {
  214. struct block_device *bdev;
  215. struct dm_block_manager *bm;
  216. struct dm_space_map *sm;
  217. struct dm_transaction_manager *tm;
  218. dm_block_t block_size;
  219. uint32_t nr_blocks;
  220. uint32_t current_era;
  221. /*
  222. * We preallocate 2 writesets. When an era rolls over we
  223. * switch between them. This means the allocation is done at
  224. * preresume time, rather than on the io path.
  225. */
  226. struct writeset writesets[2];
  227. struct writeset *current_writeset;
  228. dm_block_t writeset_tree_root;
  229. dm_block_t era_array_root;
  230. struct dm_disk_bitset bitset_info;
  231. struct dm_btree_info writeset_tree_info;
  232. struct dm_array_info era_array_info;
  233. dm_block_t metadata_snap;
  234. /*
  235. * A flag that is set whenever a writeset has been archived.
  236. */
  237. bool archived_writesets;
  238. /*
  239. * Reading the space map root can fail, so we read it into this
  240. * buffer before the superblock is locked and updated.
  241. */
  242. __u8 metadata_space_map_root[SPACE_MAP_ROOT_SIZE];
  243. };
  244. static int superblock_read_lock(struct era_metadata *md,
  245. struct dm_block **sblock)
  246. {
  247. return dm_bm_read_lock(md->bm, SUPERBLOCK_LOCATION,
  248. &sb_validator, sblock);
  249. }
  250. static int superblock_lock_zero(struct era_metadata *md,
  251. struct dm_block **sblock)
  252. {
  253. return dm_bm_write_lock_zero(md->bm, SUPERBLOCK_LOCATION,
  254. &sb_validator, sblock);
  255. }
  256. static int superblock_lock(struct era_metadata *md,
  257. struct dm_block **sblock)
  258. {
  259. return dm_bm_write_lock(md->bm, SUPERBLOCK_LOCATION,
  260. &sb_validator, sblock);
  261. }
  262. /* FIXME: duplication with cache and thin */
  263. static int superblock_all_zeroes(struct dm_block_manager *bm, bool *result)
  264. {
  265. int r;
  266. unsigned i;
  267. struct dm_block *b;
  268. __le64 *data_le, zero = cpu_to_le64(0);
  269. unsigned sb_block_size = dm_bm_block_size(bm) / sizeof(__le64);
  270. /*
  271. * We can't use a validator here - it may be all zeroes.
  272. */
  273. r = dm_bm_read_lock(bm, SUPERBLOCK_LOCATION, NULL, &b);
  274. if (r)
  275. return r;
  276. data_le = dm_block_data(b);
  277. *result = true;
  278. for (i = 0; i < sb_block_size; i++) {
  279. if (data_le[i] != zero) {
  280. *result = false;
  281. break;
  282. }
  283. }
  284. dm_bm_unlock(b);
  285. return 0;
  286. }
  287. /*----------------------------------------------------------------*/
  288. static void ws_pack(const struct writeset_metadata *core, struct writeset_disk *disk)
  289. {
  290. disk->nr_bits = cpu_to_le32(core->nr_bits);
  291. disk->root = cpu_to_le64(core->root);
  292. }
  293. static void ws_unpack(const struct writeset_disk *disk, struct writeset_metadata *core)
  294. {
  295. core->nr_bits = le32_to_cpu(disk->nr_bits);
  296. core->root = le64_to_cpu(disk->root);
  297. }
  298. static void ws_inc(void *context, const void *value)
  299. {
  300. struct era_metadata *md = context;
  301. struct writeset_disk ws_d;
  302. dm_block_t b;
  303. memcpy(&ws_d, value, sizeof(ws_d));
  304. b = le64_to_cpu(ws_d.root);
  305. dm_tm_inc(md->tm, b);
  306. }
  307. static void ws_dec(void *context, const void *value)
  308. {
  309. struct era_metadata *md = context;
  310. struct writeset_disk ws_d;
  311. dm_block_t b;
  312. memcpy(&ws_d, value, sizeof(ws_d));
  313. b = le64_to_cpu(ws_d.root);
  314. dm_bitset_del(&md->bitset_info, b);
  315. }
  316. static int ws_eq(void *context, const void *value1, const void *value2)
  317. {
  318. return !memcmp(value1, value2, sizeof(struct writeset_metadata));
  319. }
  320. /*----------------------------------------------------------------*/
  321. static void setup_writeset_tree_info(struct era_metadata *md)
  322. {
  323. struct dm_btree_value_type *vt = &md->writeset_tree_info.value_type;
  324. md->writeset_tree_info.tm = md->tm;
  325. md->writeset_tree_info.levels = 1;
  326. vt->context = md;
  327. vt->size = sizeof(struct writeset_disk);
  328. vt->inc = ws_inc;
  329. vt->dec = ws_dec;
  330. vt->equal = ws_eq;
  331. }
  332. static void setup_era_array_info(struct era_metadata *md)
  333. {
  334. struct dm_btree_value_type vt;
  335. vt.context = NULL;
  336. vt.size = sizeof(__le32);
  337. vt.inc = NULL;
  338. vt.dec = NULL;
  339. vt.equal = NULL;
  340. dm_array_info_init(&md->era_array_info, md->tm, &vt);
  341. }
  342. static void setup_infos(struct era_metadata *md)
  343. {
  344. dm_disk_bitset_init(md->tm, &md->bitset_info);
  345. setup_writeset_tree_info(md);
  346. setup_era_array_info(md);
  347. }
  348. /*----------------------------------------------------------------*/
  349. static int create_fresh_metadata(struct era_metadata *md)
  350. {
  351. int r;
  352. r = dm_tm_create_with_sm(md->bm, SUPERBLOCK_LOCATION,
  353. &md->tm, &md->sm);
  354. if (r < 0) {
  355. DMERR("dm_tm_create_with_sm failed");
  356. return r;
  357. }
  358. setup_infos(md);
  359. r = dm_btree_empty(&md->writeset_tree_info, &md->writeset_tree_root);
  360. if (r) {
  361. DMERR("couldn't create new writeset tree");
  362. goto bad;
  363. }
  364. r = dm_array_empty(&md->era_array_info, &md->era_array_root);
  365. if (r) {
  366. DMERR("couldn't create era array");
  367. goto bad;
  368. }
  369. return 0;
  370. bad:
  371. dm_sm_destroy(md->sm);
  372. dm_tm_destroy(md->tm);
  373. return r;
  374. }
  375. static int save_sm_root(struct era_metadata *md)
  376. {
  377. int r;
  378. size_t metadata_len;
  379. r = dm_sm_root_size(md->sm, &metadata_len);
  380. if (r < 0)
  381. return r;
  382. return dm_sm_copy_root(md->sm, &md->metadata_space_map_root,
  383. metadata_len);
  384. }
  385. static void copy_sm_root(struct era_metadata *md, struct superblock_disk *disk)
  386. {
  387. memcpy(&disk->metadata_space_map_root,
  388. &md->metadata_space_map_root,
  389. sizeof(md->metadata_space_map_root));
  390. }
  391. /*
  392. * Writes a superblock, including the static fields that don't get updated
  393. * with every commit (possible optimisation here). 'md' should be fully
  394. * constructed when this is called.
  395. */
  396. static void prepare_superblock(struct era_metadata *md, struct superblock_disk *disk)
  397. {
  398. disk->magic = cpu_to_le64(SUPERBLOCK_MAGIC);
  399. disk->flags = cpu_to_le32(0ul);
  400. /* FIXME: can't keep blanking the uuid (uuid is currently unused though) */
  401. memset(disk->uuid, 0, sizeof(disk->uuid));
  402. disk->version = cpu_to_le32(MAX_ERA_VERSION);
  403. copy_sm_root(md, disk);
  404. disk->data_block_size = cpu_to_le32(md->block_size);
  405. disk->metadata_block_size = cpu_to_le32(DM_ERA_METADATA_BLOCK_SIZE >> SECTOR_SHIFT);
  406. disk->nr_blocks = cpu_to_le32(md->nr_blocks);
  407. disk->current_era = cpu_to_le32(md->current_era);
  408. ws_pack(&md->current_writeset->md, &disk->current_writeset);
  409. disk->writeset_tree_root = cpu_to_le64(md->writeset_tree_root);
  410. disk->era_array_root = cpu_to_le64(md->era_array_root);
  411. disk->metadata_snap = cpu_to_le64(md->metadata_snap);
  412. }
  413. static int write_superblock(struct era_metadata *md)
  414. {
  415. int r;
  416. struct dm_block *sblock;
  417. struct superblock_disk *disk;
  418. r = save_sm_root(md);
  419. if (r) {
  420. DMERR("%s: save_sm_root failed", __func__);
  421. return r;
  422. }
  423. r = superblock_lock_zero(md, &sblock);
  424. if (r)
  425. return r;
  426. disk = dm_block_data(sblock);
  427. prepare_superblock(md, disk);
  428. return dm_tm_commit(md->tm, sblock);
  429. }
  430. /*
  431. * Assumes block_size and the infos are set.
  432. */
  433. static int format_metadata(struct era_metadata *md)
  434. {
  435. int r;
  436. r = create_fresh_metadata(md);
  437. if (r)
  438. return r;
  439. r = write_superblock(md);
  440. if (r) {
  441. dm_sm_destroy(md->sm);
  442. dm_tm_destroy(md->tm);
  443. return r;
  444. }
  445. return 0;
  446. }
  447. static int open_metadata(struct era_metadata *md)
  448. {
  449. int r;
  450. struct dm_block *sblock;
  451. struct superblock_disk *disk;
  452. r = superblock_read_lock(md, &sblock);
  453. if (r) {
  454. DMERR("couldn't read_lock superblock");
  455. return r;
  456. }
  457. disk = dm_block_data(sblock);
  458. r = dm_tm_open_with_sm(md->bm, SUPERBLOCK_LOCATION,
  459. disk->metadata_space_map_root,
  460. sizeof(disk->metadata_space_map_root),
  461. &md->tm, &md->sm);
  462. if (r) {
  463. DMERR("dm_tm_open_with_sm failed");
  464. goto bad;
  465. }
  466. setup_infos(md);
  467. md->block_size = le32_to_cpu(disk->data_block_size);
  468. md->nr_blocks = le32_to_cpu(disk->nr_blocks);
  469. md->current_era = le32_to_cpu(disk->current_era);
  470. md->writeset_tree_root = le64_to_cpu(disk->writeset_tree_root);
  471. md->era_array_root = le64_to_cpu(disk->era_array_root);
  472. md->metadata_snap = le64_to_cpu(disk->metadata_snap);
  473. md->archived_writesets = true;
  474. dm_bm_unlock(sblock);
  475. return 0;
  476. bad:
  477. dm_bm_unlock(sblock);
  478. return r;
  479. }
  480. static int open_or_format_metadata(struct era_metadata *md,
  481. bool may_format)
  482. {
  483. int r;
  484. bool unformatted = false;
  485. r = superblock_all_zeroes(md->bm, &unformatted);
  486. if (r)
  487. return r;
  488. if (unformatted)
  489. return may_format ? format_metadata(md) : -EPERM;
  490. return open_metadata(md);
  491. }
  492. static int create_persistent_data_objects(struct era_metadata *md,
  493. bool may_format)
  494. {
  495. int r;
  496. md->bm = dm_block_manager_create(md->bdev, DM_ERA_METADATA_BLOCK_SIZE,
  497. DM_ERA_METADATA_CACHE_SIZE,
  498. ERA_MAX_CONCURRENT_LOCKS);
  499. if (IS_ERR(md->bm)) {
  500. DMERR("could not create block manager");
  501. return PTR_ERR(md->bm);
  502. }
  503. r = open_or_format_metadata(md, may_format);
  504. if (r)
  505. dm_block_manager_destroy(md->bm);
  506. return r;
  507. }
  508. static void destroy_persistent_data_objects(struct era_metadata *md)
  509. {
  510. dm_sm_destroy(md->sm);
  511. dm_tm_destroy(md->tm);
  512. dm_block_manager_destroy(md->bm);
  513. }
  514. /*
  515. * This waits until all era_map threads have picked up the new filter.
  516. */
  517. static void swap_writeset(struct era_metadata *md, struct writeset *new_writeset)
  518. {
  519. rcu_assign_pointer(md->current_writeset, new_writeset);
  520. synchronize_rcu();
  521. }
  522. /*----------------------------------------------------------------
  523. * Writesets get 'digested' into the main era array.
  524. *
  525. * We're using a coroutine here so the worker thread can do the digestion,
  526. * thus avoiding synchronisation of the metadata. Digesting a whole
  527. * writeset in one go would cause too much latency.
  528. *--------------------------------------------------------------*/
  529. struct digest {
  530. uint32_t era;
  531. unsigned nr_bits, current_bit;
  532. struct writeset_metadata writeset;
  533. __le32 value;
  534. struct dm_disk_bitset info;
  535. int (*step)(struct era_metadata *, struct digest *);
  536. };
  537. static int metadata_digest_lookup_writeset(struct era_metadata *md,
  538. struct digest *d);
  539. static int metadata_digest_remove_writeset(struct era_metadata *md,
  540. struct digest *d)
  541. {
  542. int r;
  543. uint64_t key = d->era;
  544. r = dm_btree_remove(&md->writeset_tree_info, md->writeset_tree_root,
  545. &key, &md->writeset_tree_root);
  546. if (r) {
  547. DMERR("%s: dm_btree_remove failed", __func__);
  548. return r;
  549. }
  550. d->step = metadata_digest_lookup_writeset;
  551. return 0;
  552. }
  553. #define INSERTS_PER_STEP 100
  554. static int metadata_digest_transcribe_writeset(struct era_metadata *md,
  555. struct digest *d)
  556. {
  557. int r;
  558. bool marked;
  559. unsigned b, e = min(d->current_bit + INSERTS_PER_STEP, d->nr_bits);
  560. for (b = d->current_bit; b < e; b++) {
  561. r = writeset_marked_on_disk(&d->info, &d->writeset, b, &marked);
  562. if (r) {
  563. DMERR("%s: writeset_marked_on_disk failed", __func__);
  564. return r;
  565. }
  566. if (!marked)
  567. continue;
  568. __dm_bless_for_disk(&d->value);
  569. r = dm_array_set_value(&md->era_array_info, md->era_array_root,
  570. b, &d->value, &md->era_array_root);
  571. if (r) {
  572. DMERR("%s: dm_array_set_value failed", __func__);
  573. return r;
  574. }
  575. }
  576. if (b == d->nr_bits)
  577. d->step = metadata_digest_remove_writeset;
  578. else
  579. d->current_bit = b;
  580. return 0;
  581. }
  582. static int metadata_digest_lookup_writeset(struct era_metadata *md,
  583. struct digest *d)
  584. {
  585. int r;
  586. uint64_t key;
  587. struct writeset_disk disk;
  588. r = dm_btree_find_lowest_key(&md->writeset_tree_info,
  589. md->writeset_tree_root, &key);
  590. if (r < 0)
  591. return r;
  592. d->era = key;
  593. r = dm_btree_lookup(&md->writeset_tree_info,
  594. md->writeset_tree_root, &key, &disk);
  595. if (r) {
  596. if (r == -ENODATA) {
  597. d->step = NULL;
  598. return 0;
  599. }
  600. DMERR("%s: dm_btree_lookup failed", __func__);
  601. return r;
  602. }
  603. ws_unpack(&disk, &d->writeset);
  604. d->value = cpu_to_le32(key);
  605. d->nr_bits = min(d->writeset.nr_bits, md->nr_blocks);
  606. d->current_bit = 0;
  607. d->step = metadata_digest_transcribe_writeset;
  608. return 0;
  609. }
  610. static int metadata_digest_start(struct era_metadata *md, struct digest *d)
  611. {
  612. if (d->step)
  613. return 0;
  614. memset(d, 0, sizeof(*d));
  615. /*
  616. * We initialise another bitset info to avoid any caching side
  617. * effects with the previous one.
  618. */
  619. dm_disk_bitset_init(md->tm, &d->info);
  620. d->step = metadata_digest_lookup_writeset;
  621. return 0;
  622. }
  623. /*----------------------------------------------------------------
  624. * High level metadata interface. Target methods should use these, and not
  625. * the lower level ones.
  626. *--------------------------------------------------------------*/
  627. static struct era_metadata *metadata_open(struct block_device *bdev,
  628. sector_t block_size,
  629. bool may_format)
  630. {
  631. int r;
  632. struct era_metadata *md = kzalloc(sizeof(*md), GFP_KERNEL);
  633. if (!md)
  634. return NULL;
  635. md->bdev = bdev;
  636. md->block_size = block_size;
  637. md->writesets[0].md.root = INVALID_WRITESET_ROOT;
  638. md->writesets[1].md.root = INVALID_WRITESET_ROOT;
  639. md->current_writeset = &md->writesets[0];
  640. r = create_persistent_data_objects(md, may_format);
  641. if (r) {
  642. kfree(md);
  643. return ERR_PTR(r);
  644. }
  645. return md;
  646. }
  647. static void metadata_close(struct era_metadata *md)
  648. {
  649. destroy_persistent_data_objects(md);
  650. kfree(md);
  651. }
  652. static bool valid_nr_blocks(dm_block_t n)
  653. {
  654. /*
  655. * dm_bitset restricts us to 2^32. test_bit & co. restrict us
  656. * further to 2^31 - 1
  657. */
  658. return n < (1ull << 31);
  659. }
  660. static int metadata_resize(struct era_metadata *md, void *arg)
  661. {
  662. int r;
  663. dm_block_t *new_size = arg;
  664. __le32 value;
  665. if (!valid_nr_blocks(*new_size)) {
  666. DMERR("Invalid number of origin blocks %llu",
  667. (unsigned long long) *new_size);
  668. return -EINVAL;
  669. }
  670. writeset_free(&md->writesets[0]);
  671. writeset_free(&md->writesets[1]);
  672. r = writeset_alloc(&md->writesets[0], *new_size);
  673. if (r) {
  674. DMERR("%s: writeset_alloc failed for writeset 0", __func__);
  675. return r;
  676. }
  677. r = writeset_alloc(&md->writesets[1], *new_size);
  678. if (r) {
  679. DMERR("%s: writeset_alloc failed for writeset 1", __func__);
  680. return r;
  681. }
  682. value = cpu_to_le32(0u);
  683. __dm_bless_for_disk(&value);
  684. r = dm_array_resize(&md->era_array_info, md->era_array_root,
  685. md->nr_blocks, *new_size,
  686. &value, &md->era_array_root);
  687. if (r) {
  688. DMERR("%s: dm_array_resize failed", __func__);
  689. return r;
  690. }
  691. md->nr_blocks = *new_size;
  692. return 0;
  693. }
  694. static int metadata_era_archive(struct era_metadata *md)
  695. {
  696. int r;
  697. uint64_t keys[1];
  698. struct writeset_disk value;
  699. r = dm_bitset_flush(&md->bitset_info, md->current_writeset->md.root,
  700. &md->current_writeset->md.root);
  701. if (r) {
  702. DMERR("%s: dm_bitset_flush failed", __func__);
  703. return r;
  704. }
  705. ws_pack(&md->current_writeset->md, &value);
  706. md->current_writeset->md.root = INVALID_WRITESET_ROOT;
  707. keys[0] = md->current_era;
  708. __dm_bless_for_disk(&value);
  709. r = dm_btree_insert(&md->writeset_tree_info, md->writeset_tree_root,
  710. keys, &value, &md->writeset_tree_root);
  711. if (r) {
  712. DMERR("%s: couldn't insert writeset into btree", __func__);
  713. /* FIXME: fail mode */
  714. return r;
  715. }
  716. md->archived_writesets = true;
  717. return 0;
  718. }
  719. static struct writeset *next_writeset(struct era_metadata *md)
  720. {
  721. return (md->current_writeset == &md->writesets[0]) ?
  722. &md->writesets[1] : &md->writesets[0];
  723. }
  724. static int metadata_new_era(struct era_metadata *md)
  725. {
  726. int r;
  727. struct writeset *new_writeset = next_writeset(md);
  728. r = writeset_init(&md->bitset_info, new_writeset);
  729. if (r) {
  730. DMERR("%s: writeset_init failed", __func__);
  731. return r;
  732. }
  733. swap_writeset(md, new_writeset);
  734. md->current_era++;
  735. return 0;
  736. }
  737. static int metadata_era_rollover(struct era_metadata *md)
  738. {
  739. int r;
  740. if (md->current_writeset->md.root != INVALID_WRITESET_ROOT) {
  741. r = metadata_era_archive(md);
  742. if (r) {
  743. DMERR("%s: metadata_archive_era failed", __func__);
  744. /* FIXME: fail mode? */
  745. return r;
  746. }
  747. }
  748. r = metadata_new_era(md);
  749. if (r) {
  750. DMERR("%s: new era failed", __func__);
  751. /* FIXME: fail mode */
  752. return r;
  753. }
  754. return 0;
  755. }
  756. static bool metadata_current_marked(struct era_metadata *md, dm_block_t block)
  757. {
  758. bool r;
  759. struct writeset *ws;
  760. rcu_read_lock();
  761. ws = rcu_dereference(md->current_writeset);
  762. r = writeset_marked(ws, block);
  763. rcu_read_unlock();
  764. return r;
  765. }
  766. static int metadata_commit(struct era_metadata *md)
  767. {
  768. int r;
  769. struct dm_block *sblock;
  770. if (md->current_writeset->md.root != SUPERBLOCK_LOCATION) {
  771. r = dm_bitset_flush(&md->bitset_info, md->current_writeset->md.root,
  772. &md->current_writeset->md.root);
  773. if (r) {
  774. DMERR("%s: bitset flush failed", __func__);
  775. return r;
  776. }
  777. }
  778. r = dm_tm_pre_commit(md->tm);
  779. if (r) {
  780. DMERR("%s: pre commit failed", __func__);
  781. return r;
  782. }
  783. r = save_sm_root(md);
  784. if (r) {
  785. DMERR("%s: save_sm_root failed", __func__);
  786. return r;
  787. }
  788. r = superblock_lock(md, &sblock);
  789. if (r) {
  790. DMERR("%s: superblock lock failed", __func__);
  791. return r;
  792. }
  793. prepare_superblock(md, dm_block_data(sblock));
  794. return dm_tm_commit(md->tm, sblock);
  795. }
  796. static int metadata_checkpoint(struct era_metadata *md)
  797. {
  798. /*
  799. * For now we just rollover, but later I want to put a check in to
  800. * avoid this if the filter is still pretty fresh.
  801. */
  802. return metadata_era_rollover(md);
  803. }
  804. /*
  805. * Metadata snapshots allow userland to access era data.
  806. */
  807. static int metadata_take_snap(struct era_metadata *md)
  808. {
  809. int r, inc;
  810. struct dm_block *clone;
  811. if (md->metadata_snap != SUPERBLOCK_LOCATION) {
  812. DMERR("%s: metadata snapshot already exists", __func__);
  813. return -EINVAL;
  814. }
  815. r = metadata_era_rollover(md);
  816. if (r) {
  817. DMERR("%s: era rollover failed", __func__);
  818. return r;
  819. }
  820. r = metadata_commit(md);
  821. if (r) {
  822. DMERR("%s: pre commit failed", __func__);
  823. return r;
  824. }
  825. r = dm_sm_inc_block(md->sm, SUPERBLOCK_LOCATION);
  826. if (r) {
  827. DMERR("%s: couldn't increment superblock", __func__);
  828. return r;
  829. }
  830. r = dm_tm_shadow_block(md->tm, SUPERBLOCK_LOCATION,
  831. &sb_validator, &clone, &inc);
  832. if (r) {
  833. DMERR("%s: couldn't shadow superblock", __func__);
  834. dm_sm_dec_block(md->sm, SUPERBLOCK_LOCATION);
  835. return r;
  836. }
  837. BUG_ON(!inc);
  838. r = dm_sm_inc_block(md->sm, md->writeset_tree_root);
  839. if (r) {
  840. DMERR("%s: couldn't inc writeset tree root", __func__);
  841. dm_tm_unlock(md->tm, clone);
  842. return r;
  843. }
  844. r = dm_sm_inc_block(md->sm, md->era_array_root);
  845. if (r) {
  846. DMERR("%s: couldn't inc era tree root", __func__);
  847. dm_sm_dec_block(md->sm, md->writeset_tree_root);
  848. dm_tm_unlock(md->tm, clone);
  849. return r;
  850. }
  851. md->metadata_snap = dm_block_location(clone);
  852. dm_tm_unlock(md->tm, clone);
  853. return 0;
  854. }
  855. static int metadata_drop_snap(struct era_metadata *md)
  856. {
  857. int r;
  858. dm_block_t location;
  859. struct dm_block *clone;
  860. struct superblock_disk *disk;
  861. if (md->metadata_snap == SUPERBLOCK_LOCATION) {
  862. DMERR("%s: no snap to drop", __func__);
  863. return -EINVAL;
  864. }
  865. r = dm_tm_read_lock(md->tm, md->metadata_snap, &sb_validator, &clone);
  866. if (r) {
  867. DMERR("%s: couldn't read lock superblock clone", __func__);
  868. return r;
  869. }
  870. /*
  871. * Whatever happens now we'll commit with no record of the metadata
  872. * snap.
  873. */
  874. md->metadata_snap = SUPERBLOCK_LOCATION;
  875. disk = dm_block_data(clone);
  876. r = dm_btree_del(&md->writeset_tree_info,
  877. le64_to_cpu(disk->writeset_tree_root));
  878. if (r) {
  879. DMERR("%s: error deleting writeset tree clone", __func__);
  880. dm_tm_unlock(md->tm, clone);
  881. return r;
  882. }
  883. r = dm_array_del(&md->era_array_info, le64_to_cpu(disk->era_array_root));
  884. if (r) {
  885. DMERR("%s: error deleting era array clone", __func__);
  886. dm_tm_unlock(md->tm, clone);
  887. return r;
  888. }
  889. location = dm_block_location(clone);
  890. dm_tm_unlock(md->tm, clone);
  891. return dm_sm_dec_block(md->sm, location);
  892. }
  893. struct metadata_stats {
  894. dm_block_t used;
  895. dm_block_t total;
  896. dm_block_t snap;
  897. uint32_t era;
  898. };
  899. static int metadata_get_stats(struct era_metadata *md, void *ptr)
  900. {
  901. int r;
  902. struct metadata_stats *s = ptr;
  903. dm_block_t nr_free, nr_total;
  904. r = dm_sm_get_nr_free(md->sm, &nr_free);
  905. if (r) {
  906. DMERR("dm_sm_get_nr_free returned %d", r);
  907. return r;
  908. }
  909. r = dm_sm_get_nr_blocks(md->sm, &nr_total);
  910. if (r) {
  911. DMERR("dm_pool_get_metadata_dev_size returned %d", r);
  912. return r;
  913. }
  914. s->used = nr_total - nr_free;
  915. s->total = nr_total;
  916. s->snap = md->metadata_snap;
  917. s->era = md->current_era;
  918. return 0;
  919. }
  920. /*----------------------------------------------------------------*/
  921. struct era {
  922. struct dm_target *ti;
  923. struct dm_target_callbacks callbacks;
  924. struct dm_dev *metadata_dev;
  925. struct dm_dev *origin_dev;
  926. dm_block_t nr_blocks;
  927. uint32_t sectors_per_block;
  928. int sectors_per_block_shift;
  929. struct era_metadata *md;
  930. struct workqueue_struct *wq;
  931. struct work_struct worker;
  932. spinlock_t deferred_lock;
  933. struct bio_list deferred_bios;
  934. spinlock_t rpc_lock;
  935. struct list_head rpc_calls;
  936. struct digest digest;
  937. atomic_t suspended;
  938. };
  939. struct rpc {
  940. struct list_head list;
  941. int (*fn0)(struct era_metadata *);
  942. int (*fn1)(struct era_metadata *, void *);
  943. void *arg;
  944. int result;
  945. struct completion complete;
  946. };
  947. /*----------------------------------------------------------------
  948. * Remapping.
  949. *---------------------------------------------------------------*/
  950. static bool block_size_is_power_of_two(struct era *era)
  951. {
  952. return era->sectors_per_block_shift >= 0;
  953. }
  954. static dm_block_t get_block(struct era *era, struct bio *bio)
  955. {
  956. sector_t block_nr = bio->bi_iter.bi_sector;
  957. if (!block_size_is_power_of_two(era))
  958. (void) sector_div(block_nr, era->sectors_per_block);
  959. else
  960. block_nr >>= era->sectors_per_block_shift;
  961. return block_nr;
  962. }
  963. static void remap_to_origin(struct era *era, struct bio *bio)
  964. {
  965. bio->bi_bdev = era->origin_dev->bdev;
  966. }
  967. /*----------------------------------------------------------------
  968. * Worker thread
  969. *--------------------------------------------------------------*/
  970. static void wake_worker(struct era *era)
  971. {
  972. if (!atomic_read(&era->suspended))
  973. queue_work(era->wq, &era->worker);
  974. }
  975. static void process_old_eras(struct era *era)
  976. {
  977. int r;
  978. if (!era->digest.step)
  979. return;
  980. r = era->digest.step(era->md, &era->digest);
  981. if (r < 0) {
  982. DMERR("%s: digest step failed, stopping digestion", __func__);
  983. era->digest.step = NULL;
  984. } else if (era->digest.step)
  985. wake_worker(era);
  986. }
  987. static void process_deferred_bios(struct era *era)
  988. {
  989. int r;
  990. struct bio_list deferred_bios, marked_bios;
  991. struct bio *bio;
  992. bool commit_needed = false;
  993. bool failed = false;
  994. bio_list_init(&deferred_bios);
  995. bio_list_init(&marked_bios);
  996. spin_lock(&era->deferred_lock);
  997. bio_list_merge(&deferred_bios, &era->deferred_bios);
  998. bio_list_init(&era->deferred_bios);
  999. spin_unlock(&era->deferred_lock);
  1000. while ((bio = bio_list_pop(&deferred_bios))) {
  1001. r = writeset_test_and_set(&era->md->bitset_info,
  1002. era->md->current_writeset,
  1003. get_block(era, bio));
  1004. if (r < 0) {
  1005. /*
  1006. * This is bad news, we need to rollback.
  1007. * FIXME: finish.
  1008. */
  1009. failed = true;
  1010. } else if (r == 0)
  1011. commit_needed = true;
  1012. bio_list_add(&marked_bios, bio);
  1013. }
  1014. if (commit_needed) {
  1015. r = metadata_commit(era->md);
  1016. if (r)
  1017. failed = true;
  1018. }
  1019. if (failed)
  1020. while ((bio = bio_list_pop(&marked_bios)))
  1021. bio_io_error(bio);
  1022. else
  1023. while ((bio = bio_list_pop(&marked_bios)))
  1024. generic_make_request(bio);
  1025. }
  1026. static void process_rpc_calls(struct era *era)
  1027. {
  1028. int r;
  1029. bool need_commit = false;
  1030. struct list_head calls;
  1031. struct rpc *rpc, *tmp;
  1032. INIT_LIST_HEAD(&calls);
  1033. spin_lock(&era->rpc_lock);
  1034. list_splice_init(&era->rpc_calls, &calls);
  1035. spin_unlock(&era->rpc_lock);
  1036. list_for_each_entry_safe(rpc, tmp, &calls, list) {
  1037. rpc->result = rpc->fn0 ? rpc->fn0(era->md) : rpc->fn1(era->md, rpc->arg);
  1038. need_commit = true;
  1039. }
  1040. if (need_commit) {
  1041. r = metadata_commit(era->md);
  1042. if (r)
  1043. list_for_each_entry_safe(rpc, tmp, &calls, list)
  1044. rpc->result = r;
  1045. }
  1046. list_for_each_entry_safe(rpc, tmp, &calls, list)
  1047. complete(&rpc->complete);
  1048. }
  1049. static void kick_off_digest(struct era *era)
  1050. {
  1051. if (era->md->archived_writesets) {
  1052. era->md->archived_writesets = false;
  1053. metadata_digest_start(era->md, &era->digest);
  1054. }
  1055. }
  1056. static void do_work(struct work_struct *ws)
  1057. {
  1058. struct era *era = container_of(ws, struct era, worker);
  1059. kick_off_digest(era);
  1060. process_old_eras(era);
  1061. process_deferred_bios(era);
  1062. process_rpc_calls(era);
  1063. }
  1064. static void defer_bio(struct era *era, struct bio *bio)
  1065. {
  1066. spin_lock(&era->deferred_lock);
  1067. bio_list_add(&era->deferred_bios, bio);
  1068. spin_unlock(&era->deferred_lock);
  1069. wake_worker(era);
  1070. }
  1071. /*
  1072. * Make an rpc call to the worker to change the metadata.
  1073. */
  1074. static int perform_rpc(struct era *era, struct rpc *rpc)
  1075. {
  1076. rpc->result = 0;
  1077. init_completion(&rpc->complete);
  1078. spin_lock(&era->rpc_lock);
  1079. list_add(&rpc->list, &era->rpc_calls);
  1080. spin_unlock(&era->rpc_lock);
  1081. wake_worker(era);
  1082. wait_for_completion(&rpc->complete);
  1083. return rpc->result;
  1084. }
  1085. static int in_worker0(struct era *era, int (*fn)(struct era_metadata *))
  1086. {
  1087. struct rpc rpc;
  1088. rpc.fn0 = fn;
  1089. rpc.fn1 = NULL;
  1090. return perform_rpc(era, &rpc);
  1091. }
  1092. static int in_worker1(struct era *era,
  1093. int (*fn)(struct era_metadata *, void *), void *arg)
  1094. {
  1095. struct rpc rpc;
  1096. rpc.fn0 = NULL;
  1097. rpc.fn1 = fn;
  1098. rpc.arg = arg;
  1099. return perform_rpc(era, &rpc);
  1100. }
  1101. static void start_worker(struct era *era)
  1102. {
  1103. atomic_set(&era->suspended, 0);
  1104. }
  1105. static void stop_worker(struct era *era)
  1106. {
  1107. atomic_set(&era->suspended, 1);
  1108. flush_workqueue(era->wq);
  1109. }
  1110. /*----------------------------------------------------------------
  1111. * Target methods
  1112. *--------------------------------------------------------------*/
  1113. static int dev_is_congested(struct dm_dev *dev, int bdi_bits)
  1114. {
  1115. struct request_queue *q = bdev_get_queue(dev->bdev);
  1116. return bdi_congested(&q->backing_dev_info, bdi_bits);
  1117. }
  1118. static int era_is_congested(struct dm_target_callbacks *cb, int bdi_bits)
  1119. {
  1120. struct era *era = container_of(cb, struct era, callbacks);
  1121. return dev_is_congested(era->origin_dev, bdi_bits);
  1122. }
  1123. static void era_destroy(struct era *era)
  1124. {
  1125. if (era->md)
  1126. metadata_close(era->md);
  1127. if (era->wq)
  1128. destroy_workqueue(era->wq);
  1129. if (era->origin_dev)
  1130. dm_put_device(era->ti, era->origin_dev);
  1131. if (era->metadata_dev)
  1132. dm_put_device(era->ti, era->metadata_dev);
  1133. kfree(era);
  1134. }
  1135. static dm_block_t calc_nr_blocks(struct era *era)
  1136. {
  1137. return dm_sector_div_up(era->ti->len, era->sectors_per_block);
  1138. }
  1139. static bool valid_block_size(dm_block_t block_size)
  1140. {
  1141. bool greater_than_zero = block_size > 0;
  1142. bool multiple_of_min_block_size = (block_size & (MIN_BLOCK_SIZE - 1)) == 0;
  1143. return greater_than_zero && multiple_of_min_block_size;
  1144. }
  1145. /*
  1146. * <metadata dev> <data dev> <data block size (sectors)>
  1147. */
  1148. static int era_ctr(struct dm_target *ti, unsigned argc, char **argv)
  1149. {
  1150. int r;
  1151. char dummy;
  1152. struct era *era;
  1153. struct era_metadata *md;
  1154. if (argc != 3) {
  1155. ti->error = "Invalid argument count";
  1156. return -EINVAL;
  1157. }
  1158. era = kzalloc(sizeof(*era), GFP_KERNEL);
  1159. if (!era) {
  1160. ti->error = "Error allocating era structure";
  1161. return -ENOMEM;
  1162. }
  1163. era->ti = ti;
  1164. r = dm_get_device(ti, argv[0], FMODE_READ | FMODE_WRITE, &era->metadata_dev);
  1165. if (r) {
  1166. ti->error = "Error opening metadata device";
  1167. era_destroy(era);
  1168. return -EINVAL;
  1169. }
  1170. r = dm_get_device(ti, argv[1], FMODE_READ | FMODE_WRITE, &era->origin_dev);
  1171. if (r) {
  1172. ti->error = "Error opening data device";
  1173. era_destroy(era);
  1174. return -EINVAL;
  1175. }
  1176. r = sscanf(argv[2], "%u%c", &era->sectors_per_block, &dummy);
  1177. if (r != 1) {
  1178. ti->error = "Error parsing block size";
  1179. era_destroy(era);
  1180. return -EINVAL;
  1181. }
  1182. r = dm_set_target_max_io_len(ti, era->sectors_per_block);
  1183. if (r) {
  1184. ti->error = "could not set max io len";
  1185. era_destroy(era);
  1186. return -EINVAL;
  1187. }
  1188. if (!valid_block_size(era->sectors_per_block)) {
  1189. ti->error = "Invalid block size";
  1190. era_destroy(era);
  1191. return -EINVAL;
  1192. }
  1193. if (era->sectors_per_block & (era->sectors_per_block - 1))
  1194. era->sectors_per_block_shift = -1;
  1195. else
  1196. era->sectors_per_block_shift = __ffs(era->sectors_per_block);
  1197. md = metadata_open(era->metadata_dev->bdev, era->sectors_per_block, true);
  1198. if (IS_ERR(md)) {
  1199. ti->error = "Error reading metadata";
  1200. era_destroy(era);
  1201. return PTR_ERR(md);
  1202. }
  1203. era->md = md;
  1204. era->nr_blocks = calc_nr_blocks(era);
  1205. r = metadata_resize(era->md, &era->nr_blocks);
  1206. if (r) {
  1207. ti->error = "couldn't resize metadata";
  1208. era_destroy(era);
  1209. return -ENOMEM;
  1210. }
  1211. era->wq = alloc_ordered_workqueue("dm-" DM_MSG_PREFIX, WQ_MEM_RECLAIM);
  1212. if (!era->wq) {
  1213. ti->error = "could not create workqueue for metadata object";
  1214. era_destroy(era);
  1215. return -ENOMEM;
  1216. }
  1217. INIT_WORK(&era->worker, do_work);
  1218. spin_lock_init(&era->deferred_lock);
  1219. bio_list_init(&era->deferred_bios);
  1220. spin_lock_init(&era->rpc_lock);
  1221. INIT_LIST_HEAD(&era->rpc_calls);
  1222. ti->private = era;
  1223. ti->num_flush_bios = 1;
  1224. ti->flush_supported = true;
  1225. ti->num_discard_bios = 1;
  1226. ti->discards_supported = true;
  1227. era->callbacks.congested_fn = era_is_congested;
  1228. dm_table_add_target_callbacks(ti->table, &era->callbacks);
  1229. return 0;
  1230. }
  1231. static void era_dtr(struct dm_target *ti)
  1232. {
  1233. era_destroy(ti->private);
  1234. }
  1235. static int era_map(struct dm_target *ti, struct bio *bio)
  1236. {
  1237. struct era *era = ti->private;
  1238. dm_block_t block = get_block(era, bio);
  1239. /*
  1240. * All bios get remapped to the origin device. We do this now, but
  1241. * it may not get issued until later. Depending on whether the
  1242. * block is marked in this era.
  1243. */
  1244. remap_to_origin(era, bio);
  1245. /*
  1246. * REQ_FLUSH bios carry no data, so we're not interested in them.
  1247. */
  1248. if (!(bio->bi_rw & REQ_FLUSH) &&
  1249. (bio_data_dir(bio) == WRITE) &&
  1250. !metadata_current_marked(era->md, block)) {
  1251. defer_bio(era, bio);
  1252. return DM_MAPIO_SUBMITTED;
  1253. }
  1254. return DM_MAPIO_REMAPPED;
  1255. }
  1256. static void era_postsuspend(struct dm_target *ti)
  1257. {
  1258. int r;
  1259. struct era *era = ti->private;
  1260. r = in_worker0(era, metadata_era_archive);
  1261. if (r) {
  1262. DMERR("%s: couldn't archive current era", __func__);
  1263. /* FIXME: fail mode */
  1264. }
  1265. stop_worker(era);
  1266. }
  1267. static int era_preresume(struct dm_target *ti)
  1268. {
  1269. int r;
  1270. struct era *era = ti->private;
  1271. dm_block_t new_size = calc_nr_blocks(era);
  1272. if (era->nr_blocks != new_size) {
  1273. r = in_worker1(era, metadata_resize, &new_size);
  1274. if (r)
  1275. return r;
  1276. era->nr_blocks = new_size;
  1277. }
  1278. start_worker(era);
  1279. r = in_worker0(era, metadata_new_era);
  1280. if (r) {
  1281. DMERR("%s: metadata_era_rollover failed", __func__);
  1282. return r;
  1283. }
  1284. return 0;
  1285. }
  1286. /*
  1287. * Status format:
  1288. *
  1289. * <metadata block size> <#used metadata blocks>/<#total metadata blocks>
  1290. * <current era> <held metadata root | '-'>
  1291. */
  1292. static void era_status(struct dm_target *ti, status_type_t type,
  1293. unsigned status_flags, char *result, unsigned maxlen)
  1294. {
  1295. int r;
  1296. struct era *era = ti->private;
  1297. ssize_t sz = 0;
  1298. struct metadata_stats stats;
  1299. char buf[BDEVNAME_SIZE];
  1300. switch (type) {
  1301. case STATUSTYPE_INFO:
  1302. r = in_worker1(era, metadata_get_stats, &stats);
  1303. if (r)
  1304. goto err;
  1305. DMEMIT("%u %llu/%llu %u",
  1306. (unsigned) (DM_ERA_METADATA_BLOCK_SIZE >> SECTOR_SHIFT),
  1307. (unsigned long long) stats.used,
  1308. (unsigned long long) stats.total,
  1309. (unsigned) stats.era);
  1310. if (stats.snap != SUPERBLOCK_LOCATION)
  1311. DMEMIT(" %llu", stats.snap);
  1312. else
  1313. DMEMIT(" -");
  1314. break;
  1315. case STATUSTYPE_TABLE:
  1316. format_dev_t(buf, era->metadata_dev->bdev->bd_dev);
  1317. DMEMIT("%s ", buf);
  1318. format_dev_t(buf, era->origin_dev->bdev->bd_dev);
  1319. DMEMIT("%s %u", buf, era->sectors_per_block);
  1320. break;
  1321. }
  1322. return;
  1323. err:
  1324. DMEMIT("Error");
  1325. }
  1326. static int era_message(struct dm_target *ti, unsigned argc, char **argv)
  1327. {
  1328. struct era *era = ti->private;
  1329. if (argc != 1) {
  1330. DMERR("incorrect number of message arguments");
  1331. return -EINVAL;
  1332. }
  1333. if (!strcasecmp(argv[0], "checkpoint"))
  1334. return in_worker0(era, metadata_checkpoint);
  1335. if (!strcasecmp(argv[0], "take_metadata_snap"))
  1336. return in_worker0(era, metadata_take_snap);
  1337. if (!strcasecmp(argv[0], "drop_metadata_snap"))
  1338. return in_worker0(era, metadata_drop_snap);
  1339. DMERR("unsupported message '%s'", argv[0]);
  1340. return -EINVAL;
  1341. }
  1342. static sector_t get_dev_size(struct dm_dev *dev)
  1343. {
  1344. return i_size_read(dev->bdev->bd_inode) >> SECTOR_SHIFT;
  1345. }
  1346. static int era_iterate_devices(struct dm_target *ti,
  1347. iterate_devices_callout_fn fn, void *data)
  1348. {
  1349. struct era *era = ti->private;
  1350. return fn(ti, era->origin_dev, 0, get_dev_size(era->origin_dev), data);
  1351. }
  1352. static void era_io_hints(struct dm_target *ti, struct queue_limits *limits)
  1353. {
  1354. struct era *era = ti->private;
  1355. uint64_t io_opt_sectors = limits->io_opt >> SECTOR_SHIFT;
  1356. /*
  1357. * If the system-determined stacked limits are compatible with the
  1358. * era device's blocksize (io_opt is a factor) do not override them.
  1359. */
  1360. if (io_opt_sectors < era->sectors_per_block ||
  1361. do_div(io_opt_sectors, era->sectors_per_block)) {
  1362. blk_limits_io_min(limits, 0);
  1363. blk_limits_io_opt(limits, era->sectors_per_block << SECTOR_SHIFT);
  1364. }
  1365. }
  1366. /*----------------------------------------------------------------*/
  1367. static struct target_type era_target = {
  1368. .name = "era",
  1369. .version = {1, 0, 0},
  1370. .module = THIS_MODULE,
  1371. .ctr = era_ctr,
  1372. .dtr = era_dtr,
  1373. .map = era_map,
  1374. .postsuspend = era_postsuspend,
  1375. .preresume = era_preresume,
  1376. .status = era_status,
  1377. .message = era_message,
  1378. .iterate_devices = era_iterate_devices,
  1379. .io_hints = era_io_hints
  1380. };
  1381. static int __init dm_era_init(void)
  1382. {
  1383. int r;
  1384. r = dm_register_target(&era_target);
  1385. if (r) {
  1386. DMERR("era target registration failed: %d", r);
  1387. return r;
  1388. }
  1389. return 0;
  1390. }
  1391. static void __exit dm_era_exit(void)
  1392. {
  1393. dm_unregister_target(&era_target);
  1394. }
  1395. module_init(dm_era_init);
  1396. module_exit(dm_era_exit);
  1397. MODULE_DESCRIPTION(DM_NAME " era target");
  1398. MODULE_AUTHOR("Joe Thornber <ejt@redhat.com>");
  1399. MODULE_LICENSE("GPL");