dm-thin-metadata.c 31 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391
  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. r = PTR_ERR(data_sm);
  328. goto bad;
  329. }
  330. } else {
  331. struct thin_disk_superblock *disk_super = NULL;
  332. size_t space_map_root_offset =
  333. offsetof(struct thin_disk_superblock, metadata_space_map_root);
  334. r = dm_tm_open_with_sm(bm, THIN_SUPERBLOCK_LOCATION,
  335. &sb_validator, space_map_root_offset,
  336. SPACE_MAP_ROOT_SIZE, &tm, &sm, &sblock);
  337. if (r < 0) {
  338. DMERR("tm_open_with_sm failed");
  339. return r;
  340. }
  341. disk_super = dm_block_data(sblock);
  342. data_sm = dm_sm_disk_open(tm, disk_super->data_space_map_root,
  343. sizeof(disk_super->data_space_map_root));
  344. if (IS_ERR(data_sm)) {
  345. DMERR("sm_disk_open failed");
  346. r = PTR_ERR(data_sm);
  347. goto bad;
  348. }
  349. }
  350. r = dm_tm_unlock(tm, sblock);
  351. if (r < 0) {
  352. DMERR("couldn't unlock superblock");
  353. goto bad_data_sm;
  354. }
  355. pmd->bm = bm;
  356. pmd->metadata_sm = sm;
  357. pmd->data_sm = data_sm;
  358. pmd->tm = tm;
  359. pmd->nb_tm = dm_tm_create_non_blocking_clone(tm);
  360. if (!pmd->nb_tm) {
  361. DMERR("could not create clone tm");
  362. r = -ENOMEM;
  363. goto bad_data_sm;
  364. }
  365. pmd->info.tm = tm;
  366. pmd->info.levels = 2;
  367. pmd->info.value_type.context = pmd->data_sm;
  368. pmd->info.value_type.size = sizeof(__le64);
  369. pmd->info.value_type.inc = data_block_inc;
  370. pmd->info.value_type.dec = data_block_dec;
  371. pmd->info.value_type.equal = data_block_equal;
  372. memcpy(&pmd->nb_info, &pmd->info, sizeof(pmd->nb_info));
  373. pmd->nb_info.tm = pmd->nb_tm;
  374. pmd->tl_info.tm = tm;
  375. pmd->tl_info.levels = 1;
  376. pmd->tl_info.value_type.context = &pmd->info;
  377. pmd->tl_info.value_type.size = sizeof(__le64);
  378. pmd->tl_info.value_type.inc = subtree_inc;
  379. pmd->tl_info.value_type.dec = subtree_dec;
  380. pmd->tl_info.value_type.equal = subtree_equal;
  381. pmd->bl_info.tm = tm;
  382. pmd->bl_info.levels = 1;
  383. pmd->bl_info.value_type.context = pmd->data_sm;
  384. pmd->bl_info.value_type.size = sizeof(__le64);
  385. pmd->bl_info.value_type.inc = data_block_inc;
  386. pmd->bl_info.value_type.dec = data_block_dec;
  387. pmd->bl_info.value_type.equal = data_block_equal;
  388. pmd->details_info.tm = tm;
  389. pmd->details_info.levels = 1;
  390. pmd->details_info.value_type.context = NULL;
  391. pmd->details_info.value_type.size = sizeof(struct disk_device_details);
  392. pmd->details_info.value_type.inc = NULL;
  393. pmd->details_info.value_type.dec = NULL;
  394. pmd->details_info.value_type.equal = NULL;
  395. pmd->root = 0;
  396. init_rwsem(&pmd->root_lock);
  397. pmd->time = 0;
  398. pmd->need_commit = 0;
  399. pmd->details_root = 0;
  400. pmd->trans_id = 0;
  401. pmd->flags = 0;
  402. INIT_LIST_HEAD(&pmd->thin_devices);
  403. return 0;
  404. bad_data_sm:
  405. dm_sm_destroy(data_sm);
  406. bad:
  407. dm_tm_destroy(tm);
  408. dm_sm_destroy(sm);
  409. return r;
  410. }
  411. static int __begin_transaction(struct dm_pool_metadata *pmd)
  412. {
  413. int r;
  414. u32 features;
  415. struct thin_disk_superblock *disk_super;
  416. struct dm_block *sblock;
  417. /*
  418. * __maybe_commit_transaction() resets these
  419. */
  420. WARN_ON(pmd->need_commit);
  421. /*
  422. * We re-read the superblock every time. Shouldn't need to do this
  423. * really.
  424. */
  425. r = dm_bm_read_lock(pmd->bm, THIN_SUPERBLOCK_LOCATION,
  426. &sb_validator, &sblock);
  427. if (r)
  428. return r;
  429. disk_super = dm_block_data(sblock);
  430. pmd->time = le32_to_cpu(disk_super->time);
  431. pmd->root = le64_to_cpu(disk_super->data_mapping_root);
  432. pmd->details_root = le64_to_cpu(disk_super->device_details_root);
  433. pmd->trans_id = le64_to_cpu(disk_super->trans_id);
  434. pmd->flags = le32_to_cpu(disk_super->flags);
  435. pmd->data_block_size = le32_to_cpu(disk_super->data_block_size);
  436. features = le32_to_cpu(disk_super->incompat_flags) & ~THIN_FEATURE_INCOMPAT_SUPP;
  437. if (features) {
  438. DMERR("could not access metadata due to "
  439. "unsupported optional features (%lx).",
  440. (unsigned long)features);
  441. r = -EINVAL;
  442. goto out;
  443. }
  444. /*
  445. * Check for read-only metadata to skip the following RDWR checks.
  446. */
  447. if (get_disk_ro(pmd->bdev->bd_disk))
  448. goto out;
  449. features = le32_to_cpu(disk_super->compat_ro_flags) & ~THIN_FEATURE_COMPAT_RO_SUPP;
  450. if (features) {
  451. DMERR("could not access metadata RDWR due to "
  452. "unsupported optional features (%lx).",
  453. (unsigned long)features);
  454. r = -EINVAL;
  455. }
  456. out:
  457. dm_bm_unlock(sblock);
  458. return r;
  459. }
  460. static int __write_changed_details(struct dm_pool_metadata *pmd)
  461. {
  462. int r;
  463. struct dm_thin_device *td, *tmp;
  464. struct disk_device_details details;
  465. uint64_t key;
  466. list_for_each_entry_safe(td, tmp, &pmd->thin_devices, list) {
  467. if (!td->changed)
  468. continue;
  469. key = td->id;
  470. details.mapped_blocks = cpu_to_le64(td->mapped_blocks);
  471. details.transaction_id = cpu_to_le64(td->transaction_id);
  472. details.creation_time = cpu_to_le32(td->creation_time);
  473. details.snapshotted_time = cpu_to_le32(td->snapshotted_time);
  474. __dm_bless_for_disk(&details);
  475. r = dm_btree_insert(&pmd->details_info, pmd->details_root,
  476. &key, &details, &pmd->details_root);
  477. if (r)
  478. return r;
  479. if (td->open_count)
  480. td->changed = 0;
  481. else {
  482. list_del(&td->list);
  483. kfree(td);
  484. }
  485. pmd->need_commit = 1;
  486. }
  487. return 0;
  488. }
  489. static int __commit_transaction(struct dm_pool_metadata *pmd)
  490. {
  491. /*
  492. * FIXME: Associated pool should be made read-only on failure.
  493. */
  494. int r;
  495. size_t metadata_len, data_len;
  496. struct thin_disk_superblock *disk_super;
  497. struct dm_block *sblock;
  498. /*
  499. * We need to know if the thin_disk_superblock exceeds a 512-byte sector.
  500. */
  501. BUILD_BUG_ON(sizeof(struct thin_disk_superblock) > 512);
  502. r = __write_changed_details(pmd);
  503. if (r < 0)
  504. goto out;
  505. if (!pmd->need_commit)
  506. goto out;
  507. r = dm_sm_commit(pmd->data_sm);
  508. if (r < 0)
  509. goto out;
  510. r = dm_tm_pre_commit(pmd->tm);
  511. if (r < 0)
  512. goto out;
  513. r = dm_sm_root_size(pmd->metadata_sm, &metadata_len);
  514. if (r < 0)
  515. goto out;
  516. r = dm_sm_root_size(pmd->metadata_sm, &data_len);
  517. if (r < 0)
  518. goto out;
  519. r = dm_bm_write_lock(pmd->bm, THIN_SUPERBLOCK_LOCATION,
  520. &sb_validator, &sblock);
  521. if (r)
  522. goto out;
  523. disk_super = dm_block_data(sblock);
  524. disk_super->time = cpu_to_le32(pmd->time);
  525. disk_super->data_mapping_root = cpu_to_le64(pmd->root);
  526. disk_super->device_details_root = cpu_to_le64(pmd->details_root);
  527. disk_super->trans_id = cpu_to_le64(pmd->trans_id);
  528. disk_super->flags = cpu_to_le32(pmd->flags);
  529. r = dm_sm_copy_root(pmd->metadata_sm, &disk_super->metadata_space_map_root,
  530. metadata_len);
  531. if (r < 0)
  532. goto out_locked;
  533. r = dm_sm_copy_root(pmd->data_sm, &disk_super->data_space_map_root,
  534. data_len);
  535. if (r < 0)
  536. goto out_locked;
  537. r = dm_tm_commit(pmd->tm, sblock);
  538. if (!r)
  539. pmd->need_commit = 0;
  540. out:
  541. return r;
  542. out_locked:
  543. dm_bm_unlock(sblock);
  544. return r;
  545. }
  546. struct dm_pool_metadata *dm_pool_metadata_open(struct block_device *bdev,
  547. sector_t data_block_size)
  548. {
  549. int r;
  550. struct thin_disk_superblock *disk_super;
  551. struct dm_pool_metadata *pmd;
  552. sector_t bdev_size = i_size_read(bdev->bd_inode) >> SECTOR_SHIFT;
  553. struct dm_block_manager *bm;
  554. int create;
  555. struct dm_block *sblock;
  556. pmd = kmalloc(sizeof(*pmd), GFP_KERNEL);
  557. if (!pmd) {
  558. DMERR("could not allocate metadata struct");
  559. return ERR_PTR(-ENOMEM);
  560. }
  561. /*
  562. * Max hex locks:
  563. * 3 for btree insert +
  564. * 2 for btree lookup used within space map
  565. */
  566. bm = dm_block_manager_create(bdev, THIN_METADATA_BLOCK_SIZE,
  567. THIN_METADATA_CACHE_SIZE, 5);
  568. if (!bm) {
  569. DMERR("could not create block manager");
  570. kfree(pmd);
  571. return ERR_PTR(-ENOMEM);
  572. }
  573. r = superblock_all_zeroes(bm, &create);
  574. if (r) {
  575. dm_block_manager_destroy(bm);
  576. kfree(pmd);
  577. return ERR_PTR(r);
  578. }
  579. r = init_pmd(pmd, bm, 0, create);
  580. if (r) {
  581. dm_block_manager_destroy(bm);
  582. kfree(pmd);
  583. return ERR_PTR(r);
  584. }
  585. pmd->bdev = bdev;
  586. if (!create) {
  587. r = __begin_transaction(pmd);
  588. if (r < 0)
  589. goto bad;
  590. return pmd;
  591. }
  592. /*
  593. * Create.
  594. */
  595. r = dm_bm_write_lock(pmd->bm, THIN_SUPERBLOCK_LOCATION,
  596. &sb_validator, &sblock);
  597. if (r)
  598. goto bad;
  599. disk_super = dm_block_data(sblock);
  600. disk_super->magic = cpu_to_le64(THIN_SUPERBLOCK_MAGIC);
  601. disk_super->version = cpu_to_le32(THIN_VERSION);
  602. disk_super->time = 0;
  603. disk_super->metadata_block_size = cpu_to_le32(THIN_METADATA_BLOCK_SIZE >> SECTOR_SHIFT);
  604. disk_super->metadata_nr_blocks = cpu_to_le64(bdev_size >> SECTOR_TO_BLOCK_SHIFT);
  605. disk_super->data_block_size = cpu_to_le32(data_block_size);
  606. r = dm_bm_unlock(sblock);
  607. if (r < 0)
  608. goto bad;
  609. r = dm_btree_empty(&pmd->info, &pmd->root);
  610. if (r < 0)
  611. goto bad;
  612. r = dm_btree_empty(&pmd->details_info, &pmd->details_root);
  613. if (r < 0) {
  614. DMERR("couldn't create devices root");
  615. goto bad;
  616. }
  617. pmd->flags = 0;
  618. pmd->need_commit = 1;
  619. r = dm_pool_commit_metadata(pmd);
  620. if (r < 0) {
  621. DMERR("%s: dm_pool_commit_metadata() failed, error = %d",
  622. __func__, r);
  623. goto bad;
  624. }
  625. return pmd;
  626. bad:
  627. if (dm_pool_metadata_close(pmd) < 0)
  628. DMWARN("%s: dm_pool_metadata_close() failed.", __func__);
  629. return ERR_PTR(r);
  630. }
  631. int dm_pool_metadata_close(struct dm_pool_metadata *pmd)
  632. {
  633. int r;
  634. unsigned open_devices = 0;
  635. struct dm_thin_device *td, *tmp;
  636. down_read(&pmd->root_lock);
  637. list_for_each_entry_safe(td, tmp, &pmd->thin_devices, list) {
  638. if (td->open_count)
  639. open_devices++;
  640. else {
  641. list_del(&td->list);
  642. kfree(td);
  643. }
  644. }
  645. up_read(&pmd->root_lock);
  646. if (open_devices) {
  647. DMERR("attempt to close pmd when %u device(s) are still open",
  648. open_devices);
  649. return -EBUSY;
  650. }
  651. r = __commit_transaction(pmd);
  652. if (r < 0)
  653. DMWARN("%s: __commit_transaction() failed, error = %d",
  654. __func__, r);
  655. dm_tm_destroy(pmd->tm);
  656. dm_tm_destroy(pmd->nb_tm);
  657. dm_block_manager_destroy(pmd->bm);
  658. dm_sm_destroy(pmd->metadata_sm);
  659. dm_sm_destroy(pmd->data_sm);
  660. kfree(pmd);
  661. return 0;
  662. }
  663. static int __open_device(struct dm_pool_metadata *pmd,
  664. dm_thin_id dev, int create,
  665. struct dm_thin_device **td)
  666. {
  667. int r, changed = 0;
  668. struct dm_thin_device *td2;
  669. uint64_t key = dev;
  670. struct disk_device_details details_le;
  671. /*
  672. * Check the device isn't already open.
  673. */
  674. list_for_each_entry(td2, &pmd->thin_devices, list)
  675. if (td2->id == dev) {
  676. td2->open_count++;
  677. *td = td2;
  678. return 0;
  679. }
  680. /*
  681. * Check the device exists.
  682. */
  683. r = dm_btree_lookup(&pmd->details_info, pmd->details_root,
  684. &key, &details_le);
  685. if (r) {
  686. if (r != -ENODATA || !create)
  687. return r;
  688. changed = 1;
  689. details_le.mapped_blocks = 0;
  690. details_le.transaction_id = cpu_to_le64(pmd->trans_id);
  691. details_le.creation_time = cpu_to_le32(pmd->time);
  692. details_le.snapshotted_time = cpu_to_le32(pmd->time);
  693. }
  694. *td = kmalloc(sizeof(**td), GFP_NOIO);
  695. if (!*td)
  696. return -ENOMEM;
  697. (*td)->pmd = pmd;
  698. (*td)->id = dev;
  699. (*td)->open_count = 1;
  700. (*td)->changed = changed;
  701. (*td)->mapped_blocks = le64_to_cpu(details_le.mapped_blocks);
  702. (*td)->transaction_id = le64_to_cpu(details_le.transaction_id);
  703. (*td)->creation_time = le32_to_cpu(details_le.creation_time);
  704. (*td)->snapshotted_time = le32_to_cpu(details_le.snapshotted_time);
  705. list_add(&(*td)->list, &pmd->thin_devices);
  706. return 0;
  707. }
  708. static void __close_device(struct dm_thin_device *td)
  709. {
  710. --td->open_count;
  711. }
  712. static int __create_thin(struct dm_pool_metadata *pmd,
  713. dm_thin_id dev)
  714. {
  715. int r;
  716. dm_block_t dev_root;
  717. uint64_t key = dev;
  718. struct disk_device_details details_le;
  719. struct dm_thin_device *td;
  720. __le64 value;
  721. r = dm_btree_lookup(&pmd->details_info, pmd->details_root,
  722. &key, &details_le);
  723. if (!r)
  724. return -EEXIST;
  725. /*
  726. * Create an empty btree for the mappings.
  727. */
  728. r = dm_btree_empty(&pmd->bl_info, &dev_root);
  729. if (r)
  730. return r;
  731. /*
  732. * Insert it into the main mapping tree.
  733. */
  734. value = cpu_to_le64(dev_root);
  735. __dm_bless_for_disk(&value);
  736. r = dm_btree_insert(&pmd->tl_info, pmd->root, &key, &value, &pmd->root);
  737. if (r) {
  738. dm_btree_del(&pmd->bl_info, dev_root);
  739. return r;
  740. }
  741. r = __open_device(pmd, dev, 1, &td);
  742. if (r) {
  743. __close_device(td);
  744. dm_btree_remove(&pmd->tl_info, pmd->root, &key, &pmd->root);
  745. dm_btree_del(&pmd->bl_info, dev_root);
  746. return r;
  747. }
  748. td->changed = 1;
  749. __close_device(td);
  750. return r;
  751. }
  752. int dm_pool_create_thin(struct dm_pool_metadata *pmd, dm_thin_id dev)
  753. {
  754. int r;
  755. down_write(&pmd->root_lock);
  756. r = __create_thin(pmd, dev);
  757. up_write(&pmd->root_lock);
  758. return r;
  759. }
  760. static int __set_snapshot_details(struct dm_pool_metadata *pmd,
  761. struct dm_thin_device *snap,
  762. dm_thin_id origin, uint32_t time)
  763. {
  764. int r;
  765. struct dm_thin_device *td;
  766. r = __open_device(pmd, origin, 0, &td);
  767. if (r)
  768. return r;
  769. td->changed = 1;
  770. td->snapshotted_time = time;
  771. snap->mapped_blocks = td->mapped_blocks;
  772. snap->snapshotted_time = time;
  773. __close_device(td);
  774. return 0;
  775. }
  776. static int __create_snap(struct dm_pool_metadata *pmd,
  777. dm_thin_id dev, dm_thin_id origin)
  778. {
  779. int r;
  780. dm_block_t origin_root;
  781. uint64_t key = origin, dev_key = dev;
  782. struct dm_thin_device *td;
  783. struct disk_device_details details_le;
  784. __le64 value;
  785. /* check this device is unused */
  786. r = dm_btree_lookup(&pmd->details_info, pmd->details_root,
  787. &dev_key, &details_le);
  788. if (!r)
  789. return -EEXIST;
  790. /* find the mapping tree for the origin */
  791. r = dm_btree_lookup(&pmd->tl_info, pmd->root, &key, &value);
  792. if (r)
  793. return r;
  794. origin_root = le64_to_cpu(value);
  795. /* clone the origin, an inc will do */
  796. dm_tm_inc(pmd->tm, origin_root);
  797. /* insert into the main mapping tree */
  798. value = cpu_to_le64(origin_root);
  799. __dm_bless_for_disk(&value);
  800. key = dev;
  801. r = dm_btree_insert(&pmd->tl_info, pmd->root, &key, &value, &pmd->root);
  802. if (r) {
  803. dm_tm_dec(pmd->tm, origin_root);
  804. return r;
  805. }
  806. pmd->time++;
  807. r = __open_device(pmd, dev, 1, &td);
  808. if (r)
  809. goto bad;
  810. r = __set_snapshot_details(pmd, td, origin, pmd->time);
  811. if (r)
  812. goto bad;
  813. __close_device(td);
  814. return 0;
  815. bad:
  816. __close_device(td);
  817. dm_btree_remove(&pmd->tl_info, pmd->root, &key, &pmd->root);
  818. dm_btree_remove(&pmd->details_info, pmd->details_root,
  819. &key, &pmd->details_root);
  820. return r;
  821. }
  822. int dm_pool_create_snap(struct dm_pool_metadata *pmd,
  823. dm_thin_id dev,
  824. dm_thin_id origin)
  825. {
  826. int r;
  827. down_write(&pmd->root_lock);
  828. r = __create_snap(pmd, dev, origin);
  829. up_write(&pmd->root_lock);
  830. return r;
  831. }
  832. static int __delete_device(struct dm_pool_metadata *pmd, dm_thin_id dev)
  833. {
  834. int r;
  835. uint64_t key = dev;
  836. struct dm_thin_device *td;
  837. /* TODO: failure should mark the transaction invalid */
  838. r = __open_device(pmd, dev, 0, &td);
  839. if (r)
  840. return r;
  841. if (td->open_count > 1) {
  842. __close_device(td);
  843. return -EBUSY;
  844. }
  845. list_del(&td->list);
  846. kfree(td);
  847. r = dm_btree_remove(&pmd->details_info, pmd->details_root,
  848. &key, &pmd->details_root);
  849. if (r)
  850. return r;
  851. r = dm_btree_remove(&pmd->tl_info, pmd->root, &key, &pmd->root);
  852. if (r)
  853. return r;
  854. pmd->need_commit = 1;
  855. return 0;
  856. }
  857. int dm_pool_delete_thin_device(struct dm_pool_metadata *pmd,
  858. dm_thin_id dev)
  859. {
  860. int r;
  861. down_write(&pmd->root_lock);
  862. r = __delete_device(pmd, dev);
  863. up_write(&pmd->root_lock);
  864. return r;
  865. }
  866. int dm_pool_set_metadata_transaction_id(struct dm_pool_metadata *pmd,
  867. uint64_t current_id,
  868. uint64_t new_id)
  869. {
  870. down_write(&pmd->root_lock);
  871. if (pmd->trans_id != current_id) {
  872. up_write(&pmd->root_lock);
  873. DMERR("mismatched transaction id");
  874. return -EINVAL;
  875. }
  876. pmd->trans_id = new_id;
  877. pmd->need_commit = 1;
  878. up_write(&pmd->root_lock);
  879. return 0;
  880. }
  881. int dm_pool_get_metadata_transaction_id(struct dm_pool_metadata *pmd,
  882. uint64_t *result)
  883. {
  884. down_read(&pmd->root_lock);
  885. *result = pmd->trans_id;
  886. up_read(&pmd->root_lock);
  887. return 0;
  888. }
  889. static int __get_held_metadata_root(struct dm_pool_metadata *pmd,
  890. dm_block_t *result)
  891. {
  892. int r;
  893. struct thin_disk_superblock *disk_super;
  894. struct dm_block *sblock;
  895. r = dm_bm_write_lock(pmd->bm, THIN_SUPERBLOCK_LOCATION,
  896. &sb_validator, &sblock);
  897. if (r)
  898. return r;
  899. disk_super = dm_block_data(sblock);
  900. *result = le64_to_cpu(disk_super->held_root);
  901. return dm_bm_unlock(sblock);
  902. }
  903. int dm_pool_get_held_metadata_root(struct dm_pool_metadata *pmd,
  904. dm_block_t *result)
  905. {
  906. int r;
  907. down_read(&pmd->root_lock);
  908. r = __get_held_metadata_root(pmd, result);
  909. up_read(&pmd->root_lock);
  910. return r;
  911. }
  912. int dm_pool_open_thin_device(struct dm_pool_metadata *pmd, dm_thin_id dev,
  913. struct dm_thin_device **td)
  914. {
  915. int r;
  916. down_write(&pmd->root_lock);
  917. r = __open_device(pmd, dev, 0, td);
  918. up_write(&pmd->root_lock);
  919. return r;
  920. }
  921. int dm_pool_close_thin_device(struct dm_thin_device *td)
  922. {
  923. down_write(&td->pmd->root_lock);
  924. __close_device(td);
  925. up_write(&td->pmd->root_lock);
  926. return 0;
  927. }
  928. dm_thin_id dm_thin_dev_id(struct dm_thin_device *td)
  929. {
  930. return td->id;
  931. }
  932. static int __snapshotted_since(struct dm_thin_device *td, uint32_t time)
  933. {
  934. return td->snapshotted_time > time;
  935. }
  936. int dm_thin_find_block(struct dm_thin_device *td, dm_block_t block,
  937. int can_block, struct dm_thin_lookup_result *result)
  938. {
  939. int r;
  940. uint64_t block_time = 0;
  941. __le64 value;
  942. struct dm_pool_metadata *pmd = td->pmd;
  943. dm_block_t keys[2] = { td->id, block };
  944. if (can_block) {
  945. down_read(&pmd->root_lock);
  946. r = dm_btree_lookup(&pmd->info, pmd->root, keys, &value);
  947. if (!r)
  948. block_time = le64_to_cpu(value);
  949. up_read(&pmd->root_lock);
  950. } else if (down_read_trylock(&pmd->root_lock)) {
  951. r = dm_btree_lookup(&pmd->nb_info, pmd->root, keys, &value);
  952. if (!r)
  953. block_time = le64_to_cpu(value);
  954. up_read(&pmd->root_lock);
  955. } else
  956. return -EWOULDBLOCK;
  957. if (!r) {
  958. dm_block_t exception_block;
  959. uint32_t exception_time;
  960. unpack_block_time(block_time, &exception_block,
  961. &exception_time);
  962. result->block = exception_block;
  963. result->shared = __snapshotted_since(td, exception_time);
  964. }
  965. return r;
  966. }
  967. static int __insert(struct dm_thin_device *td, dm_block_t block,
  968. dm_block_t data_block)
  969. {
  970. int r, inserted;
  971. __le64 value;
  972. struct dm_pool_metadata *pmd = td->pmd;
  973. dm_block_t keys[2] = { td->id, block };
  974. pmd->need_commit = 1;
  975. value = cpu_to_le64(pack_block_time(data_block, pmd->time));
  976. __dm_bless_for_disk(&value);
  977. r = dm_btree_insert_notify(&pmd->info, pmd->root, keys, &value,
  978. &pmd->root, &inserted);
  979. if (r)
  980. return r;
  981. if (inserted) {
  982. td->mapped_blocks++;
  983. td->changed = 1;
  984. }
  985. return 0;
  986. }
  987. int dm_thin_insert_block(struct dm_thin_device *td, dm_block_t block,
  988. dm_block_t data_block)
  989. {
  990. int r;
  991. down_write(&td->pmd->root_lock);
  992. r = __insert(td, block, data_block);
  993. up_write(&td->pmd->root_lock);
  994. return r;
  995. }
  996. static int __remove(struct dm_thin_device *td, dm_block_t block)
  997. {
  998. int r;
  999. struct dm_pool_metadata *pmd = td->pmd;
  1000. dm_block_t keys[2] = { td->id, block };
  1001. r = dm_btree_remove(&pmd->info, pmd->root, keys, &pmd->root);
  1002. if (r)
  1003. return r;
  1004. pmd->need_commit = 1;
  1005. return 0;
  1006. }
  1007. int dm_thin_remove_block(struct dm_thin_device *td, dm_block_t block)
  1008. {
  1009. int r;
  1010. down_write(&td->pmd->root_lock);
  1011. r = __remove(td, block);
  1012. up_write(&td->pmd->root_lock);
  1013. return r;
  1014. }
  1015. int dm_pool_alloc_data_block(struct dm_pool_metadata *pmd, dm_block_t *result)
  1016. {
  1017. int r;
  1018. down_write(&pmd->root_lock);
  1019. r = dm_sm_new_block(pmd->data_sm, result);
  1020. pmd->need_commit = 1;
  1021. up_write(&pmd->root_lock);
  1022. return r;
  1023. }
  1024. int dm_pool_commit_metadata(struct dm_pool_metadata *pmd)
  1025. {
  1026. int r;
  1027. down_write(&pmd->root_lock);
  1028. r = __commit_transaction(pmd);
  1029. if (r <= 0)
  1030. goto out;
  1031. /*
  1032. * Open the next transaction.
  1033. */
  1034. r = __begin_transaction(pmd);
  1035. out:
  1036. up_write(&pmd->root_lock);
  1037. return r;
  1038. }
  1039. int dm_pool_get_free_block_count(struct dm_pool_metadata *pmd, dm_block_t *result)
  1040. {
  1041. int r;
  1042. down_read(&pmd->root_lock);
  1043. r = dm_sm_get_nr_free(pmd->data_sm, result);
  1044. up_read(&pmd->root_lock);
  1045. return r;
  1046. }
  1047. int dm_pool_get_free_metadata_block_count(struct dm_pool_metadata *pmd,
  1048. dm_block_t *result)
  1049. {
  1050. int r;
  1051. down_read(&pmd->root_lock);
  1052. r = dm_sm_get_nr_free(pmd->metadata_sm, result);
  1053. up_read(&pmd->root_lock);
  1054. return r;
  1055. }
  1056. int dm_pool_get_metadata_dev_size(struct dm_pool_metadata *pmd,
  1057. dm_block_t *result)
  1058. {
  1059. int r;
  1060. down_read(&pmd->root_lock);
  1061. r = dm_sm_get_nr_blocks(pmd->metadata_sm, result);
  1062. up_read(&pmd->root_lock);
  1063. return r;
  1064. }
  1065. int dm_pool_get_data_block_size(struct dm_pool_metadata *pmd, sector_t *result)
  1066. {
  1067. down_read(&pmd->root_lock);
  1068. *result = pmd->data_block_size;
  1069. up_read(&pmd->root_lock);
  1070. return 0;
  1071. }
  1072. int dm_pool_get_data_dev_size(struct dm_pool_metadata *pmd, dm_block_t *result)
  1073. {
  1074. int r;
  1075. down_read(&pmd->root_lock);
  1076. r = dm_sm_get_nr_blocks(pmd->data_sm, result);
  1077. up_read(&pmd->root_lock);
  1078. return r;
  1079. }
  1080. int dm_thin_get_mapped_count(struct dm_thin_device *td, dm_block_t *result)
  1081. {
  1082. struct dm_pool_metadata *pmd = td->pmd;
  1083. down_read(&pmd->root_lock);
  1084. *result = td->mapped_blocks;
  1085. up_read(&pmd->root_lock);
  1086. return 0;
  1087. }
  1088. static int __highest_block(struct dm_thin_device *td, dm_block_t *result)
  1089. {
  1090. int r;
  1091. __le64 value_le;
  1092. dm_block_t thin_root;
  1093. struct dm_pool_metadata *pmd = td->pmd;
  1094. r = dm_btree_lookup(&pmd->tl_info, pmd->root, &td->id, &value_le);
  1095. if (r)
  1096. return r;
  1097. thin_root = le64_to_cpu(value_le);
  1098. return dm_btree_find_highest_key(&pmd->bl_info, thin_root, result);
  1099. }
  1100. int dm_thin_get_highest_mapped_block(struct dm_thin_device *td,
  1101. dm_block_t *result)
  1102. {
  1103. int r;
  1104. struct dm_pool_metadata *pmd = td->pmd;
  1105. down_read(&pmd->root_lock);
  1106. r = __highest_block(td, result);
  1107. up_read(&pmd->root_lock);
  1108. return r;
  1109. }
  1110. static int __resize_data_dev(struct dm_pool_metadata *pmd, dm_block_t new_count)
  1111. {
  1112. int r;
  1113. dm_block_t old_count;
  1114. r = dm_sm_get_nr_blocks(pmd->data_sm, &old_count);
  1115. if (r)
  1116. return r;
  1117. if (new_count == old_count)
  1118. return 0;
  1119. if (new_count < old_count) {
  1120. DMERR("cannot reduce size of data device");
  1121. return -EINVAL;
  1122. }
  1123. r = dm_sm_extend(pmd->data_sm, new_count - old_count);
  1124. if (!r)
  1125. pmd->need_commit = 1;
  1126. return r;
  1127. }
  1128. int dm_pool_resize_data_dev(struct dm_pool_metadata *pmd, dm_block_t new_count)
  1129. {
  1130. int r;
  1131. down_write(&pmd->root_lock);
  1132. r = __resize_data_dev(pmd, new_count);
  1133. up_write(&pmd->root_lock);
  1134. return r;
  1135. }