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