dm-thin-metadata.c 45 KB

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