dm-thin-metadata.c 45 KB

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