dm-thin-metadata.c 38 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712
  1. /*
  2. * Copyright (C) 2011-2012 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. /*
  78. * 3 for btree insert +
  79. * 2 for btree lookup used within space map
  80. */
  81. #define THIN_MAX_CONCURRENT_LOCKS 5
  82. /* This should be plenty */
  83. #define SPACE_MAP_ROOT_SIZE 128
  84. /*
  85. * Little endian on-disk superblock and device details.
  86. */
  87. struct thin_disk_superblock {
  88. __le32 csum; /* Checksum of superblock except for this field. */
  89. __le32 flags;
  90. __le64 blocknr; /* This block number, dm_block_t. */
  91. __u8 uuid[16];
  92. __le64 magic;
  93. __le32 version;
  94. __le32 time;
  95. __le64 trans_id;
  96. /*
  97. * Root held by userspace transactions.
  98. */
  99. __le64 held_root;
  100. __u8 data_space_map_root[SPACE_MAP_ROOT_SIZE];
  101. __u8 metadata_space_map_root[SPACE_MAP_ROOT_SIZE];
  102. /*
  103. * 2-level btree mapping (dev_id, (dev block, time)) -> data block
  104. */
  105. __le64 data_mapping_root;
  106. /*
  107. * Device detail root mapping dev_id -> device_details
  108. */
  109. __le64 device_details_root;
  110. __le32 data_block_size; /* In 512-byte sectors. */
  111. __le32 metadata_block_size; /* In 512-byte sectors. */
  112. __le64 metadata_nr_blocks;
  113. __le32 compat_flags;
  114. __le32 compat_ro_flags;
  115. __le32 incompat_flags;
  116. } __packed;
  117. struct disk_device_details {
  118. __le64 mapped_blocks;
  119. __le64 transaction_id; /* When created. */
  120. __le32 creation_time;
  121. __le32 snapshotted_time;
  122. } __packed;
  123. struct dm_pool_metadata {
  124. struct hlist_node hash;
  125. struct block_device *bdev;
  126. struct dm_block_manager *bm;
  127. struct dm_space_map *metadata_sm;
  128. struct dm_space_map *data_sm;
  129. struct dm_transaction_manager *tm;
  130. struct dm_transaction_manager *nb_tm;
  131. /*
  132. * Two-level btree.
  133. * First level holds thin_dev_t.
  134. * Second level holds mappings.
  135. */
  136. struct dm_btree_info info;
  137. /*
  138. * Non-blocking version of the above.
  139. */
  140. struct dm_btree_info nb_info;
  141. /*
  142. * Just the top level for deleting whole devices.
  143. */
  144. struct dm_btree_info tl_info;
  145. /*
  146. * Just the bottom level for creating new devices.
  147. */
  148. struct dm_btree_info bl_info;
  149. /*
  150. * Describes the device details btree.
  151. */
  152. struct dm_btree_info details_info;
  153. struct rw_semaphore root_lock;
  154. uint32_t time;
  155. dm_block_t root;
  156. dm_block_t details_root;
  157. struct list_head thin_devices;
  158. uint64_t trans_id;
  159. unsigned long flags;
  160. sector_t data_block_size;
  161. bool read_only:1;
  162. /*
  163. * Set if a transaction has to be aborted but the attempt to roll back
  164. * to the previous (good) transaction failed. The only pool metadata
  165. * operation possible in this state is the closing of the device.
  166. */
  167. bool fail_io:1;
  168. };
  169. struct dm_thin_device {
  170. struct list_head list;
  171. struct dm_pool_metadata *pmd;
  172. dm_thin_id id;
  173. int open_count;
  174. bool changed:1;
  175. bool aborted_with_changes:1;
  176. uint64_t mapped_blocks;
  177. uint64_t transaction_id;
  178. uint32_t creation_time;
  179. uint32_t snapshotted_time;
  180. };
  181. /*----------------------------------------------------------------
  182. * superblock validator
  183. *--------------------------------------------------------------*/
  184. #define SUPERBLOCK_CSUM_XOR 160774
  185. static void sb_prepare_for_write(struct dm_block_validator *v,
  186. struct dm_block *b,
  187. size_t block_size)
  188. {
  189. struct thin_disk_superblock *disk_super = dm_block_data(b);
  190. disk_super->blocknr = cpu_to_le64(dm_block_location(b));
  191. disk_super->csum = cpu_to_le32(dm_bm_checksum(&disk_super->flags,
  192. block_size - sizeof(__le32),
  193. SUPERBLOCK_CSUM_XOR));
  194. }
  195. static int sb_check(struct dm_block_validator *v,
  196. struct dm_block *b,
  197. size_t block_size)
  198. {
  199. struct thin_disk_superblock *disk_super = dm_block_data(b);
  200. __le32 csum_le;
  201. if (dm_block_location(b) != le64_to_cpu(disk_super->blocknr)) {
  202. DMERR("sb_check failed: blocknr %llu: "
  203. "wanted %llu", le64_to_cpu(disk_super->blocknr),
  204. (unsigned long long)dm_block_location(b));
  205. return -ENOTBLK;
  206. }
  207. if (le64_to_cpu(disk_super->magic) != THIN_SUPERBLOCK_MAGIC) {
  208. DMERR("sb_check failed: magic %llu: "
  209. "wanted %llu", le64_to_cpu(disk_super->magic),
  210. (unsigned long long)THIN_SUPERBLOCK_MAGIC);
  211. return -EILSEQ;
  212. }
  213. csum_le = cpu_to_le32(dm_bm_checksum(&disk_super->flags,
  214. block_size - sizeof(__le32),
  215. SUPERBLOCK_CSUM_XOR));
  216. if (csum_le != disk_super->csum) {
  217. DMERR("sb_check failed: csum %u: wanted %u",
  218. le32_to_cpu(csum_le), le32_to_cpu(disk_super->csum));
  219. return -EILSEQ;
  220. }
  221. return 0;
  222. }
  223. static struct dm_block_validator sb_validator = {
  224. .name = "superblock",
  225. .prepare_for_write = sb_prepare_for_write,
  226. .check = sb_check
  227. };
  228. /*----------------------------------------------------------------
  229. * Methods for the btree value types
  230. *--------------------------------------------------------------*/
  231. static uint64_t pack_block_time(dm_block_t b, uint32_t t)
  232. {
  233. return (b << 24) | t;
  234. }
  235. static void unpack_block_time(uint64_t v, dm_block_t *b, uint32_t *t)
  236. {
  237. *b = v >> 24;
  238. *t = v & ((1 << 24) - 1);
  239. }
  240. static void data_block_inc(void *context, const void *value_le)
  241. {
  242. struct dm_space_map *sm = context;
  243. __le64 v_le;
  244. uint64_t b;
  245. uint32_t t;
  246. memcpy(&v_le, value_le, sizeof(v_le));
  247. unpack_block_time(le64_to_cpu(v_le), &b, &t);
  248. dm_sm_inc_block(sm, b);
  249. }
  250. static void data_block_dec(void *context, const void *value_le)
  251. {
  252. struct dm_space_map *sm = context;
  253. __le64 v_le;
  254. uint64_t b;
  255. uint32_t t;
  256. memcpy(&v_le, value_le, sizeof(v_le));
  257. unpack_block_time(le64_to_cpu(v_le), &b, &t);
  258. dm_sm_dec_block(sm, b);
  259. }
  260. static int data_block_equal(void *context, const void *value1_le, const void *value2_le)
  261. {
  262. __le64 v1_le, v2_le;
  263. uint64_t b1, b2;
  264. uint32_t t;
  265. memcpy(&v1_le, value1_le, sizeof(v1_le));
  266. memcpy(&v2_le, value2_le, sizeof(v2_le));
  267. unpack_block_time(le64_to_cpu(v1_le), &b1, &t);
  268. unpack_block_time(le64_to_cpu(v2_le), &b2, &t);
  269. return b1 == b2;
  270. }
  271. static void subtree_inc(void *context, const void *value)
  272. {
  273. struct dm_btree_info *info = context;
  274. __le64 root_le;
  275. uint64_t root;
  276. memcpy(&root_le, value, sizeof(root_le));
  277. root = le64_to_cpu(root_le);
  278. dm_tm_inc(info->tm, root);
  279. }
  280. static void subtree_dec(void *context, const void *value)
  281. {
  282. struct dm_btree_info *info = context;
  283. __le64 root_le;
  284. uint64_t root;
  285. memcpy(&root_le, value, sizeof(root_le));
  286. root = le64_to_cpu(root_le);
  287. if (dm_btree_del(info, root))
  288. DMERR("btree delete failed\n");
  289. }
  290. static int subtree_equal(void *context, const void *value1_le, const void *value2_le)
  291. {
  292. __le64 v1_le, v2_le;
  293. memcpy(&v1_le, value1_le, sizeof(v1_le));
  294. memcpy(&v2_le, value2_le, sizeof(v2_le));
  295. return v1_le == v2_le;
  296. }
  297. /*----------------------------------------------------------------*/
  298. static int superblock_lock_zero(struct dm_pool_metadata *pmd,
  299. struct dm_block **sblock)
  300. {
  301. return dm_bm_write_lock_zero(pmd->bm, THIN_SUPERBLOCK_LOCATION,
  302. &sb_validator, sblock);
  303. }
  304. static int superblock_lock(struct dm_pool_metadata *pmd,
  305. struct dm_block **sblock)
  306. {
  307. return dm_bm_write_lock(pmd->bm, THIN_SUPERBLOCK_LOCATION,
  308. &sb_validator, sblock);
  309. }
  310. static int __superblock_all_zeroes(struct dm_block_manager *bm, int *result)
  311. {
  312. int r;
  313. unsigned i;
  314. struct dm_block *b;
  315. __le64 *data_le, zero = cpu_to_le64(0);
  316. unsigned block_size = dm_bm_block_size(bm) / sizeof(__le64);
  317. /*
  318. * We can't use a validator here - it may be all zeroes.
  319. */
  320. r = dm_bm_read_lock(bm, THIN_SUPERBLOCK_LOCATION, NULL, &b);
  321. if (r)
  322. return r;
  323. data_le = dm_block_data(b);
  324. *result = 1;
  325. for (i = 0; i < block_size; i++) {
  326. if (data_le[i] != zero) {
  327. *result = 0;
  328. break;
  329. }
  330. }
  331. return dm_bm_unlock(b);
  332. }
  333. static void __setup_btree_details(struct dm_pool_metadata *pmd)
  334. {
  335. pmd->info.tm = pmd->tm;
  336. pmd->info.levels = 2;
  337. pmd->info.value_type.context = pmd->data_sm;
  338. pmd->info.value_type.size = sizeof(__le64);
  339. pmd->info.value_type.inc = data_block_inc;
  340. pmd->info.value_type.dec = data_block_dec;
  341. pmd->info.value_type.equal = data_block_equal;
  342. memcpy(&pmd->nb_info, &pmd->info, sizeof(pmd->nb_info));
  343. pmd->nb_info.tm = pmd->nb_tm;
  344. pmd->tl_info.tm = pmd->tm;
  345. pmd->tl_info.levels = 1;
  346. pmd->tl_info.value_type.context = &pmd->bl_info;
  347. pmd->tl_info.value_type.size = sizeof(__le64);
  348. pmd->tl_info.value_type.inc = subtree_inc;
  349. pmd->tl_info.value_type.dec = subtree_dec;
  350. pmd->tl_info.value_type.equal = subtree_equal;
  351. pmd->bl_info.tm = pmd->tm;
  352. pmd->bl_info.levels = 1;
  353. pmd->bl_info.value_type.context = pmd->data_sm;
  354. pmd->bl_info.value_type.size = sizeof(__le64);
  355. pmd->bl_info.value_type.inc = data_block_inc;
  356. pmd->bl_info.value_type.dec = data_block_dec;
  357. pmd->bl_info.value_type.equal = data_block_equal;
  358. pmd->details_info.tm = pmd->tm;
  359. pmd->details_info.levels = 1;
  360. pmd->details_info.value_type.context = NULL;
  361. pmd->details_info.value_type.size = sizeof(struct disk_device_details);
  362. pmd->details_info.value_type.inc = NULL;
  363. pmd->details_info.value_type.dec = NULL;
  364. pmd->details_info.value_type.equal = NULL;
  365. }
  366. static int __write_initial_superblock(struct dm_pool_metadata *pmd)
  367. {
  368. int r;
  369. struct dm_block *sblock;
  370. size_t metadata_len, data_len;
  371. struct thin_disk_superblock *disk_super;
  372. sector_t bdev_size = i_size_read(pmd->bdev->bd_inode) >> SECTOR_SHIFT;
  373. if (bdev_size > THIN_METADATA_MAX_SECTORS)
  374. bdev_size = THIN_METADATA_MAX_SECTORS;
  375. r = dm_sm_root_size(pmd->metadata_sm, &metadata_len);
  376. if (r < 0)
  377. return r;
  378. r = dm_sm_root_size(pmd->data_sm, &data_len);
  379. if (r < 0)
  380. return r;
  381. r = dm_sm_commit(pmd->data_sm);
  382. if (r < 0)
  383. return r;
  384. r = dm_tm_pre_commit(pmd->tm);
  385. if (r < 0)
  386. return r;
  387. r = superblock_lock_zero(pmd, &sblock);
  388. if (r)
  389. return r;
  390. disk_super = dm_block_data(sblock);
  391. disk_super->flags = 0;
  392. memset(disk_super->uuid, 0, sizeof(disk_super->uuid));
  393. disk_super->magic = cpu_to_le64(THIN_SUPERBLOCK_MAGIC);
  394. disk_super->version = cpu_to_le32(THIN_VERSION);
  395. disk_super->time = 0;
  396. disk_super->trans_id = 0;
  397. disk_super->held_root = 0;
  398. r = dm_sm_copy_root(pmd->metadata_sm, &disk_super->metadata_space_map_root,
  399. metadata_len);
  400. if (r < 0)
  401. goto bad_locked;
  402. r = dm_sm_copy_root(pmd->data_sm, &disk_super->data_space_map_root,
  403. data_len);
  404. if (r < 0)
  405. goto bad_locked;
  406. disk_super->data_mapping_root = cpu_to_le64(pmd->root);
  407. disk_super->device_details_root = cpu_to_le64(pmd->details_root);
  408. disk_super->metadata_block_size = cpu_to_le32(THIN_METADATA_BLOCK_SIZE >> SECTOR_SHIFT);
  409. disk_super->metadata_nr_blocks = cpu_to_le64(bdev_size >> SECTOR_TO_BLOCK_SHIFT);
  410. disk_super->data_block_size = cpu_to_le32(pmd->data_block_size);
  411. return dm_tm_commit(pmd->tm, sblock);
  412. bad_locked:
  413. dm_bm_unlock(sblock);
  414. return r;
  415. }
  416. static int __format_metadata(struct dm_pool_metadata *pmd)
  417. {
  418. int r;
  419. r = dm_tm_create_with_sm(pmd->bm, THIN_SUPERBLOCK_LOCATION,
  420. &pmd->tm, &pmd->metadata_sm);
  421. if (r < 0) {
  422. DMERR("tm_create_with_sm failed");
  423. return r;
  424. }
  425. pmd->data_sm = dm_sm_disk_create(pmd->tm, 0);
  426. if (IS_ERR(pmd->data_sm)) {
  427. DMERR("sm_disk_create failed");
  428. r = PTR_ERR(pmd->data_sm);
  429. goto bad_cleanup_tm;
  430. }
  431. pmd->nb_tm = dm_tm_create_non_blocking_clone(pmd->tm);
  432. if (!pmd->nb_tm) {
  433. DMERR("could not create non-blocking clone tm");
  434. r = -ENOMEM;
  435. goto bad_cleanup_data_sm;
  436. }
  437. __setup_btree_details(pmd);
  438. r = dm_btree_empty(&pmd->info, &pmd->root);
  439. if (r < 0)
  440. goto bad_cleanup_nb_tm;
  441. r = dm_btree_empty(&pmd->details_info, &pmd->details_root);
  442. if (r < 0) {
  443. DMERR("couldn't create devices root");
  444. goto bad_cleanup_nb_tm;
  445. }
  446. r = __write_initial_superblock(pmd);
  447. if (r)
  448. goto bad_cleanup_nb_tm;
  449. return 0;
  450. bad_cleanup_nb_tm:
  451. dm_tm_destroy(pmd->nb_tm);
  452. bad_cleanup_data_sm:
  453. dm_sm_destroy(pmd->data_sm);
  454. bad_cleanup_tm:
  455. dm_tm_destroy(pmd->tm);
  456. dm_sm_destroy(pmd->metadata_sm);
  457. return r;
  458. }
  459. static int __check_incompat_features(struct thin_disk_superblock *disk_super,
  460. struct dm_pool_metadata *pmd)
  461. {
  462. uint32_t features;
  463. features = le32_to_cpu(disk_super->incompat_flags) & ~THIN_FEATURE_INCOMPAT_SUPP;
  464. if (features) {
  465. DMERR("could not access metadata due to unsupported optional features (%lx).",
  466. (unsigned long)features);
  467. return -EINVAL;
  468. }
  469. /*
  470. * Check for read-only metadata to skip the following RDWR checks.
  471. */
  472. if (get_disk_ro(pmd->bdev->bd_disk))
  473. return 0;
  474. features = le32_to_cpu(disk_super->compat_ro_flags) & ~THIN_FEATURE_COMPAT_RO_SUPP;
  475. if (features) {
  476. DMERR("could not access metadata RDWR due to unsupported optional features (%lx).",
  477. (unsigned long)features);
  478. return -EINVAL;
  479. }
  480. return 0;
  481. }
  482. static int __open_metadata(struct dm_pool_metadata *pmd)
  483. {
  484. int r;
  485. struct dm_block *sblock;
  486. struct thin_disk_superblock *disk_super;
  487. r = dm_bm_read_lock(pmd->bm, THIN_SUPERBLOCK_LOCATION,
  488. &sb_validator, &sblock);
  489. if (r < 0) {
  490. DMERR("couldn't read superblock");
  491. return r;
  492. }
  493. disk_super = dm_block_data(sblock);
  494. r = __check_incompat_features(disk_super, pmd);
  495. if (r < 0)
  496. goto bad_unlock_sblock;
  497. r = dm_tm_open_with_sm(pmd->bm, THIN_SUPERBLOCK_LOCATION,
  498. disk_super->metadata_space_map_root,
  499. sizeof(disk_super->metadata_space_map_root),
  500. &pmd->tm, &pmd->metadata_sm);
  501. if (r < 0) {
  502. DMERR("tm_open_with_sm failed");
  503. goto bad_unlock_sblock;
  504. }
  505. pmd->data_sm = dm_sm_disk_open(pmd->tm, disk_super->data_space_map_root,
  506. sizeof(disk_super->data_space_map_root));
  507. if (IS_ERR(pmd->data_sm)) {
  508. DMERR("sm_disk_open failed");
  509. r = PTR_ERR(pmd->data_sm);
  510. goto bad_cleanup_tm;
  511. }
  512. pmd->nb_tm = dm_tm_create_non_blocking_clone(pmd->tm);
  513. if (!pmd->nb_tm) {
  514. DMERR("could not create non-blocking clone tm");
  515. r = -ENOMEM;
  516. goto bad_cleanup_data_sm;
  517. }
  518. __setup_btree_details(pmd);
  519. return dm_bm_unlock(sblock);
  520. bad_cleanup_data_sm:
  521. dm_sm_destroy(pmd->data_sm);
  522. bad_cleanup_tm:
  523. dm_tm_destroy(pmd->tm);
  524. dm_sm_destroy(pmd->metadata_sm);
  525. bad_unlock_sblock:
  526. dm_bm_unlock(sblock);
  527. return r;
  528. }
  529. static int __open_or_format_metadata(struct dm_pool_metadata *pmd, bool format_device)
  530. {
  531. int r, unformatted;
  532. r = __superblock_all_zeroes(pmd->bm, &unformatted);
  533. if (r)
  534. return r;
  535. if (unformatted)
  536. return format_device ? __format_metadata(pmd) : -EPERM;
  537. return __open_metadata(pmd);
  538. }
  539. static int __create_persistent_data_objects(struct dm_pool_metadata *pmd, bool format_device)
  540. {
  541. int r;
  542. pmd->bm = dm_block_manager_create(pmd->bdev, THIN_METADATA_BLOCK_SIZE,
  543. THIN_METADATA_CACHE_SIZE,
  544. THIN_MAX_CONCURRENT_LOCKS);
  545. if (IS_ERR(pmd->bm)) {
  546. DMERR("could not create block manager");
  547. return PTR_ERR(pmd->bm);
  548. }
  549. r = __open_or_format_metadata(pmd, format_device);
  550. if (r)
  551. dm_block_manager_destroy(pmd->bm);
  552. return r;
  553. }
  554. static void __destroy_persistent_data_objects(struct dm_pool_metadata *pmd)
  555. {
  556. dm_sm_destroy(pmd->data_sm);
  557. dm_sm_destroy(pmd->metadata_sm);
  558. dm_tm_destroy(pmd->nb_tm);
  559. dm_tm_destroy(pmd->tm);
  560. dm_block_manager_destroy(pmd->bm);
  561. }
  562. static int __begin_transaction(struct dm_pool_metadata *pmd)
  563. {
  564. int r;
  565. struct thin_disk_superblock *disk_super;
  566. struct dm_block *sblock;
  567. /*
  568. * We re-read the superblock every time. Shouldn't need to do this
  569. * really.
  570. */
  571. r = dm_bm_read_lock(pmd->bm, THIN_SUPERBLOCK_LOCATION,
  572. &sb_validator, &sblock);
  573. if (r)
  574. return r;
  575. disk_super = dm_block_data(sblock);
  576. pmd->time = le32_to_cpu(disk_super->time);
  577. pmd->root = le64_to_cpu(disk_super->data_mapping_root);
  578. pmd->details_root = le64_to_cpu(disk_super->device_details_root);
  579. pmd->trans_id = le64_to_cpu(disk_super->trans_id);
  580. pmd->flags = le32_to_cpu(disk_super->flags);
  581. pmd->data_block_size = le32_to_cpu(disk_super->data_block_size);
  582. dm_bm_unlock(sblock);
  583. return 0;
  584. }
  585. static int __write_changed_details(struct dm_pool_metadata *pmd)
  586. {
  587. int r;
  588. struct dm_thin_device *td, *tmp;
  589. struct disk_device_details details;
  590. uint64_t key;
  591. list_for_each_entry_safe(td, tmp, &pmd->thin_devices, list) {
  592. if (!td->changed)
  593. continue;
  594. key = td->id;
  595. details.mapped_blocks = cpu_to_le64(td->mapped_blocks);
  596. details.transaction_id = cpu_to_le64(td->transaction_id);
  597. details.creation_time = cpu_to_le32(td->creation_time);
  598. details.snapshotted_time = cpu_to_le32(td->snapshotted_time);
  599. __dm_bless_for_disk(&details);
  600. r = dm_btree_insert(&pmd->details_info, pmd->details_root,
  601. &key, &details, &pmd->details_root);
  602. if (r)
  603. return r;
  604. if (td->open_count)
  605. td->changed = 0;
  606. else {
  607. list_del(&td->list);
  608. kfree(td);
  609. }
  610. }
  611. return 0;
  612. }
  613. static int __commit_transaction(struct dm_pool_metadata *pmd)
  614. {
  615. int r;
  616. size_t metadata_len, data_len;
  617. struct thin_disk_superblock *disk_super;
  618. struct dm_block *sblock;
  619. /*
  620. * We need to know if the thin_disk_superblock exceeds a 512-byte sector.
  621. */
  622. BUILD_BUG_ON(sizeof(struct thin_disk_superblock) > 512);
  623. r = __write_changed_details(pmd);
  624. if (r < 0)
  625. return r;
  626. r = dm_sm_commit(pmd->data_sm);
  627. if (r < 0)
  628. return r;
  629. r = dm_tm_pre_commit(pmd->tm);
  630. if (r < 0)
  631. return r;
  632. r = dm_sm_root_size(pmd->metadata_sm, &metadata_len);
  633. if (r < 0)
  634. return r;
  635. r = dm_sm_root_size(pmd->data_sm, &data_len);
  636. if (r < 0)
  637. return r;
  638. r = superblock_lock(pmd, &sblock);
  639. if (r)
  640. return r;
  641. disk_super = dm_block_data(sblock);
  642. disk_super->time = cpu_to_le32(pmd->time);
  643. disk_super->data_mapping_root = cpu_to_le64(pmd->root);
  644. disk_super->device_details_root = cpu_to_le64(pmd->details_root);
  645. disk_super->trans_id = cpu_to_le64(pmd->trans_id);
  646. disk_super->flags = cpu_to_le32(pmd->flags);
  647. r = dm_sm_copy_root(pmd->metadata_sm, &disk_super->metadata_space_map_root,
  648. metadata_len);
  649. if (r < 0)
  650. goto out_locked;
  651. r = dm_sm_copy_root(pmd->data_sm, &disk_super->data_space_map_root,
  652. data_len);
  653. if (r < 0)
  654. goto out_locked;
  655. return dm_tm_commit(pmd->tm, sblock);
  656. out_locked:
  657. dm_bm_unlock(sblock);
  658. return r;
  659. }
  660. struct dm_pool_metadata *dm_pool_metadata_open(struct block_device *bdev,
  661. sector_t data_block_size,
  662. bool format_device)
  663. {
  664. int r;
  665. struct dm_pool_metadata *pmd;
  666. pmd = kmalloc(sizeof(*pmd), GFP_KERNEL);
  667. if (!pmd) {
  668. DMERR("could not allocate metadata struct");
  669. return ERR_PTR(-ENOMEM);
  670. }
  671. init_rwsem(&pmd->root_lock);
  672. pmd->time = 0;
  673. INIT_LIST_HEAD(&pmd->thin_devices);
  674. pmd->read_only = false;
  675. pmd->fail_io = false;
  676. pmd->bdev = bdev;
  677. pmd->data_block_size = data_block_size;
  678. r = __create_persistent_data_objects(pmd, format_device);
  679. if (r) {
  680. kfree(pmd);
  681. return ERR_PTR(r);
  682. }
  683. r = __begin_transaction(pmd);
  684. if (r < 0) {
  685. if (dm_pool_metadata_close(pmd) < 0)
  686. DMWARN("%s: dm_pool_metadata_close() failed.", __func__);
  687. return ERR_PTR(r);
  688. }
  689. return pmd;
  690. }
  691. int dm_pool_metadata_close(struct dm_pool_metadata *pmd)
  692. {
  693. int r;
  694. unsigned open_devices = 0;
  695. struct dm_thin_device *td, *tmp;
  696. down_read(&pmd->root_lock);
  697. list_for_each_entry_safe(td, tmp, &pmd->thin_devices, list) {
  698. if (td->open_count)
  699. open_devices++;
  700. else {
  701. list_del(&td->list);
  702. kfree(td);
  703. }
  704. }
  705. up_read(&pmd->root_lock);
  706. if (open_devices) {
  707. DMERR("attempt to close pmd when %u device(s) are still open",
  708. open_devices);
  709. return -EBUSY;
  710. }
  711. if (!pmd->read_only && !pmd->fail_io) {
  712. r = __commit_transaction(pmd);
  713. if (r < 0)
  714. DMWARN("%s: __commit_transaction() failed, error = %d",
  715. __func__, r);
  716. }
  717. if (!pmd->fail_io)
  718. __destroy_persistent_data_objects(pmd);
  719. kfree(pmd);
  720. return 0;
  721. }
  722. /*
  723. * __open_device: Returns @td corresponding to device with id @dev,
  724. * creating it if @create is set and incrementing @td->open_count.
  725. * On failure, @td is undefined.
  726. */
  727. static int __open_device(struct dm_pool_metadata *pmd,
  728. dm_thin_id dev, int create,
  729. struct dm_thin_device **td)
  730. {
  731. int r, changed = 0;
  732. struct dm_thin_device *td2;
  733. uint64_t key = dev;
  734. struct disk_device_details details_le;
  735. /*
  736. * If the device is already open, return it.
  737. */
  738. list_for_each_entry(td2, &pmd->thin_devices, list)
  739. if (td2->id == dev) {
  740. /*
  741. * May not create an already-open device.
  742. */
  743. if (create)
  744. return -EEXIST;
  745. td2->open_count++;
  746. *td = td2;
  747. return 0;
  748. }
  749. /*
  750. * Check the device exists.
  751. */
  752. r = dm_btree_lookup(&pmd->details_info, pmd->details_root,
  753. &key, &details_le);
  754. if (r) {
  755. if (r != -ENODATA || !create)
  756. return r;
  757. /*
  758. * Create new device.
  759. */
  760. changed = 1;
  761. details_le.mapped_blocks = 0;
  762. details_le.transaction_id = cpu_to_le64(pmd->trans_id);
  763. details_le.creation_time = cpu_to_le32(pmd->time);
  764. details_le.snapshotted_time = cpu_to_le32(pmd->time);
  765. }
  766. *td = kmalloc(sizeof(**td), GFP_NOIO);
  767. if (!*td)
  768. return -ENOMEM;
  769. (*td)->pmd = pmd;
  770. (*td)->id = dev;
  771. (*td)->open_count = 1;
  772. (*td)->changed = changed;
  773. (*td)->aborted_with_changes = false;
  774. (*td)->mapped_blocks = le64_to_cpu(details_le.mapped_blocks);
  775. (*td)->transaction_id = le64_to_cpu(details_le.transaction_id);
  776. (*td)->creation_time = le32_to_cpu(details_le.creation_time);
  777. (*td)->snapshotted_time = le32_to_cpu(details_le.snapshotted_time);
  778. list_add(&(*td)->list, &pmd->thin_devices);
  779. return 0;
  780. }
  781. static void __close_device(struct dm_thin_device *td)
  782. {
  783. --td->open_count;
  784. }
  785. static int __create_thin(struct dm_pool_metadata *pmd,
  786. dm_thin_id dev)
  787. {
  788. int r;
  789. dm_block_t dev_root;
  790. uint64_t key = dev;
  791. struct disk_device_details details_le;
  792. struct dm_thin_device *td;
  793. __le64 value;
  794. r = dm_btree_lookup(&pmd->details_info, pmd->details_root,
  795. &key, &details_le);
  796. if (!r)
  797. return -EEXIST;
  798. /*
  799. * Create an empty btree for the mappings.
  800. */
  801. r = dm_btree_empty(&pmd->bl_info, &dev_root);
  802. if (r)
  803. return r;
  804. /*
  805. * Insert it into the main mapping tree.
  806. */
  807. value = cpu_to_le64(dev_root);
  808. __dm_bless_for_disk(&value);
  809. r = dm_btree_insert(&pmd->tl_info, pmd->root, &key, &value, &pmd->root);
  810. if (r) {
  811. dm_btree_del(&pmd->bl_info, dev_root);
  812. return r;
  813. }
  814. r = __open_device(pmd, dev, 1, &td);
  815. if (r) {
  816. dm_btree_remove(&pmd->tl_info, pmd->root, &key, &pmd->root);
  817. dm_btree_del(&pmd->bl_info, dev_root);
  818. return r;
  819. }
  820. __close_device(td);
  821. return r;
  822. }
  823. int dm_pool_create_thin(struct dm_pool_metadata *pmd, dm_thin_id dev)
  824. {
  825. int r = -EINVAL;
  826. down_write(&pmd->root_lock);
  827. if (!pmd->fail_io)
  828. r = __create_thin(pmd, dev);
  829. up_write(&pmd->root_lock);
  830. return r;
  831. }
  832. static int __set_snapshot_details(struct dm_pool_metadata *pmd,
  833. struct dm_thin_device *snap,
  834. dm_thin_id origin, uint32_t time)
  835. {
  836. int r;
  837. struct dm_thin_device *td;
  838. r = __open_device(pmd, origin, 0, &td);
  839. if (r)
  840. return r;
  841. td->changed = 1;
  842. td->snapshotted_time = time;
  843. snap->mapped_blocks = td->mapped_blocks;
  844. snap->snapshotted_time = time;
  845. __close_device(td);
  846. return 0;
  847. }
  848. static int __create_snap(struct dm_pool_metadata *pmd,
  849. dm_thin_id dev, dm_thin_id origin)
  850. {
  851. int r;
  852. dm_block_t origin_root;
  853. uint64_t key = origin, dev_key = dev;
  854. struct dm_thin_device *td;
  855. struct disk_device_details details_le;
  856. __le64 value;
  857. /* check this device is unused */
  858. r = dm_btree_lookup(&pmd->details_info, pmd->details_root,
  859. &dev_key, &details_le);
  860. if (!r)
  861. return -EEXIST;
  862. /* find the mapping tree for the origin */
  863. r = dm_btree_lookup(&pmd->tl_info, pmd->root, &key, &value);
  864. if (r)
  865. return r;
  866. origin_root = le64_to_cpu(value);
  867. /* clone the origin, an inc will do */
  868. dm_tm_inc(pmd->tm, origin_root);
  869. /* insert into the main mapping tree */
  870. value = cpu_to_le64(origin_root);
  871. __dm_bless_for_disk(&value);
  872. key = dev;
  873. r = dm_btree_insert(&pmd->tl_info, pmd->root, &key, &value, &pmd->root);
  874. if (r) {
  875. dm_tm_dec(pmd->tm, origin_root);
  876. return r;
  877. }
  878. pmd->time++;
  879. r = __open_device(pmd, dev, 1, &td);
  880. if (r)
  881. goto bad;
  882. r = __set_snapshot_details(pmd, td, origin, pmd->time);
  883. __close_device(td);
  884. if (r)
  885. goto bad;
  886. return 0;
  887. bad:
  888. dm_btree_remove(&pmd->tl_info, pmd->root, &key, &pmd->root);
  889. dm_btree_remove(&pmd->details_info, pmd->details_root,
  890. &key, &pmd->details_root);
  891. return r;
  892. }
  893. int dm_pool_create_snap(struct dm_pool_metadata *pmd,
  894. dm_thin_id dev,
  895. dm_thin_id origin)
  896. {
  897. int r = -EINVAL;
  898. down_write(&pmd->root_lock);
  899. if (!pmd->fail_io)
  900. r = __create_snap(pmd, dev, origin);
  901. up_write(&pmd->root_lock);
  902. return r;
  903. }
  904. static int __delete_device(struct dm_pool_metadata *pmd, dm_thin_id dev)
  905. {
  906. int r;
  907. uint64_t key = dev;
  908. struct dm_thin_device *td;
  909. /* TODO: failure should mark the transaction invalid */
  910. r = __open_device(pmd, dev, 0, &td);
  911. if (r)
  912. return r;
  913. if (td->open_count > 1) {
  914. __close_device(td);
  915. return -EBUSY;
  916. }
  917. list_del(&td->list);
  918. kfree(td);
  919. r = dm_btree_remove(&pmd->details_info, pmd->details_root,
  920. &key, &pmd->details_root);
  921. if (r)
  922. return r;
  923. r = dm_btree_remove(&pmd->tl_info, pmd->root, &key, &pmd->root);
  924. if (r)
  925. return r;
  926. return 0;
  927. }
  928. int dm_pool_delete_thin_device(struct dm_pool_metadata *pmd,
  929. dm_thin_id dev)
  930. {
  931. int r = -EINVAL;
  932. down_write(&pmd->root_lock);
  933. if (!pmd->fail_io)
  934. r = __delete_device(pmd, dev);
  935. up_write(&pmd->root_lock);
  936. return r;
  937. }
  938. int dm_pool_set_metadata_transaction_id(struct dm_pool_metadata *pmd,
  939. uint64_t current_id,
  940. uint64_t new_id)
  941. {
  942. int r = -EINVAL;
  943. down_write(&pmd->root_lock);
  944. if (pmd->fail_io)
  945. goto out;
  946. if (pmd->trans_id != current_id) {
  947. DMERR("mismatched transaction id");
  948. goto out;
  949. }
  950. pmd->trans_id = new_id;
  951. r = 0;
  952. out:
  953. up_write(&pmd->root_lock);
  954. return r;
  955. }
  956. int dm_pool_get_metadata_transaction_id(struct dm_pool_metadata *pmd,
  957. uint64_t *result)
  958. {
  959. int r = -EINVAL;
  960. down_read(&pmd->root_lock);
  961. if (!pmd->fail_io) {
  962. *result = pmd->trans_id;
  963. r = 0;
  964. }
  965. up_read(&pmd->root_lock);
  966. return r;
  967. }
  968. static int __reserve_metadata_snap(struct dm_pool_metadata *pmd)
  969. {
  970. int r, inc;
  971. struct thin_disk_superblock *disk_super;
  972. struct dm_block *copy, *sblock;
  973. dm_block_t held_root;
  974. /*
  975. * Copy the superblock.
  976. */
  977. dm_sm_inc_block(pmd->metadata_sm, THIN_SUPERBLOCK_LOCATION);
  978. r = dm_tm_shadow_block(pmd->tm, THIN_SUPERBLOCK_LOCATION,
  979. &sb_validator, &copy, &inc);
  980. if (r)
  981. return r;
  982. BUG_ON(!inc);
  983. held_root = dm_block_location(copy);
  984. disk_super = dm_block_data(copy);
  985. if (le64_to_cpu(disk_super->held_root)) {
  986. DMWARN("Pool metadata snapshot already exists: release this before taking another.");
  987. dm_tm_dec(pmd->tm, held_root);
  988. dm_tm_unlock(pmd->tm, copy);
  989. return -EBUSY;
  990. }
  991. /*
  992. * Wipe the spacemap since we're not publishing this.
  993. */
  994. memset(&disk_super->data_space_map_root, 0,
  995. sizeof(disk_super->data_space_map_root));
  996. memset(&disk_super->metadata_space_map_root, 0,
  997. sizeof(disk_super->metadata_space_map_root));
  998. /*
  999. * Increment the data structures that need to be preserved.
  1000. */
  1001. dm_tm_inc(pmd->tm, le64_to_cpu(disk_super->data_mapping_root));
  1002. dm_tm_inc(pmd->tm, le64_to_cpu(disk_super->device_details_root));
  1003. dm_tm_unlock(pmd->tm, copy);
  1004. /*
  1005. * Write the held root into the superblock.
  1006. */
  1007. r = superblock_lock(pmd, &sblock);
  1008. if (r) {
  1009. dm_tm_dec(pmd->tm, held_root);
  1010. return r;
  1011. }
  1012. disk_super = dm_block_data(sblock);
  1013. disk_super->held_root = cpu_to_le64(held_root);
  1014. dm_bm_unlock(sblock);
  1015. return 0;
  1016. }
  1017. int dm_pool_reserve_metadata_snap(struct dm_pool_metadata *pmd)
  1018. {
  1019. int r = -EINVAL;
  1020. down_write(&pmd->root_lock);
  1021. if (!pmd->fail_io)
  1022. r = __reserve_metadata_snap(pmd);
  1023. up_write(&pmd->root_lock);
  1024. return r;
  1025. }
  1026. static int __release_metadata_snap(struct dm_pool_metadata *pmd)
  1027. {
  1028. int r;
  1029. struct thin_disk_superblock *disk_super;
  1030. struct dm_block *sblock, *copy;
  1031. dm_block_t held_root;
  1032. r = superblock_lock(pmd, &sblock);
  1033. if (r)
  1034. return r;
  1035. disk_super = dm_block_data(sblock);
  1036. held_root = le64_to_cpu(disk_super->held_root);
  1037. disk_super->held_root = cpu_to_le64(0);
  1038. dm_bm_unlock(sblock);
  1039. if (!held_root) {
  1040. DMWARN("No pool metadata snapshot found: nothing to release.");
  1041. return -EINVAL;
  1042. }
  1043. r = dm_tm_read_lock(pmd->tm, held_root, &sb_validator, &copy);
  1044. if (r)
  1045. return r;
  1046. disk_super = dm_block_data(copy);
  1047. dm_sm_dec_block(pmd->metadata_sm, le64_to_cpu(disk_super->data_mapping_root));
  1048. dm_sm_dec_block(pmd->metadata_sm, le64_to_cpu(disk_super->device_details_root));
  1049. dm_sm_dec_block(pmd->metadata_sm, held_root);
  1050. return dm_tm_unlock(pmd->tm, copy);
  1051. }
  1052. int dm_pool_release_metadata_snap(struct dm_pool_metadata *pmd)
  1053. {
  1054. int r = -EINVAL;
  1055. down_write(&pmd->root_lock);
  1056. if (!pmd->fail_io)
  1057. r = __release_metadata_snap(pmd);
  1058. up_write(&pmd->root_lock);
  1059. return r;
  1060. }
  1061. static int __get_metadata_snap(struct dm_pool_metadata *pmd,
  1062. dm_block_t *result)
  1063. {
  1064. int r;
  1065. struct thin_disk_superblock *disk_super;
  1066. struct dm_block *sblock;
  1067. r = dm_bm_read_lock(pmd->bm, THIN_SUPERBLOCK_LOCATION,
  1068. &sb_validator, &sblock);
  1069. if (r)
  1070. return r;
  1071. disk_super = dm_block_data(sblock);
  1072. *result = le64_to_cpu(disk_super->held_root);
  1073. return dm_bm_unlock(sblock);
  1074. }
  1075. int dm_pool_get_metadata_snap(struct dm_pool_metadata *pmd,
  1076. dm_block_t *result)
  1077. {
  1078. int r = -EINVAL;
  1079. down_read(&pmd->root_lock);
  1080. if (!pmd->fail_io)
  1081. r = __get_metadata_snap(pmd, result);
  1082. up_read(&pmd->root_lock);
  1083. return r;
  1084. }
  1085. int dm_pool_open_thin_device(struct dm_pool_metadata *pmd, dm_thin_id dev,
  1086. struct dm_thin_device **td)
  1087. {
  1088. int r = -EINVAL;
  1089. down_write(&pmd->root_lock);
  1090. if (!pmd->fail_io)
  1091. r = __open_device(pmd, dev, 0, td);
  1092. up_write(&pmd->root_lock);
  1093. return r;
  1094. }
  1095. int dm_pool_close_thin_device(struct dm_thin_device *td)
  1096. {
  1097. down_write(&td->pmd->root_lock);
  1098. __close_device(td);
  1099. up_write(&td->pmd->root_lock);
  1100. return 0;
  1101. }
  1102. dm_thin_id dm_thin_dev_id(struct dm_thin_device *td)
  1103. {
  1104. return td->id;
  1105. }
  1106. static bool __snapshotted_since(struct dm_thin_device *td, uint32_t time)
  1107. {
  1108. return td->snapshotted_time > time;
  1109. }
  1110. int dm_thin_find_block(struct dm_thin_device *td, dm_block_t block,
  1111. int can_block, struct dm_thin_lookup_result *result)
  1112. {
  1113. int r = -EINVAL;
  1114. uint64_t block_time = 0;
  1115. __le64 value;
  1116. struct dm_pool_metadata *pmd = td->pmd;
  1117. dm_block_t keys[2] = { td->id, block };
  1118. struct dm_btree_info *info;
  1119. if (can_block) {
  1120. down_read(&pmd->root_lock);
  1121. info = &pmd->info;
  1122. } else if (down_read_trylock(&pmd->root_lock))
  1123. info = &pmd->nb_info;
  1124. else
  1125. return -EWOULDBLOCK;
  1126. if (pmd->fail_io)
  1127. goto out;
  1128. r = dm_btree_lookup(info, pmd->root, keys, &value);
  1129. if (!r)
  1130. block_time = le64_to_cpu(value);
  1131. out:
  1132. up_read(&pmd->root_lock);
  1133. if (!r) {
  1134. dm_block_t exception_block;
  1135. uint32_t exception_time;
  1136. unpack_block_time(block_time, &exception_block,
  1137. &exception_time);
  1138. result->block = exception_block;
  1139. result->shared = __snapshotted_since(td, exception_time);
  1140. }
  1141. return r;
  1142. }
  1143. static int __insert(struct dm_thin_device *td, dm_block_t block,
  1144. dm_block_t data_block)
  1145. {
  1146. int r, inserted;
  1147. __le64 value;
  1148. struct dm_pool_metadata *pmd = td->pmd;
  1149. dm_block_t keys[2] = { td->id, block };
  1150. value = cpu_to_le64(pack_block_time(data_block, pmd->time));
  1151. __dm_bless_for_disk(&value);
  1152. r = dm_btree_insert_notify(&pmd->info, pmd->root, keys, &value,
  1153. &pmd->root, &inserted);
  1154. if (r)
  1155. return r;
  1156. td->changed = 1;
  1157. if (inserted)
  1158. td->mapped_blocks++;
  1159. return 0;
  1160. }
  1161. int dm_thin_insert_block(struct dm_thin_device *td, dm_block_t block,
  1162. dm_block_t data_block)
  1163. {
  1164. int r = -EINVAL;
  1165. down_write(&td->pmd->root_lock);
  1166. if (!td->pmd->fail_io)
  1167. r = __insert(td, block, data_block);
  1168. up_write(&td->pmd->root_lock);
  1169. return r;
  1170. }
  1171. static int __remove(struct dm_thin_device *td, dm_block_t block)
  1172. {
  1173. int r;
  1174. struct dm_pool_metadata *pmd = td->pmd;
  1175. dm_block_t keys[2] = { td->id, block };
  1176. r = dm_btree_remove(&pmd->info, pmd->root, keys, &pmd->root);
  1177. if (r)
  1178. return r;
  1179. td->mapped_blocks--;
  1180. td->changed = 1;
  1181. return 0;
  1182. }
  1183. int dm_thin_remove_block(struct dm_thin_device *td, dm_block_t block)
  1184. {
  1185. int r = -EINVAL;
  1186. down_write(&td->pmd->root_lock);
  1187. if (!td->pmd->fail_io)
  1188. r = __remove(td, block);
  1189. up_write(&td->pmd->root_lock);
  1190. return r;
  1191. }
  1192. bool dm_thin_changed_this_transaction(struct dm_thin_device *td)
  1193. {
  1194. int r;
  1195. down_read(&td->pmd->root_lock);
  1196. r = td->changed;
  1197. up_read(&td->pmd->root_lock);
  1198. return r;
  1199. }
  1200. bool dm_thin_aborted_changes(struct dm_thin_device *td)
  1201. {
  1202. bool r;
  1203. down_read(&td->pmd->root_lock);
  1204. r = td->aborted_with_changes;
  1205. up_read(&td->pmd->root_lock);
  1206. return r;
  1207. }
  1208. int dm_pool_alloc_data_block(struct dm_pool_metadata *pmd, dm_block_t *result)
  1209. {
  1210. int r = -EINVAL;
  1211. down_write(&pmd->root_lock);
  1212. if (!pmd->fail_io)
  1213. r = dm_sm_new_block(pmd->data_sm, result);
  1214. up_write(&pmd->root_lock);
  1215. return r;
  1216. }
  1217. int dm_pool_commit_metadata(struct dm_pool_metadata *pmd)
  1218. {
  1219. int r = -EINVAL;
  1220. down_write(&pmd->root_lock);
  1221. if (pmd->fail_io)
  1222. goto out;
  1223. r = __commit_transaction(pmd);
  1224. if (r <= 0)
  1225. goto out;
  1226. /*
  1227. * Open the next transaction.
  1228. */
  1229. r = __begin_transaction(pmd);
  1230. out:
  1231. up_write(&pmd->root_lock);
  1232. return r;
  1233. }
  1234. static void __set_abort_with_changes_flags(struct dm_pool_metadata *pmd)
  1235. {
  1236. struct dm_thin_device *td;
  1237. list_for_each_entry(td, &pmd->thin_devices, list)
  1238. td->aborted_with_changes = td->changed;
  1239. }
  1240. int dm_pool_abort_metadata(struct dm_pool_metadata *pmd)
  1241. {
  1242. int r = -EINVAL;
  1243. down_write(&pmd->root_lock);
  1244. if (pmd->fail_io)
  1245. goto out;
  1246. __set_abort_with_changes_flags(pmd);
  1247. __destroy_persistent_data_objects(pmd);
  1248. r = __create_persistent_data_objects(pmd, false);
  1249. if (r)
  1250. pmd->fail_io = true;
  1251. out:
  1252. up_write(&pmd->root_lock);
  1253. return r;
  1254. }
  1255. int dm_pool_get_free_block_count(struct dm_pool_metadata *pmd, dm_block_t *result)
  1256. {
  1257. int r = -EINVAL;
  1258. down_read(&pmd->root_lock);
  1259. if (!pmd->fail_io)
  1260. r = dm_sm_get_nr_free(pmd->data_sm, result);
  1261. up_read(&pmd->root_lock);
  1262. return r;
  1263. }
  1264. int dm_pool_get_free_metadata_block_count(struct dm_pool_metadata *pmd,
  1265. dm_block_t *result)
  1266. {
  1267. int r = -EINVAL;
  1268. down_read(&pmd->root_lock);
  1269. if (!pmd->fail_io)
  1270. r = dm_sm_get_nr_free(pmd->metadata_sm, result);
  1271. up_read(&pmd->root_lock);
  1272. return r;
  1273. }
  1274. int dm_pool_get_metadata_dev_size(struct dm_pool_metadata *pmd,
  1275. dm_block_t *result)
  1276. {
  1277. int r = -EINVAL;
  1278. down_read(&pmd->root_lock);
  1279. if (!pmd->fail_io)
  1280. r = dm_sm_get_nr_blocks(pmd->metadata_sm, result);
  1281. up_read(&pmd->root_lock);
  1282. return r;
  1283. }
  1284. int dm_pool_get_data_block_size(struct dm_pool_metadata *pmd, sector_t *result)
  1285. {
  1286. down_read(&pmd->root_lock);
  1287. *result = pmd->data_block_size;
  1288. up_read(&pmd->root_lock);
  1289. return 0;
  1290. }
  1291. int dm_pool_get_data_dev_size(struct dm_pool_metadata *pmd, dm_block_t *result)
  1292. {
  1293. int r = -EINVAL;
  1294. down_read(&pmd->root_lock);
  1295. if (!pmd->fail_io)
  1296. r = dm_sm_get_nr_blocks(pmd->data_sm, result);
  1297. up_read(&pmd->root_lock);
  1298. return r;
  1299. }
  1300. int dm_thin_get_mapped_count(struct dm_thin_device *td, dm_block_t *result)
  1301. {
  1302. int r = -EINVAL;
  1303. struct dm_pool_metadata *pmd = td->pmd;
  1304. down_read(&pmd->root_lock);
  1305. if (!pmd->fail_io) {
  1306. *result = td->mapped_blocks;
  1307. r = 0;
  1308. }
  1309. up_read(&pmd->root_lock);
  1310. return r;
  1311. }
  1312. static int __highest_block(struct dm_thin_device *td, dm_block_t *result)
  1313. {
  1314. int r;
  1315. __le64 value_le;
  1316. dm_block_t thin_root;
  1317. struct dm_pool_metadata *pmd = td->pmd;
  1318. r = dm_btree_lookup(&pmd->tl_info, pmd->root, &td->id, &value_le);
  1319. if (r)
  1320. return r;
  1321. thin_root = le64_to_cpu(value_le);
  1322. return dm_btree_find_highest_key(&pmd->bl_info, thin_root, result);
  1323. }
  1324. int dm_thin_get_highest_mapped_block(struct dm_thin_device *td,
  1325. dm_block_t *result)
  1326. {
  1327. int r = -EINVAL;
  1328. struct dm_pool_metadata *pmd = td->pmd;
  1329. down_read(&pmd->root_lock);
  1330. if (!pmd->fail_io)
  1331. r = __highest_block(td, result);
  1332. up_read(&pmd->root_lock);
  1333. return r;
  1334. }
  1335. static int __resize_space_map(struct dm_space_map *sm, dm_block_t new_count)
  1336. {
  1337. int r;
  1338. dm_block_t old_count;
  1339. r = dm_sm_get_nr_blocks(sm, &old_count);
  1340. if (r)
  1341. return r;
  1342. if (new_count == old_count)
  1343. return 0;
  1344. if (new_count < old_count) {
  1345. DMERR("cannot reduce size of space map");
  1346. return -EINVAL;
  1347. }
  1348. return dm_sm_extend(sm, new_count - old_count);
  1349. }
  1350. int dm_pool_resize_data_dev(struct dm_pool_metadata *pmd, dm_block_t new_count)
  1351. {
  1352. int r = -EINVAL;
  1353. down_write(&pmd->root_lock);
  1354. if (!pmd->fail_io)
  1355. r = __resize_space_map(pmd->data_sm, new_count);
  1356. up_write(&pmd->root_lock);
  1357. return r;
  1358. }
  1359. int dm_pool_resize_metadata_dev(struct dm_pool_metadata *pmd, dm_block_t new_count)
  1360. {
  1361. int r = -EINVAL;
  1362. down_write(&pmd->root_lock);
  1363. if (!pmd->fail_io)
  1364. r = __resize_space_map(pmd->metadata_sm, new_count);
  1365. up_write(&pmd->root_lock);
  1366. return r;
  1367. }
  1368. void dm_pool_metadata_read_only(struct dm_pool_metadata *pmd)
  1369. {
  1370. down_write(&pmd->root_lock);
  1371. pmd->read_only = true;
  1372. dm_bm_set_read_only(pmd->bm);
  1373. up_write(&pmd->root_lock);
  1374. }
  1375. int dm_pool_register_metadata_threshold(struct dm_pool_metadata *pmd,
  1376. dm_block_t threshold,
  1377. dm_sm_threshold_fn fn,
  1378. void *context)
  1379. {
  1380. int r;
  1381. down_write(&pmd->root_lock);
  1382. r = dm_sm_register_threshold_callback(pmd->metadata_sm, threshold, fn, context);
  1383. up_write(&pmd->root_lock);
  1384. return r;
  1385. }