dm-era-target.c 38 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732
  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_PREFLUSH bios carry no data, so we're not interested in them.
  1247. */
  1248. if (!(bio->bi_opf & REQ_PREFLUSH) &&
  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");