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