dm-thin-metadata.c 41 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815
  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_block, struct dm_thin_lookup_result *result)
  1136. {
  1137. int r = -EINVAL;
  1138. uint64_t block_time = 0;
  1139. __le64 value;
  1140. struct dm_pool_metadata *pmd = td->pmd;
  1141. dm_block_t keys[2] = { td->id, block };
  1142. struct dm_btree_info *info;
  1143. if (can_block) {
  1144. down_read(&pmd->root_lock);
  1145. info = &pmd->info;
  1146. } else if (down_read_trylock(&pmd->root_lock))
  1147. info = &pmd->nb_info;
  1148. else
  1149. return -EWOULDBLOCK;
  1150. if (pmd->fail_io)
  1151. goto out;
  1152. r = dm_btree_lookup(info, pmd->root, keys, &value);
  1153. if (!r)
  1154. block_time = le64_to_cpu(value);
  1155. out:
  1156. up_read(&pmd->root_lock);
  1157. if (!r) {
  1158. dm_block_t exception_block;
  1159. uint32_t exception_time;
  1160. unpack_block_time(block_time, &exception_block,
  1161. &exception_time);
  1162. result->block = exception_block;
  1163. result->shared = __snapshotted_since(td, exception_time);
  1164. }
  1165. return r;
  1166. }
  1167. static int __insert(struct dm_thin_device *td, dm_block_t block,
  1168. dm_block_t data_block)
  1169. {
  1170. int r, inserted;
  1171. __le64 value;
  1172. struct dm_pool_metadata *pmd = td->pmd;
  1173. dm_block_t keys[2] = { td->id, block };
  1174. value = cpu_to_le64(pack_block_time(data_block, pmd->time));
  1175. __dm_bless_for_disk(&value);
  1176. r = dm_btree_insert_notify(&pmd->info, pmd->root, keys, &value,
  1177. &pmd->root, &inserted);
  1178. if (r)
  1179. return r;
  1180. td->changed = 1;
  1181. if (inserted)
  1182. td->mapped_blocks++;
  1183. return 0;
  1184. }
  1185. int dm_thin_insert_block(struct dm_thin_device *td, dm_block_t block,
  1186. dm_block_t data_block)
  1187. {
  1188. int r = -EINVAL;
  1189. down_write(&td->pmd->root_lock);
  1190. if (!td->pmd->fail_io)
  1191. r = __insert(td, block, data_block);
  1192. up_write(&td->pmd->root_lock);
  1193. return r;
  1194. }
  1195. static int __remove(struct dm_thin_device *td, dm_block_t block)
  1196. {
  1197. int r;
  1198. struct dm_pool_metadata *pmd = td->pmd;
  1199. dm_block_t keys[2] = { td->id, block };
  1200. r = dm_btree_remove(&pmd->info, pmd->root, keys, &pmd->root);
  1201. if (r)
  1202. return r;
  1203. td->mapped_blocks--;
  1204. td->changed = 1;
  1205. return 0;
  1206. }
  1207. int dm_thin_remove_block(struct dm_thin_device *td, dm_block_t block)
  1208. {
  1209. int r = -EINVAL;
  1210. down_write(&td->pmd->root_lock);
  1211. if (!td->pmd->fail_io)
  1212. r = __remove(td, block);
  1213. up_write(&td->pmd->root_lock);
  1214. return r;
  1215. }
  1216. int dm_pool_block_is_used(struct dm_pool_metadata *pmd, dm_block_t b, bool *result)
  1217. {
  1218. int r;
  1219. uint32_t ref_count;
  1220. down_read(&pmd->root_lock);
  1221. r = dm_sm_get_count(pmd->data_sm, b, &ref_count);
  1222. if (!r)
  1223. *result = (ref_count != 0);
  1224. up_read(&pmd->root_lock);
  1225. return r;
  1226. }
  1227. bool dm_thin_changed_this_transaction(struct dm_thin_device *td)
  1228. {
  1229. int r;
  1230. down_read(&td->pmd->root_lock);
  1231. r = td->changed;
  1232. up_read(&td->pmd->root_lock);
  1233. return r;
  1234. }
  1235. bool dm_pool_changed_this_transaction(struct dm_pool_metadata *pmd)
  1236. {
  1237. bool r = false;
  1238. struct dm_thin_device *td, *tmp;
  1239. down_read(&pmd->root_lock);
  1240. list_for_each_entry_safe(td, tmp, &pmd->thin_devices, list) {
  1241. if (td->changed) {
  1242. r = td->changed;
  1243. break;
  1244. }
  1245. }
  1246. up_read(&pmd->root_lock);
  1247. return r;
  1248. }
  1249. bool dm_thin_aborted_changes(struct dm_thin_device *td)
  1250. {
  1251. bool r;
  1252. down_read(&td->pmd->root_lock);
  1253. r = td->aborted_with_changes;
  1254. up_read(&td->pmd->root_lock);
  1255. return r;
  1256. }
  1257. int dm_pool_alloc_data_block(struct dm_pool_metadata *pmd, dm_block_t *result)
  1258. {
  1259. int r = -EINVAL;
  1260. down_write(&pmd->root_lock);
  1261. if (!pmd->fail_io)
  1262. r = dm_sm_new_block(pmd->data_sm, result);
  1263. up_write(&pmd->root_lock);
  1264. return r;
  1265. }
  1266. int dm_pool_commit_metadata(struct dm_pool_metadata *pmd)
  1267. {
  1268. int r = -EINVAL;
  1269. down_write(&pmd->root_lock);
  1270. if (pmd->fail_io)
  1271. goto out;
  1272. r = __commit_transaction(pmd);
  1273. if (r <= 0)
  1274. goto out;
  1275. /*
  1276. * Open the next transaction.
  1277. */
  1278. r = __begin_transaction(pmd);
  1279. out:
  1280. up_write(&pmd->root_lock);
  1281. return r;
  1282. }
  1283. static void __set_abort_with_changes_flags(struct dm_pool_metadata *pmd)
  1284. {
  1285. struct dm_thin_device *td;
  1286. list_for_each_entry(td, &pmd->thin_devices, list)
  1287. td->aborted_with_changes = td->changed;
  1288. }
  1289. int dm_pool_abort_metadata(struct dm_pool_metadata *pmd)
  1290. {
  1291. int r = -EINVAL;
  1292. down_write(&pmd->root_lock);
  1293. if (pmd->fail_io)
  1294. goto out;
  1295. __set_abort_with_changes_flags(pmd);
  1296. __destroy_persistent_data_objects(pmd);
  1297. r = __create_persistent_data_objects(pmd, false);
  1298. if (r)
  1299. pmd->fail_io = true;
  1300. out:
  1301. up_write(&pmd->root_lock);
  1302. return r;
  1303. }
  1304. int dm_pool_get_free_block_count(struct dm_pool_metadata *pmd, dm_block_t *result)
  1305. {
  1306. int r = -EINVAL;
  1307. down_read(&pmd->root_lock);
  1308. if (!pmd->fail_io)
  1309. r = dm_sm_get_nr_free(pmd->data_sm, result);
  1310. up_read(&pmd->root_lock);
  1311. return r;
  1312. }
  1313. int dm_pool_get_free_metadata_block_count(struct dm_pool_metadata *pmd,
  1314. dm_block_t *result)
  1315. {
  1316. int r = -EINVAL;
  1317. down_read(&pmd->root_lock);
  1318. if (!pmd->fail_io)
  1319. r = dm_sm_get_nr_free(pmd->metadata_sm, result);
  1320. up_read(&pmd->root_lock);
  1321. return r;
  1322. }
  1323. int dm_pool_get_metadata_dev_size(struct dm_pool_metadata *pmd,
  1324. dm_block_t *result)
  1325. {
  1326. int r = -EINVAL;
  1327. down_read(&pmd->root_lock);
  1328. if (!pmd->fail_io)
  1329. r = dm_sm_get_nr_blocks(pmd->metadata_sm, result);
  1330. up_read(&pmd->root_lock);
  1331. return r;
  1332. }
  1333. int dm_pool_get_data_block_size(struct dm_pool_metadata *pmd, sector_t *result)
  1334. {
  1335. down_read(&pmd->root_lock);
  1336. *result = pmd->data_block_size;
  1337. up_read(&pmd->root_lock);
  1338. return 0;
  1339. }
  1340. int dm_pool_get_data_dev_size(struct dm_pool_metadata *pmd, dm_block_t *result)
  1341. {
  1342. int r = -EINVAL;
  1343. down_read(&pmd->root_lock);
  1344. if (!pmd->fail_io)
  1345. r = dm_sm_get_nr_blocks(pmd->data_sm, result);
  1346. up_read(&pmd->root_lock);
  1347. return r;
  1348. }
  1349. int dm_thin_get_mapped_count(struct dm_thin_device *td, dm_block_t *result)
  1350. {
  1351. int r = -EINVAL;
  1352. struct dm_pool_metadata *pmd = td->pmd;
  1353. down_read(&pmd->root_lock);
  1354. if (!pmd->fail_io) {
  1355. *result = td->mapped_blocks;
  1356. r = 0;
  1357. }
  1358. up_read(&pmd->root_lock);
  1359. return r;
  1360. }
  1361. static int __highest_block(struct dm_thin_device *td, dm_block_t *result)
  1362. {
  1363. int r;
  1364. __le64 value_le;
  1365. dm_block_t thin_root;
  1366. struct dm_pool_metadata *pmd = td->pmd;
  1367. r = dm_btree_lookup(&pmd->tl_info, pmd->root, &td->id, &value_le);
  1368. if (r)
  1369. return r;
  1370. thin_root = le64_to_cpu(value_le);
  1371. return dm_btree_find_highest_key(&pmd->bl_info, thin_root, result);
  1372. }
  1373. int dm_thin_get_highest_mapped_block(struct dm_thin_device *td,
  1374. dm_block_t *result)
  1375. {
  1376. int r = -EINVAL;
  1377. struct dm_pool_metadata *pmd = td->pmd;
  1378. down_read(&pmd->root_lock);
  1379. if (!pmd->fail_io)
  1380. r = __highest_block(td, result);
  1381. up_read(&pmd->root_lock);
  1382. return r;
  1383. }
  1384. static int __resize_space_map(struct dm_space_map *sm, dm_block_t new_count)
  1385. {
  1386. int r;
  1387. dm_block_t old_count;
  1388. r = dm_sm_get_nr_blocks(sm, &old_count);
  1389. if (r)
  1390. return r;
  1391. if (new_count == old_count)
  1392. return 0;
  1393. if (new_count < old_count) {
  1394. DMERR("cannot reduce size of space map");
  1395. return -EINVAL;
  1396. }
  1397. return dm_sm_extend(sm, new_count - old_count);
  1398. }
  1399. int dm_pool_resize_data_dev(struct dm_pool_metadata *pmd, dm_block_t new_count)
  1400. {
  1401. int r = -EINVAL;
  1402. down_write(&pmd->root_lock);
  1403. if (!pmd->fail_io)
  1404. r = __resize_space_map(pmd->data_sm, new_count);
  1405. up_write(&pmd->root_lock);
  1406. return r;
  1407. }
  1408. int dm_pool_resize_metadata_dev(struct dm_pool_metadata *pmd, dm_block_t new_count)
  1409. {
  1410. int r = -EINVAL;
  1411. down_write(&pmd->root_lock);
  1412. if (!pmd->fail_io)
  1413. r = __resize_space_map(pmd->metadata_sm, new_count);
  1414. up_write(&pmd->root_lock);
  1415. return r;
  1416. }
  1417. void dm_pool_metadata_read_only(struct dm_pool_metadata *pmd)
  1418. {
  1419. down_write(&pmd->root_lock);
  1420. pmd->read_only = true;
  1421. dm_bm_set_read_only(pmd->bm);
  1422. up_write(&pmd->root_lock);
  1423. }
  1424. void dm_pool_metadata_read_write(struct dm_pool_metadata *pmd)
  1425. {
  1426. down_write(&pmd->root_lock);
  1427. pmd->read_only = false;
  1428. dm_bm_set_read_write(pmd->bm);
  1429. up_write(&pmd->root_lock);
  1430. }
  1431. int dm_pool_register_metadata_threshold(struct dm_pool_metadata *pmd,
  1432. dm_block_t threshold,
  1433. dm_sm_threshold_fn fn,
  1434. void *context)
  1435. {
  1436. int r;
  1437. down_write(&pmd->root_lock);
  1438. r = dm_sm_register_threshold_callback(pmd->metadata_sm, threshold, fn, context);
  1439. up_write(&pmd->root_lock);
  1440. return r;
  1441. }
  1442. int dm_pool_metadata_set_needs_check(struct dm_pool_metadata *pmd)
  1443. {
  1444. int r;
  1445. struct dm_block *sblock;
  1446. struct thin_disk_superblock *disk_super;
  1447. down_write(&pmd->root_lock);
  1448. pmd->flags |= THIN_METADATA_NEEDS_CHECK_FLAG;
  1449. r = superblock_lock(pmd, &sblock);
  1450. if (r) {
  1451. DMERR("couldn't read superblock");
  1452. goto out;
  1453. }
  1454. disk_super = dm_block_data(sblock);
  1455. disk_super->flags = cpu_to_le32(pmd->flags);
  1456. dm_bm_unlock(sblock);
  1457. out:
  1458. up_write(&pmd->root_lock);
  1459. return r;
  1460. }
  1461. bool dm_pool_metadata_needs_check(struct dm_pool_metadata *pmd)
  1462. {
  1463. bool needs_check;
  1464. down_read(&pmd->root_lock);
  1465. needs_check = pmd->flags & THIN_METADATA_NEEDS_CHECK_FLAG;
  1466. up_read(&pmd->root_lock);
  1467. return needs_check;
  1468. }