dm-thin-metadata.c 47 KB

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