dm-thin-metadata.c 41 KB

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