dm-thin-metadata.c 31 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406
  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->metadata_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. disk_super = dm_block_data(sblock);
  601. disk_super->magic = cpu_to_le64(THIN_SUPERBLOCK_MAGIC);
  602. disk_super->version = cpu_to_le32(THIN_VERSION);
  603. disk_super->time = 0;
  604. disk_super->metadata_block_size = cpu_to_le32(THIN_METADATA_BLOCK_SIZE >> SECTOR_SHIFT);
  605. disk_super->metadata_nr_blocks = cpu_to_le64(bdev_size >> SECTOR_TO_BLOCK_SHIFT);
  606. disk_super->data_block_size = cpu_to_le32(data_block_size);
  607. r = dm_bm_unlock(sblock);
  608. if (r < 0)
  609. goto bad;
  610. r = dm_btree_empty(&pmd->info, &pmd->root);
  611. if (r < 0)
  612. goto bad;
  613. r = dm_btree_empty(&pmd->details_info, &pmd->details_root);
  614. if (r < 0) {
  615. DMERR("couldn't create devices root");
  616. goto bad;
  617. }
  618. pmd->flags = 0;
  619. pmd->need_commit = 1;
  620. r = dm_pool_commit_metadata(pmd);
  621. if (r < 0) {
  622. DMERR("%s: dm_pool_commit_metadata() failed, error = %d",
  623. __func__, r);
  624. goto bad;
  625. }
  626. return pmd;
  627. bad:
  628. if (dm_pool_metadata_close(pmd) < 0)
  629. DMWARN("%s: dm_pool_metadata_close() failed.", __func__);
  630. return ERR_PTR(r);
  631. }
  632. int dm_pool_metadata_close(struct dm_pool_metadata *pmd)
  633. {
  634. int r;
  635. unsigned open_devices = 0;
  636. struct dm_thin_device *td, *tmp;
  637. down_read(&pmd->root_lock);
  638. list_for_each_entry_safe(td, tmp, &pmd->thin_devices, list) {
  639. if (td->open_count)
  640. open_devices++;
  641. else {
  642. list_del(&td->list);
  643. kfree(td);
  644. }
  645. }
  646. up_read(&pmd->root_lock);
  647. if (open_devices) {
  648. DMERR("attempt to close pmd when %u device(s) are still open",
  649. open_devices);
  650. return -EBUSY;
  651. }
  652. r = __commit_transaction(pmd);
  653. if (r < 0)
  654. DMWARN("%s: __commit_transaction() failed, error = %d",
  655. __func__, r);
  656. dm_tm_destroy(pmd->tm);
  657. dm_tm_destroy(pmd->nb_tm);
  658. dm_block_manager_destroy(pmd->bm);
  659. dm_sm_destroy(pmd->metadata_sm);
  660. dm_sm_destroy(pmd->data_sm);
  661. kfree(pmd);
  662. return 0;
  663. }
  664. /*
  665. * __open_device: Returns @td corresponding to device with id @dev,
  666. * creating it if @create is set and incrementing @td->open_count.
  667. * On failure, @td is undefined.
  668. */
  669. static int __open_device(struct dm_pool_metadata *pmd,
  670. dm_thin_id dev, int create,
  671. struct dm_thin_device **td)
  672. {
  673. int r, changed = 0;
  674. struct dm_thin_device *td2;
  675. uint64_t key = dev;
  676. struct disk_device_details details_le;
  677. /*
  678. * If the device is already open, return it.
  679. */
  680. list_for_each_entry(td2, &pmd->thin_devices, list)
  681. if (td2->id == dev) {
  682. /*
  683. * May not create an already-open device.
  684. */
  685. if (create)
  686. return -EEXIST;
  687. td2->open_count++;
  688. *td = td2;
  689. return 0;
  690. }
  691. /*
  692. * Check the device exists.
  693. */
  694. r = dm_btree_lookup(&pmd->details_info, pmd->details_root,
  695. &key, &details_le);
  696. if (r) {
  697. if (r != -ENODATA || !create)
  698. return r;
  699. /*
  700. * Create new device.
  701. */
  702. changed = 1;
  703. details_le.mapped_blocks = 0;
  704. details_le.transaction_id = cpu_to_le64(pmd->trans_id);
  705. details_le.creation_time = cpu_to_le32(pmd->time);
  706. details_le.snapshotted_time = cpu_to_le32(pmd->time);
  707. }
  708. *td = kmalloc(sizeof(**td), GFP_NOIO);
  709. if (!*td)
  710. return -ENOMEM;
  711. (*td)->pmd = pmd;
  712. (*td)->id = dev;
  713. (*td)->open_count = 1;
  714. (*td)->changed = changed;
  715. (*td)->mapped_blocks = le64_to_cpu(details_le.mapped_blocks);
  716. (*td)->transaction_id = le64_to_cpu(details_le.transaction_id);
  717. (*td)->creation_time = le32_to_cpu(details_le.creation_time);
  718. (*td)->snapshotted_time = le32_to_cpu(details_le.snapshotted_time);
  719. list_add(&(*td)->list, &pmd->thin_devices);
  720. return 0;
  721. }
  722. static void __close_device(struct dm_thin_device *td)
  723. {
  724. --td->open_count;
  725. }
  726. static int __create_thin(struct dm_pool_metadata *pmd,
  727. dm_thin_id dev)
  728. {
  729. int r;
  730. dm_block_t dev_root;
  731. uint64_t key = dev;
  732. struct disk_device_details details_le;
  733. struct dm_thin_device *td;
  734. __le64 value;
  735. r = dm_btree_lookup(&pmd->details_info, pmd->details_root,
  736. &key, &details_le);
  737. if (!r)
  738. return -EEXIST;
  739. /*
  740. * Create an empty btree for the mappings.
  741. */
  742. r = dm_btree_empty(&pmd->bl_info, &dev_root);
  743. if (r)
  744. return r;
  745. /*
  746. * Insert it into the main mapping tree.
  747. */
  748. value = cpu_to_le64(dev_root);
  749. __dm_bless_for_disk(&value);
  750. r = dm_btree_insert(&pmd->tl_info, pmd->root, &key, &value, &pmd->root);
  751. if (r) {
  752. dm_btree_del(&pmd->bl_info, dev_root);
  753. return r;
  754. }
  755. r = __open_device(pmd, dev, 1, &td);
  756. if (r) {
  757. dm_btree_remove(&pmd->tl_info, pmd->root, &key, &pmd->root);
  758. dm_btree_del(&pmd->bl_info, dev_root);
  759. return r;
  760. }
  761. __close_device(td);
  762. return r;
  763. }
  764. int dm_pool_create_thin(struct dm_pool_metadata *pmd, dm_thin_id dev)
  765. {
  766. int r;
  767. down_write(&pmd->root_lock);
  768. r = __create_thin(pmd, dev);
  769. up_write(&pmd->root_lock);
  770. return r;
  771. }
  772. static int __set_snapshot_details(struct dm_pool_metadata *pmd,
  773. struct dm_thin_device *snap,
  774. dm_thin_id origin, uint32_t time)
  775. {
  776. int r;
  777. struct dm_thin_device *td;
  778. r = __open_device(pmd, origin, 0, &td);
  779. if (r)
  780. return r;
  781. td->changed = 1;
  782. td->snapshotted_time = time;
  783. snap->mapped_blocks = td->mapped_blocks;
  784. snap->snapshotted_time = time;
  785. __close_device(td);
  786. return 0;
  787. }
  788. static int __create_snap(struct dm_pool_metadata *pmd,
  789. dm_thin_id dev, dm_thin_id origin)
  790. {
  791. int r;
  792. dm_block_t origin_root;
  793. uint64_t key = origin, dev_key = dev;
  794. struct dm_thin_device *td;
  795. struct disk_device_details details_le;
  796. __le64 value;
  797. /* check this device is unused */
  798. r = dm_btree_lookup(&pmd->details_info, pmd->details_root,
  799. &dev_key, &details_le);
  800. if (!r)
  801. return -EEXIST;
  802. /* find the mapping tree for the origin */
  803. r = dm_btree_lookup(&pmd->tl_info, pmd->root, &key, &value);
  804. if (r)
  805. return r;
  806. origin_root = le64_to_cpu(value);
  807. /* clone the origin, an inc will do */
  808. dm_tm_inc(pmd->tm, origin_root);
  809. /* insert into the main mapping tree */
  810. value = cpu_to_le64(origin_root);
  811. __dm_bless_for_disk(&value);
  812. key = dev;
  813. r = dm_btree_insert(&pmd->tl_info, pmd->root, &key, &value, &pmd->root);
  814. if (r) {
  815. dm_tm_dec(pmd->tm, origin_root);
  816. return r;
  817. }
  818. pmd->time++;
  819. r = __open_device(pmd, dev, 1, &td);
  820. if (r)
  821. goto bad;
  822. r = __set_snapshot_details(pmd, td, origin, pmd->time);
  823. __close_device(td);
  824. if (r)
  825. goto bad;
  826. return 0;
  827. bad:
  828. dm_btree_remove(&pmd->tl_info, pmd->root, &key, &pmd->root);
  829. dm_btree_remove(&pmd->details_info, pmd->details_root,
  830. &key, &pmd->details_root);
  831. return r;
  832. }
  833. int dm_pool_create_snap(struct dm_pool_metadata *pmd,
  834. dm_thin_id dev,
  835. dm_thin_id origin)
  836. {
  837. int r;
  838. down_write(&pmd->root_lock);
  839. r = __create_snap(pmd, dev, origin);
  840. up_write(&pmd->root_lock);
  841. return r;
  842. }
  843. static int __delete_device(struct dm_pool_metadata *pmd, dm_thin_id dev)
  844. {
  845. int r;
  846. uint64_t key = dev;
  847. struct dm_thin_device *td;
  848. /* TODO: failure should mark the transaction invalid */
  849. r = __open_device(pmd, dev, 0, &td);
  850. if (r)
  851. return r;
  852. if (td->open_count > 1) {
  853. __close_device(td);
  854. return -EBUSY;
  855. }
  856. list_del(&td->list);
  857. kfree(td);
  858. r = dm_btree_remove(&pmd->details_info, pmd->details_root,
  859. &key, &pmd->details_root);
  860. if (r)
  861. return r;
  862. r = dm_btree_remove(&pmd->tl_info, pmd->root, &key, &pmd->root);
  863. if (r)
  864. return r;
  865. pmd->need_commit = 1;
  866. return 0;
  867. }
  868. int dm_pool_delete_thin_device(struct dm_pool_metadata *pmd,
  869. dm_thin_id dev)
  870. {
  871. int r;
  872. down_write(&pmd->root_lock);
  873. r = __delete_device(pmd, dev);
  874. up_write(&pmd->root_lock);
  875. return r;
  876. }
  877. int dm_pool_set_metadata_transaction_id(struct dm_pool_metadata *pmd,
  878. uint64_t current_id,
  879. uint64_t new_id)
  880. {
  881. down_write(&pmd->root_lock);
  882. if (pmd->trans_id != current_id) {
  883. up_write(&pmd->root_lock);
  884. DMERR("mismatched transaction id");
  885. return -EINVAL;
  886. }
  887. pmd->trans_id = new_id;
  888. pmd->need_commit = 1;
  889. up_write(&pmd->root_lock);
  890. return 0;
  891. }
  892. int dm_pool_get_metadata_transaction_id(struct dm_pool_metadata *pmd,
  893. uint64_t *result)
  894. {
  895. down_read(&pmd->root_lock);
  896. *result = pmd->trans_id;
  897. up_read(&pmd->root_lock);
  898. return 0;
  899. }
  900. static int __get_held_metadata_root(struct dm_pool_metadata *pmd,
  901. dm_block_t *result)
  902. {
  903. int r;
  904. struct thin_disk_superblock *disk_super;
  905. struct dm_block *sblock;
  906. r = dm_bm_write_lock(pmd->bm, THIN_SUPERBLOCK_LOCATION,
  907. &sb_validator, &sblock);
  908. if (r)
  909. return r;
  910. disk_super = dm_block_data(sblock);
  911. *result = le64_to_cpu(disk_super->held_root);
  912. return dm_bm_unlock(sblock);
  913. }
  914. int dm_pool_get_held_metadata_root(struct dm_pool_metadata *pmd,
  915. dm_block_t *result)
  916. {
  917. int r;
  918. down_read(&pmd->root_lock);
  919. r = __get_held_metadata_root(pmd, result);
  920. up_read(&pmd->root_lock);
  921. return r;
  922. }
  923. int dm_pool_open_thin_device(struct dm_pool_metadata *pmd, dm_thin_id dev,
  924. struct dm_thin_device **td)
  925. {
  926. int r;
  927. down_write(&pmd->root_lock);
  928. r = __open_device(pmd, dev, 0, td);
  929. up_write(&pmd->root_lock);
  930. return r;
  931. }
  932. int dm_pool_close_thin_device(struct dm_thin_device *td)
  933. {
  934. down_write(&td->pmd->root_lock);
  935. __close_device(td);
  936. up_write(&td->pmd->root_lock);
  937. return 0;
  938. }
  939. dm_thin_id dm_thin_dev_id(struct dm_thin_device *td)
  940. {
  941. return td->id;
  942. }
  943. static int __snapshotted_since(struct dm_thin_device *td, uint32_t time)
  944. {
  945. return td->snapshotted_time > time;
  946. }
  947. int dm_thin_find_block(struct dm_thin_device *td, dm_block_t block,
  948. int can_block, struct dm_thin_lookup_result *result)
  949. {
  950. int r;
  951. uint64_t block_time = 0;
  952. __le64 value;
  953. struct dm_pool_metadata *pmd = td->pmd;
  954. dm_block_t keys[2] = { td->id, block };
  955. if (can_block) {
  956. down_read(&pmd->root_lock);
  957. r = dm_btree_lookup(&pmd->info, pmd->root, keys, &value);
  958. if (!r)
  959. block_time = le64_to_cpu(value);
  960. up_read(&pmd->root_lock);
  961. } else if (down_read_trylock(&pmd->root_lock)) {
  962. r = dm_btree_lookup(&pmd->nb_info, pmd->root, keys, &value);
  963. if (!r)
  964. block_time = le64_to_cpu(value);
  965. up_read(&pmd->root_lock);
  966. } else
  967. return -EWOULDBLOCK;
  968. if (!r) {
  969. dm_block_t exception_block;
  970. uint32_t exception_time;
  971. unpack_block_time(block_time, &exception_block,
  972. &exception_time);
  973. result->block = exception_block;
  974. result->shared = __snapshotted_since(td, exception_time);
  975. }
  976. return r;
  977. }
  978. static int __insert(struct dm_thin_device *td, dm_block_t block,
  979. dm_block_t data_block)
  980. {
  981. int r, inserted;
  982. __le64 value;
  983. struct dm_pool_metadata *pmd = td->pmd;
  984. dm_block_t keys[2] = { td->id, block };
  985. pmd->need_commit = 1;
  986. value = cpu_to_le64(pack_block_time(data_block, pmd->time));
  987. __dm_bless_for_disk(&value);
  988. r = dm_btree_insert_notify(&pmd->info, pmd->root, keys, &value,
  989. &pmd->root, &inserted);
  990. if (r)
  991. return r;
  992. if (inserted) {
  993. td->mapped_blocks++;
  994. td->changed = 1;
  995. }
  996. return 0;
  997. }
  998. int dm_thin_insert_block(struct dm_thin_device *td, dm_block_t block,
  999. dm_block_t data_block)
  1000. {
  1001. int r;
  1002. down_write(&td->pmd->root_lock);
  1003. r = __insert(td, block, data_block);
  1004. up_write(&td->pmd->root_lock);
  1005. return r;
  1006. }
  1007. static int __remove(struct dm_thin_device *td, dm_block_t block)
  1008. {
  1009. int r;
  1010. struct dm_pool_metadata *pmd = td->pmd;
  1011. dm_block_t keys[2] = { td->id, block };
  1012. r = dm_btree_remove(&pmd->info, pmd->root, keys, &pmd->root);
  1013. if (r)
  1014. return r;
  1015. td->mapped_blocks--;
  1016. td->changed = 1;
  1017. pmd->need_commit = 1;
  1018. return 0;
  1019. }
  1020. int dm_thin_remove_block(struct dm_thin_device *td, dm_block_t block)
  1021. {
  1022. int r;
  1023. down_write(&td->pmd->root_lock);
  1024. r = __remove(td, block);
  1025. up_write(&td->pmd->root_lock);
  1026. return r;
  1027. }
  1028. int dm_pool_alloc_data_block(struct dm_pool_metadata *pmd, dm_block_t *result)
  1029. {
  1030. int r;
  1031. down_write(&pmd->root_lock);
  1032. r = dm_sm_new_block(pmd->data_sm, result);
  1033. pmd->need_commit = 1;
  1034. up_write(&pmd->root_lock);
  1035. return r;
  1036. }
  1037. int dm_pool_commit_metadata(struct dm_pool_metadata *pmd)
  1038. {
  1039. int r;
  1040. down_write(&pmd->root_lock);
  1041. r = __commit_transaction(pmd);
  1042. if (r <= 0)
  1043. goto out;
  1044. /*
  1045. * Open the next transaction.
  1046. */
  1047. r = __begin_transaction(pmd);
  1048. out:
  1049. up_write(&pmd->root_lock);
  1050. return r;
  1051. }
  1052. int dm_pool_get_free_block_count(struct dm_pool_metadata *pmd, dm_block_t *result)
  1053. {
  1054. int r;
  1055. down_read(&pmd->root_lock);
  1056. r = dm_sm_get_nr_free(pmd->data_sm, result);
  1057. up_read(&pmd->root_lock);
  1058. return r;
  1059. }
  1060. int dm_pool_get_free_metadata_block_count(struct dm_pool_metadata *pmd,
  1061. dm_block_t *result)
  1062. {
  1063. int r;
  1064. down_read(&pmd->root_lock);
  1065. r = dm_sm_get_nr_free(pmd->metadata_sm, result);
  1066. up_read(&pmd->root_lock);
  1067. return r;
  1068. }
  1069. int dm_pool_get_metadata_dev_size(struct dm_pool_metadata *pmd,
  1070. dm_block_t *result)
  1071. {
  1072. int r;
  1073. down_read(&pmd->root_lock);
  1074. r = dm_sm_get_nr_blocks(pmd->metadata_sm, result);
  1075. up_read(&pmd->root_lock);
  1076. return r;
  1077. }
  1078. int dm_pool_get_data_block_size(struct dm_pool_metadata *pmd, sector_t *result)
  1079. {
  1080. down_read(&pmd->root_lock);
  1081. *result = pmd->data_block_size;
  1082. up_read(&pmd->root_lock);
  1083. return 0;
  1084. }
  1085. int dm_pool_get_data_dev_size(struct dm_pool_metadata *pmd, dm_block_t *result)
  1086. {
  1087. int r;
  1088. down_read(&pmd->root_lock);
  1089. r = dm_sm_get_nr_blocks(pmd->data_sm, result);
  1090. up_read(&pmd->root_lock);
  1091. return r;
  1092. }
  1093. int dm_thin_get_mapped_count(struct dm_thin_device *td, dm_block_t *result)
  1094. {
  1095. struct dm_pool_metadata *pmd = td->pmd;
  1096. down_read(&pmd->root_lock);
  1097. *result = td->mapped_blocks;
  1098. up_read(&pmd->root_lock);
  1099. return 0;
  1100. }
  1101. static int __highest_block(struct dm_thin_device *td, dm_block_t *result)
  1102. {
  1103. int r;
  1104. __le64 value_le;
  1105. dm_block_t thin_root;
  1106. struct dm_pool_metadata *pmd = td->pmd;
  1107. r = dm_btree_lookup(&pmd->tl_info, pmd->root, &td->id, &value_le);
  1108. if (r)
  1109. return r;
  1110. thin_root = le64_to_cpu(value_le);
  1111. return dm_btree_find_highest_key(&pmd->bl_info, thin_root, result);
  1112. }
  1113. int dm_thin_get_highest_mapped_block(struct dm_thin_device *td,
  1114. dm_block_t *result)
  1115. {
  1116. int r;
  1117. struct dm_pool_metadata *pmd = td->pmd;
  1118. down_read(&pmd->root_lock);
  1119. r = __highest_block(td, result);
  1120. up_read(&pmd->root_lock);
  1121. return r;
  1122. }
  1123. static int __resize_data_dev(struct dm_pool_metadata *pmd, dm_block_t new_count)
  1124. {
  1125. int r;
  1126. dm_block_t old_count;
  1127. r = dm_sm_get_nr_blocks(pmd->data_sm, &old_count);
  1128. if (r)
  1129. return r;
  1130. if (new_count == old_count)
  1131. return 0;
  1132. if (new_count < old_count) {
  1133. DMERR("cannot reduce size of data device");
  1134. return -EINVAL;
  1135. }
  1136. r = dm_sm_extend(pmd->data_sm, new_count - old_count);
  1137. if (!r)
  1138. pmd->need_commit = 1;
  1139. return r;
  1140. }
  1141. int dm_pool_resize_data_dev(struct dm_pool_metadata *pmd, dm_block_t new_count)
  1142. {
  1143. int r;
  1144. down_write(&pmd->root_lock);
  1145. r = __resize_data_dev(pmd, new_count);
  1146. up_write(&pmd->root_lock);
  1147. return r;
  1148. }