PageRenderTime 78ms CodeModel.GetById 24ms RepoModel.GetById 0ms app.codeStats 1ms

/drivers/md/dm-thin.c

http://github.com/mirrors/linux
C | 4549 lines | 3149 code | 782 blank | 618 comment | 431 complexity | 8b180e4f08d96d2cd5c069d9d49c4789 MD5 | raw file
Possible License(s): AGPL-1.0, GPL-2.0, LGPL-2.0
  1. /*
  2. * Copyright (C) 2011-2012 Red Hat UK.
  3. *
  4. * This file is released under the GPL.
  5. */
  6. #include "dm-thin-metadata.h"
  7. #include "dm-bio-prison-v1.h"
  8. #include "dm.h"
  9. #include <linux/device-mapper.h>
  10. #include <linux/dm-io.h>
  11. #include <linux/dm-kcopyd.h>
  12. #include <linux/jiffies.h>
  13. #include <linux/log2.h>
  14. #include <linux/list.h>
  15. #include <linux/rculist.h>
  16. #include <linux/init.h>
  17. #include <linux/module.h>
  18. #include <linux/slab.h>
  19. #include <linux/vmalloc.h>
  20. #include <linux/sort.h>
  21. #include <linux/rbtree.h>
  22. #define DM_MSG_PREFIX "thin"
  23. /*
  24. * Tunable constants
  25. */
  26. #define ENDIO_HOOK_POOL_SIZE 1024
  27. #define MAPPING_POOL_SIZE 1024
  28. #define COMMIT_PERIOD HZ
  29. #define NO_SPACE_TIMEOUT_SECS 60
  30. static unsigned no_space_timeout_secs = NO_SPACE_TIMEOUT_SECS;
  31. DECLARE_DM_KCOPYD_THROTTLE_WITH_MODULE_PARM(snapshot_copy_throttle,
  32. "A percentage of time allocated for copy on write");
  33. /*
  34. * The block size of the device holding pool data must be
  35. * between 64KB and 1GB.
  36. */
  37. #define DATA_DEV_BLOCK_SIZE_MIN_SECTORS (64 * 1024 >> SECTOR_SHIFT)
  38. #define DATA_DEV_BLOCK_SIZE_MAX_SECTORS (1024 * 1024 * 1024 >> SECTOR_SHIFT)
  39. /*
  40. * Device id is restricted to 24 bits.
  41. */
  42. #define MAX_DEV_ID ((1 << 24) - 1)
  43. /*
  44. * How do we handle breaking sharing of data blocks?
  45. * =================================================
  46. *
  47. * We use a standard copy-on-write btree to store the mappings for the
  48. * devices (note I'm talking about copy-on-write of the metadata here, not
  49. * the data). When you take an internal snapshot you clone the root node
  50. * of the origin btree. After this there is no concept of an origin or a
  51. * snapshot. They are just two device trees that happen to point to the
  52. * same data blocks.
  53. *
  54. * When we get a write in we decide if it's to a shared data block using
  55. * some timestamp magic. If it is, we have to break sharing.
  56. *
  57. * Let's say we write to a shared block in what was the origin. The
  58. * steps are:
  59. *
  60. * i) plug io further to this physical block. (see bio_prison code).
  61. *
  62. * ii) quiesce any read io to that shared data block. Obviously
  63. * including all devices that share this block. (see dm_deferred_set code)
  64. *
  65. * iii) copy the data block to a newly allocate block. This step can be
  66. * missed out if the io covers the block. (schedule_copy).
  67. *
  68. * iv) insert the new mapping into the origin's btree
  69. * (process_prepared_mapping). This act of inserting breaks some
  70. * sharing of btree nodes between the two devices. Breaking sharing only
  71. * effects the btree of that specific device. Btrees for the other
  72. * devices that share the block never change. The btree for the origin
  73. * device as it was after the last commit is untouched, ie. we're using
  74. * persistent data structures in the functional programming sense.
  75. *
  76. * v) unplug io to this physical block, including the io that triggered
  77. * the breaking of sharing.
  78. *
  79. * Steps (ii) and (iii) occur in parallel.
  80. *
  81. * The metadata _doesn't_ need to be committed before the io continues. We
  82. * get away with this because the io is always written to a _new_ block.
  83. * If there's a crash, then:
  84. *
  85. * - The origin mapping will point to the old origin block (the shared
  86. * one). This will contain the data as it was before the io that triggered
  87. * the breaking of sharing came in.
  88. *
  89. * - The snap mapping still points to the old block. As it would after
  90. * the commit.
  91. *
  92. * The downside of this scheme is the timestamp magic isn't perfect, and
  93. * will continue to think that data block in the snapshot device is shared
  94. * even after the write to the origin has broken sharing. I suspect data
  95. * blocks will typically be shared by many different devices, so we're
  96. * breaking sharing n + 1 times, rather than n, where n is the number of
  97. * devices that reference this data block. At the moment I think the
  98. * benefits far, far outweigh the disadvantages.
  99. */
  100. /*----------------------------------------------------------------*/
  101. /*
  102. * Key building.
  103. */
  104. enum lock_space {
  105. VIRTUAL,
  106. PHYSICAL
  107. };
  108. static void build_key(struct dm_thin_device *td, enum lock_space ls,
  109. dm_block_t b, dm_block_t e, struct dm_cell_key *key)
  110. {
  111. key->virtual = (ls == VIRTUAL);
  112. key->dev = dm_thin_dev_id(td);
  113. key->block_begin = b;
  114. key->block_end = e;
  115. }
  116. static void build_data_key(struct dm_thin_device *td, dm_block_t b,
  117. struct dm_cell_key *key)
  118. {
  119. build_key(td, PHYSICAL, b, b + 1llu, key);
  120. }
  121. static void build_virtual_key(struct dm_thin_device *td, dm_block_t b,
  122. struct dm_cell_key *key)
  123. {
  124. build_key(td, VIRTUAL, b, b + 1llu, key);
  125. }
  126. /*----------------------------------------------------------------*/
  127. #define THROTTLE_THRESHOLD (1 * HZ)
  128. struct throttle {
  129. struct rw_semaphore lock;
  130. unsigned long threshold;
  131. bool throttle_applied;
  132. };
  133. static void throttle_init(struct throttle *t)
  134. {
  135. init_rwsem(&t->lock);
  136. t->throttle_applied = false;
  137. }
  138. static void throttle_work_start(struct throttle *t)
  139. {
  140. t->threshold = jiffies + THROTTLE_THRESHOLD;
  141. }
  142. static void throttle_work_update(struct throttle *t)
  143. {
  144. if (!t->throttle_applied && jiffies > t->threshold) {
  145. down_write(&t->lock);
  146. t->throttle_applied = true;
  147. }
  148. }
  149. static void throttle_work_complete(struct throttle *t)
  150. {
  151. if (t->throttle_applied) {
  152. t->throttle_applied = false;
  153. up_write(&t->lock);
  154. }
  155. }
  156. static void throttle_lock(struct throttle *t)
  157. {
  158. down_read(&t->lock);
  159. }
  160. static void throttle_unlock(struct throttle *t)
  161. {
  162. up_read(&t->lock);
  163. }
  164. /*----------------------------------------------------------------*/
  165. /*
  166. * A pool device ties together a metadata device and a data device. It
  167. * also provides the interface for creating and destroying internal
  168. * devices.
  169. */
  170. struct dm_thin_new_mapping;
  171. /*
  172. * The pool runs in various modes. Ordered in degraded order for comparisons.
  173. */
  174. enum pool_mode {
  175. PM_WRITE, /* metadata may be changed */
  176. PM_OUT_OF_DATA_SPACE, /* metadata may be changed, though data may not be allocated */
  177. /*
  178. * Like READ_ONLY, except may switch back to WRITE on metadata resize. Reported as READ_ONLY.
  179. */
  180. PM_OUT_OF_METADATA_SPACE,
  181. PM_READ_ONLY, /* metadata may not be changed */
  182. PM_FAIL, /* all I/O fails */
  183. };
  184. struct pool_features {
  185. enum pool_mode mode;
  186. bool zero_new_blocks:1;
  187. bool discard_enabled:1;
  188. bool discard_passdown:1;
  189. bool error_if_no_space:1;
  190. };
  191. struct thin_c;
  192. typedef void (*process_bio_fn)(struct thin_c *tc, struct bio *bio);
  193. typedef void (*process_cell_fn)(struct thin_c *tc, struct dm_bio_prison_cell *cell);
  194. typedef void (*process_mapping_fn)(struct dm_thin_new_mapping *m);
  195. #define CELL_SORT_ARRAY_SIZE 8192
  196. struct pool {
  197. struct list_head list;
  198. struct dm_target *ti; /* Only set if a pool target is bound */
  199. struct mapped_device *pool_md;
  200. struct block_device *data_dev;
  201. struct block_device *md_dev;
  202. struct dm_pool_metadata *pmd;
  203. dm_block_t low_water_blocks;
  204. uint32_t sectors_per_block;
  205. int sectors_per_block_shift;
  206. struct pool_features pf;
  207. bool low_water_triggered:1; /* A dm event has been sent */
  208. bool suspended:1;
  209. bool out_of_data_space:1;
  210. struct dm_bio_prison *prison;
  211. struct dm_kcopyd_client *copier;
  212. struct work_struct worker;
  213. struct workqueue_struct *wq;
  214. struct throttle throttle;
  215. struct delayed_work waker;
  216. struct delayed_work no_space_timeout;
  217. unsigned long last_commit_jiffies;
  218. unsigned ref_count;
  219. spinlock_t lock;
  220. struct bio_list deferred_flush_bios;
  221. struct bio_list deferred_flush_completions;
  222. struct list_head prepared_mappings;
  223. struct list_head prepared_discards;
  224. struct list_head prepared_discards_pt2;
  225. struct list_head active_thins;
  226. struct dm_deferred_set *shared_read_ds;
  227. struct dm_deferred_set *all_io_ds;
  228. struct dm_thin_new_mapping *next_mapping;
  229. process_bio_fn process_bio;
  230. process_bio_fn process_discard;
  231. process_cell_fn process_cell;
  232. process_cell_fn process_discard_cell;
  233. process_mapping_fn process_prepared_mapping;
  234. process_mapping_fn process_prepared_discard;
  235. process_mapping_fn process_prepared_discard_pt2;
  236. struct dm_bio_prison_cell **cell_sort_array;
  237. mempool_t mapping_pool;
  238. struct bio flush_bio;
  239. };
  240. static void metadata_operation_failed(struct pool *pool, const char *op, int r);
  241. static enum pool_mode get_pool_mode(struct pool *pool)
  242. {
  243. return pool->pf.mode;
  244. }
  245. static void notify_of_pool_mode_change(struct pool *pool)
  246. {
  247. const char *descs[] = {
  248. "write",
  249. "out-of-data-space",
  250. "read-only",
  251. "read-only",
  252. "fail"
  253. };
  254. const char *extra_desc = NULL;
  255. enum pool_mode mode = get_pool_mode(pool);
  256. if (mode == PM_OUT_OF_DATA_SPACE) {
  257. if (!pool->pf.error_if_no_space)
  258. extra_desc = " (queue IO)";
  259. else
  260. extra_desc = " (error IO)";
  261. }
  262. dm_table_event(pool->ti->table);
  263. DMINFO("%s: switching pool to %s%s mode",
  264. dm_device_name(pool->pool_md),
  265. descs[(int)mode], extra_desc ? : "");
  266. }
  267. /*
  268. * Target context for a pool.
  269. */
  270. struct pool_c {
  271. struct dm_target *ti;
  272. struct pool *pool;
  273. struct dm_dev *data_dev;
  274. struct dm_dev *metadata_dev;
  275. struct dm_target_callbacks callbacks;
  276. dm_block_t low_water_blocks;
  277. struct pool_features requested_pf; /* Features requested during table load */
  278. struct pool_features adjusted_pf; /* Features used after adjusting for constituent devices */
  279. };
  280. /*
  281. * Target context for a thin.
  282. */
  283. struct thin_c {
  284. struct list_head list;
  285. struct dm_dev *pool_dev;
  286. struct dm_dev *origin_dev;
  287. sector_t origin_size;
  288. dm_thin_id dev_id;
  289. struct pool *pool;
  290. struct dm_thin_device *td;
  291. struct mapped_device *thin_md;
  292. bool requeue_mode:1;
  293. spinlock_t lock;
  294. struct list_head deferred_cells;
  295. struct bio_list deferred_bio_list;
  296. struct bio_list retry_on_resume_list;
  297. struct rb_root sort_bio_list; /* sorted list of deferred bios */
  298. /*
  299. * Ensures the thin is not destroyed until the worker has finished
  300. * iterating the active_thins list.
  301. */
  302. refcount_t refcount;
  303. struct completion can_destroy;
  304. };
  305. /*----------------------------------------------------------------*/
  306. static bool block_size_is_power_of_two(struct pool *pool)
  307. {
  308. return pool->sectors_per_block_shift >= 0;
  309. }
  310. static sector_t block_to_sectors(struct pool *pool, dm_block_t b)
  311. {
  312. return block_size_is_power_of_two(pool) ?
  313. (b << pool->sectors_per_block_shift) :
  314. (b * pool->sectors_per_block);
  315. }
  316. /*----------------------------------------------------------------*/
  317. struct discard_op {
  318. struct thin_c *tc;
  319. struct blk_plug plug;
  320. struct bio *parent_bio;
  321. struct bio *bio;
  322. };
  323. static void begin_discard(struct discard_op *op, struct thin_c *tc, struct bio *parent)
  324. {
  325. BUG_ON(!parent);
  326. op->tc = tc;
  327. blk_start_plug(&op->plug);
  328. op->parent_bio = parent;
  329. op->bio = NULL;
  330. }
  331. static int issue_discard(struct discard_op *op, dm_block_t data_b, dm_block_t data_e)
  332. {
  333. struct thin_c *tc = op->tc;
  334. sector_t s = block_to_sectors(tc->pool, data_b);
  335. sector_t len = block_to_sectors(tc->pool, data_e - data_b);
  336. return __blkdev_issue_discard(tc->pool_dev->bdev, s, len,
  337. GFP_NOWAIT, 0, &op->bio);
  338. }
  339. static void end_discard(struct discard_op *op, int r)
  340. {
  341. if (op->bio) {
  342. /*
  343. * Even if one of the calls to issue_discard failed, we
  344. * need to wait for the chain to complete.
  345. */
  346. bio_chain(op->bio, op->parent_bio);
  347. bio_set_op_attrs(op->bio, REQ_OP_DISCARD, 0);
  348. submit_bio(op->bio);
  349. }
  350. blk_finish_plug(&op->plug);
  351. /*
  352. * Even if r is set, there could be sub discards in flight that we
  353. * need to wait for.
  354. */
  355. if (r && !op->parent_bio->bi_status)
  356. op->parent_bio->bi_status = errno_to_blk_status(r);
  357. bio_endio(op->parent_bio);
  358. }
  359. /*----------------------------------------------------------------*/
  360. /*
  361. * wake_worker() is used when new work is queued and when pool_resume is
  362. * ready to continue deferred IO processing.
  363. */
  364. static void wake_worker(struct pool *pool)
  365. {
  366. queue_work(pool->wq, &pool->worker);
  367. }
  368. /*----------------------------------------------------------------*/
  369. static int bio_detain(struct pool *pool, struct dm_cell_key *key, struct bio *bio,
  370. struct dm_bio_prison_cell **cell_result)
  371. {
  372. int r;
  373. struct dm_bio_prison_cell *cell_prealloc;
  374. /*
  375. * Allocate a cell from the prison's mempool.
  376. * This might block but it can't fail.
  377. */
  378. cell_prealloc = dm_bio_prison_alloc_cell(pool->prison, GFP_NOIO);
  379. r = dm_bio_detain(pool->prison, key, bio, cell_prealloc, cell_result);
  380. if (r)
  381. /*
  382. * We reused an old cell; we can get rid of
  383. * the new one.
  384. */
  385. dm_bio_prison_free_cell(pool->prison, cell_prealloc);
  386. return r;
  387. }
  388. static void cell_release(struct pool *pool,
  389. struct dm_bio_prison_cell *cell,
  390. struct bio_list *bios)
  391. {
  392. dm_cell_release(pool->prison, cell, bios);
  393. dm_bio_prison_free_cell(pool->prison, cell);
  394. }
  395. static void cell_visit_release(struct pool *pool,
  396. void (*fn)(void *, struct dm_bio_prison_cell *),
  397. void *context,
  398. struct dm_bio_prison_cell *cell)
  399. {
  400. dm_cell_visit_release(pool->prison, fn, context, cell);
  401. dm_bio_prison_free_cell(pool->prison, cell);
  402. }
  403. static void cell_release_no_holder(struct pool *pool,
  404. struct dm_bio_prison_cell *cell,
  405. struct bio_list *bios)
  406. {
  407. dm_cell_release_no_holder(pool->prison, cell, bios);
  408. dm_bio_prison_free_cell(pool->prison, cell);
  409. }
  410. static void cell_error_with_code(struct pool *pool,
  411. struct dm_bio_prison_cell *cell, blk_status_t error_code)
  412. {
  413. dm_cell_error(pool->prison, cell, error_code);
  414. dm_bio_prison_free_cell(pool->prison, cell);
  415. }
  416. static blk_status_t get_pool_io_error_code(struct pool *pool)
  417. {
  418. return pool->out_of_data_space ? BLK_STS_NOSPC : BLK_STS_IOERR;
  419. }
  420. static void cell_error(struct pool *pool, struct dm_bio_prison_cell *cell)
  421. {
  422. cell_error_with_code(pool, cell, get_pool_io_error_code(pool));
  423. }
  424. static void cell_success(struct pool *pool, struct dm_bio_prison_cell *cell)
  425. {
  426. cell_error_with_code(pool, cell, 0);
  427. }
  428. static void cell_requeue(struct pool *pool, struct dm_bio_prison_cell *cell)
  429. {
  430. cell_error_with_code(pool, cell, BLK_STS_DM_REQUEUE);
  431. }
  432. /*----------------------------------------------------------------*/
  433. /*
  434. * A global list of pools that uses a struct mapped_device as a key.
  435. */
  436. static struct dm_thin_pool_table {
  437. struct mutex mutex;
  438. struct list_head pools;
  439. } dm_thin_pool_table;
  440. static void pool_table_init(void)
  441. {
  442. mutex_init(&dm_thin_pool_table.mutex);
  443. INIT_LIST_HEAD(&dm_thin_pool_table.pools);
  444. }
  445. static void pool_table_exit(void)
  446. {
  447. mutex_destroy(&dm_thin_pool_table.mutex);
  448. }
  449. static void __pool_table_insert(struct pool *pool)
  450. {
  451. BUG_ON(!mutex_is_locked(&dm_thin_pool_table.mutex));
  452. list_add(&pool->list, &dm_thin_pool_table.pools);
  453. }
  454. static void __pool_table_remove(struct pool *pool)
  455. {
  456. BUG_ON(!mutex_is_locked(&dm_thin_pool_table.mutex));
  457. list_del(&pool->list);
  458. }
  459. static struct pool *__pool_table_lookup(struct mapped_device *md)
  460. {
  461. struct pool *pool = NULL, *tmp;
  462. BUG_ON(!mutex_is_locked(&dm_thin_pool_table.mutex));
  463. list_for_each_entry(tmp, &dm_thin_pool_table.pools, list) {
  464. if (tmp->pool_md == md) {
  465. pool = tmp;
  466. break;
  467. }
  468. }
  469. return pool;
  470. }
  471. static struct pool *__pool_table_lookup_metadata_dev(struct block_device *md_dev)
  472. {
  473. struct pool *pool = NULL, *tmp;
  474. BUG_ON(!mutex_is_locked(&dm_thin_pool_table.mutex));
  475. list_for_each_entry(tmp, &dm_thin_pool_table.pools, list) {
  476. if (tmp->md_dev == md_dev) {
  477. pool = tmp;
  478. break;
  479. }
  480. }
  481. return pool;
  482. }
  483. /*----------------------------------------------------------------*/
  484. struct dm_thin_endio_hook {
  485. struct thin_c *tc;
  486. struct dm_deferred_entry *shared_read_entry;
  487. struct dm_deferred_entry *all_io_entry;
  488. struct dm_thin_new_mapping *overwrite_mapping;
  489. struct rb_node rb_node;
  490. struct dm_bio_prison_cell *cell;
  491. };
  492. static void __merge_bio_list(struct bio_list *bios, struct bio_list *master)
  493. {
  494. bio_list_merge(bios, master);
  495. bio_list_init(master);
  496. }
  497. static void error_bio_list(struct bio_list *bios, blk_status_t error)
  498. {
  499. struct bio *bio;
  500. while ((bio = bio_list_pop(bios))) {
  501. bio->bi_status = error;
  502. bio_endio(bio);
  503. }
  504. }
  505. static void error_thin_bio_list(struct thin_c *tc, struct bio_list *master,
  506. blk_status_t error)
  507. {
  508. struct bio_list bios;
  509. bio_list_init(&bios);
  510. spin_lock_irq(&tc->lock);
  511. __merge_bio_list(&bios, master);
  512. spin_unlock_irq(&tc->lock);
  513. error_bio_list(&bios, error);
  514. }
  515. static void requeue_deferred_cells(struct thin_c *tc)
  516. {
  517. struct pool *pool = tc->pool;
  518. struct list_head cells;
  519. struct dm_bio_prison_cell *cell, *tmp;
  520. INIT_LIST_HEAD(&cells);
  521. spin_lock_irq(&tc->lock);
  522. list_splice_init(&tc->deferred_cells, &cells);
  523. spin_unlock_irq(&tc->lock);
  524. list_for_each_entry_safe(cell, tmp, &cells, user_list)
  525. cell_requeue(pool, cell);
  526. }
  527. static void requeue_io(struct thin_c *tc)
  528. {
  529. struct bio_list bios;
  530. bio_list_init(&bios);
  531. spin_lock_irq(&tc->lock);
  532. __merge_bio_list(&bios, &tc->deferred_bio_list);
  533. __merge_bio_list(&bios, &tc->retry_on_resume_list);
  534. spin_unlock_irq(&tc->lock);
  535. error_bio_list(&bios, BLK_STS_DM_REQUEUE);
  536. requeue_deferred_cells(tc);
  537. }
  538. static void error_retry_list_with_code(struct pool *pool, blk_status_t error)
  539. {
  540. struct thin_c *tc;
  541. rcu_read_lock();
  542. list_for_each_entry_rcu(tc, &pool->active_thins, list)
  543. error_thin_bio_list(tc, &tc->retry_on_resume_list, error);
  544. rcu_read_unlock();
  545. }
  546. static void error_retry_list(struct pool *pool)
  547. {
  548. error_retry_list_with_code(pool, get_pool_io_error_code(pool));
  549. }
  550. /*
  551. * This section of code contains the logic for processing a thin device's IO.
  552. * Much of the code depends on pool object resources (lists, workqueues, etc)
  553. * but most is exclusively called from the thin target rather than the thin-pool
  554. * target.
  555. */
  556. static dm_block_t get_bio_block(struct thin_c *tc, struct bio *bio)
  557. {
  558. struct pool *pool = tc->pool;
  559. sector_t block_nr = bio->bi_iter.bi_sector;
  560. if (block_size_is_power_of_two(pool))
  561. block_nr >>= pool->sectors_per_block_shift;
  562. else
  563. (void) sector_div(block_nr, pool->sectors_per_block);
  564. return block_nr;
  565. }
  566. /*
  567. * Returns the _complete_ blocks that this bio covers.
  568. */
  569. static void get_bio_block_range(struct thin_c *tc, struct bio *bio,
  570. dm_block_t *begin, dm_block_t *end)
  571. {
  572. struct pool *pool = tc->pool;
  573. sector_t b = bio->bi_iter.bi_sector;
  574. sector_t e = b + (bio->bi_iter.bi_size >> SECTOR_SHIFT);
  575. b += pool->sectors_per_block - 1ull; /* so we round up */
  576. if (block_size_is_power_of_two(pool)) {
  577. b >>= pool->sectors_per_block_shift;
  578. e >>= pool->sectors_per_block_shift;
  579. } else {
  580. (void) sector_div(b, pool->sectors_per_block);
  581. (void) sector_div(e, pool->sectors_per_block);
  582. }
  583. if (e < b)
  584. /* Can happen if the bio is within a single block. */
  585. e = b;
  586. *begin = b;
  587. *end = e;
  588. }
  589. static void remap(struct thin_c *tc, struct bio *bio, dm_block_t block)
  590. {
  591. struct pool *pool = tc->pool;
  592. sector_t bi_sector = bio->bi_iter.bi_sector;
  593. bio_set_dev(bio, tc->pool_dev->bdev);
  594. if (block_size_is_power_of_two(pool))
  595. bio->bi_iter.bi_sector =
  596. (block << pool->sectors_per_block_shift) |
  597. (bi_sector & (pool->sectors_per_block - 1));
  598. else
  599. bio->bi_iter.bi_sector = (block * pool->sectors_per_block) +
  600. sector_div(bi_sector, pool->sectors_per_block);
  601. }
  602. static void remap_to_origin(struct thin_c *tc, struct bio *bio)
  603. {
  604. bio_set_dev(bio, tc->origin_dev->bdev);
  605. }
  606. static int bio_triggers_commit(struct thin_c *tc, struct bio *bio)
  607. {
  608. return op_is_flush(bio->bi_opf) &&
  609. dm_thin_changed_this_transaction(tc->td);
  610. }
  611. static void inc_all_io_entry(struct pool *pool, struct bio *bio)
  612. {
  613. struct dm_thin_endio_hook *h;
  614. if (bio_op(bio) == REQ_OP_DISCARD)
  615. return;
  616. h = dm_per_bio_data(bio, sizeof(struct dm_thin_endio_hook));
  617. h->all_io_entry = dm_deferred_entry_inc(pool->all_io_ds);
  618. }
  619. static void issue(struct thin_c *tc, struct bio *bio)
  620. {
  621. struct pool *pool = tc->pool;
  622. if (!bio_triggers_commit(tc, bio)) {
  623. generic_make_request(bio);
  624. return;
  625. }
  626. /*
  627. * Complete bio with an error if earlier I/O caused changes to
  628. * the metadata that can't be committed e.g, due to I/O errors
  629. * on the metadata device.
  630. */
  631. if (dm_thin_aborted_changes(tc->td)) {
  632. bio_io_error(bio);
  633. return;
  634. }
  635. /*
  636. * Batch together any bios that trigger commits and then issue a
  637. * single commit for them in process_deferred_bios().
  638. */
  639. spin_lock_irq(&pool->lock);
  640. bio_list_add(&pool->deferred_flush_bios, bio);
  641. spin_unlock_irq(&pool->lock);
  642. }
  643. static void remap_to_origin_and_issue(struct thin_c *tc, struct bio *bio)
  644. {
  645. remap_to_origin(tc, bio);
  646. issue(tc, bio);
  647. }
  648. static void remap_and_issue(struct thin_c *tc, struct bio *bio,
  649. dm_block_t block)
  650. {
  651. remap(tc, bio, block);
  652. issue(tc, bio);
  653. }
  654. /*----------------------------------------------------------------*/
  655. /*
  656. * Bio endio functions.
  657. */
  658. struct dm_thin_new_mapping {
  659. struct list_head list;
  660. bool pass_discard:1;
  661. bool maybe_shared:1;
  662. /*
  663. * Track quiescing, copying and zeroing preparation actions. When this
  664. * counter hits zero the block is prepared and can be inserted into the
  665. * btree.
  666. */
  667. atomic_t prepare_actions;
  668. blk_status_t status;
  669. struct thin_c *tc;
  670. dm_block_t virt_begin, virt_end;
  671. dm_block_t data_block;
  672. struct dm_bio_prison_cell *cell;
  673. /*
  674. * If the bio covers the whole area of a block then we can avoid
  675. * zeroing or copying. Instead this bio is hooked. The bio will
  676. * still be in the cell, so care has to be taken to avoid issuing
  677. * the bio twice.
  678. */
  679. struct bio *bio;
  680. bio_end_io_t *saved_bi_end_io;
  681. };
  682. static void __complete_mapping_preparation(struct dm_thin_new_mapping *m)
  683. {
  684. struct pool *pool = m->tc->pool;
  685. if (atomic_dec_and_test(&m->prepare_actions)) {
  686. list_add_tail(&m->list, &pool->prepared_mappings);
  687. wake_worker(pool);
  688. }
  689. }
  690. static void complete_mapping_preparation(struct dm_thin_new_mapping *m)
  691. {
  692. unsigned long flags;
  693. struct pool *pool = m->tc->pool;
  694. spin_lock_irqsave(&pool->lock, flags);
  695. __complete_mapping_preparation(m);
  696. spin_unlock_irqrestore(&pool->lock, flags);
  697. }
  698. static void copy_complete(int read_err, unsigned long write_err, void *context)
  699. {
  700. struct dm_thin_new_mapping *m = context;
  701. m->status = read_err || write_err ? BLK_STS_IOERR : 0;
  702. complete_mapping_preparation(m);
  703. }
  704. static void overwrite_endio(struct bio *bio)
  705. {
  706. struct dm_thin_endio_hook *h = dm_per_bio_data(bio, sizeof(struct dm_thin_endio_hook));
  707. struct dm_thin_new_mapping *m = h->overwrite_mapping;
  708. bio->bi_end_io = m->saved_bi_end_io;
  709. m->status = bio->bi_status;
  710. complete_mapping_preparation(m);
  711. }
  712. /*----------------------------------------------------------------*/
  713. /*
  714. * Workqueue.
  715. */
  716. /*
  717. * Prepared mapping jobs.
  718. */
  719. /*
  720. * This sends the bios in the cell, except the original holder, back
  721. * to the deferred_bios list.
  722. */
  723. static void cell_defer_no_holder(struct thin_c *tc, struct dm_bio_prison_cell *cell)
  724. {
  725. struct pool *pool = tc->pool;
  726. unsigned long flags;
  727. int has_work;
  728. spin_lock_irqsave(&tc->lock, flags);
  729. cell_release_no_holder(pool, cell, &tc->deferred_bio_list);
  730. has_work = !bio_list_empty(&tc->deferred_bio_list);
  731. spin_unlock_irqrestore(&tc->lock, flags);
  732. if (has_work)
  733. wake_worker(pool);
  734. }
  735. static void thin_defer_bio(struct thin_c *tc, struct bio *bio);
  736. struct remap_info {
  737. struct thin_c *tc;
  738. struct bio_list defer_bios;
  739. struct bio_list issue_bios;
  740. };
  741. static void __inc_remap_and_issue_cell(void *context,
  742. struct dm_bio_prison_cell *cell)
  743. {
  744. struct remap_info *info = context;
  745. struct bio *bio;
  746. while ((bio = bio_list_pop(&cell->bios))) {
  747. if (op_is_flush(bio->bi_opf) || bio_op(bio) == REQ_OP_DISCARD)
  748. bio_list_add(&info->defer_bios, bio);
  749. else {
  750. inc_all_io_entry(info->tc->pool, bio);
  751. /*
  752. * We can't issue the bios with the bio prison lock
  753. * held, so we add them to a list to issue on
  754. * return from this function.
  755. */
  756. bio_list_add(&info->issue_bios, bio);
  757. }
  758. }
  759. }
  760. static void inc_remap_and_issue_cell(struct thin_c *tc,
  761. struct dm_bio_prison_cell *cell,
  762. dm_block_t block)
  763. {
  764. struct bio *bio;
  765. struct remap_info info;
  766. info.tc = tc;
  767. bio_list_init(&info.defer_bios);
  768. bio_list_init(&info.issue_bios);
  769. /*
  770. * We have to be careful to inc any bios we're about to issue
  771. * before the cell is released, and avoid a race with new bios
  772. * being added to the cell.
  773. */
  774. cell_visit_release(tc->pool, __inc_remap_and_issue_cell,
  775. &info, cell);
  776. while ((bio = bio_list_pop(&info.defer_bios)))
  777. thin_defer_bio(tc, bio);
  778. while ((bio = bio_list_pop(&info.issue_bios)))
  779. remap_and_issue(info.tc, bio, block);
  780. }
  781. static void process_prepared_mapping_fail(struct dm_thin_new_mapping *m)
  782. {
  783. cell_error(m->tc->pool, m->cell);
  784. list_del(&m->list);
  785. mempool_free(m, &m->tc->pool->mapping_pool);
  786. }
  787. static void complete_overwrite_bio(struct thin_c *tc, struct bio *bio)
  788. {
  789. struct pool *pool = tc->pool;
  790. /*
  791. * If the bio has the REQ_FUA flag set we must commit the metadata
  792. * before signaling its completion.
  793. */
  794. if (!bio_triggers_commit(tc, bio)) {
  795. bio_endio(bio);
  796. return;
  797. }
  798. /*
  799. * Complete bio with an error if earlier I/O caused changes to the
  800. * metadata that can't be committed, e.g, due to I/O errors on the
  801. * metadata device.
  802. */
  803. if (dm_thin_aborted_changes(tc->td)) {
  804. bio_io_error(bio);
  805. return;
  806. }
  807. /*
  808. * Batch together any bios that trigger commits and then issue a
  809. * single commit for them in process_deferred_bios().
  810. */
  811. spin_lock_irq(&pool->lock);
  812. bio_list_add(&pool->deferred_flush_completions, bio);
  813. spin_unlock_irq(&pool->lock);
  814. }
  815. static void process_prepared_mapping(struct dm_thin_new_mapping *m)
  816. {
  817. struct thin_c *tc = m->tc;
  818. struct pool *pool = tc->pool;
  819. struct bio *bio = m->bio;
  820. int r;
  821. if (m->status) {
  822. cell_error(pool, m->cell);
  823. goto out;
  824. }
  825. /*
  826. * Commit the prepared block into the mapping btree.
  827. * Any I/O for this block arriving after this point will get
  828. * remapped to it directly.
  829. */
  830. r = dm_thin_insert_block(tc->td, m->virt_begin, m->data_block);
  831. if (r) {
  832. metadata_operation_failed(pool, "dm_thin_insert_block", r);
  833. cell_error(pool, m->cell);
  834. goto out;
  835. }
  836. /*
  837. * Release any bios held while the block was being provisioned.
  838. * If we are processing a write bio that completely covers the block,
  839. * we already processed it so can ignore it now when processing
  840. * the bios in the cell.
  841. */
  842. if (bio) {
  843. inc_remap_and_issue_cell(tc, m->cell, m->data_block);
  844. complete_overwrite_bio(tc, bio);
  845. } else {
  846. inc_all_io_entry(tc->pool, m->cell->holder);
  847. remap_and_issue(tc, m->cell->holder, m->data_block);
  848. inc_remap_and_issue_cell(tc, m->cell, m->data_block);
  849. }
  850. out:
  851. list_del(&m->list);
  852. mempool_free(m, &pool->mapping_pool);
  853. }
  854. /*----------------------------------------------------------------*/
  855. static void free_discard_mapping(struct dm_thin_new_mapping *m)
  856. {
  857. struct thin_c *tc = m->tc;
  858. if (m->cell)
  859. cell_defer_no_holder(tc, m->cell);
  860. mempool_free(m, &tc->pool->mapping_pool);
  861. }
  862. static void process_prepared_discard_fail(struct dm_thin_new_mapping *m)
  863. {
  864. bio_io_error(m->bio);
  865. free_discard_mapping(m);
  866. }
  867. static void process_prepared_discard_success(struct dm_thin_new_mapping *m)
  868. {
  869. bio_endio(m->bio);
  870. free_discard_mapping(m);
  871. }
  872. static void process_prepared_discard_no_passdown(struct dm_thin_new_mapping *m)
  873. {
  874. int r;
  875. struct thin_c *tc = m->tc;
  876. r = dm_thin_remove_range(tc->td, m->cell->key.block_begin, m->cell->key.block_end);
  877. if (r) {
  878. metadata_operation_failed(tc->pool, "dm_thin_remove_range", r);
  879. bio_io_error(m->bio);
  880. } else
  881. bio_endio(m->bio);
  882. cell_defer_no_holder(tc, m->cell);
  883. mempool_free(m, &tc->pool->mapping_pool);
  884. }
  885. /*----------------------------------------------------------------*/
  886. static void passdown_double_checking_shared_status(struct dm_thin_new_mapping *m,
  887. struct bio *discard_parent)
  888. {
  889. /*
  890. * We've already unmapped this range of blocks, but before we
  891. * passdown we have to check that these blocks are now unused.
  892. */
  893. int r = 0;
  894. bool shared = true;
  895. struct thin_c *tc = m->tc;
  896. struct pool *pool = tc->pool;
  897. dm_block_t b = m->data_block, e, end = m->data_block + m->virt_end - m->virt_begin;
  898. struct discard_op op;
  899. begin_discard(&op, tc, discard_parent);
  900. while (b != end) {
  901. /* find start of unmapped run */
  902. for (; b < end; b++) {
  903. r = dm_pool_block_is_shared(pool->pmd, b, &shared);
  904. if (r)
  905. goto out;
  906. if (!shared)
  907. break;
  908. }
  909. if (b == end)
  910. break;
  911. /* find end of run */
  912. for (e = b + 1; e != end; e++) {
  913. r = dm_pool_block_is_shared(pool->pmd, e, &shared);
  914. if (r)
  915. goto out;
  916. if (shared)
  917. break;
  918. }
  919. r = issue_discard(&op, b, e);
  920. if (r)
  921. goto out;
  922. b = e;
  923. }
  924. out:
  925. end_discard(&op, r);
  926. }
  927. static void queue_passdown_pt2(struct dm_thin_new_mapping *m)
  928. {
  929. unsigned long flags;
  930. struct pool *pool = m->tc->pool;
  931. spin_lock_irqsave(&pool->lock, flags);
  932. list_add_tail(&m->list, &pool->prepared_discards_pt2);
  933. spin_unlock_irqrestore(&pool->lock, flags);
  934. wake_worker(pool);
  935. }
  936. static void passdown_endio(struct bio *bio)
  937. {
  938. /*
  939. * It doesn't matter if the passdown discard failed, we still want
  940. * to unmap (we ignore err).
  941. */
  942. queue_passdown_pt2(bio->bi_private);
  943. bio_put(bio);
  944. }
  945. static void process_prepared_discard_passdown_pt1(struct dm_thin_new_mapping *m)
  946. {
  947. int r;
  948. struct thin_c *tc = m->tc;
  949. struct pool *pool = tc->pool;
  950. struct bio *discard_parent;
  951. dm_block_t data_end = m->data_block + (m->virt_end - m->virt_begin);
  952. /*
  953. * Only this thread allocates blocks, so we can be sure that the
  954. * newly unmapped blocks will not be allocated before the end of
  955. * the function.
  956. */
  957. r = dm_thin_remove_range(tc->td, m->virt_begin, m->virt_end);
  958. if (r) {
  959. metadata_operation_failed(pool, "dm_thin_remove_range", r);
  960. bio_io_error(m->bio);
  961. cell_defer_no_holder(tc, m->cell);
  962. mempool_free(m, &pool->mapping_pool);
  963. return;
  964. }
  965. /*
  966. * Increment the unmapped blocks. This prevents a race between the
  967. * passdown io and reallocation of freed blocks.
  968. */
  969. r = dm_pool_inc_data_range(pool->pmd, m->data_block, data_end);
  970. if (r) {
  971. metadata_operation_failed(pool, "dm_pool_inc_data_range", r);
  972. bio_io_error(m->bio);
  973. cell_defer_no_holder(tc, m->cell);
  974. mempool_free(m, &pool->mapping_pool);
  975. return;
  976. }
  977. discard_parent = bio_alloc(GFP_NOIO, 1);
  978. if (!discard_parent) {
  979. DMWARN("%s: unable to allocate top level discard bio for passdown. Skipping passdown.",
  980. dm_device_name(tc->pool->pool_md));
  981. queue_passdown_pt2(m);
  982. } else {
  983. discard_parent->bi_end_io = passdown_endio;
  984. discard_parent->bi_private = m;
  985. if (m->maybe_shared)
  986. passdown_double_checking_shared_status(m, discard_parent);
  987. else {
  988. struct discard_op op;
  989. begin_discard(&op, tc, discard_parent);
  990. r = issue_discard(&op, m->data_block, data_end);
  991. end_discard(&op, r);
  992. }
  993. }
  994. }
  995. static void process_prepared_discard_passdown_pt2(struct dm_thin_new_mapping *m)
  996. {
  997. int r;
  998. struct thin_c *tc = m->tc;
  999. struct pool *pool = tc->pool;
  1000. /*
  1001. * The passdown has completed, so now we can decrement all those
  1002. * unmapped blocks.
  1003. */
  1004. r = dm_pool_dec_data_range(pool->pmd, m->data_block,
  1005. m->data_block + (m->virt_end - m->virt_begin));
  1006. if (r) {
  1007. metadata_operation_failed(pool, "dm_pool_dec_data_range", r);
  1008. bio_io_error(m->bio);
  1009. } else
  1010. bio_endio(m->bio);
  1011. cell_defer_no_holder(tc, m->cell);
  1012. mempool_free(m, &pool->mapping_pool);
  1013. }
  1014. static void process_prepared(struct pool *pool, struct list_head *head,
  1015. process_mapping_fn *fn)
  1016. {
  1017. struct list_head maps;
  1018. struct dm_thin_new_mapping *m, *tmp;
  1019. INIT_LIST_HEAD(&maps);
  1020. spin_lock_irq(&pool->lock);
  1021. list_splice_init(head, &maps);
  1022. spin_unlock_irq(&pool->lock);
  1023. list_for_each_entry_safe(m, tmp, &maps, list)
  1024. (*fn)(m);
  1025. }
  1026. /*
  1027. * Deferred bio jobs.
  1028. */
  1029. static int io_overlaps_block(struct pool *pool, struct bio *bio)
  1030. {
  1031. return bio->bi_iter.bi_size ==
  1032. (pool->sectors_per_block << SECTOR_SHIFT);
  1033. }
  1034. static int io_overwrites_block(struct pool *pool, struct bio *bio)
  1035. {
  1036. return (bio_data_dir(bio) == WRITE) &&
  1037. io_overlaps_block(pool, bio);
  1038. }
  1039. static void save_and_set_endio(struct bio *bio, bio_end_io_t **save,
  1040. bio_end_io_t *fn)
  1041. {
  1042. *save = bio->bi_end_io;
  1043. bio->bi_end_io = fn;
  1044. }
  1045. static int ensure_next_mapping(struct pool *pool)
  1046. {
  1047. if (pool->next_mapping)
  1048. return 0;
  1049. pool->next_mapping = mempool_alloc(&pool->mapping_pool, GFP_ATOMIC);
  1050. return pool->next_mapping ? 0 : -ENOMEM;
  1051. }
  1052. static struct dm_thin_new_mapping *get_next_mapping(struct pool *pool)
  1053. {
  1054. struct dm_thin_new_mapping *m = pool->next_mapping;
  1055. BUG_ON(!pool->next_mapping);
  1056. memset(m, 0, sizeof(struct dm_thin_new_mapping));
  1057. INIT_LIST_HEAD(&m->list);
  1058. m->bio = NULL;
  1059. pool->next_mapping = NULL;
  1060. return m;
  1061. }
  1062. static void ll_zero(struct thin_c *tc, struct dm_thin_new_mapping *m,
  1063. sector_t begin, sector_t end)
  1064. {
  1065. struct dm_io_region to;
  1066. to.bdev = tc->pool_dev->bdev;
  1067. to.sector = begin;
  1068. to.count = end - begin;
  1069. dm_kcopyd_zero(tc->pool->copier, 1, &to, 0, copy_complete, m);
  1070. }
  1071. static void remap_and_issue_overwrite(struct thin_c *tc, struct bio *bio,
  1072. dm_block_t data_begin,
  1073. struct dm_thin_new_mapping *m)
  1074. {
  1075. struct pool *pool = tc->pool;
  1076. struct dm_thin_endio_hook *h = dm_per_bio_data(bio, sizeof(struct dm_thin_endio_hook));
  1077. h->overwrite_mapping = m;
  1078. m->bio = bio;
  1079. save_and_set_endio(bio, &m->saved_bi_end_io, overwrite_endio);
  1080. inc_all_io_entry(pool, bio);
  1081. remap_and_issue(tc, bio, data_begin);
  1082. }
  1083. /*
  1084. * A partial copy also needs to zero the uncopied region.
  1085. */
  1086. static void schedule_copy(struct thin_c *tc, dm_block_t virt_block,
  1087. struct dm_dev *origin, dm_block_t data_origin,
  1088. dm_block_t data_dest,
  1089. struct dm_bio_prison_cell *cell, struct bio *bio,
  1090. sector_t len)
  1091. {
  1092. struct pool *pool = tc->pool;
  1093. struct dm_thin_new_mapping *m = get_next_mapping(pool);
  1094. m->tc = tc;
  1095. m->virt_begin = virt_block;
  1096. m->virt_end = virt_block + 1u;
  1097. m->data_block = data_dest;
  1098. m->cell = cell;
  1099. /*
  1100. * quiesce action + copy action + an extra reference held for the
  1101. * duration of this function (we may need to inc later for a
  1102. * partial zero).
  1103. */
  1104. atomic_set(&m->prepare_actions, 3);
  1105. if (!dm_deferred_set_add_work(pool->shared_read_ds, &m->list))
  1106. complete_mapping_preparation(m); /* already quiesced */
  1107. /*
  1108. * IO to pool_dev remaps to the pool target's data_dev.
  1109. *
  1110. * If the whole block of data is being overwritten, we can issue the
  1111. * bio immediately. Otherwise we use kcopyd to clone the data first.
  1112. */
  1113. if (io_overwrites_block(pool, bio))
  1114. remap_and_issue_overwrite(tc, bio, data_dest, m);
  1115. else {
  1116. struct dm_io_region from, to;
  1117. from.bdev = origin->bdev;
  1118. from.sector = data_origin * pool->sectors_per_block;
  1119. from.count = len;
  1120. to.bdev = tc->pool_dev->bdev;
  1121. to.sector = data_dest * pool->sectors_per_block;
  1122. to.count = len;
  1123. dm_kcopyd_copy(pool->copier, &from, 1, &to,
  1124. 0, copy_complete, m);
  1125. /*
  1126. * Do we need to zero a tail region?
  1127. */
  1128. if (len < pool->sectors_per_block && pool->pf.zero_new_blocks) {
  1129. atomic_inc(&m->prepare_actions);
  1130. ll_zero(tc, m,
  1131. data_dest * pool->sectors_per_block + len,
  1132. (data_dest + 1) * pool->sectors_per_block);
  1133. }
  1134. }
  1135. complete_mapping_preparation(m); /* drop our ref */
  1136. }
  1137. static void schedule_internal_copy(struct thin_c *tc, dm_block_t virt_block,
  1138. dm_block_t data_origin, dm_block_t data_dest,
  1139. struct dm_bio_prison_cell *cell, struct bio *bio)
  1140. {
  1141. schedule_copy(tc, virt_block, tc->pool_dev,
  1142. data_origin, data_dest, cell, bio,
  1143. tc->pool->sectors_per_block);
  1144. }
  1145. static void schedule_zero(struct thin_c *tc, dm_block_t virt_block,
  1146. dm_block_t data_block, struct dm_bio_prison_cell *cell,
  1147. struct bio *bio)
  1148. {
  1149. struct pool *pool = tc->pool;
  1150. struct dm_thin_new_mapping *m = get_next_mapping(pool);
  1151. atomic_set(&m->prepare_actions, 1); /* no need to quiesce */
  1152. m->tc = tc;
  1153. m->virt_begin = virt_block;
  1154. m->virt_end = virt_block + 1u;
  1155. m->data_block = data_block;
  1156. m->cell = cell;
  1157. /*
  1158. * If the whole block of data is being overwritten or we are not
  1159. * zeroing pre-existing data, we can issue the bio immediately.
  1160. * Otherwise we use kcopyd to zero the data first.
  1161. */
  1162. if (pool->pf.zero_new_blocks) {
  1163. if (io_overwrites_block(pool, bio))
  1164. remap_and_issue_overwrite(tc, bio, data_block, m);
  1165. else
  1166. ll_zero(tc, m, data_block * pool->sectors_per_block,
  1167. (data_block + 1) * pool->sectors_per_block);
  1168. } else
  1169. process_prepared_mapping(m);
  1170. }
  1171. static void schedule_external_copy(struct thin_c *tc, dm_block_t virt_block,
  1172. dm_block_t data_dest,
  1173. struct dm_bio_prison_cell *cell, struct bio *bio)
  1174. {
  1175. struct pool *pool = tc->pool;
  1176. sector_t virt_block_begin = virt_block * pool->sectors_per_block;
  1177. sector_t virt_block_end = (virt_block + 1) * pool->sectors_per_block;
  1178. if (virt_block_end <= tc->origin_size)
  1179. schedule_copy(tc, virt_block, tc->origin_dev,
  1180. virt_block, data_dest, cell, bio,
  1181. pool->sectors_per_block);
  1182. else if (virt_block_begin < tc->origin_size)
  1183. schedule_copy(tc, virt_block, tc->origin_dev,
  1184. virt_block, data_dest, cell, bio,
  1185. tc->origin_size - virt_block_begin);
  1186. else
  1187. schedule_zero(tc, virt_block, data_dest, cell, bio);
  1188. }
  1189. static void set_pool_mode(struct pool *pool, enum pool_mode new_mode);
  1190. static void requeue_bios(struct pool *pool);
  1191. static bool is_read_only_pool_mode(enum pool_mode mode)
  1192. {
  1193. return (mode == PM_OUT_OF_METADATA_SPACE || mode == PM_READ_ONLY);
  1194. }
  1195. static bool is_read_only(struct pool *pool)
  1196. {
  1197. return is_read_only_pool_mode(get_pool_mode(pool));
  1198. }
  1199. static void check_for_metadata_space(struct pool *pool)
  1200. {
  1201. int r;
  1202. const char *ooms_reason = NULL;
  1203. dm_block_t nr_free;
  1204. r = dm_pool_get_free_metadata_block_count(pool->pmd, &nr_free);
  1205. if (r)
  1206. ooms_reason = "Could not get free metadata blocks";
  1207. else if (!nr_free)
  1208. ooms_reason = "No free metadata blocks";
  1209. if (ooms_reason && !is_read_only(pool)) {
  1210. DMERR("%s", ooms_reason);
  1211. set_pool_mode(pool, PM_OUT_OF_METADATA_SPACE);
  1212. }
  1213. }
  1214. static void check_for_data_space(struct pool *pool)
  1215. {
  1216. int r;
  1217. dm_block_t nr_free;
  1218. if (get_pool_mode(pool) != PM_OUT_OF_DATA_SPACE)
  1219. return;
  1220. r = dm_pool_get_free_block_count(pool->pmd, &nr_free);
  1221. if (r)
  1222. return;
  1223. if (nr_free) {
  1224. set_pool_mode(pool, PM_WRITE);
  1225. requeue_bios(pool);
  1226. }
  1227. }
  1228. /*
  1229. * A non-zero return indicates read_only or fail_io mode.
  1230. * Many callers don't care about the return value.
  1231. */
  1232. static int commit(struct pool *pool)
  1233. {
  1234. int r;
  1235. if (get_pool_mode(pool) >= PM_OUT_OF_METADATA_SPACE)
  1236. return -EINVAL;
  1237. r = dm_pool_commit_metadata(pool->pmd);
  1238. if (r)
  1239. metadata_operation_failed(pool, "dm_pool_commit_metadata", r);
  1240. else {
  1241. check_for_metadata_space(pool);
  1242. check_for_data_space(pool);
  1243. }
  1244. return r;
  1245. }
  1246. static void check_low_water_mark(struct pool *pool, dm_block_t free_blocks)
  1247. {
  1248. if (free_blocks <= pool->low_water_blocks && !pool->low_water_triggered) {
  1249. DMWARN("%s: reached low water mark for data device: sending event.",
  1250. dm_device_name(pool->pool_md));
  1251. spin_lock_irq(&pool->lock);
  1252. pool->low_water_triggered = true;
  1253. spin_unlock_irq(&pool->lock);
  1254. dm_table_event(pool->ti->table);
  1255. }
  1256. }
  1257. static int alloc_data_block(struct thin_c *tc, dm_block_t *result)
  1258. {
  1259. int r;
  1260. dm_block_t free_blocks;
  1261. struct pool *pool = tc->pool;
  1262. if (WARN_ON(get_pool_mode(pool) != PM_WRITE))
  1263. return -EINVAL;
  1264. r = dm_pool_get_free_block_count(pool->pmd, &free_blocks);
  1265. if (r) {
  1266. metadata_operation_failed(pool, "dm_pool_get_free_block_count", r);
  1267. return r;
  1268. }
  1269. check_low_water_mark(pool, free_blocks);
  1270. if (!free_blocks) {
  1271. /*
  1272. * Try to commit to see if that will free up some
  1273. * more space.
  1274. */
  1275. r = commit(pool);
  1276. if (r)
  1277. return r;
  1278. r = dm_pool_get_free_block_count(pool->pmd, &free_blocks);
  1279. if (r) {
  1280. metadata_operation_failed(pool, "dm_pool_get_free_block_count", r);
  1281. return r;
  1282. }
  1283. if (!free_blocks) {
  1284. set_pool_mode(pool, PM_OUT_OF_DATA_SPACE);
  1285. return -ENOSPC;
  1286. }
  1287. }
  1288. r = dm_pool_alloc_data_block(pool->pmd, result);
  1289. if (r) {
  1290. if (r == -ENOSPC)
  1291. set_pool_mode(pool, PM_OUT_OF_DATA_SPACE);
  1292. else
  1293. metadata_operation_failed(pool, "dm_pool_alloc_data_block", r);
  1294. return r;
  1295. }
  1296. r = dm_pool_get_free_metadata_block_count(pool->pmd, &free_blocks);
  1297. if (r) {
  1298. metadata_operation_failed(pool, "dm_pool_get_free_metadata_block_count", r);
  1299. return r;
  1300. }
  1301. if (!free_blocks) {
  1302. /* Let's commit before we use up the metadata reserve. */
  1303. r = commit(pool);
  1304. if (r)
  1305. return r;
  1306. }
  1307. return 0;
  1308. }
  1309. /*
  1310. * If we have run out of space, queue bios until the device is
  1311. * resumed, presumably after having been reloaded with more space.
  1312. */
  1313. static void retry_on_resume(struct bio *bio)
  1314. {
  1315. struct dm_thin_endio_hook *h = dm_per_bio_data(bio, sizeof(struct dm_thin_endio_hook));
  1316. struct thin_c *tc = h->tc;
  1317. spin_lock_irq(&tc->lock);
  1318. bio_list_add(&tc->retry_on_resume_list, bio);
  1319. spin_unlock_irq(&tc->lock);
  1320. }
  1321. static blk_status_t should_error_unserviceable_bio(struct pool *pool)
  1322. {
  1323. enum pool_mode m = get_pool_mode(pool);
  1324. switch (m) {
  1325. case PM_WRITE:
  1326. /* Shouldn't get here */
  1327. DMERR_LIMIT("bio unserviceable, yet pool is in PM_WRITE mode");
  1328. return BLK_STS_IOERR;
  1329. case PM_OUT_OF_DATA_SPACE:
  1330. return pool->pf.error_if_no_space ? BLK_STS_NOSPC : 0;
  1331. case PM_OUT_OF_METADATA_SPACE:
  1332. case PM_READ_ONLY:
  1333. case PM_FAIL:
  1334. return BLK_STS_IOERR;
  1335. default:
  1336. /* Shouldn't get here */
  1337. DMERR_LIMIT("bio unserviceable, yet pool has an unknown mode");
  1338. return BLK_STS_IOERR;
  1339. }
  1340. }
  1341. static void handle_unserviceable_bio(struct pool *pool, struct bio *bio)
  1342. {
  1343. blk_status_t error = should_error_unserviceable_bio(pool);
  1344. if (error) {
  1345. bio->bi_status = error;
  1346. bio_endio(bio);
  1347. } else
  1348. retry_on_resume(bio);
  1349. }
  1350. static void retry_bios_on_resume(struct pool *pool, struct dm_bio_prison_cell *cell)
  1351. {
  1352. struct bio *bio;
  1353. struct bio_list bios;
  1354. blk_status_t error;
  1355. error = should_error_unserviceable_bio(pool);
  1356. if (error) {
  1357. cell_error_with_code(pool, cell, error);
  1358. return;
  1359. }
  1360. bio_list_init(&bios);
  1361. cell_release(pool, cell, &bios);
  1362. while ((bio = bio_list_pop(&bios)))
  1363. retry_on_resume(bio);
  1364. }
  1365. static void process_discard_cell_no_passdown(struct thin_c *tc,
  1366. struct dm_bio_prison_cell *virt_cell)
  1367. {
  1368. struct pool *pool = tc->pool;
  1369. struct dm_thin_new_mapping *m = get_next_mapping(pool);
  1370. /*
  1371. * We don't need to lock the data blocks, since there's no
  1372. * passdown. We only lock data blocks for allocation and breaking sharing.
  1373. */
  1374. m->tc = tc;
  1375. m->virt_begin = virt_cell->key.block_begin;
  1376. m->virt_end = virt_cell->key.block_end;
  1377. m->cell = virt_cell;
  1378. m->bio = virt_cell->holder;
  1379. if (!dm_deferred_set_add_work(pool->all_io_ds, &m->list))
  1380. pool->process_prepared_discard(m);
  1381. }
  1382. static void break_up_discard_bio(struct thin_c *tc, dm_block_t begin, dm_block_t end,
  1383. struct bio *bio)
  1384. {
  1385. struct pool *pool = tc->pool;
  1386. int r;
  1387. bool maybe_shared;
  1388. struct dm_cell_key data_key;
  1389. struct dm_bio_prison_cell *data_cell;
  1390. struct dm_thin_new_mapping *m;
  1391. dm_block_t virt_begin, virt_end, data_begin;
  1392. while (begin != end) {
  1393. r = ensure_next_mapping(pool);
  1394. if (r)
  1395. /* we did our best */
  1396. return;
  1397. r = dm_thin_find_mapped_range(tc->td, begin, end, &virt_begin, &virt_end,
  1398. &data_begin, &maybe_shared);
  1399. if (r)
  1400. /*
  1401. * Silently fail, letting any mappings we've
  1402. * created complete.
  1403. */
  1404. break;
  1405. build_key(tc->td, PHYSICAL, data_begin, data_begin + (virt_end - virt_begin), &data_key);
  1406. if (bio_detain(tc->pool, &data_key, NULL, &data_cell)) {
  1407. /* contention, we'll give up with this range */
  1408. begin = virt_end;
  1409. continue;
  1410. }
  1411. /*
  1412. * IO may still be going to the destination block. We must
  1413. * quiesce before we can do the removal.
  1414. */
  1415. m = get_next_mapping(pool);
  1416. m->tc = tc;
  1417. m->maybe_shared = maybe_shared;
  1418. m->virt_begin = virt_begin;
  1419. m->virt_end = virt_end;
  1420. m->data_block = data_begin;
  1421. m->cell = data_cell;
  1422. m->bio = bio;
  1423. /*
  1424. * The parent bio must not complete before sub discard bios are
  1425. * chained to it (see end_discard's bio_chain)!
  1426. *
  1427. * This per-mapping bi_remaining increment is paired with
  1428. * the implicit decrement that occurs via bio_endio() in
  1429. * end_discard().
  1430. */
  1431. bio_inc_remaining(bio);
  1432. if (!dm_deferred_set_add_work(pool->all_io_ds, &m->list))
  1433. pool->process_prepared_discard(m);
  1434. begin = virt_end;
  1435. }
  1436. }
  1437. static void process_discard_cell_passdown(struct thin_c *tc, struct dm_bio_prison_cell *virt_cell)
  1438. {
  1439. struct bio *bio = virt_cell->holder;
  1440. struct dm_thin_endio_hook *h = dm_per_bio_data(bio, sizeof(struct dm_thin_endio_hook));
  1441. /*
  1442. * The virt_cell will only get freed once the origin bio completes.
  1443. * This means it will remain locked while all the individual
  1444. * passdown bios are in flight.
  1445. */
  1446. h->cell = virt_cell;
  1447. break_up_discard_bio(tc, virt_cell->key.block_begin, virt_cell->key.block_end, bio);
  1448. /*
  1449. * We complete the bio now, knowing that the bi_remaining field
  1450. * will prevent completion until the sub range discards have
  1451. * completed.
  1452. */
  1453. bio_endio(bio);
  1454. }
  1455. static void process_discard_bio(struct thin_c *tc, struct bio *bio)
  1456. {
  1457. dm_block_t begin, end;
  1458. struct dm_cell_key virt_key;
  1459. struct dm_bio_prison_cell *virt_cell;
  1460. get_bio_block_range(tc, bio, &begin, &end);
  1461. if (begin == end) {
  1462. /*
  1463. * The discard covers less than a block.
  1464. */
  1465. bio_endio(bio);
  1466. return;
  1467. }
  1468. build_key(tc->td, VIRTUAL, begin, end, &virt_key);
  1469. if (bio_detain(tc->pool, &virt_key, bio, &virt_cell))
  1470. /*
  1471. * Potential starvation issue: We're relying on the
  1472. * fs/application being well behaved, and not trying to
  1473. * send IO to a region at the same time as discarding it.
  1474. * If they do this persistently then it's possible this
  1475. * cell will never be granted.
  1476. */
  1477. return;
  1478. tc->pool->process_discard_cell(tc, virt_cell);
  1479. }
  1480. static void break_sharing(struct thin_c *tc, struct bio *bio, dm_block_t block,
  1481. struct dm_cell_key *key,
  1482. struct dm_thin_lookup_result *lookup_result,
  1483. struct dm_bio_prison_cell *cell)
  1484. {
  1485. int r;
  1486. dm_block_t data_block;
  1487. struct pool *pool = tc->pool;
  1488. r = alloc_data_block(tc, &data_block);
  1489. switch (r) {
  1490. case 0:
  1491. schedule_internal_copy(tc, block, lookup_result->block,
  1492. data_block, cell, bio);
  1493. break;
  1494. case -ENOSPC:
  1495. retry_bios_on_resume(pool, cell);
  1496. break;
  1497. default:
  1498. DMERR_LIMIT("%s: alloc_data_block() failed: error = %d",
  1499. __func__, r);
  1500. cell_error(pool, cell);
  1501. break;
  1502. }
  1503. }
  1504. static void __remap_and_issue_shared_cell(void *context,
  1505. struct dm_bio_prison_cell *cell)
  1506. {
  1507. struct remap_info *info = context;
  1508. struct bio *bio;
  1509. while ((bio = bio_list_pop(&cell->bios))) {
  1510. if (bio_data_dir(bio) == WRITE || op_is_flush(bio->bi_opf) ||
  1511. bio_op(bio) == REQ_OP_DISCARD)
  1512. bio_list_add(&info->defer_bios, bio);
  1513. else {
  1514. struct dm_thin_endio_hook *h = dm_per_bio_data(bio, sizeof(struct dm_thin_endio_hook));
  1515. h->shared_read_entry = dm_deferred_entry_inc(info->tc->pool->shared_read_ds);
  1516. inc_all_io_entry(info->tc->pool, bio);
  1517. bio_list_add(&info->issue_bios, bio);
  1518. }
  1519. }
  1520. }
  1521. static void remap_and_issue_shared_cell(struct thin_c *tc,
  1522. struct dm_bio_prison_cell *cell,
  1523. dm_block_t block)
  1524. {
  1525. struct bio *bio;
  1526. struct remap_info info;
  1527. info.tc = tc;
  1528. bio_list_init(&info.defer_bios);
  1529. bio_list_init(&info.issue_bios);
  1530. cell_visit_release(tc->pool, __remap_and_issue_shared_cell,
  1531. &info, cell);
  1532. while ((bio = bio_list_pop(&info.defer_bios)))
  1533. thin_defer_bio(tc, bio);
  1534. while ((bio = bio_list_pop(&info.issue_bios)))
  1535. remap_and_issue(tc, bio, block);
  1536. }
  1537. static void process_shared_bio(struct thin_c *tc, struct bio *bio,
  1538. dm_block_t block,
  1539. struct dm_thin_lookup_result *lookup_result,
  1540. struct dm_bio_prison_cell *virt_cell)
  1541. {
  1542. struct dm_bio_prison_cell *data_cell;
  1543. struct pool *pool = tc->pool;
  1544. struct dm_cell_key key;
  1545. /*
  1546. * If cell is already occupied, then sharing is already in the process
  1547. * of being broken so we have nothing further to do here.
  1548. */
  1549. build_data_key(tc->td, lookup_result->block, &key);
  1550. if (bio_detain(pool, &key, bio, &data_cell)) {
  1551. cell_defer_no_holder(tc, virt_cell);
  1552. return;
  1553. }
  1554. if (bio_data_dir(bio) == WRITE && bio->bi_iter.bi_size) {
  1555. break_sharing(tc, bio, block, &key, lookup_result, data_cell);
  1556. cell_defer_no_holder(tc, virt_cell);
  1557. } else {
  1558. struct dm_thin_endio_hook *h = dm_per_bio_data(bio, sizeof(struct dm_thin_endio_hook));
  1559. h->shared_read_entry = dm_deferred_entry_inc(pool->shared_read_ds);
  1560. inc_all_io_entry(pool, bio);
  1561. remap_and_issue(tc, bio, lookup_result->block);
  1562. remap_and_issue_shared_cell(tc, data_cell, lookup_result->block);
  1563. remap_and_issue_shared_cell(tc, virt_cell, lookup_result->block);
  1564. }
  1565. }
  1566. static void provision_block(struct thin_c *tc, struct bio *bio, dm_block_t block,
  1567. struct dm_bio_prison_cell *cell)
  1568. {
  1569. int r;
  1570. dm_block_t data_block;
  1571. struct pool *pool = tc->pool;
  1572. /*
  1573. * Remap empty bios (flushes) immediately, without provisioning.
  1574. */
  1575. if (!bio->bi_iter.bi_size) {
  1576. inc_all_io_entry(pool, bio);
  1577. cell_defer_no_holder(tc, cell);
  1578. remap_and_issue(tc, bio, 0);
  1579. return;
  1580. }
  1581. /*
  1582. * Fill read bios with zeroes and complete them immediately.
  1583. */
  1584. if (bio_data_dir(bio) == READ) {
  1585. zero_fill_bio(bio);
  1586. cell_defer_no_holder(tc, cell);
  1587. bio_endio(bio);
  1588. return;
  1589. }
  1590. r = alloc_data_block(tc, &data_block);
  1591. switch (r) {
  1592. case 0:
  1593. if (tc->origin_dev)
  1594. schedule_external_copy(tc, block, data_block, cell, bio);
  1595. else
  1596. schedule_zero(tc, block, data_block, cell, bio);
  1597. break;
  1598. case -ENOSPC:
  1599. retry_bios_on_resume(pool, cell);
  1600. break;
  1601. default:
  1602. DMERR_LIMIT("%s: alloc_data_block() failed: error = %d",
  1603. __func__, r);
  1604. cell_error(pool, cell);
  1605. break;
  1606. }
  1607. }
  1608. static void process_cell(struct thin_c *tc, struct dm_bio_prison_cell *cell)
  1609. {
  1610. int r;
  1611. struct pool *pool = tc->pool;
  1612. struct bio *bio = cell->holder;
  1613. dm_block_t block = get_bio_block(tc, bio);
  1614. struct dm_thin_lookup_result lookup_result;
  1615. if (tc->requeue_mode) {
  1616. cell_requeue(pool, cell);
  1617. return;
  1618. }
  1619. r = dm_thin_find_block(tc->td, block, 1, &lookup_result);
  1620. switch (r) {
  1621. case 0:
  1622. if (lookup_result.shared)
  1623. process_shared_bio(tc, bio, block, &lookup_result, cell);
  1624. else {
  1625. inc_all_io_entry(pool, bio);
  1626. remap_and_issue(tc, bio, lookup_result.block);
  1627. inc_remap_and_issue_cell(tc, cell, lookup_result.block);
  1628. }
  1629. break;
  1630. case -ENODATA:
  1631. if (bio_data_dir(bio) == READ && tc->origin_dev) {
  1632. inc_all_io_entry(pool, bio);
  1633. cell_defer_no_holder(tc, cell);
  1634. if (bio_end_sector(bio) <= tc->origin_size)
  1635. remap_to_origin_and_issue(tc, bio);
  1636. else if (bio->bi_iter.bi_sector < tc->origin_size) {
  1637. zero_fill_bio(bio);
  1638. bio->bi_iter.bi_size = (tc->origin_size - bio->bi_iter.bi_sector) << SECTOR_SHIFT;
  1639. remap_to_origin_and_issue(tc, bio);
  1640. } else {
  1641. zero_fill_bio(bio);
  1642. bio_endio(bio);
  1643. }
  1644. } else
  1645. provision_block(tc, bio, block, cell);
  1646. break;
  1647. default:
  1648. DMERR_LIMIT("%s: dm_thin_find_block() failed: error = %d",
  1649. __func__, r);
  1650. cell_defer_no_holder(tc, cell);
  1651. bio_io_error(bio);
  1652. break;
  1653. }
  1654. }
  1655. static void process_bio(struct thin_c *tc, struct bio *bio)
  1656. {
  1657. struct pool *pool = tc->pool;
  1658. dm_block_t block = get_bio_block(tc, bio);
  1659. struct dm_bio_prison_cell *cell;
  1660. struct dm_cell_key key;
  1661. /*
  1662. * If cell is already occupied, then the block is already
  1663. * being provisioned so we have nothing further to do here.
  1664. */
  1665. build_virtual_key(tc->td, block, &key);
  1666. if (bio_detain(pool, &key, bio, &cell))
  1667. return;
  1668. process_cell(tc, cell);
  1669. }
  1670. static void __process_bio_read_only(struct thin_c *tc, struct bio *bio,
  1671. struct dm_bio_prison_cell *cell)
  1672. {
  1673. int r;
  1674. int rw = bio_data_dir(bio);
  1675. dm_block_t block = get_bio_block(tc, bio);
  1676. struct dm_thin_lookup_result lookup_result;
  1677. r = dm_thin_find_block(tc->td, block, 1, &lookup_result);
  1678. switch (r) {
  1679. case 0:
  1680. if (lookup_result.shared && (rw == WRITE) && bio->bi_iter.bi_size) {
  1681. handle_unserviceable_bio(tc->pool, bio);
  1682. if (cell)
  1683. cell_defer_no_holder(tc, cell);
  1684. } else {
  1685. inc_all_io_entry(tc->pool, bio);
  1686. remap_and_issue(tc, bio, lookup_result.block);
  1687. if (cell)
  1688. inc_remap_and_issue_cell(tc, cell, lookup_result.block);
  1689. }
  1690. break;
  1691. case -ENODATA:
  1692. if (cell)
  1693. cell_defer_no_holder(tc, cell);
  1694. if (rw != READ) {
  1695. handle_unserviceable_bio(tc->pool, bio);
  1696. break;
  1697. }
  1698. if (tc->origin_dev) {
  1699. inc_all_io_entry(tc->pool, bio);
  1700. remap_to_origin_and_issue(tc, bio);
  1701. break;
  1702. }
  1703. zero_fill_bio(bio);
  1704. bio_endio(bio);
  1705. break;
  1706. default:
  1707. DMERR_LIMIT("%s: dm_thin_find_block() failed: error = %d",
  1708. __func__, r);
  1709. if (cell)
  1710. cell_defer_no_holder(tc, cell);
  1711. bio_io_error(bio);
  1712. break;
  1713. }
  1714. }
  1715. static void process_bio_read_only(struct thin_c *tc, struct bio *bio)
  1716. {
  1717. __process_bio_read_only(tc, bio, NULL);
  1718. }
  1719. static void process_cell_read_only(struct thin_c *tc, struct dm_bio_prison_cell *cell)
  1720. {
  1721. __process_bio_read_only(tc, cell->holder, cell);
  1722. }
  1723. static void process_bio_success(struct thin_c *tc, struct bio *bio)
  1724. {
  1725. bio_endio(bio);
  1726. }
  1727. static void process_bio_fail(struct thin_c *tc, struct bio *bio)
  1728. {
  1729. bio_io_error(bio);
  1730. }
  1731. static void process_cell_success(struct thin_c *tc, struct dm_bio_prison_cell *cell)
  1732. {
  1733. cell_success(tc->pool, cell);
  1734. }
  1735. static void process_cell_fail(struct thin_c *tc, struct dm_bio_prison_cell *cell)
  1736. {
  1737. cell_error(tc->pool, cell);
  1738. }
  1739. /*
  1740. * FIXME: should we also commit due to size of transaction, measured in
  1741. * metadata blocks?
  1742. */
  1743. static int need_commit_due_to_time(struct pool *pool)
  1744. {
  1745. return !time_in_range(jiffies, pool->last_commit_jiffies,
  1746. pool->last_commit_jiffies + COMMIT_PERIOD);
  1747. }
  1748. #define thin_pbd(node) rb_entry((node), struct dm_thin_endio_hook, rb_node)
  1749. #define thin_bio(pbd) dm_bio_from_per_bio_data((pbd), sizeof(struct dm_thin_endio_hook))
  1750. static void __thin_bio_rb_add(struct thin_c *tc, struct bio *bio)
  1751. {
  1752. struct rb_node **rbp, *parent;
  1753. struct dm_thin_endio_hook *pbd;
  1754. sector_t bi_sector = bio->bi_iter.bi_sector;
  1755. rbp = &tc->sort_bio_list.rb_node;
  1756. parent = NULL;
  1757. while (*rbp) {
  1758. parent = *rbp;
  1759. pbd = thin_pbd(parent);
  1760. if (bi_sector < thin_bio(pbd)->bi_iter.bi_sector)
  1761. rbp = &(*rbp)->rb_left;
  1762. else
  1763. rbp = &(*rbp)->rb_right;
  1764. }
  1765. pbd = dm_per_bio_data(bio, sizeof(struct dm_thin_endio_hook));
  1766. rb_link_node(&pbd->rb_node, parent, rbp);
  1767. rb_insert_color(&pbd->rb_node, &tc->sort_bio_list);
  1768. }
  1769. static void __extract_sorted_bios(struct thin_c *tc)
  1770. {
  1771. struct rb_node *node;
  1772. struct dm_thin_endio_hook *pbd;
  1773. struct bio *bio;
  1774. for (node = rb_first(&tc->sort_bio_list); node; node = rb_next(node)) {
  1775. pbd = thin_pbd(node);
  1776. bio = thin_bio(pbd);
  1777. bio_list_add(&tc->deferred_bio_list, bio);
  1778. rb_erase(&pbd->rb_node, &tc->sort_bio_list);
  1779. }
  1780. WARN_ON(!RB_EMPTY_ROOT(&tc->sort_bio_list));
  1781. }
  1782. static void __sort_thin_deferred_bios(struct thin_c *tc)
  1783. {
  1784. struct bio *bio;
  1785. struct bio_list bios;
  1786. bio_list_init(&bios);
  1787. bio_list_merge(&bios, &tc->deferred_bio_list);
  1788. bio_list_init(&tc->deferred_bio_list);
  1789. /* Sort deferred_bio_list using rb-tree */
  1790. while ((bio = bio_list_pop(&bios)))
  1791. __thin_bio_rb_add(tc, bio);
  1792. /*
  1793. * Transfer the sorted bios in sort_bio_list back to
  1794. * deferred_bio_list to allow lockless submission of
  1795. * all bios.
  1796. */
  1797. __extract_sorted_bios(tc);
  1798. }
  1799. static void process_thin_deferred_bios(struct thin_c *tc)
  1800. {
  1801. struct pool *pool = tc->pool;
  1802. struct bio *bio;
  1803. struct bio_list bios;
  1804. struct blk_plug plug;
  1805. unsigned count = 0;
  1806. if (tc->requeue_mode) {
  1807. error_thin_bio_list(tc, &tc->deferred_bio_list,
  1808. BLK_STS_DM_REQUEUE);
  1809. return;
  1810. }
  1811. bio_list_init(&bios);
  1812. spin_lock_irq(&tc->lock);
  1813. if (bio_list_empty(&tc->deferred_bio_list)) {
  1814. spin_unlock_irq(&tc->lock);
  1815. return;
  1816. }
  1817. __sort_thin_deferred_bios(tc);
  1818. bio_list_merge(&bios, &tc->deferred_bio_list);
  1819. bio_list_init(&tc->deferred_bio_list);
  1820. spin_unlock_irq(&tc->lock);
  1821. blk_start_plug(&plug);
  1822. while ((bio = bio_list_pop(&bios))) {
  1823. /*
  1824. * If we've got no free new_mapping structs, and processing
  1825. * this bio might require one, we pause until there are some
  1826. * prepared mappings to process.
  1827. */
  1828. if (ensure_next_mapping(pool)) {
  1829. spin_lock_irq(&tc->lock);
  1830. bio_list_add(&tc->deferred_bio_list, bio);
  1831. bio_list_merge(&tc->deferred_bio_list, &bios);
  1832. spin_unlock_irq(&tc->lock);
  1833. break;
  1834. }
  1835. if (bio_op(bio) == REQ_OP_DISCARD)
  1836. pool->process_discard(tc, bio);
  1837. else
  1838. pool->process_bio(tc, bio);
  1839. if ((count++ & 127) == 0) {
  1840. throttle_work_update(&pool->throttle);
  1841. dm_pool_issue_prefetches(pool->pmd);
  1842. }
  1843. }
  1844. blk_finish_plug(&plug);
  1845. }
  1846. static int cmp_cells(const void *lhs, const void *rhs)
  1847. {
  1848. struct dm_bio_prison_cell *lhs_cell = *((struct dm_bio_prison_cell **) lhs);
  1849. struct dm_bio_prison_cell *rhs_cell = *((struct dm_bio_prison_cell **) rhs);
  1850. BUG_ON(!lhs_cell->holder);
  1851. BUG_ON(!rhs_cell->holder);
  1852. if (lhs_cell->holder->bi_iter.bi_sector < rhs_cell->holder->bi_iter.bi_sector)
  1853. return -1;
  1854. if (lhs_cell->holder->bi_iter.bi_sector > rhs_cell->holder->bi_iter.bi_sector)
  1855. return 1;
  1856. return 0;
  1857. }
  1858. static unsigned sort_cells(struct pool *pool, struct list_head *cells)
  1859. {
  1860. unsigned count = 0;
  1861. struct dm_bio_prison_cell *cell, *tmp;
  1862. list_for_each_entry_safe(cell, tmp, cells, user_list) {
  1863. if (count >= CELL_SORT_ARRAY_SIZE)
  1864. break;
  1865. pool->cell_sort_array[count++] = cell;
  1866. list_del(&cell->user_list);
  1867. }
  1868. sort(pool->cell_sort_array, count, sizeof(cell), cmp_cells, NULL);
  1869. return count;
  1870. }
  1871. static void process_thin_deferred_cells(struct thin_c *tc)
  1872. {
  1873. struct pool *pool = tc->pool;
  1874. struct list_head cells;
  1875. struct dm_bio_prison_cell *cell;
  1876. unsigned i, j, count;
  1877. INIT_LIST_HEAD(&cells);
  1878. spin_lock_irq(&tc->lock);
  1879. list_splice_init(&tc->deferred_cells, &cells);
  1880. spin_unlock_irq(&tc->lock);
  1881. if (list_empty(&cells))
  1882. return;
  1883. do {
  1884. count = sort_cells(tc->pool, &cells);
  1885. for (i = 0; i < count; i++) {
  1886. cell = pool->cell_sort_array[i];
  1887. BUG_ON(!cell->holder);
  1888. /*
  1889. * If we've got no free new_mapping structs, and processing
  1890. * this bio might require one, we pause until there are some
  1891. * prepared mappings to process.
  1892. */
  1893. if (ensure_next_mapping(pool)) {
  1894. for (j = i; j < count; j++)
  1895. list_add(&pool->cell_sort_array[j]->user_list, &cells);
  1896. spin_lock_irq(&tc->lock);
  1897. list_splice(&cells, &tc->deferred_cells);
  1898. spin_unlock_irq(&tc->lock);
  1899. return;
  1900. }
  1901. if (bio_op(cell->holder) == REQ_OP_DISCARD)
  1902. pool->process_discard_cell(tc, cell);
  1903. else
  1904. pool->process_cell(tc, cell);
  1905. }
  1906. } while (!list_empty(&cells));
  1907. }
  1908. static void thin_get(struct thin_c *tc);
  1909. static void thin_put(struct thin_c *tc);
  1910. /*
  1911. * We can't hold rcu_read_lock() around code that can block. So we
  1912. * find a thin with the rcu lock held; bump a refcount; then drop
  1913. * the lock.
  1914. */
  1915. static struct thin_c *get_first_thin(struct pool *pool)
  1916. {
  1917. struct thin_c *tc = NULL;
  1918. rcu_read_lock();
  1919. if (!list_empty(&pool->active_thins)) {
  1920. tc = list_entry_rcu(pool->active_thins.next, struct thin_c, list);
  1921. thin_get(tc);
  1922. }
  1923. rcu_read_unlock();
  1924. return tc;
  1925. }
  1926. static struct thin_c *get_next_thin(struct pool *pool, struct thin_c *tc)
  1927. {
  1928. struct thin_c *old_tc = tc;
  1929. rcu_read_lock();
  1930. list_for_each_entry_continue_rcu(tc, &pool->active_thins, list) {
  1931. thin_get(tc);
  1932. thin_put(old_tc);
  1933. rcu_read_unlock();
  1934. return tc;
  1935. }
  1936. thin_put(old_tc);
  1937. rcu_read_unlock();
  1938. return NULL;
  1939. }
  1940. static void process_deferred_bios(struct pool *pool)
  1941. {
  1942. struct bio *bio;
  1943. struct bio_list bios, bio_completions;
  1944. struct thin_c *tc;
  1945. tc = get_first_thin(pool);
  1946. while (tc) {
  1947. process_thin_deferred_cells(tc);
  1948. process_thin_deferred_bios(tc);
  1949. tc = get_next_thin(pool, tc);
  1950. }
  1951. /*
  1952. * If there are any deferred flush bios, we must commit the metadata
  1953. * before issuing them or signaling their completion.
  1954. */
  1955. bio_list_init(&bios);
  1956. bio_list_init(&bio_completions);
  1957. spin_lock_irq(&pool->lock);
  1958. bio_list_merge(&bios, &pool->deferred_flush_bios);
  1959. bio_list_init(&pool->deferred_flush_bios);
  1960. bio_list_merge(&bio_completions, &pool->deferred_flush_completions);
  1961. bio_list_init(&pool->deferred_flush_completions);
  1962. spin_unlock_irq(&pool->lock);
  1963. if (bio_list_empty(&bios) && bio_list_empty(&bio_completions) &&
  1964. !(dm_pool_changed_this_transaction(pool->pmd) && need_commit_due_to_time(pool)))
  1965. return;
  1966. if (commit(pool)) {
  1967. bio_list_merge(&bios, &bio_completions);
  1968. while ((bio = bio_list_pop(&bios)))
  1969. bio_io_error(bio);
  1970. return;
  1971. }
  1972. pool->last_commit_jiffies = jiffies;
  1973. while ((bio = bio_list_pop(&bio_completions)))
  1974. bio_endio(bio);
  1975. while ((bio = bio_list_pop(&bios))) {
  1976. /*
  1977. * The data device was flushed as part of metadata commit,
  1978. * so complete redundant flushes immediately.
  1979. */
  1980. if (bio->bi_opf & REQ_PREFLUSH)
  1981. bio_endio(bio);
  1982. else
  1983. generic_make_request(bio);
  1984. }
  1985. }
  1986. static void do_worker(struct work_struct *ws)
  1987. {
  1988. struct pool *pool = container_of(ws, struct pool, worker);
  1989. throttle_work_start(&pool->throttle);
  1990. dm_pool_issue_prefetches(pool->pmd);
  1991. throttle_work_update(&pool->throttle);
  1992. process_prepared(pool, &pool->prepared_mappings, &pool->process_prepared_mapping);
  1993. throttle_work_update(&pool->throttle);
  1994. process_prepared(pool, &pool->prepared_discards, &pool->process_prepared_discard);
  1995. throttle_work_update(&pool->throttle);
  1996. process_prepared(pool, &pool->prepared_discards_pt2, &pool->process_prepared_discard_pt2);
  1997. throttle_work_update(&pool->throttle);
  1998. process_deferred_bios(pool);
  1999. throttle_work_complete(&pool->throttle);
  2000. }
  2001. /*
  2002. * We want to commit periodically so that not too much
  2003. * unwritten data builds up.
  2004. */
  2005. static void do_waker(struct work_struct *ws)
  2006. {
  2007. struct pool *pool = container_of(to_delayed_work(ws), struct pool, waker);
  2008. wake_worker(pool);
  2009. queue_delayed_work(pool->wq, &pool->waker, COMMIT_PERIOD);
  2010. }
  2011. /*
  2012. * We're holding onto IO to allow userland time to react. After the
  2013. * timeout either the pool will have been resized (and thus back in
  2014. * PM_WRITE mode), or we degrade to PM_OUT_OF_DATA_SPACE w/ error_if_no_space.
  2015. */
  2016. static void do_no_space_timeout(struct work_struct *ws)
  2017. {
  2018. struct pool *pool = container_of(to_delayed_work(ws), struct pool,
  2019. no_space_timeout);
  2020. if (get_pool_mode(pool) == PM_OUT_OF_DATA_SPACE && !pool->pf.error_if_no_space) {
  2021. pool->pf.error_if_no_space = true;
  2022. notify_of_pool_mode_change(pool);
  2023. error_retry_list_with_code(pool, BLK_STS_NOSPC);
  2024. }
  2025. }
  2026. /*----------------------------------------------------------------*/
  2027. struct pool_work {
  2028. struct work_struct worker;
  2029. struct completion complete;
  2030. };
  2031. static struct pool_work *to_pool_work(struct work_struct *ws)
  2032. {
  2033. return container_of(ws, struct pool_work, worker);
  2034. }
  2035. static void pool_work_complete(struct pool_work *pw)
  2036. {
  2037. complete(&pw->complete);
  2038. }
  2039. static void pool_work_wait(struct pool_work *pw, struct pool *pool,
  2040. void (*fn)(struct work_struct *))
  2041. {
  2042. INIT_WORK_ONSTACK(&pw->worker, fn);
  2043. init_completion(&pw->complete);
  2044. queue_work(pool->wq, &pw->worker);
  2045. wait_for_completion(&pw->complete);
  2046. }
  2047. /*----------------------------------------------------------------*/
  2048. struct noflush_work {
  2049. struct pool_work pw;
  2050. struct thin_c *tc;
  2051. };
  2052. static struct noflush_work *to_noflush(struct work_struct *ws)
  2053. {
  2054. return container_of(to_pool_work(ws), struct noflush_work, pw);
  2055. }
  2056. static void do_noflush_start(struct work_struct *ws)
  2057. {
  2058. struct noflush_work *w = to_noflush(ws);
  2059. w->tc->requeue_mode = true;
  2060. requeue_io(w->tc);
  2061. pool_work_complete(&w->pw);
  2062. }
  2063. static void do_noflush_stop(struct work_struct *ws)
  2064. {
  2065. struct noflush_work *w = to_noflush(ws);
  2066. w->tc->requeue_mode = false;
  2067. pool_work_complete(&w->pw);
  2068. }
  2069. static void noflush_work(struct thin_c *tc, void (*fn)(struct work_struct *))
  2070. {
  2071. struct noflush_work w;
  2072. w.tc = tc;
  2073. pool_work_wait(&w.pw, tc->pool, fn);
  2074. }
  2075. /*----------------------------------------------------------------*/
  2076. static bool passdown_enabled(struct pool_c *pt)
  2077. {
  2078. return pt->adjusted_pf.discard_passdown;
  2079. }
  2080. static void set_discard_callbacks(struct pool *pool)
  2081. {
  2082. struct pool_c *pt = pool->ti->private;
  2083. if (passdown_enabled(pt)) {
  2084. pool->process_discard_cell = process_discard_cell_passdown;
  2085. pool->process_prepared_discard = process_prepared_discard_passdown_pt1;
  2086. pool->process_prepared_discard_pt2 = process_prepared_discard_passdown_pt2;
  2087. } else {
  2088. pool->process_discard_cell = process_discard_cell_no_passdown;
  2089. pool->process_prepared_discard = process_prepared_discard_no_passdown;
  2090. }
  2091. }
  2092. static void set_pool_mode(struct pool *pool, enum pool_mode new_mode)
  2093. {
  2094. struct pool_c *pt = pool->ti->private;
  2095. bool needs_check = dm_pool_metadata_needs_check(pool->pmd);
  2096. enum pool_mode old_mode = get_pool_mode(pool);
  2097. unsigned long no_space_timeout = READ_ONCE(no_space_timeout_secs) * HZ;
  2098. /*
  2099. * Never allow the pool to transition to PM_WRITE mode if user
  2100. * intervention is required to verify metadata and data consistency.
  2101. */
  2102. if (new_mode == PM_WRITE && needs_check) {
  2103. DMERR("%s: unable to switch pool to write mode until repaired.",
  2104. dm_device_name(pool->pool_md));
  2105. if (old_mode != new_mode)
  2106. new_mode = old_mode;
  2107. else
  2108. new_mode = PM_READ_ONLY;
  2109. }
  2110. /*
  2111. * If we were in PM_FAIL mode, rollback of metadata failed. We're
  2112. * not going to recover without a thin_repair. So we never let the
  2113. * pool move out of the old mode.
  2114. */
  2115. if (old_mode == PM_FAIL)
  2116. new_mode = old_mode;
  2117. switch (new_mode) {
  2118. case PM_FAIL:
  2119. dm_pool_metadata_read_only(pool->pmd);
  2120. pool->process_bio = process_bio_fail;
  2121. pool->process_discard = process_bio_fail;
  2122. pool->process_cell = process_cell_fail;
  2123. pool->process_discard_cell = process_cell_fail;
  2124. pool->process_prepared_mapping = process_prepared_mapping_fail;
  2125. pool->process_prepared_discard = process_prepared_discard_fail;
  2126. error_retry_list(pool);
  2127. break;
  2128. case PM_OUT_OF_METADATA_SPACE:
  2129. case PM_READ_ONLY:
  2130. dm_pool_metadata_read_only(pool->pmd);
  2131. pool->process_bio = process_bio_read_only;
  2132. pool->process_discard = process_bio_success;
  2133. pool->process_cell = process_cell_read_only;
  2134. pool->process_discard_cell = process_cell_success;
  2135. pool->process_prepared_mapping = process_prepared_mapping_fail;
  2136. pool->process_prepared_discard = process_prepared_discard_success;
  2137. error_retry_list(pool);
  2138. break;
  2139. case PM_OUT_OF_DATA_SPACE:
  2140. /*
  2141. * Ideally we'd never hit this state; the low water mark
  2142. * would trigger userland to extend the pool before we
  2143. * completely run out of data space. However, many small
  2144. * IOs to unprovisioned space can consume data space at an
  2145. * alarming rate. Adjust your low water mark if you're
  2146. * frequently seeing this mode.
  2147. */
  2148. pool->out_of_data_space = true;
  2149. pool->process_bio = process_bio_read_only;
  2150. pool->process_discard = process_discard_bio;
  2151. pool->process_cell = process_cell_read_only;
  2152. pool->process_prepared_mapping = process_prepared_mapping;
  2153. set_discard_callbacks(pool);
  2154. if (!pool->pf.error_if_no_space && no_space_timeout)
  2155. queue_delayed_work(pool->wq, &pool->no_space_timeout, no_space_timeout);
  2156. break;
  2157. case PM_WRITE:
  2158. if (old_mode == PM_OUT_OF_DATA_SPACE)
  2159. cancel_delayed_work_sync(&pool->no_space_timeout);
  2160. pool->out_of_data_space = false;
  2161. pool->pf.error_if_no_space = pt->requested_pf.error_if_no_space;
  2162. dm_pool_metadata_read_write(pool->pmd);
  2163. pool->process_bio = process_bio;
  2164. pool->process_discard = process_discard_bio;
  2165. pool->process_cell = process_cell;
  2166. pool->process_prepared_mapping = process_prepared_mapping;
  2167. set_discard_callbacks(pool);
  2168. break;
  2169. }
  2170. pool->pf.mode = new_mode;
  2171. /*
  2172. * The pool mode may have changed, sync it so bind_control_target()
  2173. * doesn't cause an unexpected mode transition on resume.
  2174. */
  2175. pt->adjusted_pf.mode = new_mode;
  2176. if (old_mode != new_mode)
  2177. notify_of_pool_mode_change(pool);
  2178. }
  2179. static void abort_transaction(struct pool *pool)
  2180. {
  2181. const char *dev_name = dm_device_name(pool->pool_md);
  2182. DMERR_LIMIT("%s: aborting current metadata transaction", dev_name);
  2183. if (dm_pool_abort_metadata(pool->pmd)) {
  2184. DMERR("%s: failed to abort metadata transaction", dev_name);
  2185. set_pool_mode(pool, PM_FAIL);
  2186. }
  2187. if (dm_pool_metadata_set_needs_check(pool->pmd)) {
  2188. DMERR("%s: failed to set 'needs_check' flag in metadata", dev_name);
  2189. set_pool_mode(pool, PM_FAIL);
  2190. }
  2191. }
  2192. static void metadata_operation_failed(struct pool *pool, const char *op, int r)
  2193. {
  2194. DMERR_LIMIT("%s: metadata operation '%s' failed: error = %d",
  2195. dm_device_name(pool->pool_md), op, r);
  2196. abort_transaction(pool);
  2197. set_pool_mode(pool, PM_READ_ONLY);
  2198. }
  2199. /*----------------------------------------------------------------*/
  2200. /*
  2201. * Mapping functions.
  2202. */
  2203. /*
  2204. * Called only while mapping a thin bio to hand it over to the workqueue.
  2205. */
  2206. static void thin_defer_bio(struct thin_c *tc, struct bio *bio)
  2207. {
  2208. struct pool *pool = tc->pool;
  2209. spin_lock_irq(&tc->lock);
  2210. bio_list_add(&tc->deferred_bio_list, bio);
  2211. spin_unlock_irq(&tc->lock);
  2212. wake_worker(pool);
  2213. }
  2214. static void thin_defer_bio_with_throttle(struct thin_c *tc, struct bio *bio)
  2215. {
  2216. struct pool *pool = tc->pool;
  2217. throttle_lock(&pool->throttle);
  2218. thin_defer_bio(tc, bio);
  2219. throttle_unlock(&pool->throttle);
  2220. }
  2221. static void thin_defer_cell(struct thin_c *tc, struct dm_bio_prison_cell *cell)
  2222. {
  2223. struct pool *pool = tc->pool;
  2224. throttle_lock(&pool->throttle);
  2225. spin_lock_irq(&tc->lock);
  2226. list_add_tail(&cell->user_list, &tc->deferred_cells);
  2227. spin_unlock_irq(&tc->lock);
  2228. throttle_unlock(&pool->throttle);
  2229. wake_worker(pool);
  2230. }
  2231. static void thin_hook_bio(struct thin_c *tc, struct bio *bio)
  2232. {
  2233. struct dm_thin_endio_hook *h = dm_per_bio_data(bio, sizeof(struct dm_thin_endio_hook));
  2234. h->tc = tc;
  2235. h->shared_read_entry = NULL;
  2236. h->all_io_entry = NULL;
  2237. h->overwrite_mapping = NULL;
  2238. h->cell = NULL;
  2239. }
  2240. /*
  2241. * Non-blocking function called from the thin target's map function.
  2242. */
  2243. static int thin_bio_map(struct dm_target *ti, struct bio *bio)
  2244. {
  2245. int r;
  2246. struct thin_c *tc = ti->private;
  2247. dm_block_t block = get_bio_block(tc, bio);
  2248. struct dm_thin_device *td = tc->td;
  2249. struct dm_thin_lookup_result result;
  2250. struct dm_bio_prison_cell *virt_cell, *data_cell;
  2251. struct dm_cell_key key;
  2252. thin_hook_bio(tc, bio);
  2253. if (tc->requeue_mode) {
  2254. bio->bi_status = BLK_STS_DM_REQUEUE;
  2255. bio_endio(bio);
  2256. return DM_MAPIO_SUBMITTED;
  2257. }
  2258. if (get_pool_mode(tc->pool) == PM_FAIL) {
  2259. bio_io_error(bio);
  2260. return DM_MAPIO_SUBMITTED;
  2261. }
  2262. if (op_is_flush(bio->bi_opf) || bio_op(bio) == REQ_OP_DISCARD) {
  2263. thin_defer_bio_with_throttle(tc, bio);
  2264. return DM_MAPIO_SUBMITTED;
  2265. }
  2266. /*
  2267. * We must hold the virtual cell before doing the lookup, otherwise
  2268. * there's a race with discard.
  2269. */
  2270. build_virtual_key(tc->td, block, &key);
  2271. if (bio_detain(tc->pool, &key, bio, &virt_cell))
  2272. return DM_MAPIO_SUBMITTED;
  2273. r = dm_thin_find_block(td, block, 0, &result);
  2274. /*
  2275. * Note that we defer readahead too.
  2276. */
  2277. switch (r) {
  2278. case 0:
  2279. if (unlikely(result.shared)) {
  2280. /*
  2281. * We have a race condition here between the
  2282. * result.shared value returned by the lookup and
  2283. * snapshot creation, which may cause new
  2284. * sharing.
  2285. *
  2286. * To avoid this always quiesce the origin before
  2287. * taking the snap. You want to do this anyway to
  2288. * ensure a consistent application view
  2289. * (i.e. lockfs).
  2290. *
  2291. * More distant ancestors are irrelevant. The
  2292. * shared flag will be set in their case.
  2293. */
  2294. thin_defer_cell(tc, virt_cell);
  2295. return DM_MAPIO_SUBMITTED;
  2296. }
  2297. build_data_key(tc->td, result.block, &key);
  2298. if (bio_detain(tc->pool, &key, bio, &data_cell)) {
  2299. cell_defer_no_holder(tc, virt_cell);
  2300. return DM_MAPIO_SUBMITTED;
  2301. }
  2302. inc_all_io_entry(tc->pool, bio);
  2303. cell_defer_no_holder(tc, data_cell);
  2304. cell_defer_no_holder(tc, virt_cell);
  2305. remap(tc, bio, result.block);
  2306. return DM_MAPIO_REMAPPED;
  2307. case -ENODATA:
  2308. case -EWOULDBLOCK:
  2309. thin_defer_cell(tc, virt_cell);
  2310. return DM_MAPIO_SUBMITTED;
  2311. default:
  2312. /*
  2313. * Must always call bio_io_error on failure.
  2314. * dm_thin_find_block can fail with -EINVAL if the
  2315. * pool is switched to fail-io mode.
  2316. */
  2317. bio_io_error(bio);
  2318. cell_defer_no_holder(tc, virt_cell);
  2319. return DM_MAPIO_SUBMITTED;
  2320. }
  2321. }
  2322. static int pool_is_congested(struct dm_target_callbacks *cb, int bdi_bits)
  2323. {
  2324. struct pool_c *pt = container_of(cb, struct pool_c, callbacks);
  2325. struct request_queue *q;
  2326. if (get_pool_mode(pt->pool) == PM_OUT_OF_DATA_SPACE)
  2327. return 1;
  2328. q = bdev_get_queue(pt->data_dev->bdev);
  2329. return bdi_congested(q->backing_dev_info, bdi_bits);
  2330. }
  2331. static void requeue_bios(struct pool *pool)
  2332. {
  2333. struct thin_c *tc;
  2334. rcu_read_lock();
  2335. list_for_each_entry_rcu(tc, &pool->active_thins, list) {
  2336. spin_lock_irq(&tc->lock);
  2337. bio_list_merge(&tc->deferred_bio_list, &tc->retry_on_resume_list);
  2338. bio_list_init(&tc->retry_on_resume_list);
  2339. spin_unlock_irq(&tc->lock);
  2340. }
  2341. rcu_read_unlock();
  2342. }
  2343. /*----------------------------------------------------------------
  2344. * Binding of control targets to a pool object
  2345. *--------------------------------------------------------------*/
  2346. static bool data_dev_supports_discard(struct pool_c *pt)
  2347. {
  2348. struct request_queue *q = bdev_get_queue(pt->data_dev->bdev);
  2349. return q && blk_queue_discard(q);
  2350. }
  2351. static bool is_factor(sector_t block_size, uint32_t n)
  2352. {
  2353. return !sector_div(block_size, n);
  2354. }
  2355. /*
  2356. * If discard_passdown was enabled verify that the data device
  2357. * supports discards. Disable discard_passdown if not.
  2358. */
  2359. static void disable_passdown_if_not_supported(struct pool_c *pt)
  2360. {
  2361. struct pool *pool = pt->pool;
  2362. struct block_device *data_bdev = pt->data_dev->bdev;
  2363. struct queue_limits *data_limits = &bdev_get_queue(data_bdev)->limits;
  2364. const char *reason = NULL;
  2365. char buf[BDEVNAME_SIZE];
  2366. if (!pt->adjusted_pf.discard_passdown)
  2367. return;
  2368. if (!data_dev_supports_discard(pt))
  2369. reason = "discard unsupported";
  2370. else if (data_limits->max_discard_sectors < pool->sectors_per_block)
  2371. reason = "max discard sectors smaller than a block";
  2372. if (reason) {
  2373. DMWARN("Data device (%s) %s: Disabling discard passdown.", bdevname(data_bdev, buf), reason);
  2374. pt->adjusted_pf.discard_passdown = false;
  2375. }
  2376. }
  2377. static int bind_control_target(struct pool *pool, struct dm_target *ti)
  2378. {
  2379. struct pool_c *pt = ti->private;
  2380. /*
  2381. * We want to make sure that a pool in PM_FAIL mode is never upgraded.
  2382. */
  2383. enum pool_mode old_mode = get_pool_mode(pool);
  2384. enum pool_mode new_mode = pt->adjusted_pf.mode;
  2385. /*
  2386. * Don't change the pool's mode until set_pool_mode() below.
  2387. * Otherwise the pool's process_* function pointers may
  2388. * not match the desired pool mode.
  2389. */
  2390. pt->adjusted_pf.mode = old_mode;
  2391. pool->ti = ti;
  2392. pool->pf = pt->adjusted_pf;
  2393. pool->low_water_blocks = pt->low_water_blocks;
  2394. set_pool_mode(pool, new_mode);
  2395. return 0;
  2396. }
  2397. static void unbind_control_target(struct pool *pool, struct dm_target *ti)
  2398. {
  2399. if (pool->ti == ti)
  2400. pool->ti = NULL;
  2401. }
  2402. /*----------------------------------------------------------------
  2403. * Pool creation
  2404. *--------------------------------------------------------------*/
  2405. /* Initialize pool features. */
  2406. static void pool_features_init(struct pool_features *pf)
  2407. {
  2408. pf->mode = PM_WRITE;
  2409. pf->zero_new_blocks = true;
  2410. pf->discard_enabled = true;
  2411. pf->discard_passdown = true;
  2412. pf->error_if_no_space = false;
  2413. }
  2414. static void __pool_destroy(struct pool *pool)
  2415. {
  2416. __pool_table_remove(pool);
  2417. vfree(pool->cell_sort_array);
  2418. if (dm_pool_metadata_close(pool->pmd) < 0)
  2419. DMWARN("%s: dm_pool_metadata_close() failed.", __func__);
  2420. dm_bio_prison_destroy(pool->prison);
  2421. dm_kcopyd_client_destroy(pool->copier);
  2422. if (pool->wq)
  2423. destroy_workqueue(pool->wq);
  2424. if (pool->next_mapping)
  2425. mempool_free(pool->next_mapping, &pool->mapping_pool);
  2426. mempool_exit(&pool->mapping_pool);
  2427. bio_uninit(&pool->flush_bio);
  2428. dm_deferred_set_destroy(pool->shared_read_ds);
  2429. dm_deferred_set_destroy(pool->all_io_ds);
  2430. kfree(pool);
  2431. }
  2432. static struct kmem_cache *_new_mapping_cache;
  2433. static struct pool *pool_create(struct mapped_device *pool_md,
  2434. struct block_device *metadata_dev,
  2435. struct block_device *data_dev,
  2436. unsigned long block_size,
  2437. int read_only, char **error)
  2438. {
  2439. int r;
  2440. void *err_p;
  2441. struct pool *pool;
  2442. struct dm_pool_metadata *pmd;
  2443. bool format_device = read_only ? false : true;
  2444. pmd = dm_pool_metadata_open(metadata_dev, block_size, format_device);
  2445. if (IS_ERR(pmd)) {
  2446. *error = "Error creating metadata object";
  2447. return (struct pool *)pmd;
  2448. }
  2449. pool = kzalloc(sizeof(*pool), GFP_KERNEL);
  2450. if (!pool) {
  2451. *error = "Error allocating memory for pool";
  2452. err_p = ERR_PTR(-ENOMEM);
  2453. goto bad_pool;
  2454. }
  2455. pool->pmd = pmd;
  2456. pool->sectors_per_block = block_size;
  2457. if (block_size & (block_size - 1))
  2458. pool->sectors_per_block_shift = -1;
  2459. else
  2460. pool->sectors_per_block_shift = __ffs(block_size);
  2461. pool->low_water_blocks = 0;
  2462. pool_features_init(&pool->pf);
  2463. pool->prison = dm_bio_prison_create();
  2464. if (!pool->prison) {
  2465. *error = "Error creating pool's bio prison";
  2466. err_p = ERR_PTR(-ENOMEM);
  2467. goto bad_prison;
  2468. }
  2469. pool->copier = dm_kcopyd_client_create(&dm_kcopyd_throttle);
  2470. if (IS_ERR(pool->copier)) {
  2471. r = PTR_ERR(pool->copier);
  2472. *error = "Error creating pool's kcopyd client";
  2473. err_p = ERR_PTR(r);
  2474. goto bad_kcopyd_client;
  2475. }
  2476. /*
  2477. * Create singlethreaded workqueue that will service all devices
  2478. * that use this metadata.
  2479. */
  2480. pool->wq = alloc_ordered_workqueue("dm-" DM_MSG_PREFIX, WQ_MEM_RECLAIM);
  2481. if (!pool->wq) {
  2482. *error = "Error creating pool's workqueue";
  2483. err_p = ERR_PTR(-ENOMEM);
  2484. goto bad_wq;
  2485. }
  2486. throttle_init(&pool->throttle);
  2487. INIT_WORK(&pool->worker, do_worker);
  2488. INIT_DELAYED_WORK(&pool->waker, do_waker);
  2489. INIT_DELAYED_WORK(&pool->no_space_timeout, do_no_space_timeout);
  2490. spin_lock_init(&pool->lock);
  2491. bio_list_init(&pool->deferred_flush_bios);
  2492. bio_list_init(&pool->deferred_flush_completions);
  2493. INIT_LIST_HEAD(&pool->prepared_mappings);
  2494. INIT_LIST_HEAD(&pool->prepared_discards);
  2495. INIT_LIST_HEAD(&pool->prepared_discards_pt2);
  2496. INIT_LIST_HEAD(&pool->active_thins);
  2497. pool->low_water_triggered = false;
  2498. pool->suspended = true;
  2499. pool->out_of_data_space = false;
  2500. bio_init(&pool->flush_bio, NULL, 0);
  2501. pool->shared_read_ds = dm_deferred_set_create();
  2502. if (!pool->shared_read_ds) {
  2503. *error = "Error creating pool's shared read deferred set";
  2504. err_p = ERR_PTR(-ENOMEM);
  2505. goto bad_shared_read_ds;
  2506. }
  2507. pool->all_io_ds = dm_deferred_set_create();
  2508. if (!pool->all_io_ds) {
  2509. *error = "Error creating pool's all io deferred set";
  2510. err_p = ERR_PTR(-ENOMEM);
  2511. goto bad_all_io_ds;
  2512. }
  2513. pool->next_mapping = NULL;
  2514. r = mempool_init_slab_pool(&pool->mapping_pool, MAPPING_POOL_SIZE,
  2515. _new_mapping_cache);
  2516. if (r) {
  2517. *error = "Error creating pool's mapping mempool";
  2518. err_p = ERR_PTR(r);
  2519. goto bad_mapping_pool;
  2520. }
  2521. pool->cell_sort_array =
  2522. vmalloc(array_size(CELL_SORT_ARRAY_SIZE,
  2523. sizeof(*pool->cell_sort_array)));
  2524. if (!pool->cell_sort_array) {
  2525. *error = "Error allocating cell sort array";
  2526. err_p = ERR_PTR(-ENOMEM);
  2527. goto bad_sort_array;
  2528. }
  2529. pool->ref_count = 1;
  2530. pool->last_commit_jiffies = jiffies;
  2531. pool->pool_md = pool_md;
  2532. pool->md_dev = metadata_dev;
  2533. pool->data_dev = data_dev;
  2534. __pool_table_insert(pool);
  2535. return pool;
  2536. bad_sort_array:
  2537. mempool_exit(&pool->mapping_pool);
  2538. bad_mapping_pool:
  2539. dm_deferred_set_destroy(pool->all_io_ds);
  2540. bad_all_io_ds:
  2541. dm_deferred_set_destroy(pool->shared_read_ds);
  2542. bad_shared_read_ds:
  2543. destroy_workqueue(pool->wq);
  2544. bad_wq:
  2545. dm_kcopyd_client_destroy(pool->copier);
  2546. bad_kcopyd_client:
  2547. dm_bio_prison_destroy(pool->prison);
  2548. bad_prison:
  2549. kfree(pool);
  2550. bad_pool:
  2551. if (dm_pool_metadata_close(pmd))
  2552. DMWARN("%s: dm_pool_metadata_close() failed.", __func__);
  2553. return err_p;
  2554. }
  2555. static void __pool_inc(struct pool *pool)
  2556. {
  2557. BUG_ON(!mutex_is_locked(&dm_thin_pool_table.mutex));
  2558. pool->ref_count++;
  2559. }
  2560. static void __pool_dec(struct pool *pool)
  2561. {
  2562. BUG_ON(!mutex_is_locked(&dm_thin_pool_table.mutex));
  2563. BUG_ON(!pool->ref_count);
  2564. if (!--pool->ref_count)
  2565. __pool_destroy(pool);
  2566. }
  2567. static struct pool *__pool_find(struct mapped_device *pool_md,
  2568. struct block_device *metadata_dev,
  2569. struct block_device *data_dev,
  2570. unsigned long block_size, int read_only,
  2571. char **error, int *created)
  2572. {
  2573. struct pool *pool = __pool_table_lookup_metadata_dev(metadata_dev);
  2574. if (pool) {
  2575. if (pool->pool_md != pool_md) {
  2576. *error = "metadata device already in use by a pool";
  2577. return ERR_PTR(-EBUSY);
  2578. }
  2579. if (pool->data_dev != data_dev) {
  2580. *error = "data device already in use by a pool";
  2581. return ERR_PTR(-EBUSY);
  2582. }
  2583. __pool_inc(pool);
  2584. } else {
  2585. pool = __pool_table_lookup(pool_md);
  2586. if (pool) {
  2587. if (pool->md_dev != metadata_dev || pool->data_dev != data_dev) {
  2588. *error = "different pool cannot replace a pool";
  2589. return ERR_PTR(-EINVAL);
  2590. }
  2591. __pool_inc(pool);
  2592. } else {
  2593. pool = pool_create(pool_md, metadata_dev, data_dev, block_size, read_only, error);
  2594. *created = 1;
  2595. }
  2596. }
  2597. return pool;
  2598. }
  2599. /*----------------------------------------------------------------
  2600. * Pool target methods
  2601. *--------------------------------------------------------------*/
  2602. static void pool_dtr(struct dm_target *ti)
  2603. {
  2604. struct pool_c *pt = ti->private;
  2605. mutex_lock(&dm_thin_pool_table.mutex);
  2606. unbind_control_target(pt->pool, ti);
  2607. __pool_dec(pt->pool);
  2608. dm_put_device(ti, pt->metadata_dev);
  2609. dm_put_device(ti, pt->data_dev);
  2610. kfree(pt);
  2611. mutex_unlock(&dm_thin_pool_table.mutex);
  2612. }
  2613. static int parse_pool_features(struct dm_arg_set *as, struct pool_features *pf,
  2614. struct dm_target *ti)
  2615. {
  2616. int r;
  2617. unsigned argc;
  2618. const char *arg_name;
  2619. static const struct dm_arg _args[] = {
  2620. {0, 4, "Invalid number of pool feature arguments"},
  2621. };
  2622. /*
  2623. * No feature arguments supplied.
  2624. */
  2625. if (!as->argc)
  2626. return 0;
  2627. r = dm_read_arg_group(_args, as, &argc, &ti->error);
  2628. if (r)
  2629. return -EINVAL;
  2630. while (argc && !r) {
  2631. arg_name = dm_shift_arg(as);
  2632. argc--;
  2633. if (!strcasecmp(arg_name, "skip_block_zeroing"))
  2634. pf->zero_new_blocks = false;
  2635. else if (!strcasecmp(arg_name, "ignore_discard"))
  2636. pf->discard_enabled = false;
  2637. else if (!strcasecmp(arg_name, "no_discard_passdown"))
  2638. pf->discard_passdown = false;
  2639. else if (!strcasecmp(arg_name, "read_only"))
  2640. pf->mode = PM_READ_ONLY;
  2641. else if (!strcasecmp(arg_name, "error_if_no_space"))
  2642. pf->error_if_no_space = true;
  2643. else {
  2644. ti->error = "Unrecognised pool feature requested";
  2645. r = -EINVAL;
  2646. break;
  2647. }
  2648. }
  2649. return r;
  2650. }
  2651. static void metadata_low_callback(void *context)
  2652. {
  2653. struct pool *pool = context;
  2654. DMWARN("%s: reached low water mark for metadata device: sending event.",
  2655. dm_device_name(pool->pool_md));
  2656. dm_table_event(pool->ti->table);
  2657. }
  2658. /*
  2659. * We need to flush the data device **before** committing the metadata.
  2660. *
  2661. * This ensures that the data blocks of any newly inserted mappings are
  2662. * properly written to non-volatile storage and won't be lost in case of a
  2663. * crash.
  2664. *
  2665. * Failure to do so can result in data corruption in the case of internal or
  2666. * external snapshots and in the case of newly provisioned blocks, when block
  2667. * zeroing is enabled.
  2668. */
  2669. static int metadata_pre_commit_callback(void *context)
  2670. {
  2671. struct pool *pool = context;
  2672. struct bio *flush_bio = &pool->flush_bio;
  2673. bio_reset(flush_bio);
  2674. bio_set_dev(flush_bio, pool->data_dev);
  2675. flush_bio->bi_opf = REQ_OP_WRITE | REQ_PREFLUSH;
  2676. return submit_bio_wait(flush_bio);
  2677. }
  2678. static sector_t get_dev_size(struct block_device *bdev)
  2679. {
  2680. return i_size_read(bdev->bd_inode) >> SECTOR_SHIFT;
  2681. }
  2682. static void warn_if_metadata_device_too_big(struct block_device *bdev)
  2683. {
  2684. sector_t metadata_dev_size = get_dev_size(bdev);
  2685. char buffer[BDEVNAME_SIZE];
  2686. if (metadata_dev_size > THIN_METADATA_MAX_SECTORS_WARNING)
  2687. DMWARN("Metadata device %s is larger than %u sectors: excess space will not be used.",
  2688. bdevname(bdev, buffer), THIN_METADATA_MAX_SECTORS);
  2689. }
  2690. static sector_t get_metadata_dev_size(struct block_device *bdev)
  2691. {
  2692. sector_t metadata_dev_size = get_dev_size(bdev);
  2693. if (metadata_dev_size > THIN_METADATA_MAX_SECTORS)
  2694. metadata_dev_size = THIN_METADATA_MAX_SECTORS;
  2695. return metadata_dev_size;
  2696. }
  2697. static dm_block_t get_metadata_dev_size_in_blocks(struct block_device *bdev)
  2698. {
  2699. sector_t metadata_dev_size = get_metadata_dev_size(bdev);
  2700. sector_div(metadata_dev_size, THIN_METADATA_BLOCK_SIZE);
  2701. return metadata_dev_size;
  2702. }
  2703. /*
  2704. * When a metadata threshold is crossed a dm event is triggered, and
  2705. * userland should respond by growing the metadata device. We could let
  2706. * userland set the threshold, like we do with the data threshold, but I'm
  2707. * not sure they know enough to do this well.
  2708. */
  2709. static dm_block_t calc_metadata_threshold(struct pool_c *pt)
  2710. {
  2711. /*
  2712. * 4M is ample for all ops with the possible exception of thin
  2713. * device deletion which is harmless if it fails (just retry the
  2714. * delete after you've grown the device).
  2715. */
  2716. dm_block_t quarter = get_metadata_dev_size_in_blocks(pt->metadata_dev->bdev) / 4;
  2717. return min((dm_block_t)1024ULL /* 4M */, quarter);
  2718. }
  2719. /*
  2720. * thin-pool <metadata dev> <data dev>
  2721. * <data block size (sectors)>
  2722. * <low water mark (blocks)>
  2723. * [<#feature args> [<arg>]*]
  2724. *
  2725. * Optional feature arguments are:
  2726. * skip_block_zeroing: skips the zeroing of newly-provisioned blocks.
  2727. * ignore_discard: disable discard
  2728. * no_discard_passdown: don't pass discards down to the data device
  2729. * read_only: Don't allow any changes to be made to the pool metadata.
  2730. * error_if_no_space: error IOs, instead of queueing, if no space.
  2731. */
  2732. static int pool_ctr(struct dm_target *ti, unsigned argc, char **argv)
  2733. {
  2734. int r, pool_created = 0;
  2735. struct pool_c *pt;
  2736. struct pool *pool;
  2737. struct pool_features pf;
  2738. struct dm_arg_set as;
  2739. struct dm_dev *data_dev;
  2740. unsigned long block_size;
  2741. dm_block_t low_water_blocks;
  2742. struct dm_dev *metadata_dev;
  2743. fmode_t metadata_mode;
  2744. /*
  2745. * FIXME Remove validation from scope of lock.
  2746. */
  2747. mutex_lock(&dm_thin_pool_table.mutex);
  2748. if (argc < 4) {
  2749. ti->error = "Invalid argument count";
  2750. r = -EINVAL;
  2751. goto out_unlock;
  2752. }
  2753. as.argc = argc;
  2754. as.argv = argv;
  2755. /* make sure metadata and data are different devices */
  2756. if (!strcmp(argv[0], argv[1])) {
  2757. ti->error = "Error setting metadata or data device";
  2758. r = -EINVAL;
  2759. goto out_unlock;
  2760. }
  2761. /*
  2762. * Set default pool features.
  2763. */
  2764. pool_features_init(&pf);
  2765. dm_consume_args(&as, 4);
  2766. r = parse_pool_features(&as, &pf, ti);
  2767. if (r)
  2768. goto out_unlock;
  2769. metadata_mode = FMODE_READ | ((pf.mode == PM_READ_ONLY) ? 0 : FMODE_WRITE);
  2770. r = dm_get_device(ti, argv[0], metadata_mode, &metadata_dev);
  2771. if (r) {
  2772. ti->error = "Error opening metadata block device";
  2773. goto out_unlock;
  2774. }
  2775. warn_if_metadata_device_too_big(metadata_dev->bdev);
  2776. r = dm_get_device(ti, argv[1], FMODE_READ | FMODE_WRITE, &data_dev);
  2777. if (r) {
  2778. ti->error = "Error getting data device";
  2779. goto out_metadata;
  2780. }
  2781. if (kstrtoul(argv[2], 10, &block_size) || !block_size ||
  2782. block_size < DATA_DEV_BLOCK_SIZE_MIN_SECTORS ||
  2783. block_size > DATA_DEV_BLOCK_SIZE_MAX_SECTORS ||
  2784. block_size & (DATA_DEV_BLOCK_SIZE_MIN_SECTORS - 1)) {
  2785. ti->error = "Invalid block size";
  2786. r = -EINVAL;
  2787. goto out;
  2788. }
  2789. if (kstrtoull(argv[3], 10, (unsigned long long *)&low_water_blocks)) {
  2790. ti->error = "Invalid low water mark";
  2791. r = -EINVAL;
  2792. goto out;
  2793. }
  2794. pt = kzalloc(sizeof(*pt), GFP_KERNEL);
  2795. if (!pt) {
  2796. r = -ENOMEM;
  2797. goto out;
  2798. }
  2799. pool = __pool_find(dm_table_get_md(ti->table), metadata_dev->bdev, data_dev->bdev,
  2800. block_size, pf.mode == PM_READ_ONLY, &ti->error, &pool_created);
  2801. if (IS_ERR(pool)) {
  2802. r = PTR_ERR(pool);
  2803. goto out_free_pt;
  2804. }
  2805. /*
  2806. * 'pool_created' reflects whether this is the first table load.
  2807. * Top level discard support is not allowed to be changed after
  2808. * initial load. This would require a pool reload to trigger thin
  2809. * device changes.
  2810. */
  2811. if (!pool_created && pf.discard_enabled != pool->pf.discard_enabled) {
  2812. ti->error = "Discard support cannot be disabled once enabled";
  2813. r = -EINVAL;
  2814. goto out_flags_changed;
  2815. }
  2816. pt->pool = pool;
  2817. pt->ti = ti;
  2818. pt->metadata_dev = metadata_dev;
  2819. pt->data_dev = data_dev;
  2820. pt->low_water_blocks = low_water_blocks;
  2821. pt->adjusted_pf = pt->requested_pf = pf;
  2822. ti->num_flush_bios = 1;
  2823. /*
  2824. * Only need to enable discards if the pool should pass
  2825. * them down to the data device. The thin device's discard
  2826. * processing will cause mappings to be removed from the btree.
  2827. */
  2828. if (pf.discard_enabled && pf.discard_passdown) {
  2829. ti->num_discard_bios = 1;
  2830. /*
  2831. * Setting 'discards_supported' circumvents the normal
  2832. * stacking of discard limits (this keeps the pool and
  2833. * thin devices' discard limits consistent).
  2834. */
  2835. ti->discards_supported = true;
  2836. }
  2837. ti->private = pt;
  2838. r = dm_pool_register_metadata_threshold(pt->pool->pmd,
  2839. calc_metadata_threshold(pt),
  2840. metadata_low_callback,
  2841. pool);
  2842. if (r)
  2843. goto out_flags_changed;
  2844. dm_pool_register_pre_commit_callback(pool->pmd,
  2845. metadata_pre_commit_callback, pool);
  2846. pt->callbacks.congested_fn = pool_is_congested;
  2847. dm_table_add_target_callbacks(ti->table, &pt->callbacks);
  2848. mutex_unlock(&dm_thin_pool_table.mutex);
  2849. return 0;
  2850. out_flags_changed:
  2851. __pool_dec(pool);
  2852. out_free_pt:
  2853. kfree(pt);
  2854. out:
  2855. dm_put_device(ti, data_dev);
  2856. out_metadata:
  2857. dm_put_device(ti, metadata_dev);
  2858. out_unlock:
  2859. mutex_unlock(&dm_thin_pool_table.mutex);
  2860. return r;
  2861. }
  2862. static int pool_map(struct dm_target *ti, struct bio *bio)
  2863. {
  2864. int r;
  2865. struct pool_c *pt = ti->private;
  2866. struct pool *pool = pt->pool;
  2867. /*
  2868. * As this is a singleton target, ti->begin is always zero.
  2869. */
  2870. spin_lock_irq(&pool->lock);
  2871. bio_set_dev(bio, pt->data_dev->bdev);
  2872. r = DM_MAPIO_REMAPPED;
  2873. spin_unlock_irq(&pool->lock);
  2874. return r;
  2875. }
  2876. static int maybe_resize_data_dev(struct dm_target *ti, bool *need_commit)
  2877. {
  2878. int r;
  2879. struct pool_c *pt = ti->private;
  2880. struct pool *pool = pt->pool;
  2881. sector_t data_size = ti->len;
  2882. dm_block_t sb_data_size;
  2883. *need_commit = false;
  2884. (void) sector_div(data_size, pool->sectors_per_block);
  2885. r = dm_pool_get_data_dev_size(pool->pmd, &sb_data_size);
  2886. if (r) {
  2887. DMERR("%s: failed to retrieve data device size",
  2888. dm_device_name(pool->pool_md));
  2889. return r;
  2890. }
  2891. if (data_size < sb_data_size) {
  2892. DMERR("%s: pool target (%llu blocks) too small: expected %llu",
  2893. dm_device_name(pool->pool_md),
  2894. (unsigned long long)data_size, sb_data_size);
  2895. return -EINVAL;
  2896. } else if (data_size > sb_data_size) {
  2897. if (dm_pool_metadata_needs_check(pool->pmd)) {
  2898. DMERR("%s: unable to grow the data device until repaired.",
  2899. dm_device_name(pool->pool_md));
  2900. return 0;
  2901. }
  2902. if (sb_data_size)
  2903. DMINFO("%s: growing the data device from %llu to %llu blocks",
  2904. dm_device_name(pool->pool_md),
  2905. sb_data_size, (unsigned long long)data_size);
  2906. r = dm_pool_resize_data_dev(pool->pmd, data_size);
  2907. if (r) {
  2908. metadata_operation_failed(pool, "dm_pool_resize_data_dev", r);
  2909. return r;
  2910. }
  2911. *need_commit = true;
  2912. }
  2913. return 0;
  2914. }
  2915. static int maybe_resize_metadata_dev(struct dm_target *ti, bool *need_commit)
  2916. {
  2917. int r;
  2918. struct pool_c *pt = ti->private;
  2919. struct pool *pool = pt->pool;
  2920. dm_block_t metadata_dev_size, sb_metadata_dev_size;
  2921. *need_commit = false;
  2922. metadata_dev_size = get_metadata_dev_size_in_blocks(pool->md_dev);
  2923. r = dm_pool_get_metadata_dev_size(pool->pmd, &sb_metadata_dev_size);
  2924. if (r) {
  2925. DMERR("%s: failed to retrieve metadata device size",
  2926. dm_device_name(pool->pool_md));
  2927. return r;
  2928. }
  2929. if (metadata_dev_size < sb_metadata_dev_size) {
  2930. DMERR("%s: metadata device (%llu blocks) too small: expected %llu",
  2931. dm_device_name(pool->pool_md),
  2932. metadata_dev_size, sb_metadata_dev_size);
  2933. return -EINVAL;
  2934. } else if (metadata_dev_size > sb_metadata_dev_size) {
  2935. if (dm_pool_metadata_needs_check(pool->pmd)) {
  2936. DMERR("%s: unable to grow the metadata device until repaired.",
  2937. dm_device_name(pool->pool_md));
  2938. return 0;
  2939. }
  2940. warn_if_metadata_device_too_big(pool->md_dev);
  2941. DMINFO("%s: growing the metadata device from %llu to %llu blocks",
  2942. dm_device_name(pool->pool_md),
  2943. sb_metadata_dev_size, metadata_dev_size);
  2944. if (get_pool_mode(pool) == PM_OUT_OF_METADATA_SPACE)
  2945. set_pool_mode(pool, PM_WRITE);
  2946. r = dm_pool_resize_metadata_dev(pool->pmd, metadata_dev_size);
  2947. if (r) {
  2948. metadata_operation_failed(pool, "dm_pool_resize_metadata_dev", r);
  2949. return r;
  2950. }
  2951. *need_commit = true;
  2952. }
  2953. return 0;
  2954. }
  2955. /*
  2956. * Retrieves the number of blocks of the data device from
  2957. * the superblock and compares it to the actual device size,
  2958. * thus resizing the data device in case it has grown.
  2959. *
  2960. * This both copes with opening preallocated data devices in the ctr
  2961. * being followed by a resume
  2962. * -and-
  2963. * calling the resume method individually after userspace has
  2964. * grown the data device in reaction to a table event.
  2965. */
  2966. static int pool_preresume(struct dm_target *ti)
  2967. {
  2968. int r;
  2969. bool need_commit1, need_commit2;
  2970. struct pool_c *pt = ti->private;
  2971. struct pool *pool = pt->pool;
  2972. /*
  2973. * Take control of the pool object.
  2974. */
  2975. r = bind_control_target(pool, ti);
  2976. if (r)
  2977. return r;
  2978. r = maybe_resize_data_dev(ti, &need_commit1);
  2979. if (r)
  2980. return r;
  2981. r = maybe_resize_metadata_dev(ti, &need_commit2);
  2982. if (r)
  2983. return r;
  2984. if (need_commit1 || need_commit2)
  2985. (void) commit(pool);
  2986. return 0;
  2987. }
  2988. static void pool_suspend_active_thins(struct pool *pool)
  2989. {
  2990. struct thin_c *tc;
  2991. /* Suspend all active thin devices */
  2992. tc = get_first_thin(pool);
  2993. while (tc) {
  2994. dm_internal_suspend_noflush(tc->thin_md);
  2995. tc = get_next_thin(pool, tc);
  2996. }
  2997. }
  2998. static void pool_resume_active_thins(struct pool *pool)
  2999. {
  3000. struct thin_c *tc;
  3001. /* Resume all active thin devices */
  3002. tc = get_first_thin(pool);
  3003. while (tc) {
  3004. dm_internal_resume(tc->thin_md);
  3005. tc = get_next_thin(pool, tc);
  3006. }
  3007. }
  3008. static void pool_resume(struct dm_target *ti)
  3009. {
  3010. struct pool_c *pt = ti->private;
  3011. struct pool *pool = pt->pool;
  3012. /*
  3013. * Must requeue active_thins' bios and then resume
  3014. * active_thins _before_ clearing 'suspend' flag.
  3015. */
  3016. requeue_bios(pool);
  3017. pool_resume_active_thins(pool);
  3018. spin_lock_irq(&pool->lock);
  3019. pool->low_water_triggered = false;
  3020. pool->suspended = false;
  3021. spin_unlock_irq(&pool->lock);
  3022. do_waker(&pool->waker.work);
  3023. }
  3024. static void pool_presuspend(struct dm_target *ti)
  3025. {
  3026. struct pool_c *pt = ti->private;
  3027. struct pool *pool = pt->pool;
  3028. spin_lock_irq(&pool->lock);
  3029. pool->suspended = true;
  3030. spin_unlock_irq(&pool->lock);
  3031. pool_suspend_active_thins(pool);
  3032. }
  3033. static void pool_presuspend_undo(struct dm_target *ti)
  3034. {
  3035. struct pool_c *pt = ti->private;
  3036. struct pool *pool = pt->pool;
  3037. pool_resume_active_thins(pool);
  3038. spin_lock_irq(&pool->lock);
  3039. pool->suspended = false;
  3040. spin_unlock_irq(&pool->lock);
  3041. }
  3042. static void pool_postsuspend(struct dm_target *ti)
  3043. {
  3044. struct pool_c *pt = ti->private;
  3045. struct pool *pool = pt->pool;
  3046. cancel_delayed_work_sync(&pool->waker);
  3047. cancel_delayed_work_sync(&pool->no_space_timeout);
  3048. flush_workqueue(pool->wq);
  3049. (void) commit(pool);
  3050. }
  3051. static int check_arg_count(unsigned argc, unsigned args_required)
  3052. {
  3053. if (argc != args_required) {
  3054. DMWARN("Message received with %u arguments instead of %u.",
  3055. argc, args_required);
  3056. return -EINVAL;
  3057. }
  3058. return 0;
  3059. }
  3060. static int read_dev_id(char *arg, dm_thin_id *dev_id, int warning)
  3061. {
  3062. if (!kstrtoull(arg, 10, (unsigned long long *)dev_id) &&
  3063. *dev_id <= MAX_DEV_ID)
  3064. return 0;
  3065. if (warning)
  3066. DMWARN("Message received with invalid device id: %s", arg);
  3067. return -EINVAL;
  3068. }
  3069. static int process_create_thin_mesg(unsigned argc, char **argv, struct pool *pool)
  3070. {
  3071. dm_thin_id dev_id;
  3072. int r;
  3073. r = check_arg_count(argc, 2);
  3074. if (r)
  3075. return r;
  3076. r = read_dev_id(argv[1], &dev_id, 1);
  3077. if (r)
  3078. return r;
  3079. r = dm_pool_create_thin(pool->pmd, dev_id);
  3080. if (r) {
  3081. DMWARN("Creation of new thinly-provisioned device with id %s failed.",
  3082. argv[1]);
  3083. return r;
  3084. }
  3085. return 0;
  3086. }
  3087. static int process_create_snap_mesg(unsigned argc, char **argv, struct pool *pool)
  3088. {
  3089. dm_thin_id dev_id;
  3090. dm_thin_id origin_dev_id;
  3091. int r;
  3092. r = check_arg_count(argc, 3);
  3093. if (r)
  3094. return r;
  3095. r = read_dev_id(argv[1], &dev_id, 1);
  3096. if (r)
  3097. return r;
  3098. r = read_dev_id(argv[2], &origin_dev_id, 1);
  3099. if (r)
  3100. return r;
  3101. r = dm_pool_create_snap(pool->pmd, dev_id, origin_dev_id);
  3102. if (r) {
  3103. DMWARN("Creation of new snapshot %s of device %s failed.",
  3104. argv[1], argv[2]);
  3105. return r;
  3106. }
  3107. return 0;
  3108. }
  3109. static int process_delete_mesg(unsigned argc, char **argv, struct pool *pool)
  3110. {
  3111. dm_thin_id dev_id;
  3112. int r;
  3113. r = check_arg_count(argc, 2);
  3114. if (r)
  3115. return r;
  3116. r = read_dev_id(argv[1], &dev_id, 1);
  3117. if (r)
  3118. return r;
  3119. r = dm_pool_delete_thin_device(pool->pmd, dev_id);
  3120. if (r)
  3121. DMWARN("Deletion of thin device %s failed.", argv[1]);
  3122. return r;
  3123. }
  3124. static int process_set_transaction_id_mesg(unsigned argc, char **argv, struct pool *pool)
  3125. {
  3126. dm_thin_id old_id, new_id;
  3127. int r;
  3128. r = check_arg_count(argc, 3);
  3129. if (r)
  3130. return r;
  3131. if (kstrtoull(argv[1], 10, (unsigned long long *)&old_id)) {
  3132. DMWARN("set_transaction_id message: Unrecognised id %s.", argv[1]);
  3133. return -EINVAL;
  3134. }
  3135. if (kstrtoull(argv[2], 10, (unsigned long long *)&new_id)) {
  3136. DMWARN("set_transaction_id message: Unrecognised new id %s.", argv[2]);
  3137. return -EINVAL;
  3138. }
  3139. r = dm_pool_set_metadata_transaction_id(pool->pmd, old_id, new_id);
  3140. if (r) {
  3141. DMWARN("Failed to change transaction id from %s to %s.",
  3142. argv[1], argv[2]);
  3143. return r;
  3144. }
  3145. return 0;
  3146. }
  3147. static int process_reserve_metadata_snap_mesg(unsigned argc, char **argv, struct pool *pool)
  3148. {
  3149. int r;
  3150. r = check_arg_count(argc, 1);
  3151. if (r)
  3152. return r;
  3153. (void) commit(pool);
  3154. r = dm_pool_reserve_metadata_snap(pool->pmd);
  3155. if (r)
  3156. DMWARN("reserve_metadata_snap message failed.");
  3157. return r;
  3158. }
  3159. static int process_release_metadata_snap_mesg(unsigned argc, char **argv, struct pool *pool)
  3160. {
  3161. int r;
  3162. r = check_arg_count(argc, 1);
  3163. if (r)
  3164. return r;
  3165. r = dm_pool_release_metadata_snap(pool->pmd);
  3166. if (r)
  3167. DMWARN("release_metadata_snap message failed.");
  3168. return r;
  3169. }
  3170. /*
  3171. * Messages supported:
  3172. * create_thin <dev_id>
  3173. * create_snap <dev_id> <origin_id>
  3174. * delete <dev_id>
  3175. * set_transaction_id <current_trans_id> <new_trans_id>
  3176. * reserve_metadata_snap
  3177. * release_metadata_snap
  3178. */
  3179. static int pool_message(struct dm_target *ti, unsigned argc, char **argv,
  3180. char *result, unsigned maxlen)
  3181. {
  3182. int r = -EINVAL;
  3183. struct pool_c *pt = ti->private;
  3184. struct pool *pool = pt->pool;
  3185. if (get_pool_mode(pool) >= PM_OUT_OF_METADATA_SPACE) {
  3186. DMERR("%s: unable to service pool target messages in READ_ONLY or FAIL mode",
  3187. dm_device_name(pool->pool_md));
  3188. return -EOPNOTSUPP;
  3189. }
  3190. if (!strcasecmp(argv[0], "create_thin"))
  3191. r = process_create_thin_mesg(argc, argv, pool);
  3192. else if (!strcasecmp(argv[0], "create_snap"))
  3193. r = process_create_snap_mesg(argc, argv, pool);
  3194. else if (!strcasecmp(argv[0], "delete"))
  3195. r = process_delete_mesg(argc, argv, pool);
  3196. else if (!strcasecmp(argv[0], "set_transaction_id"))
  3197. r = process_set_transaction_id_mesg(argc, argv, pool);
  3198. else if (!strcasecmp(argv[0], "reserve_metadata_snap"))
  3199. r = process_reserve_metadata_snap_mesg(argc, argv, pool);
  3200. else if (!strcasecmp(argv[0], "release_metadata_snap"))
  3201. r = process_release_metadata_snap_mesg(argc, argv, pool);
  3202. else
  3203. DMWARN("Unrecognised thin pool target message received: %s", argv[0]);
  3204. if (!r)
  3205. (void) commit(pool);
  3206. return r;
  3207. }
  3208. static void emit_flags(struct pool_features *pf, char *result,
  3209. unsigned sz, unsigned maxlen)
  3210. {
  3211. unsigned count = !pf->zero_new_blocks + !pf->discard_enabled +
  3212. !pf->discard_passdown + (pf->mode == PM_READ_ONLY) +
  3213. pf->error_if_no_space;
  3214. DMEMIT("%u ", count);
  3215. if (!pf->zero_new_blocks)
  3216. DMEMIT("skip_block_zeroing ");
  3217. if (!pf->discard_enabled)
  3218. DMEMIT("ignore_discard ");
  3219. if (!pf->discard_passdown)
  3220. DMEMIT("no_discard_passdown ");
  3221. if (pf->mode == PM_READ_ONLY)
  3222. DMEMIT("read_only ");
  3223. if (pf->error_if_no_space)
  3224. DMEMIT("error_if_no_space ");
  3225. }
  3226. /*
  3227. * Status line is:
  3228. * <transaction id> <used metadata sectors>/<total metadata sectors>
  3229. * <used data sectors>/<total data sectors> <held metadata root>
  3230. * <pool mode> <discard config> <no space config> <needs_check>
  3231. */
  3232. static void pool_status(struct dm_target *ti, status_type_t type,
  3233. unsigned status_flags, char *result, unsigned maxlen)
  3234. {
  3235. int r;
  3236. unsigned sz = 0;
  3237. uint64_t transaction_id;
  3238. dm_block_t nr_free_blocks_data;
  3239. dm_block_t nr_free_blocks_metadata;
  3240. dm_block_t nr_blocks_data;
  3241. dm_block_t nr_blocks_metadata;
  3242. dm_block_t held_root;
  3243. enum pool_mode mode;
  3244. char buf[BDEVNAME_SIZE];
  3245. char buf2[BDEVNAME_SIZE];
  3246. struct pool_c *pt = ti->private;
  3247. struct pool *pool = pt->pool;
  3248. switch (type) {
  3249. case STATUSTYPE_INFO:
  3250. if (get_pool_mode(pool) == PM_FAIL) {
  3251. DMEMIT("Fail");
  3252. break;
  3253. }
  3254. /* Commit to ensure statistics aren't out-of-date */
  3255. if (!(status_flags & DM_STATUS_NOFLUSH_FLAG) && !dm_suspended(ti))
  3256. (void) commit(pool);
  3257. r = dm_pool_get_metadata_transaction_id(pool->pmd, &transaction_id);
  3258. if (r) {
  3259. DMERR("%s: dm_pool_get_metadata_transaction_id returned %d",
  3260. dm_device_name(pool->pool_md), r);
  3261. goto err;
  3262. }
  3263. r = dm_pool_get_free_metadata_block_count(pool->pmd, &nr_free_blocks_metadata);
  3264. if (r) {
  3265. DMERR("%s: dm_pool_get_free_metadata_block_count returned %d",
  3266. dm_device_name(pool->pool_md), r);
  3267. goto err;
  3268. }
  3269. r = dm_pool_get_metadata_dev_size(pool->pmd, &nr_blocks_metadata);
  3270. if (r) {
  3271. DMERR("%s: dm_pool_get_metadata_dev_size returned %d",
  3272. dm_device_name(pool->pool_md), r);
  3273. goto err;
  3274. }
  3275. r = dm_pool_get_free_block_count(pool->pmd, &nr_free_blocks_data);
  3276. if (r) {
  3277. DMERR("%s: dm_pool_get_free_block_count returned %d",
  3278. dm_device_name(pool->pool_md), r);
  3279. goto err;
  3280. }
  3281. r = dm_pool_get_data_dev_size(pool->pmd, &nr_blocks_data);
  3282. if (r) {
  3283. DMERR("%s: dm_pool_get_data_dev_size returned %d",
  3284. dm_device_name(pool->pool_md), r);
  3285. goto err;
  3286. }
  3287. r = dm_pool_get_metadata_snap(pool->pmd, &held_root);
  3288. if (r) {
  3289. DMERR("%s: dm_pool_get_metadata_snap returned %d",
  3290. dm_device_name(pool->pool_md), r);
  3291. goto err;
  3292. }
  3293. DMEMIT("%llu %llu/%llu %llu/%llu ",
  3294. (unsigned long long)transaction_id,
  3295. (unsigned long long)(nr_blocks_metadata - nr_free_blocks_metadata),
  3296. (unsigned long long)nr_blocks_metadata,
  3297. (unsigned long long)(nr_blocks_data - nr_free_blocks_data),
  3298. (unsigned long long)nr_blocks_data);
  3299. if (held_root)
  3300. DMEMIT("%llu ", held_root);
  3301. else
  3302. DMEMIT("- ");
  3303. mode = get_pool_mode(pool);
  3304. if (mode == PM_OUT_OF_DATA_SPACE)
  3305. DMEMIT("out_of_data_space ");
  3306. else if (is_read_only_pool_mode(mode))
  3307. DMEMIT("ro ");
  3308. else
  3309. DMEMIT("rw ");
  3310. if (!pool->pf.discard_enabled)
  3311. DMEMIT("ignore_discard ");
  3312. else if (pool->pf.discard_passdown)
  3313. DMEMIT("discard_passdown ");
  3314. else
  3315. DMEMIT("no_discard_passdown ");
  3316. if (pool->pf.error_if_no_space)
  3317. DMEMIT("error_if_no_space ");
  3318. else
  3319. DMEMIT("queue_if_no_space ");
  3320. if (dm_pool_metadata_needs_check(pool->pmd))
  3321. DMEMIT("needs_check ");
  3322. else
  3323. DMEMIT("- ");
  3324. DMEMIT("%llu ", (unsigned long long)calc_metadata_threshold(pt));
  3325. break;
  3326. case STATUSTYPE_TABLE:
  3327. DMEMIT("%s %s %lu %llu ",
  3328. format_dev_t(buf, pt->metadata_dev->bdev->bd_dev),
  3329. format_dev_t(buf2, pt->data_dev->bdev->bd_dev),
  3330. (unsigned long)pool->sectors_per_block,
  3331. (unsigned long long)pt->low_water_blocks);
  3332. emit_flags(&pt->requested_pf, result, sz, maxlen);
  3333. break;
  3334. }
  3335. return;
  3336. err:
  3337. DMEMIT("Error");
  3338. }
  3339. static int pool_iterate_devices(struct dm_target *ti,
  3340. iterate_devices_callout_fn fn, void *data)
  3341. {
  3342. struct pool_c *pt = ti->private;
  3343. return fn(ti, pt->data_dev, 0, ti->len, data);
  3344. }
  3345. static void pool_io_hints(struct dm_target *ti, struct queue_limits *limits)
  3346. {
  3347. struct pool_c *pt = ti->private;
  3348. struct pool *pool = pt->pool;
  3349. sector_t io_opt_sectors = limits->io_opt >> SECTOR_SHIFT;
  3350. /*
  3351. * If max_sectors is smaller than pool->sectors_per_block adjust it
  3352. * to the highest possible power-of-2 factor of pool->sectors_per_block.
  3353. * This is especially beneficial when the pool's data device is a RAID
  3354. * device that has a full stripe width that matches pool->sectors_per_block
  3355. * -- because even though partial RAID stripe-sized IOs will be issued to a
  3356. * single RAID stripe; when aggregated they will end on a full RAID stripe
  3357. * boundary.. which avoids additional partial RAID stripe writes cascading
  3358. */
  3359. if (limits->max_sectors < pool->sectors_per_block) {
  3360. while (!is_factor(pool->sectors_per_block, limits->max_sectors)) {
  3361. if ((limits->max_sectors & (limits->max_sectors - 1)) == 0)
  3362. limits->max_sectors--;
  3363. limits->max_sectors = rounddown_pow_of_two(limits->max_sectors);
  3364. }
  3365. }
  3366. /*
  3367. * If the system-determined stacked limits are compatible with the
  3368. * pool's blocksize (io_opt is a factor) do not override them.
  3369. */
  3370. if (io_opt_sectors < pool->sectors_per_block ||
  3371. !is_factor(io_opt_sectors, pool->sectors_per_block)) {
  3372. if (is_factor(pool->sectors_per_block, limits->max_sectors))
  3373. blk_limits_io_min(limits, limits->max_sectors << SECTOR_SHIFT);
  3374. else
  3375. blk_limits_io_min(limits, pool->sectors_per_block << SECTOR_SHIFT);
  3376. blk_limits_io_opt(limits, pool->sectors_per_block << SECTOR_SHIFT);
  3377. }
  3378. /*
  3379. * pt->adjusted_pf is a staging area for the actual features to use.
  3380. * They get transferred to the live pool in bind_control_target()
  3381. * called from pool_preresume().
  3382. */
  3383. if (!pt->adjusted_pf.discard_enabled) {
  3384. /*
  3385. * Must explicitly disallow stacking discard limits otherwise the
  3386. * block layer will stack them if pool's data device has support.
  3387. * QUEUE_FLAG_DISCARD wouldn't be set but there is no way for the
  3388. * user to see that, so make sure to set all discard limits to 0.
  3389. */
  3390. limits->discard_granularity = 0;
  3391. return;
  3392. }
  3393. disable_passdown_if_not_supported(pt);
  3394. /*
  3395. * The pool uses the same discard limits as the underlying data
  3396. * device. DM core has already set this up.
  3397. */
  3398. }
  3399. static struct target_type pool_target = {
  3400. .name = "thin-pool",
  3401. .features = DM_TARGET_SINGLETON | DM_TARGET_ALWAYS_WRITEABLE |
  3402. DM_TARGET_IMMUTABLE,
  3403. .version = {1, 22, 0},
  3404. .module = THIS_MODULE,
  3405. .ctr = pool_ctr,
  3406. .dtr = pool_dtr,
  3407. .map = pool_map,
  3408. .presuspend = pool_presuspend,
  3409. .presuspend_undo = pool_presuspend_undo,
  3410. .postsuspend = pool_postsuspend,
  3411. .preresume = pool_preresume,
  3412. .resume = pool_resume,
  3413. .message = pool_message,
  3414. .status = pool_status,
  3415. .iterate_devices = pool_iterate_devices,
  3416. .io_hints = pool_io_hints,
  3417. };
  3418. /*----------------------------------------------------------------
  3419. * Thin target methods
  3420. *--------------------------------------------------------------*/
  3421. static void thin_get(struct thin_c *tc)
  3422. {
  3423. refcount_inc(&tc->refcount);
  3424. }
  3425. static void thin_put(struct thin_c *tc)
  3426. {
  3427. if (refcount_dec_and_test(&tc->refcount))
  3428. complete(&tc->can_destroy);
  3429. }
  3430. static void thin_dtr(struct dm_target *ti)
  3431. {
  3432. struct thin_c *tc = ti->private;
  3433. spin_lock_irq(&tc->pool->lock);
  3434. list_del_rcu(&tc->list);
  3435. spin_unlock_irq(&tc->pool->lock);
  3436. synchronize_rcu();
  3437. thin_put(tc);
  3438. wait_for_completion(&tc->can_destroy);
  3439. mutex_lock(&dm_thin_pool_table.mutex);
  3440. __pool_dec(tc->pool);
  3441. dm_pool_close_thin_device(tc->td);
  3442. dm_put_device(ti, tc->pool_dev);
  3443. if (tc->origin_dev)
  3444. dm_put_device(ti, tc->origin_dev);
  3445. kfree(tc);
  3446. mutex_unlock(&dm_thin_pool_table.mutex);
  3447. }
  3448. /*
  3449. * Thin target parameters:
  3450. *
  3451. * <pool_dev> <dev_id> [origin_dev]
  3452. *
  3453. * pool_dev: the path to the pool (eg, /dev/mapper/my_pool)
  3454. * dev_id: the internal device identifier
  3455. * origin_dev: a device external to the pool that should act as the origin
  3456. *
  3457. * If the pool device has discards disabled, they get disabled for the thin
  3458. * device as well.
  3459. */
  3460. static int thin_ctr(struct dm_target *ti, unsigned argc, char **argv)
  3461. {
  3462. int r;
  3463. struct thin_c *tc;
  3464. struct dm_dev *pool_dev, *origin_dev;
  3465. struct mapped_device *pool_md;
  3466. mutex_lock(&dm_thin_pool_table.mutex);
  3467. if (argc != 2 && argc != 3) {
  3468. ti->error = "Invalid argument count";
  3469. r = -EINVAL;
  3470. goto out_unlock;
  3471. }
  3472. tc = ti->private = kzalloc(sizeof(*tc), GFP_KERNEL);
  3473. if (!tc) {
  3474. ti->error = "Out of memory";
  3475. r = -ENOMEM;
  3476. goto out_unlock;
  3477. }
  3478. tc->thin_md = dm_table_get_md(ti->table);
  3479. spin_lock_init(&tc->lock);
  3480. INIT_LIST_HEAD(&tc->deferred_cells);
  3481. bio_list_init(&tc->deferred_bio_list);
  3482. bio_list_init(&tc->retry_on_resume_list);
  3483. tc->sort_bio_list = RB_ROOT;
  3484. if (argc == 3) {
  3485. if (!strcmp(argv[0], argv[2])) {
  3486. ti->error = "Error setting origin device";
  3487. r = -EINVAL;
  3488. goto bad_origin_dev;
  3489. }
  3490. r = dm_get_device(ti, argv[2], FMODE_READ, &origin_dev);
  3491. if (r) {
  3492. ti->error = "Error opening origin device";
  3493. goto bad_origin_dev;
  3494. }
  3495. tc->origin_dev = origin_dev;
  3496. }
  3497. r = dm_get_device(ti, argv[0], dm_table_get_mode(ti->table), &pool_dev);
  3498. if (r) {
  3499. ti->error = "Error opening pool device";
  3500. goto bad_pool_dev;
  3501. }
  3502. tc->pool_dev = pool_dev;
  3503. if (read_dev_id(argv[1], (unsigned long long *)&tc->dev_id, 0)) {
  3504. ti->error = "Invalid device id";
  3505. r = -EINVAL;
  3506. goto bad_common;
  3507. }
  3508. pool_md = dm_get_md(tc->pool_dev->bdev->bd_dev);
  3509. if (!pool_md) {
  3510. ti->error = "Couldn't get pool mapped device";
  3511. r = -EINVAL;
  3512. goto bad_common;
  3513. }
  3514. tc->pool = __pool_table_lookup(pool_md);
  3515. if (!tc->pool) {
  3516. ti->error = "Couldn't find pool object";
  3517. r = -EINVAL;
  3518. goto bad_pool_lookup;
  3519. }
  3520. __pool_inc(tc->pool);
  3521. if (get_pool_mode(tc->pool) == PM_FAIL) {
  3522. ti->error = "Couldn't open thin device, Pool is in fail mode";
  3523. r = -EINVAL;
  3524. goto bad_pool;
  3525. }
  3526. r = dm_pool_open_thin_device(tc->pool->pmd, tc->dev_id, &tc->td);
  3527. if (r) {
  3528. ti->error = "Couldn't open thin internal device";
  3529. goto bad_pool;
  3530. }
  3531. r = dm_set_target_max_io_len(ti, tc->pool->sectors_per_block);
  3532. if (r)
  3533. goto bad;
  3534. ti->num_flush_bios = 1;
  3535. ti->flush_supported = true;
  3536. ti->per_io_data_size = sizeof(struct dm_thin_endio_hook);
  3537. /* In case the pool supports discards, pass them on. */
  3538. if (tc->pool->pf.discard_enabled) {
  3539. ti->discards_supported = true;
  3540. ti->num_discard_bios = 1;
  3541. }
  3542. mutex_unlock(&dm_thin_pool_table.mutex);
  3543. spin_lock_irq(&tc->pool->lock);
  3544. if (tc->pool->suspended) {
  3545. spin_unlock_irq(&tc->pool->lock);
  3546. mutex_lock(&dm_thin_pool_table.mutex); /* reacquire for __pool_dec */
  3547. ti->error = "Unable to activate thin device while pool is suspended";
  3548. r = -EINVAL;
  3549. goto bad;
  3550. }
  3551. refcount_set(&tc->refcount, 1);
  3552. init_completion(&tc->can_destroy);
  3553. list_add_tail_rcu(&tc->list, &tc->pool->active_thins);
  3554. spin_unlock_irq(&tc->pool->lock);
  3555. /*
  3556. * This synchronize_rcu() call is needed here otherwise we risk a
  3557. * wake_worker() call finding no bios to process (because the newly
  3558. * added tc isn't yet visible). So this reduces latency since we
  3559. * aren't then dependent on the periodic commit to wake_worker().
  3560. */
  3561. synchronize_rcu();
  3562. dm_put(pool_md);
  3563. return 0;
  3564. bad:
  3565. dm_pool_close_thin_device(tc->td);
  3566. bad_pool:
  3567. __pool_dec(tc->pool);
  3568. bad_pool_lookup:
  3569. dm_put(pool_md);
  3570. bad_common:
  3571. dm_put_device(ti, tc->pool_dev);
  3572. bad_pool_dev:
  3573. if (tc->origin_dev)
  3574. dm_put_device(ti, tc->origin_dev);
  3575. bad_origin_dev:
  3576. kfree(tc);
  3577. out_unlock:
  3578. mutex_unlock(&dm_thin_pool_table.mutex);
  3579. return r;
  3580. }
  3581. static int thin_map(struct dm_target *ti, struct bio *bio)
  3582. {
  3583. bio->bi_iter.bi_sector = dm_target_offset(ti, bio->bi_iter.bi_sector);
  3584. return thin_bio_map(ti, bio);
  3585. }
  3586. static int thin_endio(struct dm_target *ti, struct bio *bio,
  3587. blk_status_t *err)
  3588. {
  3589. unsigned long flags;
  3590. struct dm_thin_endio_hook *h = dm_per_bio_data(bio, sizeof(struct dm_thin_endio_hook));
  3591. struct list_head work;
  3592. struct dm_thin_new_mapping *m, *tmp;
  3593. struct pool *pool = h->tc->pool;
  3594. if (h->shared_read_entry) {
  3595. INIT_LIST_HEAD(&work);
  3596. dm_deferred_entry_dec(h->shared_read_entry, &work);
  3597. spin_lock_irqsave(&pool->lock, flags);
  3598. list_for_each_entry_safe(m, tmp, &work, list) {
  3599. list_del(&m->list);
  3600. __complete_mapping_preparation(m);
  3601. }
  3602. spin_unlock_irqrestore(&pool->lock, flags);
  3603. }
  3604. if (h->all_io_entry) {
  3605. INIT_LIST_HEAD(&work);
  3606. dm_deferred_entry_dec(h->all_io_entry, &work);
  3607. if (!list_empty(&work)) {
  3608. spin_lock_irqsave(&pool->lock, flags);
  3609. list_for_each_entry_safe(m, tmp, &work, list)
  3610. list_add_tail(&m->list, &pool->prepared_discards);
  3611. spin_unlock_irqrestore(&pool->lock, flags);
  3612. wake_worker(pool);
  3613. }
  3614. }
  3615. if (h->cell)
  3616. cell_defer_no_holder(h->tc, h->cell);
  3617. return DM_ENDIO_DONE;
  3618. }
  3619. static void thin_presuspend(struct dm_target *ti)
  3620. {
  3621. struct thin_c *tc = ti->private;
  3622. if (dm_noflush_suspending(ti))
  3623. noflush_work(tc, do_noflush_start);
  3624. }
  3625. static void thin_postsuspend(struct dm_target *ti)
  3626. {
  3627. struct thin_c *tc = ti->private;
  3628. /*
  3629. * The dm_noflush_suspending flag has been cleared by now, so
  3630. * unfortunately we must always run this.
  3631. */
  3632. noflush_work(tc, do_noflush_stop);
  3633. }
  3634. static int thin_preresume(struct dm_target *ti)
  3635. {
  3636. struct thin_c *tc = ti->private;
  3637. if (tc->origin_dev)
  3638. tc->origin_size = get_dev_size(tc->origin_dev->bdev);
  3639. return 0;
  3640. }
  3641. /*
  3642. * <nr mapped sectors> <highest mapped sector>
  3643. */
  3644. static void thin_status(struct dm_target *ti, status_type_t type,
  3645. unsigned status_flags, char *result, unsigned maxlen)
  3646. {
  3647. int r;
  3648. ssize_t sz = 0;
  3649. dm_block_t mapped, highest;
  3650. char buf[BDEVNAME_SIZE];
  3651. struct thin_c *tc = ti->private;
  3652. if (get_pool_mode(tc->pool) == PM_FAIL) {
  3653. DMEMIT("Fail");
  3654. return;
  3655. }
  3656. if (!tc->td)
  3657. DMEMIT("-");
  3658. else {
  3659. switch (type) {
  3660. case STATUSTYPE_INFO:
  3661. r = dm_thin_get_mapped_count(tc->td, &mapped);
  3662. if (r) {
  3663. DMERR("dm_thin_get_mapped_count returned %d", r);
  3664. goto err;
  3665. }
  3666. r = dm_thin_get_highest_mapped_block(tc->td, &highest);
  3667. if (r < 0) {
  3668. DMERR("dm_thin_get_highest_mapped_block returned %d", r);
  3669. goto err;
  3670. }
  3671. DMEMIT("%llu ", mapped * tc->pool->sectors_per_block);
  3672. if (r)
  3673. DMEMIT("%llu", ((highest + 1) *
  3674. tc->pool->sectors_per_block) - 1);
  3675. else
  3676. DMEMIT("-");
  3677. break;
  3678. case STATUSTYPE_TABLE:
  3679. DMEMIT("%s %lu",
  3680. format_dev_t(buf, tc->pool_dev->bdev->bd_dev),
  3681. (unsigned long) tc->dev_id);
  3682. if (tc->origin_dev)
  3683. DMEMIT(" %s", format_dev_t(buf, tc->origin_dev->bdev->bd_dev));
  3684. break;
  3685. }
  3686. }
  3687. return;
  3688. err:
  3689. DMEMIT("Error");
  3690. }
  3691. static int thin_iterate_devices(struct dm_target *ti,
  3692. iterate_devices_callout_fn fn, void *data)
  3693. {
  3694. sector_t blocks;
  3695. struct thin_c *tc = ti->private;
  3696. struct pool *pool = tc->pool;
  3697. /*
  3698. * We can't call dm_pool_get_data_dev_size() since that blocks. So
  3699. * we follow a more convoluted path through to the pool's target.
  3700. */
  3701. if (!pool->ti)
  3702. return 0; /* nothing is bound */
  3703. blocks = pool->ti->len;
  3704. (void) sector_div(blocks, pool->sectors_per_block);
  3705. if (blocks)
  3706. return fn(ti, tc->pool_dev, 0, pool->sectors_per_block * blocks, data);
  3707. return 0;
  3708. }
  3709. static void thin_io_hints(struct dm_target *ti, struct queue_limits *limits)
  3710. {
  3711. struct thin_c *tc = ti->private;
  3712. struct pool *pool = tc->pool;
  3713. if (!pool->pf.discard_enabled)
  3714. return;
  3715. limits->discard_granularity = pool->sectors_per_block << SECTOR_SHIFT;
  3716. limits->max_discard_sectors = 2048 * 1024 * 16; /* 16G */
  3717. }
  3718. static struct target_type thin_target = {
  3719. .name = "thin",
  3720. .version = {1, 22, 0},
  3721. .module = THIS_MODULE,
  3722. .ctr = thin_ctr,
  3723. .dtr = thin_dtr,
  3724. .map = thin_map,
  3725. .end_io = thin_endio,
  3726. .preresume = thin_preresume,
  3727. .presuspend = thin_presuspend,
  3728. .postsuspend = thin_postsuspend,
  3729. .status = thin_status,
  3730. .iterate_devices = thin_iterate_devices,
  3731. .io_hints = thin_io_hints,
  3732. };
  3733. /*----------------------------------------------------------------*/
  3734. static int __init dm_thin_init(void)
  3735. {
  3736. int r = -ENOMEM;
  3737. pool_table_init();
  3738. _new_mapping_cache = KMEM_CACHE(dm_thin_new_mapping, 0);
  3739. if (!_new_mapping_cache)
  3740. return r;
  3741. r = dm_register_target(&thin_target);
  3742. if (r)
  3743. goto bad_new_mapping_cache;
  3744. r = dm_register_target(&pool_target);
  3745. if (r)
  3746. goto bad_thin_target;
  3747. return 0;
  3748. bad_thin_target:
  3749. dm_unregister_target(&thin_target);
  3750. bad_new_mapping_cache:
  3751. kmem_cache_destroy(_new_mapping_cache);
  3752. return r;
  3753. }
  3754. static void dm_thin_exit(void)
  3755. {
  3756. dm_unregister_target(&thin_target);
  3757. dm_unregister_target(&pool_target);
  3758. kmem_cache_destroy(_new_mapping_cache);
  3759. pool_table_exit();
  3760. }
  3761. module_init(dm_thin_init);
  3762. module_exit(dm_thin_exit);
  3763. module_param_named(no_space_timeout, no_space_timeout_secs, uint, S_IRUGO | S_IWUSR);
  3764. MODULE_PARM_DESC(no_space_timeout, "Out of data space queue IO timeout in seconds");
  3765. MODULE_DESCRIPTION(DM_NAME " thin provisioning target");
  3766. MODULE_AUTHOR("Joe Thornber <dm-devel@redhat.com>");
  3767. MODULE_LICENSE("GPL");