dm-era-target.c 38 KB

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