dm-thin-metadata.c 31 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410
  1. /*
  2. * Copyright (C) 2011 Red Hat, Inc.
  3. *
  4. * This file is released under the GPL.
  5. */
  6. #include "dm-thin-metadata.h"
  7. #include "persistent-data/dm-btree.h"
  8. #include "persistent-data/dm-space-map.h"
  9. #include "persistent-data/dm-space-map-disk.h"
  10. #include "persistent-data/dm-transaction-manager.h"
  11. #include <linux/list.h>
  12. #include <linux/device-mapper.h>
  13. #include <linux/workqueue.h>
  14. /*--------------------------------------------------------------------------
  15. * As far as the metadata goes, there is:
  16. *
  17. * - A superblock in block zero, taking up fewer than 512 bytes for
  18. * atomic writes.
  19. *
  20. * - A space map managing the metadata blocks.
  21. *
  22. * - A space map managing the data blocks.
  23. *
  24. * - A btree mapping our internal thin dev ids onto struct disk_device_details.
  25. *
  26. * - A hierarchical btree, with 2 levels which effectively maps (thin
  27. * dev id, virtual block) -> block_time. Block time is a 64-bit
  28. * field holding the time in the low 24 bits, and block in the top 48
  29. * bits.
  30. *
  31. * BTrees consist solely of btree_nodes, that fill a block. Some are
  32. * internal nodes, as such their values are a __le64 pointing to other
  33. * nodes. Leaf nodes can store data of any reasonable size (ie. much
  34. * smaller than the block size). The nodes consist of the header,
  35. * followed by an array of keys, followed by an array of values. We have
  36. * to binary search on the keys so they're all held together to help the
  37. * cpu cache.
  38. *
  39. * Space maps have 2 btrees:
  40. *
  41. * - One maps a uint64_t onto a struct index_entry. Which points to a
  42. * bitmap block, and has some details about how many free entries there
  43. * are etc.
  44. *
  45. * - The bitmap blocks have a header (for the checksum). Then the rest
  46. * of the block is pairs of bits. With the meaning being:
  47. *
  48. * 0 - ref count is 0
  49. * 1 - ref count is 1
  50. * 2 - ref count is 2
  51. * 3 - ref count is higher than 2
  52. *
  53. * - If the count is higher than 2 then the ref count is entered in a
  54. * second btree that directly maps the block_address to a uint32_t ref
  55. * count.
  56. *
  57. * The space map metadata variant doesn't have a bitmaps btree. Instead
  58. * it has one single blocks worth of index_entries. This avoids
  59. * recursive issues with the bitmap btree needing to allocate space in
  60. * order to insert. With a small data block size such as 64k the
  61. * metadata support data devices that are hundreds of terrabytes.
  62. *
  63. * The space maps allocate space linearly from front to back. Space that
  64. * is freed in a transaction is never recycled within that transaction.
  65. * To try and avoid fragmenting _free_ space the allocator always goes
  66. * back and fills in gaps.
  67. *
  68. * All metadata io is in THIN_METADATA_BLOCK_SIZE sized/aligned chunks
  69. * from the block manager.
  70. *--------------------------------------------------------------------------*/
  71. #define DM_MSG_PREFIX "thin metadata"
  72. #define THIN_SUPERBLOCK_MAGIC 27022010
  73. #define THIN_SUPERBLOCK_LOCATION 0
  74. #define THIN_VERSION 1
  75. #define THIN_METADATA_CACHE_SIZE 64
  76. #define SECTOR_TO_BLOCK_SHIFT 3
  77. /* This should be plenty */
  78. #define SPACE_MAP_ROOT_SIZE 128
  79. /*
  80. * Little endian on-disk superblock and device details.
  81. */
  82. struct thin_disk_superblock {
  83. __le32 csum; /* Checksum of superblock except for this field. */
  84. __le32 flags;
  85. __le64 blocknr; /* This block number, dm_block_t. */
  86. __u8 uuid[16];
  87. __le64 magic;
  88. __le32 version;
  89. __le32 time;
  90. __le64 trans_id;
  91. /*
  92. * Root held by userspace transactions.
  93. */
  94. __le64 held_root;
  95. __u8 data_space_map_root[SPACE_MAP_ROOT_SIZE];
  96. __u8 metadata_space_map_root[SPACE_MAP_ROOT_SIZE];
  97. /*
  98. * 2-level btree mapping (dev_id, (dev block, time)) -> data block
  99. */
  100. __le64 data_mapping_root;
  101. /*
  102. * Device detail root mapping dev_id -> device_details
  103. */
  104. __le64 device_details_root;
  105. __le32 data_block_size; /* In 512-byte sectors. */
  106. __le32 metadata_block_size; /* In 512-byte sectors. */
  107. __le64 metadata_nr_blocks;
  108. __le32 compat_flags;
  109. __le32 compat_ro_flags;
  110. __le32 incompat_flags;
  111. } __packed;
  112. struct disk_device_details {
  113. __le64 mapped_blocks;
  114. __le64 transaction_id; /* When created. */
  115. __le32 creation_time;
  116. __le32 snapshotted_time;
  117. } __packed;
  118. struct dm_pool_metadata {
  119. struct hlist_node hash;
  120. struct block_device *bdev;
  121. struct dm_block_manager *bm;
  122. struct dm_space_map *metadata_sm;
  123. struct dm_space_map *data_sm;
  124. struct dm_transaction_manager *tm;
  125. struct dm_transaction_manager *nb_tm;
  126. /*
  127. * Two-level btree.
  128. * First level holds thin_dev_t.
  129. * Second level holds mappings.
  130. */
  131. struct dm_btree_info info;
  132. /*
  133. * Non-blocking version of the above.
  134. */
  135. struct dm_btree_info nb_info;
  136. /*
  137. * Just the top level for deleting whole devices.
  138. */
  139. struct dm_btree_info tl_info;
  140. /*
  141. * Just the bottom level for creating new devices.
  142. */
  143. struct dm_btree_info bl_info;
  144. /*
  145. * Describes the device details btree.
  146. */
  147. struct dm_btree_info details_info;
  148. struct rw_semaphore root_lock;
  149. uint32_t time;
  150. int need_commit;
  151. dm_block_t root;
  152. dm_block_t details_root;
  153. struct list_head thin_devices;
  154. uint64_t trans_id;
  155. unsigned long flags;
  156. sector_t data_block_size;
  157. };
  158. struct dm_thin_device {
  159. struct list_head list;
  160. struct dm_pool_metadata *pmd;
  161. dm_thin_id id;
  162. int open_count;
  163. int changed;
  164. uint64_t mapped_blocks;
  165. uint64_t transaction_id;
  166. uint32_t creation_time;
  167. uint32_t snapshotted_time;
  168. };
  169. /*----------------------------------------------------------------
  170. * superblock validator
  171. *--------------------------------------------------------------*/
  172. #define SUPERBLOCK_CSUM_XOR 160774
  173. static void sb_prepare_for_write(struct dm_block_validator *v,
  174. struct dm_block *b,
  175. size_t block_size)
  176. {
  177. struct thin_disk_superblock *disk_super = dm_block_data(b);
  178. disk_super->blocknr = cpu_to_le64(dm_block_location(b));
  179. disk_super->csum = cpu_to_le32(dm_bm_checksum(&disk_super->flags,
  180. block_size - sizeof(__le32),
  181. SUPERBLOCK_CSUM_XOR));
  182. }
  183. static int sb_check(struct dm_block_validator *v,
  184. struct dm_block *b,
  185. size_t block_size)
  186. {
  187. struct thin_disk_superblock *disk_super = dm_block_data(b);
  188. __le32 csum_le;
  189. if (dm_block_location(b) != le64_to_cpu(disk_super->blocknr)) {
  190. DMERR("sb_check failed: blocknr %llu: "
  191. "wanted %llu", le64_to_cpu(disk_super->blocknr),
  192. (unsigned long long)dm_block_location(b));
  193. return -ENOTBLK;
  194. }
  195. if (le64_to_cpu(disk_super->magic) != THIN_SUPERBLOCK_MAGIC) {
  196. DMERR("sb_check failed: magic %llu: "
  197. "wanted %llu", le64_to_cpu(disk_super->magic),
  198. (unsigned long long)THIN_SUPERBLOCK_MAGIC);
  199. return -EILSEQ;
  200. }
  201. csum_le = cpu_to_le32(dm_bm_checksum(&disk_super->flags,
  202. block_size - sizeof(__le32),
  203. SUPERBLOCK_CSUM_XOR));
  204. if (csum_le != disk_super->csum) {
  205. DMERR("sb_check failed: csum %u: wanted %u",
  206. le32_to_cpu(csum_le), le32_to_cpu(disk_super->csum));
  207. return -EILSEQ;
  208. }
  209. return 0;
  210. }
  211. static struct dm_block_validator sb_validator = {
  212. .name = "superblock",
  213. .prepare_for_write = sb_prepare_for_write,
  214. .check = sb_check
  215. };
  216. /*----------------------------------------------------------------
  217. * Methods for the btree value types
  218. *--------------------------------------------------------------*/
  219. static uint64_t pack_block_time(dm_block_t b, uint32_t t)
  220. {
  221. return (b << 24) | t;
  222. }
  223. static void unpack_block_time(uint64_t v, dm_block_t *b, uint32_t *t)
  224. {
  225. *b = v >> 24;
  226. *t = v & ((1 << 24) - 1);
  227. }
  228. static void data_block_inc(void *context, void *value_le)
  229. {
  230. struct dm_space_map *sm = context;
  231. __le64 v_le;
  232. uint64_t b;
  233. uint32_t t;
  234. memcpy(&v_le, value_le, sizeof(v_le));
  235. unpack_block_time(le64_to_cpu(v_le), &b, &t);
  236. dm_sm_inc_block(sm, b);
  237. }
  238. static void data_block_dec(void *context, void *value_le)
  239. {
  240. struct dm_space_map *sm = context;
  241. __le64 v_le;
  242. uint64_t b;
  243. uint32_t t;
  244. memcpy(&v_le, value_le, sizeof(v_le));
  245. unpack_block_time(le64_to_cpu(v_le), &b, &t);
  246. dm_sm_dec_block(sm, b);
  247. }
  248. static int data_block_equal(void *context, void *value1_le, void *value2_le)
  249. {
  250. __le64 v1_le, v2_le;
  251. uint64_t b1, b2;
  252. uint32_t t;
  253. memcpy(&v1_le, value1_le, sizeof(v1_le));
  254. memcpy(&v2_le, value2_le, sizeof(v2_le));
  255. unpack_block_time(le64_to_cpu(v1_le), &b1, &t);
  256. unpack_block_time(le64_to_cpu(v2_le), &b2, &t);
  257. return b1 == b2;
  258. }
  259. static void subtree_inc(void *context, void *value)
  260. {
  261. struct dm_btree_info *info = context;
  262. __le64 root_le;
  263. uint64_t root;
  264. memcpy(&root_le, value, sizeof(root_le));
  265. root = le64_to_cpu(root_le);
  266. dm_tm_inc(info->tm, root);
  267. }
  268. static void subtree_dec(void *context, void *value)
  269. {
  270. struct dm_btree_info *info = context;
  271. __le64 root_le;
  272. uint64_t root;
  273. memcpy(&root_le, value, sizeof(root_le));
  274. root = le64_to_cpu(root_le);
  275. if (dm_btree_del(info, root))
  276. DMERR("btree delete failed\n");
  277. }
  278. static int subtree_equal(void *context, void *value1_le, void *value2_le)
  279. {
  280. __le64 v1_le, v2_le;
  281. memcpy(&v1_le, value1_le, sizeof(v1_le));
  282. memcpy(&v2_le, value2_le, sizeof(v2_le));
  283. return v1_le == v2_le;
  284. }
  285. /*----------------------------------------------------------------*/
  286. static int superblock_all_zeroes(struct dm_block_manager *bm, int *result)
  287. {
  288. int r;
  289. unsigned i;
  290. struct dm_block *b;
  291. __le64 *data_le, zero = cpu_to_le64(0);
  292. unsigned block_size = dm_bm_block_size(bm) / sizeof(__le64);
  293. /*
  294. * We can't use a validator here - it may be all zeroes.
  295. */
  296. r = dm_bm_read_lock(bm, THIN_SUPERBLOCK_LOCATION, NULL, &b);
  297. if (r)
  298. return r;
  299. data_le = dm_block_data(b);
  300. *result = 1;
  301. for (i = 0; i < block_size; i++) {
  302. if (data_le[i] != zero) {
  303. *result = 0;
  304. break;
  305. }
  306. }
  307. return dm_bm_unlock(b);
  308. }
  309. static int init_pmd(struct dm_pool_metadata *pmd,
  310. struct dm_block_manager *bm,
  311. dm_block_t nr_blocks, int create)
  312. {
  313. int r;
  314. struct dm_space_map *sm, *data_sm;
  315. struct dm_transaction_manager *tm;
  316. struct dm_block *sblock;
  317. if (create) {
  318. r = dm_tm_create_with_sm(bm, THIN_SUPERBLOCK_LOCATION,
  319. &sb_validator, &tm, &sm, &sblock);
  320. if (r < 0) {
  321. DMERR("tm_create_with_sm failed");
  322. return r;
  323. }
  324. data_sm = dm_sm_disk_create(tm, nr_blocks);
  325. if (IS_ERR(data_sm)) {
  326. DMERR("sm_disk_create failed");
  327. dm_tm_unlock(tm, sblock);
  328. r = PTR_ERR(data_sm);
  329. goto bad;
  330. }
  331. } else {
  332. struct thin_disk_superblock *disk_super = NULL;
  333. size_t space_map_root_offset =
  334. offsetof(struct thin_disk_superblock, metadata_space_map_root);
  335. r = dm_tm_open_with_sm(bm, THIN_SUPERBLOCK_LOCATION,
  336. &sb_validator, space_map_root_offset,
  337. SPACE_MAP_ROOT_SIZE, &tm, &sm, &sblock);
  338. if (r < 0) {
  339. DMERR("tm_open_with_sm failed");
  340. return r;
  341. }
  342. disk_super = dm_block_data(sblock);
  343. data_sm = dm_sm_disk_open(tm, disk_super->data_space_map_root,
  344. sizeof(disk_super->data_space_map_root));
  345. if (IS_ERR(data_sm)) {
  346. DMERR("sm_disk_open failed");
  347. r = PTR_ERR(data_sm);
  348. goto bad;
  349. }
  350. }
  351. r = dm_tm_unlock(tm, sblock);
  352. if (r < 0) {
  353. DMERR("couldn't unlock superblock");
  354. goto bad_data_sm;
  355. }
  356. pmd->bm = bm;
  357. pmd->metadata_sm = sm;
  358. pmd->data_sm = data_sm;
  359. pmd->tm = tm;
  360. pmd->nb_tm = dm_tm_create_non_blocking_clone(tm);
  361. if (!pmd->nb_tm) {
  362. DMERR("could not create clone tm");
  363. r = -ENOMEM;
  364. goto bad_data_sm;
  365. }
  366. pmd->info.tm = tm;
  367. pmd->info.levels = 2;
  368. pmd->info.value_type.context = pmd->data_sm;
  369. pmd->info.value_type.size = sizeof(__le64);
  370. pmd->info.value_type.inc = data_block_inc;
  371. pmd->info.value_type.dec = data_block_dec;
  372. pmd->info.value_type.equal = data_block_equal;
  373. memcpy(&pmd->nb_info, &pmd->info, sizeof(pmd->nb_info));
  374. pmd->nb_info.tm = pmd->nb_tm;
  375. pmd->tl_info.tm = tm;
  376. pmd->tl_info.levels = 1;
  377. pmd->tl_info.value_type.context = &pmd->info;
  378. pmd->tl_info.value_type.size = sizeof(__le64);
  379. pmd->tl_info.value_type.inc = subtree_inc;
  380. pmd->tl_info.value_type.dec = subtree_dec;
  381. pmd->tl_info.value_type.equal = subtree_equal;
  382. pmd->bl_info.tm = tm;
  383. pmd->bl_info.levels = 1;
  384. pmd->bl_info.value_type.context = pmd->data_sm;
  385. pmd->bl_info.value_type.size = sizeof(__le64);
  386. pmd->bl_info.value_type.inc = data_block_inc;
  387. pmd->bl_info.value_type.dec = data_block_dec;
  388. pmd->bl_info.value_type.equal = data_block_equal;
  389. pmd->details_info.tm = tm;
  390. pmd->details_info.levels = 1;
  391. pmd->details_info.value_type.context = NULL;
  392. pmd->details_info.value_type.size = sizeof(struct disk_device_details);
  393. pmd->details_info.value_type.inc = NULL;
  394. pmd->details_info.value_type.dec = NULL;
  395. pmd->details_info.value_type.equal = NULL;
  396. pmd->root = 0;
  397. init_rwsem(&pmd->root_lock);
  398. pmd->time = 0;
  399. pmd->need_commit = 0;
  400. pmd->details_root = 0;
  401. pmd->trans_id = 0;
  402. pmd->flags = 0;
  403. INIT_LIST_HEAD(&pmd->thin_devices);
  404. return 0;
  405. bad_data_sm:
  406. dm_sm_destroy(data_sm);
  407. bad:
  408. dm_tm_destroy(tm);
  409. dm_sm_destroy(sm);
  410. return r;
  411. }
  412. static int __begin_transaction(struct dm_pool_metadata *pmd)
  413. {
  414. int r;
  415. u32 features;
  416. struct thin_disk_superblock *disk_super;
  417. struct dm_block *sblock;
  418. /*
  419. * __maybe_commit_transaction() resets these
  420. */
  421. WARN_ON(pmd->need_commit);
  422. /*
  423. * We re-read the superblock every time. Shouldn't need to do this
  424. * really.
  425. */
  426. r = dm_bm_read_lock(pmd->bm, THIN_SUPERBLOCK_LOCATION,
  427. &sb_validator, &sblock);
  428. if (r)
  429. return r;
  430. disk_super = dm_block_data(sblock);
  431. pmd->time = le32_to_cpu(disk_super->time);
  432. pmd->root = le64_to_cpu(disk_super->data_mapping_root);
  433. pmd->details_root = le64_to_cpu(disk_super->device_details_root);
  434. pmd->trans_id = le64_to_cpu(disk_super->trans_id);
  435. pmd->flags = le32_to_cpu(disk_super->flags);
  436. pmd->data_block_size = le32_to_cpu(disk_super->data_block_size);
  437. features = le32_to_cpu(disk_super->incompat_flags) & ~THIN_FEATURE_INCOMPAT_SUPP;
  438. if (features) {
  439. DMERR("could not access metadata due to "
  440. "unsupported optional features (%lx).",
  441. (unsigned long)features);
  442. r = -EINVAL;
  443. goto out;
  444. }
  445. /*
  446. * Check for read-only metadata to skip the following RDWR checks.
  447. */
  448. if (get_disk_ro(pmd->bdev->bd_disk))
  449. goto out;
  450. features = le32_to_cpu(disk_super->compat_ro_flags) & ~THIN_FEATURE_COMPAT_RO_SUPP;
  451. if (features) {
  452. DMERR("could not access metadata RDWR due to "
  453. "unsupported optional features (%lx).",
  454. (unsigned long)features);
  455. r = -EINVAL;
  456. }
  457. out:
  458. dm_bm_unlock(sblock);
  459. return r;
  460. }
  461. static int __write_changed_details(struct dm_pool_metadata *pmd)
  462. {
  463. int r;
  464. struct dm_thin_device *td, *tmp;
  465. struct disk_device_details details;
  466. uint64_t key;
  467. list_for_each_entry_safe(td, tmp, &pmd->thin_devices, list) {
  468. if (!td->changed)
  469. continue;
  470. key = td->id;
  471. details.mapped_blocks = cpu_to_le64(td->mapped_blocks);
  472. details.transaction_id = cpu_to_le64(td->transaction_id);
  473. details.creation_time = cpu_to_le32(td->creation_time);
  474. details.snapshotted_time = cpu_to_le32(td->snapshotted_time);
  475. __dm_bless_for_disk(&details);
  476. r = dm_btree_insert(&pmd->details_info, pmd->details_root,
  477. &key, &details, &pmd->details_root);
  478. if (r)
  479. return r;
  480. if (td->open_count)
  481. td->changed = 0;
  482. else {
  483. list_del(&td->list);
  484. kfree(td);
  485. }
  486. pmd->need_commit = 1;
  487. }
  488. return 0;
  489. }
  490. static int __commit_transaction(struct dm_pool_metadata *pmd)
  491. {
  492. /*
  493. * FIXME: Associated pool should be made read-only on failure.
  494. */
  495. int r;
  496. size_t metadata_len, data_len;
  497. struct thin_disk_superblock *disk_super;
  498. struct dm_block *sblock;
  499. /*
  500. * We need to know if the thin_disk_superblock exceeds a 512-byte sector.
  501. */
  502. BUILD_BUG_ON(sizeof(struct thin_disk_superblock) > 512);
  503. r = __write_changed_details(pmd);
  504. if (r < 0)
  505. goto out;
  506. if (!pmd->need_commit)
  507. goto out;
  508. r = dm_sm_commit(pmd->data_sm);
  509. if (r < 0)
  510. goto out;
  511. r = dm_tm_pre_commit(pmd->tm);
  512. if (r < 0)
  513. goto out;
  514. r = dm_sm_root_size(pmd->metadata_sm, &metadata_len);
  515. if (r < 0)
  516. goto out;
  517. r = dm_sm_root_size(pmd->data_sm, &data_len);
  518. if (r < 0)
  519. goto out;
  520. r = dm_bm_write_lock(pmd->bm, THIN_SUPERBLOCK_LOCATION,
  521. &sb_validator, &sblock);
  522. if (r)
  523. goto out;
  524. disk_super = dm_block_data(sblock);
  525. disk_super->time = cpu_to_le32(pmd->time);
  526. disk_super->data_mapping_root = cpu_to_le64(pmd->root);
  527. disk_super->device_details_root = cpu_to_le64(pmd->details_root);
  528. disk_super->trans_id = cpu_to_le64(pmd->trans_id);
  529. disk_super->flags = cpu_to_le32(pmd->flags);
  530. r = dm_sm_copy_root(pmd->metadata_sm, &disk_super->metadata_space_map_root,
  531. metadata_len);
  532. if (r < 0)
  533. goto out_locked;
  534. r = dm_sm_copy_root(pmd->data_sm, &disk_super->data_space_map_root,
  535. data_len);
  536. if (r < 0)
  537. goto out_locked;
  538. r = dm_tm_commit(pmd->tm, sblock);
  539. if (!r)
  540. pmd->need_commit = 0;
  541. out:
  542. return r;
  543. out_locked:
  544. dm_bm_unlock(sblock);
  545. return r;
  546. }
  547. struct dm_pool_metadata *dm_pool_metadata_open(struct block_device *bdev,
  548. sector_t data_block_size)
  549. {
  550. int r;
  551. struct thin_disk_superblock *disk_super;
  552. struct dm_pool_metadata *pmd;
  553. sector_t bdev_size = i_size_read(bdev->bd_inode) >> SECTOR_SHIFT;
  554. struct dm_block_manager *bm;
  555. int create;
  556. struct dm_block *sblock;
  557. pmd = kmalloc(sizeof(*pmd), GFP_KERNEL);
  558. if (!pmd) {
  559. DMERR("could not allocate metadata struct");
  560. return ERR_PTR(-ENOMEM);
  561. }
  562. /*
  563. * Max hex locks:
  564. * 3 for btree insert +
  565. * 2 for btree lookup used within space map
  566. */
  567. bm = dm_block_manager_create(bdev, THIN_METADATA_BLOCK_SIZE,
  568. THIN_METADATA_CACHE_SIZE, 5);
  569. if (!bm) {
  570. DMERR("could not create block manager");
  571. kfree(pmd);
  572. return ERR_PTR(-ENOMEM);
  573. }
  574. r = superblock_all_zeroes(bm, &create);
  575. if (r) {
  576. dm_block_manager_destroy(bm);
  577. kfree(pmd);
  578. return ERR_PTR(r);
  579. }
  580. r = init_pmd(pmd, bm, 0, create);
  581. if (r) {
  582. dm_block_manager_destroy(bm);
  583. kfree(pmd);
  584. return ERR_PTR(r);
  585. }
  586. pmd->bdev = bdev;
  587. if (!create) {
  588. r = __begin_transaction(pmd);
  589. if (r < 0)
  590. goto bad;
  591. return pmd;
  592. }
  593. /*
  594. * Create.
  595. */
  596. r = dm_bm_write_lock(pmd->bm, THIN_SUPERBLOCK_LOCATION,
  597. &sb_validator, &sblock);
  598. if (r)
  599. goto bad;
  600. if (bdev_size > THIN_METADATA_MAX_SECTORS)
  601. bdev_size = THIN_METADATA_MAX_SECTORS;
  602. disk_super = dm_block_data(sblock);
  603. disk_super->magic = cpu_to_le64(THIN_SUPERBLOCK_MAGIC);
  604. disk_super->version = cpu_to_le32(THIN_VERSION);
  605. disk_super->time = 0;
  606. disk_super->metadata_block_size = cpu_to_le32(THIN_METADATA_BLOCK_SIZE >> SECTOR_SHIFT);
  607. disk_super->metadata_nr_blocks = cpu_to_le64(bdev_size >> SECTOR_TO_BLOCK_SHIFT);
  608. disk_super->data_block_size = cpu_to_le32(data_block_size);
  609. r = dm_bm_unlock(sblock);
  610. if (r < 0)
  611. goto bad;
  612. r = dm_btree_empty(&pmd->info, &pmd->root);
  613. if (r < 0)
  614. goto bad;
  615. r = dm_btree_empty(&pmd->details_info, &pmd->details_root);
  616. if (r < 0) {
  617. DMERR("couldn't create devices root");
  618. goto bad;
  619. }
  620. pmd->flags = 0;
  621. pmd->need_commit = 1;
  622. r = dm_pool_commit_metadata(pmd);
  623. if (r < 0) {
  624. DMERR("%s: dm_pool_commit_metadata() failed, error = %d",
  625. __func__, r);
  626. goto bad;
  627. }
  628. return pmd;
  629. bad:
  630. if (dm_pool_metadata_close(pmd) < 0)
  631. DMWARN("%s: dm_pool_metadata_close() failed.", __func__);
  632. return ERR_PTR(r);
  633. }
  634. int dm_pool_metadata_close(struct dm_pool_metadata *pmd)
  635. {
  636. int r;
  637. unsigned open_devices = 0;
  638. struct dm_thin_device *td, *tmp;
  639. down_read(&pmd->root_lock);
  640. list_for_each_entry_safe(td, tmp, &pmd->thin_devices, list) {
  641. if (td->open_count)
  642. open_devices++;
  643. else {
  644. list_del(&td->list);
  645. kfree(td);
  646. }
  647. }
  648. up_read(&pmd->root_lock);
  649. if (open_devices) {
  650. DMERR("attempt to close pmd when %u device(s) are still open",
  651. open_devices);
  652. return -EBUSY;
  653. }
  654. r = __commit_transaction(pmd);
  655. if (r < 0)
  656. DMWARN("%s: __commit_transaction() failed, error = %d",
  657. __func__, r);
  658. dm_tm_destroy(pmd->tm);
  659. dm_tm_destroy(pmd->nb_tm);
  660. dm_block_manager_destroy(pmd->bm);
  661. dm_sm_destroy(pmd->metadata_sm);
  662. dm_sm_destroy(pmd->data_sm);
  663. kfree(pmd);
  664. return 0;
  665. }
  666. /*
  667. * __open_device: Returns @td corresponding to device with id @dev,
  668. * creating it if @create is set and incrementing @td->open_count.
  669. * On failure, @td is undefined.
  670. */
  671. static int __open_device(struct dm_pool_metadata *pmd,
  672. dm_thin_id dev, int create,
  673. struct dm_thin_device **td)
  674. {
  675. int r, changed = 0;
  676. struct dm_thin_device *td2;
  677. uint64_t key = dev;
  678. struct disk_device_details details_le;
  679. /*
  680. * If the device is already open, return it.
  681. */
  682. list_for_each_entry(td2, &pmd->thin_devices, list)
  683. if (td2->id == dev) {
  684. /*
  685. * May not create an already-open device.
  686. */
  687. if (create)
  688. return -EEXIST;
  689. td2->open_count++;
  690. *td = td2;
  691. return 0;
  692. }
  693. /*
  694. * Check the device exists.
  695. */
  696. r = dm_btree_lookup(&pmd->details_info, pmd->details_root,
  697. &key, &details_le);
  698. if (r) {
  699. if (r != -ENODATA || !create)
  700. return r;
  701. /*
  702. * Create new device.
  703. */
  704. changed = 1;
  705. details_le.mapped_blocks = 0;
  706. details_le.transaction_id = cpu_to_le64(pmd->trans_id);
  707. details_le.creation_time = cpu_to_le32(pmd->time);
  708. details_le.snapshotted_time = cpu_to_le32(pmd->time);
  709. }
  710. *td = kmalloc(sizeof(**td), GFP_NOIO);
  711. if (!*td)
  712. return -ENOMEM;
  713. (*td)->pmd = pmd;
  714. (*td)->id = dev;
  715. (*td)->open_count = 1;
  716. (*td)->changed = changed;
  717. (*td)->mapped_blocks = le64_to_cpu(details_le.mapped_blocks);
  718. (*td)->transaction_id = le64_to_cpu(details_le.transaction_id);
  719. (*td)->creation_time = le32_to_cpu(details_le.creation_time);
  720. (*td)->snapshotted_time = le32_to_cpu(details_le.snapshotted_time);
  721. list_add(&(*td)->list, &pmd->thin_devices);
  722. return 0;
  723. }
  724. static void __close_device(struct dm_thin_device *td)
  725. {
  726. --td->open_count;
  727. }
  728. static int __create_thin(struct dm_pool_metadata *pmd,
  729. dm_thin_id dev)
  730. {
  731. int r;
  732. dm_block_t dev_root;
  733. uint64_t key = dev;
  734. struct disk_device_details details_le;
  735. struct dm_thin_device *td;
  736. __le64 value;
  737. r = dm_btree_lookup(&pmd->details_info, pmd->details_root,
  738. &key, &details_le);
  739. if (!r)
  740. return -EEXIST;
  741. /*
  742. * Create an empty btree for the mappings.
  743. */
  744. r = dm_btree_empty(&pmd->bl_info, &dev_root);
  745. if (r)
  746. return r;
  747. /*
  748. * Insert it into the main mapping tree.
  749. */
  750. value = cpu_to_le64(dev_root);
  751. __dm_bless_for_disk(&value);
  752. r = dm_btree_insert(&pmd->tl_info, pmd->root, &key, &value, &pmd->root);
  753. if (r) {
  754. dm_btree_del(&pmd->bl_info, dev_root);
  755. return r;
  756. }
  757. r = __open_device(pmd, dev, 1, &td);
  758. if (r) {
  759. dm_btree_remove(&pmd->tl_info, pmd->root, &key, &pmd->root);
  760. dm_btree_del(&pmd->bl_info, dev_root);
  761. return r;
  762. }
  763. __close_device(td);
  764. return r;
  765. }
  766. int dm_pool_create_thin(struct dm_pool_metadata *pmd, dm_thin_id dev)
  767. {
  768. int r;
  769. down_write(&pmd->root_lock);
  770. r = __create_thin(pmd, dev);
  771. up_write(&pmd->root_lock);
  772. return r;
  773. }
  774. static int __set_snapshot_details(struct dm_pool_metadata *pmd,
  775. struct dm_thin_device *snap,
  776. dm_thin_id origin, uint32_t time)
  777. {
  778. int r;
  779. struct dm_thin_device *td;
  780. r = __open_device(pmd, origin, 0, &td);
  781. if (r)
  782. return r;
  783. td->changed = 1;
  784. td->snapshotted_time = time;
  785. snap->mapped_blocks = td->mapped_blocks;
  786. snap->snapshotted_time = time;
  787. __close_device(td);
  788. return 0;
  789. }
  790. static int __create_snap(struct dm_pool_metadata *pmd,
  791. dm_thin_id dev, dm_thin_id origin)
  792. {
  793. int r;
  794. dm_block_t origin_root;
  795. uint64_t key = origin, dev_key = dev;
  796. struct dm_thin_device *td;
  797. struct disk_device_details details_le;
  798. __le64 value;
  799. /* check this device is unused */
  800. r = dm_btree_lookup(&pmd->details_info, pmd->details_root,
  801. &dev_key, &details_le);
  802. if (!r)
  803. return -EEXIST;
  804. /* find the mapping tree for the origin */
  805. r = dm_btree_lookup(&pmd->tl_info, pmd->root, &key, &value);
  806. if (r)
  807. return r;
  808. origin_root = le64_to_cpu(value);
  809. /* clone the origin, an inc will do */
  810. dm_tm_inc(pmd->tm, origin_root);
  811. /* insert into the main mapping tree */
  812. value = cpu_to_le64(origin_root);
  813. __dm_bless_for_disk(&value);
  814. key = dev;
  815. r = dm_btree_insert(&pmd->tl_info, pmd->root, &key, &value, &pmd->root);
  816. if (r) {
  817. dm_tm_dec(pmd->tm, origin_root);
  818. return r;
  819. }
  820. pmd->time++;
  821. r = __open_device(pmd, dev, 1, &td);
  822. if (r)
  823. goto bad;
  824. r = __set_snapshot_details(pmd, td, origin, pmd->time);
  825. __close_device(td);
  826. if (r)
  827. goto bad;
  828. return 0;
  829. bad:
  830. dm_btree_remove(&pmd->tl_info, pmd->root, &key, &pmd->root);
  831. dm_btree_remove(&pmd->details_info, pmd->details_root,
  832. &key, &pmd->details_root);
  833. return r;
  834. }
  835. int dm_pool_create_snap(struct dm_pool_metadata *pmd,
  836. dm_thin_id dev,
  837. dm_thin_id origin)
  838. {
  839. int r;
  840. down_write(&pmd->root_lock);
  841. r = __create_snap(pmd, dev, origin);
  842. up_write(&pmd->root_lock);
  843. return r;
  844. }
  845. static int __delete_device(struct dm_pool_metadata *pmd, dm_thin_id dev)
  846. {
  847. int r;
  848. uint64_t key = dev;
  849. struct dm_thin_device *td;
  850. /* TODO: failure should mark the transaction invalid */
  851. r = __open_device(pmd, dev, 0, &td);
  852. if (r)
  853. return r;
  854. if (td->open_count > 1) {
  855. __close_device(td);
  856. return -EBUSY;
  857. }
  858. list_del(&td->list);
  859. kfree(td);
  860. r = dm_btree_remove(&pmd->details_info, pmd->details_root,
  861. &key, &pmd->details_root);
  862. if (r)
  863. return r;
  864. r = dm_btree_remove(&pmd->tl_info, pmd->root, &key, &pmd->root);
  865. if (r)
  866. return r;
  867. pmd->need_commit = 1;
  868. return 0;
  869. }
  870. int dm_pool_delete_thin_device(struct dm_pool_metadata *pmd,
  871. dm_thin_id dev)
  872. {
  873. int r;
  874. down_write(&pmd->root_lock);
  875. r = __delete_device(pmd, dev);
  876. up_write(&pmd->root_lock);
  877. return r;
  878. }
  879. int dm_pool_set_metadata_transaction_id(struct dm_pool_metadata *pmd,
  880. uint64_t current_id,
  881. uint64_t new_id)
  882. {
  883. down_write(&pmd->root_lock);
  884. if (pmd->trans_id != current_id) {
  885. up_write(&pmd->root_lock);
  886. DMERR("mismatched transaction id");
  887. return -EINVAL;
  888. }
  889. pmd->trans_id = new_id;
  890. pmd->need_commit = 1;
  891. up_write(&pmd->root_lock);
  892. return 0;
  893. }
  894. int dm_pool_get_metadata_transaction_id(struct dm_pool_metadata *pmd,
  895. uint64_t *result)
  896. {
  897. down_read(&pmd->root_lock);
  898. *result = pmd->trans_id;
  899. up_read(&pmd->root_lock);
  900. return 0;
  901. }
  902. static int __get_held_metadata_root(struct dm_pool_metadata *pmd,
  903. dm_block_t *result)
  904. {
  905. int r;
  906. struct thin_disk_superblock *disk_super;
  907. struct dm_block *sblock;
  908. r = dm_bm_write_lock(pmd->bm, THIN_SUPERBLOCK_LOCATION,
  909. &sb_validator, &sblock);
  910. if (r)
  911. return r;
  912. disk_super = dm_block_data(sblock);
  913. *result = le64_to_cpu(disk_super->held_root);
  914. return dm_bm_unlock(sblock);
  915. }
  916. int dm_pool_get_held_metadata_root(struct dm_pool_metadata *pmd,
  917. dm_block_t *result)
  918. {
  919. int r;
  920. down_read(&pmd->root_lock);
  921. r = __get_held_metadata_root(pmd, result);
  922. up_read(&pmd->root_lock);
  923. return r;
  924. }
  925. int dm_pool_open_thin_device(struct dm_pool_metadata *pmd, dm_thin_id dev,
  926. struct dm_thin_device **td)
  927. {
  928. int r;
  929. down_write(&pmd->root_lock);
  930. r = __open_device(pmd, dev, 0, td);
  931. up_write(&pmd->root_lock);
  932. return r;
  933. }
  934. int dm_pool_close_thin_device(struct dm_thin_device *td)
  935. {
  936. down_write(&td->pmd->root_lock);
  937. __close_device(td);
  938. up_write(&td->pmd->root_lock);
  939. return 0;
  940. }
  941. dm_thin_id dm_thin_dev_id(struct dm_thin_device *td)
  942. {
  943. return td->id;
  944. }
  945. static int __snapshotted_since(struct dm_thin_device *td, uint32_t time)
  946. {
  947. return td->snapshotted_time > time;
  948. }
  949. int dm_thin_find_block(struct dm_thin_device *td, dm_block_t block,
  950. int can_block, struct dm_thin_lookup_result *result)
  951. {
  952. int r;
  953. uint64_t block_time = 0;
  954. __le64 value;
  955. struct dm_pool_metadata *pmd = td->pmd;
  956. dm_block_t keys[2] = { td->id, block };
  957. if (can_block) {
  958. down_read(&pmd->root_lock);
  959. r = dm_btree_lookup(&pmd->info, pmd->root, keys, &value);
  960. if (!r)
  961. block_time = le64_to_cpu(value);
  962. up_read(&pmd->root_lock);
  963. } else if (down_read_trylock(&pmd->root_lock)) {
  964. r = dm_btree_lookup(&pmd->nb_info, pmd->root, keys, &value);
  965. if (!r)
  966. block_time = le64_to_cpu(value);
  967. up_read(&pmd->root_lock);
  968. } else
  969. return -EWOULDBLOCK;
  970. if (!r) {
  971. dm_block_t exception_block;
  972. uint32_t exception_time;
  973. unpack_block_time(block_time, &exception_block,
  974. &exception_time);
  975. result->block = exception_block;
  976. result->shared = __snapshotted_since(td, exception_time);
  977. }
  978. return r;
  979. }
  980. static int __insert(struct dm_thin_device *td, dm_block_t block,
  981. dm_block_t data_block)
  982. {
  983. int r, inserted;
  984. __le64 value;
  985. struct dm_pool_metadata *pmd = td->pmd;
  986. dm_block_t keys[2] = { td->id, block };
  987. pmd->need_commit = 1;
  988. value = cpu_to_le64(pack_block_time(data_block, pmd->time));
  989. __dm_bless_for_disk(&value);
  990. r = dm_btree_insert_notify(&pmd->info, pmd->root, keys, &value,
  991. &pmd->root, &inserted);
  992. if (r)
  993. return r;
  994. if (inserted) {
  995. td->mapped_blocks++;
  996. td->changed = 1;
  997. }
  998. return 0;
  999. }
  1000. int dm_thin_insert_block(struct dm_thin_device *td, dm_block_t block,
  1001. dm_block_t data_block)
  1002. {
  1003. int r;
  1004. down_write(&td->pmd->root_lock);
  1005. r = __insert(td, block, data_block);
  1006. up_write(&td->pmd->root_lock);
  1007. return r;
  1008. }
  1009. static int __remove(struct dm_thin_device *td, dm_block_t block)
  1010. {
  1011. int r;
  1012. struct dm_pool_metadata *pmd = td->pmd;
  1013. dm_block_t keys[2] = { td->id, block };
  1014. r = dm_btree_remove(&pmd->info, pmd->root, keys, &pmd->root);
  1015. if (r)
  1016. return r;
  1017. td->mapped_blocks--;
  1018. td->changed = 1;
  1019. pmd->need_commit = 1;
  1020. return 0;
  1021. }
  1022. int dm_thin_remove_block(struct dm_thin_device *td, dm_block_t block)
  1023. {
  1024. int r;
  1025. down_write(&td->pmd->root_lock);
  1026. r = __remove(td, block);
  1027. up_write(&td->pmd->root_lock);
  1028. return r;
  1029. }
  1030. int dm_pool_alloc_data_block(struct dm_pool_metadata *pmd, dm_block_t *result)
  1031. {
  1032. int r;
  1033. down_write(&pmd->root_lock);
  1034. r = dm_sm_new_block(pmd->data_sm, result);
  1035. pmd->need_commit = 1;
  1036. up_write(&pmd->root_lock);
  1037. return r;
  1038. }
  1039. int dm_pool_commit_metadata(struct dm_pool_metadata *pmd)
  1040. {
  1041. int r;
  1042. down_write(&pmd->root_lock);
  1043. r = __commit_transaction(pmd);
  1044. if (r <= 0)
  1045. goto out;
  1046. /*
  1047. * Open the next transaction.
  1048. */
  1049. r = __begin_transaction(pmd);
  1050. out:
  1051. up_write(&pmd->root_lock);
  1052. return r;
  1053. }
  1054. int dm_pool_get_free_block_count(struct dm_pool_metadata *pmd, dm_block_t *result)
  1055. {
  1056. int r;
  1057. down_read(&pmd->root_lock);
  1058. r = dm_sm_get_nr_free(pmd->data_sm, result);
  1059. up_read(&pmd->root_lock);
  1060. return r;
  1061. }
  1062. int dm_pool_get_free_metadata_block_count(struct dm_pool_metadata *pmd,
  1063. dm_block_t *result)
  1064. {
  1065. int r;
  1066. down_read(&pmd->root_lock);
  1067. r = dm_sm_get_nr_free(pmd->metadata_sm, result);
  1068. up_read(&pmd->root_lock);
  1069. return r;
  1070. }
  1071. int dm_pool_get_metadata_dev_size(struct dm_pool_metadata *pmd,
  1072. dm_block_t *result)
  1073. {
  1074. int r;
  1075. down_read(&pmd->root_lock);
  1076. r = dm_sm_get_nr_blocks(pmd->metadata_sm, result);
  1077. up_read(&pmd->root_lock);
  1078. return r;
  1079. }
  1080. int dm_pool_get_data_block_size(struct dm_pool_metadata *pmd, sector_t *result)
  1081. {
  1082. down_read(&pmd->root_lock);
  1083. *result = pmd->data_block_size;
  1084. up_read(&pmd->root_lock);
  1085. return 0;
  1086. }
  1087. int dm_pool_get_data_dev_size(struct dm_pool_metadata *pmd, dm_block_t *result)
  1088. {
  1089. int r;
  1090. down_read(&pmd->root_lock);
  1091. r = dm_sm_get_nr_blocks(pmd->data_sm, result);
  1092. up_read(&pmd->root_lock);
  1093. return r;
  1094. }
  1095. int dm_thin_get_mapped_count(struct dm_thin_device *td, dm_block_t *result)
  1096. {
  1097. struct dm_pool_metadata *pmd = td->pmd;
  1098. down_read(&pmd->root_lock);
  1099. *result = td->mapped_blocks;
  1100. up_read(&pmd->root_lock);
  1101. return 0;
  1102. }
  1103. static int __highest_block(struct dm_thin_device *td, dm_block_t *result)
  1104. {
  1105. int r;
  1106. __le64 value_le;
  1107. dm_block_t thin_root;
  1108. struct dm_pool_metadata *pmd = td->pmd;
  1109. r = dm_btree_lookup(&pmd->tl_info, pmd->root, &td->id, &value_le);
  1110. if (r)
  1111. return r;
  1112. thin_root = le64_to_cpu(value_le);
  1113. return dm_btree_find_highest_key(&pmd->bl_info, thin_root, result);
  1114. }
  1115. int dm_thin_get_highest_mapped_block(struct dm_thin_device *td,
  1116. dm_block_t *result)
  1117. {
  1118. int r;
  1119. struct dm_pool_metadata *pmd = td->pmd;
  1120. down_read(&pmd->root_lock);
  1121. r = __highest_block(td, result);
  1122. up_read(&pmd->root_lock);
  1123. return r;
  1124. }
  1125. static int __resize_data_dev(struct dm_pool_metadata *pmd, dm_block_t new_count)
  1126. {
  1127. int r;
  1128. dm_block_t old_count;
  1129. r = dm_sm_get_nr_blocks(pmd->data_sm, &old_count);
  1130. if (r)
  1131. return r;
  1132. if (new_count == old_count)
  1133. return 0;
  1134. if (new_count < old_count) {
  1135. DMERR("cannot reduce size of data device");
  1136. return -EINVAL;
  1137. }
  1138. r = dm_sm_extend(pmd->data_sm, new_count - old_count);
  1139. if (!r)
  1140. pmd->need_commit = 1;
  1141. return r;
  1142. }
  1143. int dm_pool_resize_data_dev(struct dm_pool_metadata *pmd, dm_block_t new_count)
  1144. {
  1145. int r;
  1146. down_write(&pmd->root_lock);
  1147. r = __resize_data_dev(pmd, new_count);
  1148. up_write(&pmd->root_lock);
  1149. return r;
  1150. }