dm-thin-metadata.c 38 KB

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