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 | ||
452d7a62 MS |
893 | static void remap_and_issue_overwrite(struct thin_c *tc, struct bio *bio, |
894 | dm_block_t data_block, | |
895 | struct dm_thin_new_mapping *m) | |
896 | { | |
897 | struct pool *pool = tc->pool; | |
898 | struct dm_thin_endio_hook *h = dm_per_bio_data(bio, sizeof(struct dm_thin_endio_hook)); | |
899 | ||
900 | h->overwrite_mapping = m; | |
901 | m->bio = bio; | |
902 | save_and_set_endio(bio, &m->saved_bi_end_io, overwrite_endio); | |
903 | inc_all_io_entry(pool, bio); | |
904 | remap_and_issue(tc, bio, data_block); | |
905 | } | |
906 | ||
e5aea7b4 JT |
907 | /* |
908 | * A partial copy also needs to zero the uncopied region. | |
909 | */ | |
991d9fa0 | 910 | static void schedule_copy(struct thin_c *tc, dm_block_t virt_block, |
2dd9c257 JT |
911 | struct dm_dev *origin, dm_block_t data_origin, |
912 | dm_block_t data_dest, | |
e5aea7b4 JT |
913 | struct dm_bio_prison_cell *cell, struct bio *bio, |
914 | sector_t len) | |
991d9fa0 JT |
915 | { |
916 | int r; | |
917 | struct pool *pool = tc->pool; | |
a24c2569 | 918 | struct dm_thin_new_mapping *m = get_next_mapping(pool); |
991d9fa0 | 919 | |
991d9fa0 JT |
920 | m->tc = tc; |
921 | m->virt_block = virt_block; | |
922 | m->data_block = data_dest; | |
923 | m->cell = cell; | |
991d9fa0 | 924 | |
e5aea7b4 JT |
925 | /* |
926 | * quiesce action + copy action + an extra reference held for the | |
927 | * duration of this function (we may need to inc later for a | |
928 | * partial zero). | |
929 | */ | |
930 | atomic_set(&m->prepare_actions, 3); | |
931 | ||
44feb387 | 932 | if (!dm_deferred_set_add_work(pool->shared_read_ds, &m->list)) |
e5aea7b4 | 933 | complete_mapping_preparation(m); /* already quiesced */ |
991d9fa0 JT |
934 | |
935 | /* | |
936 | * IO to pool_dev remaps to the pool target's data_dev. | |
937 | * | |
938 | * If the whole block of data is being overwritten, we can issue the | |
939 | * bio immediately. Otherwise we use kcopyd to clone the data first. | |
940 | */ | |
452d7a62 MS |
941 | if (io_overwrites_block(pool, bio)) |
942 | remap_and_issue_overwrite(tc, bio, data_dest, m); | |
943 | else { | |
991d9fa0 JT |
944 | struct dm_io_region from, to; |
945 | ||
2dd9c257 | 946 | from.bdev = origin->bdev; |
991d9fa0 | 947 | from.sector = data_origin * pool->sectors_per_block; |
e5aea7b4 | 948 | from.count = len; |
991d9fa0 JT |
949 | |
950 | to.bdev = tc->pool_dev->bdev; | |
951 | to.sector = data_dest * pool->sectors_per_block; | |
e5aea7b4 | 952 | to.count = len; |
991d9fa0 JT |
953 | |
954 | r = dm_kcopyd_copy(pool->copier, &from, 1, &to, | |
955 | 0, copy_complete, m); | |
956 | if (r < 0) { | |
c397741c | 957 | DMERR_LIMIT("dm_kcopyd_copy() failed"); |
e5aea7b4 JT |
958 | copy_complete(1, 1, m); |
959 | ||
960 | /* | |
961 | * We allow the zero to be issued, to simplify the | |
962 | * error path. Otherwise we'd need to start | |
963 | * worrying about decrementing the prepare_actions | |
964 | * counter. | |
965 | */ | |
966 | } | |
967 | ||
968 | /* | |
969 | * Do we need to zero a tail region? | |
970 | */ | |
971 | if (len < pool->sectors_per_block && pool->pf.zero_new_blocks) { | |
972 | atomic_inc(&m->prepare_actions); | |
973 | ll_zero(tc, m, | |
974 | data_dest * pool->sectors_per_block + len, | |
975 | (data_dest + 1) * pool->sectors_per_block); | |
991d9fa0 JT |
976 | } |
977 | } | |
e5aea7b4 JT |
978 | |
979 | complete_mapping_preparation(m); /* drop our ref */ | |
991d9fa0 JT |
980 | } |
981 | ||
2dd9c257 JT |
982 | static void schedule_internal_copy(struct thin_c *tc, dm_block_t virt_block, |
983 | dm_block_t data_origin, dm_block_t data_dest, | |
a24c2569 | 984 | struct dm_bio_prison_cell *cell, struct bio *bio) |
2dd9c257 JT |
985 | { |
986 | schedule_copy(tc, virt_block, tc->pool_dev, | |
e5aea7b4 JT |
987 | data_origin, data_dest, cell, bio, |
988 | tc->pool->sectors_per_block); | |
2dd9c257 JT |
989 | } |
990 | ||
991d9fa0 | 991 | static void schedule_zero(struct thin_c *tc, dm_block_t virt_block, |
a24c2569 | 992 | dm_block_t data_block, struct dm_bio_prison_cell *cell, |
991d9fa0 JT |
993 | struct bio *bio) |
994 | { | |
995 | struct pool *pool = tc->pool; | |
a24c2569 | 996 | struct dm_thin_new_mapping *m = get_next_mapping(pool); |
991d9fa0 | 997 | |
50f3c3ef | 998 | atomic_set(&m->prepare_actions, 1); /* no need to quiesce */ |
991d9fa0 JT |
999 | m->tc = tc; |
1000 | m->virt_block = virt_block; | |
1001 | m->data_block = data_block; | |
1002 | m->cell = cell; | |
991d9fa0 JT |
1003 | |
1004 | /* | |
1005 | * If the whole block of data is being overwritten or we are not | |
1006 | * zeroing pre-existing data, we can issue the bio immediately. | |
1007 | * Otherwise we use kcopyd to zero the data first. | |
1008 | */ | |
67e2e2b2 | 1009 | if (!pool->pf.zero_new_blocks) |
991d9fa0 JT |
1010 | process_prepared_mapping(m); |
1011 | ||
452d7a62 MS |
1012 | else if (io_overwrites_block(pool, bio)) |
1013 | remap_and_issue_overwrite(tc, bio, data_block, m); | |
991d9fa0 | 1014 | |
452d7a62 | 1015 | else |
e5aea7b4 JT |
1016 | ll_zero(tc, m, |
1017 | data_block * pool->sectors_per_block, | |
1018 | (data_block + 1) * pool->sectors_per_block); | |
1019 | } | |
991d9fa0 | 1020 | |
e5aea7b4 JT |
1021 | static void schedule_external_copy(struct thin_c *tc, dm_block_t virt_block, |
1022 | dm_block_t data_dest, | |
1023 | struct dm_bio_prison_cell *cell, struct bio *bio) | |
1024 | { | |
1025 | struct pool *pool = tc->pool; | |
1026 | sector_t virt_block_begin = virt_block * pool->sectors_per_block; | |
1027 | sector_t virt_block_end = (virt_block + 1) * pool->sectors_per_block; | |
1028 | ||
1029 | if (virt_block_end <= tc->origin_size) | |
1030 | schedule_copy(tc, virt_block, tc->origin_dev, | |
1031 | virt_block, data_dest, cell, bio, | |
1032 | pool->sectors_per_block); | |
1033 | ||
1034 | else if (virt_block_begin < tc->origin_size) | |
1035 | schedule_copy(tc, virt_block, tc->origin_dev, | |
1036 | virt_block, data_dest, cell, bio, | |
1037 | tc->origin_size - virt_block_begin); | |
1038 | ||
1039 | else | |
1040 | schedule_zero(tc, virt_block, data_dest, cell, bio); | |
991d9fa0 JT |
1041 | } |
1042 | ||
e49e5829 JT |
1043 | /* |
1044 | * A non-zero return indicates read_only or fail_io mode. | |
1045 | * Many callers don't care about the return value. | |
1046 | */ | |
020cc3b5 | 1047 | static int commit(struct pool *pool) |
e49e5829 JT |
1048 | { |
1049 | int r; | |
1050 | ||
8d07e8a5 | 1051 | if (get_pool_mode(pool) >= PM_READ_ONLY) |
e49e5829 JT |
1052 | return -EINVAL; |
1053 | ||
020cc3b5 | 1054 | r = dm_pool_commit_metadata(pool->pmd); |
b5330655 JT |
1055 | if (r) |
1056 | metadata_operation_failed(pool, "dm_pool_commit_metadata", r); | |
e49e5829 JT |
1057 | |
1058 | return r; | |
1059 | } | |
1060 | ||
88a6621b JT |
1061 | static void check_low_water_mark(struct pool *pool, dm_block_t free_blocks) |
1062 | { | |
1063 | unsigned long flags; | |
1064 | ||
1065 | if (free_blocks <= pool->low_water_blocks && !pool->low_water_triggered) { | |
1066 | DMWARN("%s: reached low water mark for data device: sending event.", | |
1067 | dm_device_name(pool->pool_md)); | |
1068 | spin_lock_irqsave(&pool->lock, flags); | |
1069 | pool->low_water_triggered = true; | |
1070 | spin_unlock_irqrestore(&pool->lock, flags); | |
1071 | dm_table_event(pool->ti->table); | |
1072 | } | |
1073 | } | |
1074 | ||
3e1a0699 JT |
1075 | static void set_pool_mode(struct pool *pool, enum pool_mode new_mode); |
1076 | ||
991d9fa0 JT |
1077 | static int alloc_data_block(struct thin_c *tc, dm_block_t *result) |
1078 | { | |
1079 | int r; | |
1080 | dm_block_t free_blocks; | |
991d9fa0 JT |
1081 | struct pool *pool = tc->pool; |
1082 | ||
3e1a0699 | 1083 | if (WARN_ON(get_pool_mode(pool) != PM_WRITE)) |
8d30abff JT |
1084 | return -EINVAL; |
1085 | ||
991d9fa0 | 1086 | r = dm_pool_get_free_block_count(pool->pmd, &free_blocks); |
b5330655 JT |
1087 | if (r) { |
1088 | metadata_operation_failed(pool, "dm_pool_get_free_block_count", r); | |
991d9fa0 | 1089 | return r; |
b5330655 | 1090 | } |
991d9fa0 | 1091 | |
88a6621b | 1092 | check_low_water_mark(pool, free_blocks); |
991d9fa0 JT |
1093 | |
1094 | if (!free_blocks) { | |
94563bad MS |
1095 | /* |
1096 | * Try to commit to see if that will free up some | |
1097 | * more space. | |
1098 | */ | |
020cc3b5 JT |
1099 | r = commit(pool); |
1100 | if (r) | |
1101 | return r; | |
991d9fa0 | 1102 | |
94563bad | 1103 | r = dm_pool_get_free_block_count(pool->pmd, &free_blocks); |
b5330655 JT |
1104 | if (r) { |
1105 | metadata_operation_failed(pool, "dm_pool_get_free_block_count", r); | |
94563bad | 1106 | return r; |
b5330655 | 1107 | } |
991d9fa0 | 1108 | |
94563bad | 1109 | if (!free_blocks) { |
3e1a0699 | 1110 | set_pool_mode(pool, PM_OUT_OF_DATA_SPACE); |
94563bad | 1111 | return -ENOSPC; |
991d9fa0 JT |
1112 | } |
1113 | } | |
1114 | ||
1115 | r = dm_pool_alloc_data_block(pool->pmd, result); | |
4a02b34e | 1116 | if (r) { |
b5330655 | 1117 | metadata_operation_failed(pool, "dm_pool_alloc_data_block", r); |
991d9fa0 | 1118 | return r; |
4a02b34e | 1119 | } |
991d9fa0 JT |
1120 | |
1121 | return 0; | |
1122 | } | |
1123 | ||
1124 | /* | |
1125 | * If we have run out of space, queue bios until the device is | |
1126 | * resumed, presumably after having been reloaded with more space. | |
1127 | */ | |
1128 | static void retry_on_resume(struct bio *bio) | |
1129 | { | |
59c3d2c6 | 1130 | struct dm_thin_endio_hook *h = dm_per_bio_data(bio, sizeof(struct dm_thin_endio_hook)); |
eb2aa48d | 1131 | struct thin_c *tc = h->tc; |
991d9fa0 JT |
1132 | unsigned long flags; |
1133 | ||
c140e1c4 MS |
1134 | spin_lock_irqsave(&tc->lock, flags); |
1135 | bio_list_add(&tc->retry_on_resume_list, bio); | |
1136 | spin_unlock_irqrestore(&tc->lock, flags); | |
991d9fa0 JT |
1137 | } |
1138 | ||
af91805a | 1139 | static int should_error_unserviceable_bio(struct pool *pool) |
8c0f0e8c | 1140 | { |
3e1a0699 JT |
1141 | enum pool_mode m = get_pool_mode(pool); |
1142 | ||
1143 | switch (m) { | |
1144 | case PM_WRITE: | |
1145 | /* Shouldn't get here */ | |
1146 | DMERR_LIMIT("bio unserviceable, yet pool is in PM_WRITE mode"); | |
af91805a | 1147 | return -EIO; |
3e1a0699 JT |
1148 | |
1149 | case PM_OUT_OF_DATA_SPACE: | |
af91805a | 1150 | return pool->pf.error_if_no_space ? -ENOSPC : 0; |
3e1a0699 JT |
1151 | |
1152 | case PM_READ_ONLY: | |
1153 | case PM_FAIL: | |
af91805a | 1154 | return -EIO; |
3e1a0699 JT |
1155 | default: |
1156 | /* Shouldn't get here */ | |
1157 | DMERR_LIMIT("bio unserviceable, yet pool has an unknown mode"); | |
af91805a | 1158 | return -EIO; |
3e1a0699 JT |
1159 | } |
1160 | } | |
8c0f0e8c | 1161 | |
3e1a0699 JT |
1162 | static void handle_unserviceable_bio(struct pool *pool, struct bio *bio) |
1163 | { | |
af91805a MS |
1164 | int error = should_error_unserviceable_bio(pool); |
1165 | ||
1166 | if (error) | |
1167 | bio_endio(bio, error); | |
6d16202b MS |
1168 | else |
1169 | retry_on_resume(bio); | |
8c0f0e8c MS |
1170 | } |
1171 | ||
399caddf | 1172 | static void retry_bios_on_resume(struct pool *pool, struct dm_bio_prison_cell *cell) |
991d9fa0 JT |
1173 | { |
1174 | struct bio *bio; | |
1175 | struct bio_list bios; | |
af91805a | 1176 | int error; |
991d9fa0 | 1177 | |
af91805a MS |
1178 | error = should_error_unserviceable_bio(pool); |
1179 | if (error) { | |
1180 | cell_error_with_code(pool, cell, error); | |
3e1a0699 JT |
1181 | return; |
1182 | } | |
1183 | ||
991d9fa0 | 1184 | bio_list_init(&bios); |
6beca5eb | 1185 | cell_release(pool, cell, &bios); |
991d9fa0 | 1186 | |
af91805a MS |
1187 | error = should_error_unserviceable_bio(pool); |
1188 | if (error) | |
3e1a0699 | 1189 | while ((bio = bio_list_pop(&bios))) |
af91805a | 1190 | bio_endio(bio, error); |
3e1a0699 JT |
1191 | else |
1192 | while ((bio = bio_list_pop(&bios))) | |
1193 | retry_on_resume(bio); | |
991d9fa0 JT |
1194 | } |
1195 | ||
104655fd JT |
1196 | static void process_discard(struct thin_c *tc, struct bio *bio) |
1197 | { | |
1198 | int r; | |
1199 | struct pool *pool = tc->pool; | |
a24c2569 | 1200 | struct dm_bio_prison_cell *cell, *cell2; |
44feb387 | 1201 | struct dm_cell_key key, key2; |
104655fd JT |
1202 | dm_block_t block = get_bio_block(tc, bio); |
1203 | struct dm_thin_lookup_result lookup_result; | |
a24c2569 | 1204 | struct dm_thin_new_mapping *m; |
104655fd JT |
1205 | |
1206 | build_virtual_key(tc->td, block, &key); | |
6beca5eb | 1207 | if (bio_detain(tc->pool, &key, bio, &cell)) |
104655fd JT |
1208 | return; |
1209 | ||
1210 | r = dm_thin_find_block(tc->td, block, 1, &lookup_result); | |
1211 | switch (r) { | |
1212 | case 0: | |
1213 | /* | |
1214 | * Check nobody is fiddling with this pool block. This can | |
1215 | * happen if someone's in the process of breaking sharing | |
1216 | * on this block. | |
1217 | */ | |
1218 | build_data_key(tc->td, lookup_result.block, &key2); | |
6beca5eb | 1219 | if (bio_detain(tc->pool, &key2, bio, &cell2)) { |
f286ba0e | 1220 | cell_defer_no_holder(tc, cell); |
104655fd JT |
1221 | break; |
1222 | } | |
1223 | ||
1224 | if (io_overlaps_block(pool, bio)) { | |
1225 | /* | |
1226 | * IO may still be going to the destination block. We must | |
1227 | * quiesce before we can do the removal. | |
1228 | */ | |
1229 | m = get_next_mapping(pool); | |
1230 | m->tc = tc; | |
19fa1a67 JT |
1231 | m->pass_discard = pool->pf.discard_passdown; |
1232 | m->definitely_not_shared = !lookup_result.shared; | |
104655fd JT |
1233 | m->virt_block = block; |
1234 | m->data_block = lookup_result.block; | |
1235 | m->cell = cell; | |
1236 | m->cell2 = cell2; | |
104655fd JT |
1237 | m->bio = bio; |
1238 | ||
7a7e97ca JT |
1239 | if (!dm_deferred_set_add_work(pool->all_io_ds, &m->list)) |
1240 | pool->process_prepared_discard(m); | |
1241 | ||
104655fd | 1242 | } else { |
e8088073 | 1243 | inc_all_io_entry(pool, bio); |
f286ba0e JT |
1244 | cell_defer_no_holder(tc, cell); |
1245 | cell_defer_no_holder(tc, cell2); | |
e8088073 | 1246 | |
104655fd | 1247 | /* |
49296309 MP |
1248 | * The DM core makes sure that the discard doesn't span |
1249 | * a block boundary. So we submit the discard of a | |
1250 | * partial block appropriately. | |
104655fd | 1251 | */ |
650d2a06 MP |
1252 | if ((!lookup_result.shared) && pool->pf.discard_passdown) |
1253 | remap_and_issue(tc, bio, lookup_result.block); | |
1254 | else | |
1255 | bio_endio(bio, 0); | |
104655fd JT |
1256 | } |
1257 | break; | |
1258 | ||
1259 | case -ENODATA: | |
1260 | /* | |
1261 | * It isn't provisioned, just forget it. | |
1262 | */ | |
f286ba0e | 1263 | cell_defer_no_holder(tc, cell); |
104655fd JT |
1264 | bio_endio(bio, 0); |
1265 | break; | |
1266 | ||
1267 | default: | |
c397741c MS |
1268 | DMERR_LIMIT("%s: dm_thin_find_block() failed: error = %d", |
1269 | __func__, r); | |
f286ba0e | 1270 | cell_defer_no_holder(tc, cell); |
104655fd JT |
1271 | bio_io_error(bio); |
1272 | break; | |
1273 | } | |
1274 | } | |
1275 | ||
991d9fa0 | 1276 | static void break_sharing(struct thin_c *tc, struct bio *bio, dm_block_t block, |
44feb387 | 1277 | struct dm_cell_key *key, |
991d9fa0 | 1278 | struct dm_thin_lookup_result *lookup_result, |
a24c2569 | 1279 | struct dm_bio_prison_cell *cell) |
991d9fa0 JT |
1280 | { |
1281 | int r; | |
1282 | dm_block_t data_block; | |
d6fc2042 | 1283 | struct pool *pool = tc->pool; |
991d9fa0 JT |
1284 | |
1285 | r = alloc_data_block(tc, &data_block); | |
1286 | switch (r) { | |
1287 | case 0: | |
2dd9c257 JT |
1288 | schedule_internal_copy(tc, block, lookup_result->block, |
1289 | data_block, cell, bio); | |
991d9fa0 JT |
1290 | break; |
1291 | ||
1292 | case -ENOSPC: | |
399caddf | 1293 | retry_bios_on_resume(pool, cell); |
991d9fa0 JT |
1294 | break; |
1295 | ||
1296 | default: | |
c397741c MS |
1297 | DMERR_LIMIT("%s: alloc_data_block() failed: error = %d", |
1298 | __func__, r); | |
d6fc2042 | 1299 | cell_error(pool, cell); |
991d9fa0 JT |
1300 | break; |
1301 | } | |
1302 | } | |
1303 | ||
1304 | static void process_shared_bio(struct thin_c *tc, struct bio *bio, | |
1305 | dm_block_t block, | |
1306 | struct dm_thin_lookup_result *lookup_result) | |
1307 | { | |
a24c2569 | 1308 | struct dm_bio_prison_cell *cell; |
991d9fa0 | 1309 | struct pool *pool = tc->pool; |
44feb387 | 1310 | struct dm_cell_key key; |
991d9fa0 JT |
1311 | |
1312 | /* | |
1313 | * If cell is already occupied, then sharing is already in the process | |
1314 | * of being broken so we have nothing further to do here. | |
1315 | */ | |
1316 | build_data_key(tc->td, lookup_result->block, &key); | |
6beca5eb | 1317 | if (bio_detain(pool, &key, bio, &cell)) |
991d9fa0 JT |
1318 | return; |
1319 | ||
4f024f37 | 1320 | if (bio_data_dir(bio) == WRITE && bio->bi_iter.bi_size) |
991d9fa0 JT |
1321 | break_sharing(tc, bio, block, &key, lookup_result, cell); |
1322 | else { | |
59c3d2c6 | 1323 | struct dm_thin_endio_hook *h = dm_per_bio_data(bio, sizeof(struct dm_thin_endio_hook)); |
991d9fa0 | 1324 | |
44feb387 | 1325 | h->shared_read_entry = dm_deferred_entry_inc(pool->shared_read_ds); |
e8088073 | 1326 | inc_all_io_entry(pool, bio); |
f286ba0e | 1327 | cell_defer_no_holder(tc, cell); |
e8088073 | 1328 | |
991d9fa0 JT |
1329 | remap_and_issue(tc, bio, lookup_result->block); |
1330 | } | |
1331 | } | |
1332 | ||
1333 | static void provision_block(struct thin_c *tc, struct bio *bio, dm_block_t block, | |
a24c2569 | 1334 | struct dm_bio_prison_cell *cell) |
991d9fa0 JT |
1335 | { |
1336 | int r; | |
1337 | dm_block_t data_block; | |
6beca5eb | 1338 | struct pool *pool = tc->pool; |
991d9fa0 JT |
1339 | |
1340 | /* | |
1341 | * Remap empty bios (flushes) immediately, without provisioning. | |
1342 | */ | |
4f024f37 | 1343 | if (!bio->bi_iter.bi_size) { |
6beca5eb | 1344 | inc_all_io_entry(pool, bio); |
f286ba0e | 1345 | cell_defer_no_holder(tc, cell); |
e8088073 | 1346 | |
991d9fa0 JT |
1347 | remap_and_issue(tc, bio, 0); |
1348 | return; | |
1349 | } | |
1350 | ||
1351 | /* | |
1352 | * Fill read bios with zeroes and complete them immediately. | |
1353 | */ | |
1354 | if (bio_data_dir(bio) == READ) { | |
1355 | zero_fill_bio(bio); | |
f286ba0e | 1356 | cell_defer_no_holder(tc, cell); |
991d9fa0 JT |
1357 | bio_endio(bio, 0); |
1358 | return; | |
1359 | } | |
1360 | ||
1361 | r = alloc_data_block(tc, &data_block); | |
1362 | switch (r) { | |
1363 | case 0: | |
2dd9c257 JT |
1364 | if (tc->origin_dev) |
1365 | schedule_external_copy(tc, block, data_block, cell, bio); | |
1366 | else | |
1367 | schedule_zero(tc, block, data_block, cell, bio); | |
991d9fa0 JT |
1368 | break; |
1369 | ||
1370 | case -ENOSPC: | |
399caddf | 1371 | retry_bios_on_resume(pool, cell); |
991d9fa0 JT |
1372 | break; |
1373 | ||
1374 | default: | |
c397741c MS |
1375 | DMERR_LIMIT("%s: alloc_data_block() failed: error = %d", |
1376 | __func__, r); | |
6beca5eb | 1377 | cell_error(pool, cell); |
991d9fa0 JT |
1378 | break; |
1379 | } | |
1380 | } | |
1381 | ||
1382 | static void process_bio(struct thin_c *tc, struct bio *bio) | |
1383 | { | |
1384 | int r; | |
6beca5eb | 1385 | struct pool *pool = tc->pool; |
991d9fa0 | 1386 | dm_block_t block = get_bio_block(tc, bio); |
a24c2569 | 1387 | struct dm_bio_prison_cell *cell; |
44feb387 | 1388 | struct dm_cell_key key; |
991d9fa0 JT |
1389 | struct dm_thin_lookup_result lookup_result; |
1390 | ||
1391 | /* | |
1392 | * If cell is already occupied, then the block is already | |
1393 | * being provisioned so we have nothing further to do here. | |
1394 | */ | |
1395 | build_virtual_key(tc->td, block, &key); | |
6beca5eb | 1396 | if (bio_detain(pool, &key, bio, &cell)) |
991d9fa0 JT |
1397 | return; |
1398 | ||
1399 | r = dm_thin_find_block(tc->td, block, 1, &lookup_result); | |
1400 | switch (r) { | |
1401 | case 0: | |
e8088073 | 1402 | if (lookup_result.shared) { |
991d9fa0 | 1403 | process_shared_bio(tc, bio, block, &lookup_result); |
6beca5eb | 1404 | cell_defer_no_holder(tc, cell); /* FIXME: pass this cell into process_shared? */ |
e8088073 | 1405 | } else { |
6beca5eb | 1406 | inc_all_io_entry(pool, bio); |
f286ba0e | 1407 | cell_defer_no_holder(tc, cell); |
e8088073 | 1408 | |
991d9fa0 | 1409 | remap_and_issue(tc, bio, lookup_result.block); |
e8088073 | 1410 | } |
991d9fa0 JT |
1411 | break; |
1412 | ||
1413 | case -ENODATA: | |
2dd9c257 | 1414 | if (bio_data_dir(bio) == READ && tc->origin_dev) { |
6beca5eb | 1415 | inc_all_io_entry(pool, bio); |
f286ba0e | 1416 | cell_defer_no_holder(tc, cell); |
e8088073 | 1417 | |
e5aea7b4 JT |
1418 | if (bio_end_sector(bio) <= tc->origin_size) |
1419 | remap_to_origin_and_issue(tc, bio); | |
1420 | ||
1421 | else if (bio->bi_iter.bi_sector < tc->origin_size) { | |
1422 | zero_fill_bio(bio); | |
1423 | bio->bi_iter.bi_size = (tc->origin_size - bio->bi_iter.bi_sector) << SECTOR_SHIFT; | |
1424 | remap_to_origin_and_issue(tc, bio); | |
1425 | ||
1426 | } else { | |
1427 | zero_fill_bio(bio); | |
1428 | bio_endio(bio, 0); | |
1429 | } | |
2dd9c257 JT |
1430 | } else |
1431 | provision_block(tc, bio, block, cell); | |
991d9fa0 JT |
1432 | break; |
1433 | ||
1434 | default: | |
c397741c MS |
1435 | DMERR_LIMIT("%s: dm_thin_find_block() failed: error = %d", |
1436 | __func__, r); | |
f286ba0e | 1437 | cell_defer_no_holder(tc, cell); |
991d9fa0 JT |
1438 | bio_io_error(bio); |
1439 | break; | |
1440 | } | |
1441 | } | |
1442 | ||
e49e5829 JT |
1443 | static void process_bio_read_only(struct thin_c *tc, struct bio *bio) |
1444 | { | |
1445 | int r; | |
1446 | int rw = bio_data_dir(bio); | |
1447 | dm_block_t block = get_bio_block(tc, bio); | |
1448 | struct dm_thin_lookup_result lookup_result; | |
1449 | ||
1450 | r = dm_thin_find_block(tc->td, block, 1, &lookup_result); | |
1451 | switch (r) { | |
1452 | case 0: | |
4f024f37 | 1453 | if (lookup_result.shared && (rw == WRITE) && bio->bi_iter.bi_size) |
8c0f0e8c | 1454 | handle_unserviceable_bio(tc->pool, bio); |
e8088073 JT |
1455 | else { |
1456 | inc_all_io_entry(tc->pool, bio); | |
e49e5829 | 1457 | remap_and_issue(tc, bio, lookup_result.block); |
e8088073 | 1458 | } |
e49e5829 JT |
1459 | break; |
1460 | ||
1461 | case -ENODATA: | |
1462 | if (rw != READ) { | |
8c0f0e8c | 1463 | handle_unserviceable_bio(tc->pool, bio); |
e49e5829 JT |
1464 | break; |
1465 | } | |
1466 | ||
1467 | if (tc->origin_dev) { | |
e8088073 | 1468 | inc_all_io_entry(tc->pool, bio); |
e49e5829 JT |
1469 | remap_to_origin_and_issue(tc, bio); |
1470 | break; | |
1471 | } | |
1472 | ||
1473 | zero_fill_bio(bio); | |
1474 | bio_endio(bio, 0); | |
1475 | break; | |
1476 | ||
1477 | default: | |
c397741c MS |
1478 | DMERR_LIMIT("%s: dm_thin_find_block() failed: error = %d", |
1479 | __func__, r); | |
e49e5829 JT |
1480 | bio_io_error(bio); |
1481 | break; | |
1482 | } | |
1483 | } | |
1484 | ||
3e1a0699 JT |
1485 | static void process_bio_success(struct thin_c *tc, struct bio *bio) |
1486 | { | |
1487 | bio_endio(bio, 0); | |
1488 | } | |
1489 | ||
e49e5829 JT |
1490 | static void process_bio_fail(struct thin_c *tc, struct bio *bio) |
1491 | { | |
1492 | bio_io_error(bio); | |
1493 | } | |
1494 | ||
ac8c3f3d JT |
1495 | /* |
1496 | * FIXME: should we also commit due to size of transaction, measured in | |
1497 | * metadata blocks? | |
1498 | */ | |
905e51b3 JT |
1499 | static int need_commit_due_to_time(struct pool *pool) |
1500 | { | |
1501 | return jiffies < pool->last_commit_jiffies || | |
1502 | jiffies > pool->last_commit_jiffies + COMMIT_PERIOD; | |
1503 | } | |
1504 | ||
67324ea1 MS |
1505 | #define thin_pbd(node) rb_entry((node), struct dm_thin_endio_hook, rb_node) |
1506 | #define thin_bio(pbd) dm_bio_from_per_bio_data((pbd), sizeof(struct dm_thin_endio_hook)) | |
1507 | ||
1508 | static void __thin_bio_rb_add(struct thin_c *tc, struct bio *bio) | |
1509 | { | |
1510 | struct rb_node **rbp, *parent; | |
1511 | struct dm_thin_endio_hook *pbd; | |
1512 | sector_t bi_sector = bio->bi_iter.bi_sector; | |
1513 | ||
1514 | rbp = &tc->sort_bio_list.rb_node; | |
1515 | parent = NULL; | |
1516 | while (*rbp) { | |
1517 | parent = *rbp; | |
1518 | pbd = thin_pbd(parent); | |
1519 | ||
1520 | if (bi_sector < thin_bio(pbd)->bi_iter.bi_sector) | |
1521 | rbp = &(*rbp)->rb_left; | |
1522 | else | |
1523 | rbp = &(*rbp)->rb_right; | |
1524 | } | |
1525 | ||
1526 | pbd = dm_per_bio_data(bio, sizeof(struct dm_thin_endio_hook)); | |
1527 | rb_link_node(&pbd->rb_node, parent, rbp); | |
1528 | rb_insert_color(&pbd->rb_node, &tc->sort_bio_list); | |
1529 | } | |
1530 | ||
1531 | static void __extract_sorted_bios(struct thin_c *tc) | |
1532 | { | |
1533 | struct rb_node *node; | |
1534 | struct dm_thin_endio_hook *pbd; | |
1535 | struct bio *bio; | |
1536 | ||
1537 | for (node = rb_first(&tc->sort_bio_list); node; node = rb_next(node)) { | |
1538 | pbd = thin_pbd(node); | |
1539 | bio = thin_bio(pbd); | |
1540 | ||
1541 | bio_list_add(&tc->deferred_bio_list, bio); | |
1542 | rb_erase(&pbd->rb_node, &tc->sort_bio_list); | |
1543 | } | |
1544 | ||
1545 | WARN_ON(!RB_EMPTY_ROOT(&tc->sort_bio_list)); | |
1546 | } | |
1547 | ||
1548 | static void __sort_thin_deferred_bios(struct thin_c *tc) | |
1549 | { | |
1550 | struct bio *bio; | |
1551 | struct bio_list bios; | |
1552 | ||
1553 | bio_list_init(&bios); | |
1554 | bio_list_merge(&bios, &tc->deferred_bio_list); | |
1555 | bio_list_init(&tc->deferred_bio_list); | |
1556 | ||
1557 | /* Sort deferred_bio_list using rb-tree */ | |
1558 | while ((bio = bio_list_pop(&bios))) | |
1559 | __thin_bio_rb_add(tc, bio); | |
1560 | ||
1561 | /* | |
1562 | * Transfer the sorted bios in sort_bio_list back to | |
1563 | * deferred_bio_list to allow lockless submission of | |
1564 | * all bios. | |
1565 | */ | |
1566 | __extract_sorted_bios(tc); | |
1567 | } | |
1568 | ||
c140e1c4 | 1569 | static void process_thin_deferred_bios(struct thin_c *tc) |
991d9fa0 | 1570 | { |
c140e1c4 | 1571 | struct pool *pool = tc->pool; |
991d9fa0 JT |
1572 | unsigned long flags; |
1573 | struct bio *bio; | |
1574 | struct bio_list bios; | |
67324ea1 | 1575 | struct blk_plug plug; |
8a01a6af | 1576 | unsigned count = 0; |
991d9fa0 | 1577 | |
c140e1c4 MS |
1578 | if (tc->requeue_mode) { |
1579 | requeue_bio_list(tc, &tc->deferred_bio_list); | |
1580 | return; | |
1581 | } | |
1582 | ||
991d9fa0 JT |
1583 | bio_list_init(&bios); |
1584 | ||
c140e1c4 | 1585 | spin_lock_irqsave(&tc->lock, flags); |
67324ea1 MS |
1586 | |
1587 | if (bio_list_empty(&tc->deferred_bio_list)) { | |
1588 | spin_unlock_irqrestore(&tc->lock, flags); | |
1589 | return; | |
1590 | } | |
1591 | ||
1592 | __sort_thin_deferred_bios(tc); | |
1593 | ||
c140e1c4 MS |
1594 | bio_list_merge(&bios, &tc->deferred_bio_list); |
1595 | bio_list_init(&tc->deferred_bio_list); | |
67324ea1 | 1596 | |
c140e1c4 | 1597 | spin_unlock_irqrestore(&tc->lock, flags); |
991d9fa0 | 1598 | |
67324ea1 | 1599 | blk_start_plug(&plug); |
991d9fa0 | 1600 | while ((bio = bio_list_pop(&bios))) { |
991d9fa0 JT |
1601 | /* |
1602 | * If we've got no free new_mapping structs, and processing | |
1603 | * this bio might require one, we pause until there are some | |
1604 | * prepared mappings to process. | |
1605 | */ | |
1606 | if (ensure_next_mapping(pool)) { | |
c140e1c4 MS |
1607 | spin_lock_irqsave(&tc->lock, flags); |
1608 | bio_list_add(&tc->deferred_bio_list, bio); | |
1609 | bio_list_merge(&tc->deferred_bio_list, &bios); | |
1610 | spin_unlock_irqrestore(&tc->lock, flags); | |
991d9fa0 JT |
1611 | break; |
1612 | } | |
104655fd JT |
1613 | |
1614 | if (bio->bi_rw & REQ_DISCARD) | |
e49e5829 | 1615 | pool->process_discard(tc, bio); |
104655fd | 1616 | else |
e49e5829 | 1617 | pool->process_bio(tc, bio); |
8a01a6af JT |
1618 | |
1619 | if ((count++ & 127) == 0) { | |
7d327fe0 | 1620 | throttle_work_update(&pool->throttle); |
8a01a6af JT |
1621 | dm_pool_issue_prefetches(pool->pmd); |
1622 | } | |
991d9fa0 | 1623 | } |
67324ea1 | 1624 | blk_finish_plug(&plug); |
c140e1c4 MS |
1625 | } |
1626 | ||
b10ebd34 JT |
1627 | static void thin_get(struct thin_c *tc); |
1628 | static void thin_put(struct thin_c *tc); | |
1629 | ||
1630 | /* | |
1631 | * We can't hold rcu_read_lock() around code that can block. So we | |
1632 | * find a thin with the rcu lock held; bump a refcount; then drop | |
1633 | * the lock. | |
1634 | */ | |
1635 | static struct thin_c *get_first_thin(struct pool *pool) | |
1636 | { | |
1637 | struct thin_c *tc = NULL; | |
1638 | ||
1639 | rcu_read_lock(); | |
1640 | if (!list_empty(&pool->active_thins)) { | |
1641 | tc = list_entry_rcu(pool->active_thins.next, struct thin_c, list); | |
1642 | thin_get(tc); | |
1643 | } | |
1644 | rcu_read_unlock(); | |
1645 | ||
1646 | return tc; | |
1647 | } | |
1648 | ||
1649 | static struct thin_c *get_next_thin(struct pool *pool, struct thin_c *tc) | |
1650 | { | |
1651 | struct thin_c *old_tc = tc; | |
1652 | ||
1653 | rcu_read_lock(); | |
1654 | list_for_each_entry_continue_rcu(tc, &pool->active_thins, list) { | |
1655 | thin_get(tc); | |
1656 | thin_put(old_tc); | |
1657 | rcu_read_unlock(); | |
1658 | return tc; | |
1659 | } | |
1660 | thin_put(old_tc); | |
1661 | rcu_read_unlock(); | |
1662 | ||
1663 | return NULL; | |
1664 | } | |
1665 | ||
c140e1c4 MS |
1666 | static void process_deferred_bios(struct pool *pool) |
1667 | { | |
1668 | unsigned long flags; | |
1669 | struct bio *bio; | |
1670 | struct bio_list bios; | |
1671 | struct thin_c *tc; | |
1672 | ||
b10ebd34 JT |
1673 | tc = get_first_thin(pool); |
1674 | while (tc) { | |
c140e1c4 | 1675 | process_thin_deferred_bios(tc); |
b10ebd34 JT |
1676 | tc = get_next_thin(pool, tc); |
1677 | } | |
991d9fa0 JT |
1678 | |
1679 | /* | |
1680 | * If there are any deferred flush bios, we must commit | |
1681 | * the metadata before issuing them. | |
1682 | */ | |
1683 | bio_list_init(&bios); | |
1684 | spin_lock_irqsave(&pool->lock, flags); | |
1685 | bio_list_merge(&bios, &pool->deferred_flush_bios); | |
1686 | bio_list_init(&pool->deferred_flush_bios); | |
1687 | spin_unlock_irqrestore(&pool->lock, flags); | |
1688 | ||
4d1662a3 MS |
1689 | if (bio_list_empty(&bios) && |
1690 | !(dm_pool_changed_this_transaction(pool->pmd) && need_commit_due_to_time(pool))) | |
991d9fa0 JT |
1691 | return; |
1692 | ||
020cc3b5 | 1693 | if (commit(pool)) { |
991d9fa0 JT |
1694 | while ((bio = bio_list_pop(&bios))) |
1695 | bio_io_error(bio); | |
1696 | return; | |
1697 | } | |
905e51b3 | 1698 | pool->last_commit_jiffies = jiffies; |
991d9fa0 JT |
1699 | |
1700 | while ((bio = bio_list_pop(&bios))) | |
1701 | generic_make_request(bio); | |
1702 | } | |
1703 | ||
1704 | static void do_worker(struct work_struct *ws) | |
1705 | { | |
1706 | struct pool *pool = container_of(ws, struct pool, worker); | |
1707 | ||
7d327fe0 | 1708 | throttle_work_start(&pool->throttle); |
8a01a6af | 1709 | dm_pool_issue_prefetches(pool->pmd); |
7d327fe0 | 1710 | throttle_work_update(&pool->throttle); |
e49e5829 | 1711 | process_prepared(pool, &pool->prepared_mappings, &pool->process_prepared_mapping); |
7d327fe0 | 1712 | throttle_work_update(&pool->throttle); |
e49e5829 | 1713 | process_prepared(pool, &pool->prepared_discards, &pool->process_prepared_discard); |
7d327fe0 | 1714 | throttle_work_update(&pool->throttle); |
991d9fa0 | 1715 | process_deferred_bios(pool); |
7d327fe0 | 1716 | throttle_work_complete(&pool->throttle); |
991d9fa0 JT |
1717 | } |
1718 | ||
905e51b3 JT |
1719 | /* |
1720 | * We want to commit periodically so that not too much | |
1721 | * unwritten data builds up. | |
1722 | */ | |
1723 | static void do_waker(struct work_struct *ws) | |
1724 | { | |
1725 | struct pool *pool = container_of(to_delayed_work(ws), struct pool, waker); | |
1726 | wake_worker(pool); | |
1727 | queue_delayed_work(pool->wq, &pool->waker, COMMIT_PERIOD); | |
1728 | } | |
1729 | ||
85ad643b JT |
1730 | /* |
1731 | * We're holding onto IO to allow userland time to react. After the | |
1732 | * timeout either the pool will have been resized (and thus back in | |
1733 | * PM_WRITE mode), or we degrade to PM_READ_ONLY and start erroring IO. | |
1734 | */ | |
1735 | static void do_no_space_timeout(struct work_struct *ws) | |
1736 | { | |
1737 | struct pool *pool = container_of(to_delayed_work(ws), struct pool, | |
1738 | no_space_timeout); | |
1739 | ||
1740 | if (get_pool_mode(pool) == PM_OUT_OF_DATA_SPACE && !pool->pf.error_if_no_space) | |
1741 | set_pool_mode(pool, PM_READ_ONLY); | |
1742 | } | |
1743 | ||
991d9fa0 JT |
1744 | /*----------------------------------------------------------------*/ |
1745 | ||
e7a3e871 | 1746 | struct pool_work { |
738211f7 | 1747 | struct work_struct worker; |
e7a3e871 JT |
1748 | struct completion complete; |
1749 | }; | |
1750 | ||
1751 | static struct pool_work *to_pool_work(struct work_struct *ws) | |
1752 | { | |
1753 | return container_of(ws, struct pool_work, worker); | |
1754 | } | |
1755 | ||
1756 | static void pool_work_complete(struct pool_work *pw) | |
1757 | { | |
1758 | complete(&pw->complete); | |
1759 | } | |
738211f7 | 1760 | |
e7a3e871 JT |
1761 | static void pool_work_wait(struct pool_work *pw, struct pool *pool, |
1762 | void (*fn)(struct work_struct *)) | |
1763 | { | |
1764 | INIT_WORK_ONSTACK(&pw->worker, fn); | |
1765 | init_completion(&pw->complete); | |
1766 | queue_work(pool->wq, &pw->worker); | |
1767 | wait_for_completion(&pw->complete); | |
1768 | } | |
1769 | ||
1770 | /*----------------------------------------------------------------*/ | |
1771 | ||
1772 | struct noflush_work { | |
1773 | struct pool_work pw; | |
1774 | struct thin_c *tc; | |
738211f7 JT |
1775 | }; |
1776 | ||
e7a3e871 | 1777 | static struct noflush_work *to_noflush(struct work_struct *ws) |
738211f7 | 1778 | { |
e7a3e871 | 1779 | return container_of(to_pool_work(ws), struct noflush_work, pw); |
738211f7 JT |
1780 | } |
1781 | ||
1782 | static void do_noflush_start(struct work_struct *ws) | |
1783 | { | |
e7a3e871 | 1784 | struct noflush_work *w = to_noflush(ws); |
738211f7 JT |
1785 | w->tc->requeue_mode = true; |
1786 | requeue_io(w->tc); | |
e7a3e871 | 1787 | pool_work_complete(&w->pw); |
738211f7 JT |
1788 | } |
1789 | ||
1790 | static void do_noflush_stop(struct work_struct *ws) | |
1791 | { | |
e7a3e871 | 1792 | struct noflush_work *w = to_noflush(ws); |
738211f7 | 1793 | w->tc->requeue_mode = false; |
e7a3e871 | 1794 | pool_work_complete(&w->pw); |
738211f7 JT |
1795 | } |
1796 | ||
1797 | static void noflush_work(struct thin_c *tc, void (*fn)(struct work_struct *)) | |
1798 | { | |
1799 | struct noflush_work w; | |
1800 | ||
738211f7 | 1801 | w.tc = tc; |
e7a3e871 | 1802 | pool_work_wait(&w.pw, tc->pool, fn); |
738211f7 JT |
1803 | } |
1804 | ||
1805 | /*----------------------------------------------------------------*/ | |
1806 | ||
e49e5829 JT |
1807 | static enum pool_mode get_pool_mode(struct pool *pool) |
1808 | { | |
1809 | return pool->pf.mode; | |
1810 | } | |
1811 | ||
3e1a0699 JT |
1812 | static void notify_of_pool_mode_change(struct pool *pool, const char *new_mode) |
1813 | { | |
1814 | dm_table_event(pool->ti->table); | |
1815 | DMINFO("%s: switching pool to %s mode", | |
1816 | dm_device_name(pool->pool_md), new_mode); | |
1817 | } | |
1818 | ||
8b64e881 | 1819 | static void set_pool_mode(struct pool *pool, enum pool_mode new_mode) |
e49e5829 | 1820 | { |
cdc2b415 | 1821 | struct pool_c *pt = pool->ti->private; |
07f2b6e0 MS |
1822 | bool needs_check = dm_pool_metadata_needs_check(pool->pmd); |
1823 | enum pool_mode old_mode = get_pool_mode(pool); | |
80c57893 | 1824 | unsigned long no_space_timeout = ACCESS_ONCE(no_space_timeout_secs) * HZ; |
07f2b6e0 MS |
1825 | |
1826 | /* | |
1827 | * Never allow the pool to transition to PM_WRITE mode if user | |
1828 | * intervention is required to verify metadata and data consistency. | |
1829 | */ | |
1830 | if (new_mode == PM_WRITE && needs_check) { | |
1831 | DMERR("%s: unable to switch pool to write mode until repaired.", | |
1832 | dm_device_name(pool->pool_md)); | |
1833 | if (old_mode != new_mode) | |
1834 | new_mode = old_mode; | |
1835 | else | |
1836 | new_mode = PM_READ_ONLY; | |
1837 | } | |
1838 | /* | |
1839 | * If we were in PM_FAIL mode, rollback of metadata failed. We're | |
1840 | * not going to recover without a thin_repair. So we never let the | |
1841 | * pool move out of the old mode. | |
1842 | */ | |
1843 | if (old_mode == PM_FAIL) | |
1844 | new_mode = old_mode; | |
e49e5829 | 1845 | |
8b64e881 | 1846 | switch (new_mode) { |
e49e5829 | 1847 | case PM_FAIL: |
8b64e881 | 1848 | if (old_mode != new_mode) |
3e1a0699 | 1849 | notify_of_pool_mode_change(pool, "failure"); |
5383ef3a | 1850 | dm_pool_metadata_read_only(pool->pmd); |
e49e5829 JT |
1851 | pool->process_bio = process_bio_fail; |
1852 | pool->process_discard = process_bio_fail; | |
1853 | pool->process_prepared_mapping = process_prepared_mapping_fail; | |
1854 | pool->process_prepared_discard = process_prepared_discard_fail; | |
3e1a0699 JT |
1855 | |
1856 | error_retry_list(pool); | |
e49e5829 JT |
1857 | break; |
1858 | ||
1859 | case PM_READ_ONLY: | |
8b64e881 | 1860 | if (old_mode != new_mode) |
3e1a0699 JT |
1861 | notify_of_pool_mode_change(pool, "read-only"); |
1862 | dm_pool_metadata_read_only(pool->pmd); | |
1863 | pool->process_bio = process_bio_read_only; | |
1864 | pool->process_discard = process_bio_success; | |
1865 | pool->process_prepared_mapping = process_prepared_mapping_fail; | |
1866 | pool->process_prepared_discard = process_prepared_discard_passdown; | |
1867 | ||
1868 | error_retry_list(pool); | |
1869 | break; | |
1870 | ||
1871 | case PM_OUT_OF_DATA_SPACE: | |
1872 | /* | |
1873 | * Ideally we'd never hit this state; the low water mark | |
1874 | * would trigger userland to extend the pool before we | |
1875 | * completely run out of data space. However, many small | |
1876 | * IOs to unprovisioned space can consume data space at an | |
1877 | * alarming rate. Adjust your low water mark if you're | |
1878 | * frequently seeing this mode. | |
1879 | */ | |
1880 | if (old_mode != new_mode) | |
1881 | notify_of_pool_mode_change(pool, "out-of-data-space"); | |
1882 | pool->process_bio = process_bio_read_only; | |
1883 | pool->process_discard = process_discard; | |
1884 | pool->process_prepared_mapping = process_prepared_mapping; | |
1885 | pool->process_prepared_discard = process_prepared_discard_passdown; | |
85ad643b | 1886 | |
80c57893 MS |
1887 | if (!pool->pf.error_if_no_space && no_space_timeout) |
1888 | queue_delayed_work(pool->wq, &pool->no_space_timeout, no_space_timeout); | |
e49e5829 JT |
1889 | break; |
1890 | ||
1891 | case PM_WRITE: | |
8b64e881 | 1892 | if (old_mode != new_mode) |
3e1a0699 | 1893 | notify_of_pool_mode_change(pool, "write"); |
9b7aaa64 | 1894 | dm_pool_metadata_read_write(pool->pmd); |
e49e5829 JT |
1895 | pool->process_bio = process_bio; |
1896 | pool->process_discard = process_discard; | |
1897 | pool->process_prepared_mapping = process_prepared_mapping; | |
1898 | pool->process_prepared_discard = process_prepared_discard; | |
1899 | break; | |
1900 | } | |
8b64e881 MS |
1901 | |
1902 | pool->pf.mode = new_mode; | |
cdc2b415 MS |
1903 | /* |
1904 | * The pool mode may have changed, sync it so bind_control_target() | |
1905 | * doesn't cause an unexpected mode transition on resume. | |
1906 | */ | |
1907 | pt->adjusted_pf.mode = new_mode; | |
e49e5829 JT |
1908 | } |
1909 | ||
07f2b6e0 | 1910 | static void abort_transaction(struct pool *pool) |
b5330655 | 1911 | { |
07f2b6e0 MS |
1912 | const char *dev_name = dm_device_name(pool->pool_md); |
1913 | ||
1914 | DMERR_LIMIT("%s: aborting current metadata transaction", dev_name); | |
1915 | if (dm_pool_abort_metadata(pool->pmd)) { | |
1916 | DMERR("%s: failed to abort metadata transaction", dev_name); | |
1917 | set_pool_mode(pool, PM_FAIL); | |
1918 | } | |
1919 | ||
1920 | if (dm_pool_metadata_set_needs_check(pool->pmd)) { | |
1921 | DMERR("%s: failed to set 'needs_check' flag in metadata", dev_name); | |
1922 | set_pool_mode(pool, PM_FAIL); | |
1923 | } | |
1924 | } | |
399caddf | 1925 | |
07f2b6e0 MS |
1926 | static void metadata_operation_failed(struct pool *pool, const char *op, int r) |
1927 | { | |
b5330655 JT |
1928 | DMERR_LIMIT("%s: metadata operation '%s' failed: error = %d", |
1929 | dm_device_name(pool->pool_md), op, r); | |
1930 | ||
07f2b6e0 | 1931 | abort_transaction(pool); |
b5330655 JT |
1932 | set_pool_mode(pool, PM_READ_ONLY); |
1933 | } | |
1934 | ||
e49e5829 JT |
1935 | /*----------------------------------------------------------------*/ |
1936 | ||
991d9fa0 JT |
1937 | /* |
1938 | * Mapping functions. | |
1939 | */ | |
1940 | ||
1941 | /* | |
1942 | * Called only while mapping a thin bio to hand it over to the workqueue. | |
1943 | */ | |
1944 | static void thin_defer_bio(struct thin_c *tc, struct bio *bio) | |
1945 | { | |
1946 | unsigned long flags; | |
1947 | struct pool *pool = tc->pool; | |
1948 | ||
c140e1c4 MS |
1949 | spin_lock_irqsave(&tc->lock, flags); |
1950 | bio_list_add(&tc->deferred_bio_list, bio); | |
1951 | spin_unlock_irqrestore(&tc->lock, flags); | |
991d9fa0 JT |
1952 | |
1953 | wake_worker(pool); | |
1954 | } | |
1955 | ||
7d327fe0 JT |
1956 | static void thin_defer_bio_with_throttle(struct thin_c *tc, struct bio *bio) |
1957 | { | |
1958 | struct pool *pool = tc->pool; | |
1959 | ||
1960 | throttle_lock(&pool->throttle); | |
1961 | thin_defer_bio(tc, bio); | |
1962 | throttle_unlock(&pool->throttle); | |
1963 | } | |
1964 | ||
59c3d2c6 | 1965 | static void thin_hook_bio(struct thin_c *tc, struct bio *bio) |
eb2aa48d | 1966 | { |
59c3d2c6 | 1967 | struct dm_thin_endio_hook *h = dm_per_bio_data(bio, sizeof(struct dm_thin_endio_hook)); |
eb2aa48d JT |
1968 | |
1969 | h->tc = tc; | |
1970 | h->shared_read_entry = NULL; | |
e8088073 | 1971 | h->all_io_entry = NULL; |
eb2aa48d | 1972 | h->overwrite_mapping = NULL; |
eb2aa48d JT |
1973 | } |
1974 | ||
991d9fa0 JT |
1975 | /* |
1976 | * Non-blocking function called from the thin target's map function. | |
1977 | */ | |
7de3ee57 | 1978 | static int thin_bio_map(struct dm_target *ti, struct bio *bio) |
991d9fa0 JT |
1979 | { |
1980 | int r; | |
1981 | struct thin_c *tc = ti->private; | |
1982 | dm_block_t block = get_bio_block(tc, bio); | |
1983 | struct dm_thin_device *td = tc->td; | |
1984 | struct dm_thin_lookup_result result; | |
025b9685 JT |
1985 | struct dm_bio_prison_cell cell1, cell2; |
1986 | struct dm_bio_prison_cell *cell_result; | |
e8088073 | 1987 | struct dm_cell_key key; |
991d9fa0 | 1988 | |
59c3d2c6 | 1989 | thin_hook_bio(tc, bio); |
e49e5829 | 1990 | |
738211f7 JT |
1991 | if (tc->requeue_mode) { |
1992 | bio_endio(bio, DM_ENDIO_REQUEUE); | |
1993 | return DM_MAPIO_SUBMITTED; | |
1994 | } | |
1995 | ||
e49e5829 JT |
1996 | if (get_pool_mode(tc->pool) == PM_FAIL) { |
1997 | bio_io_error(bio); | |
1998 | return DM_MAPIO_SUBMITTED; | |
1999 | } | |
2000 | ||
104655fd | 2001 | if (bio->bi_rw & (REQ_DISCARD | REQ_FLUSH | REQ_FUA)) { |
7d327fe0 | 2002 | thin_defer_bio_with_throttle(tc, bio); |
991d9fa0 JT |
2003 | return DM_MAPIO_SUBMITTED; |
2004 | } | |
2005 | ||
c822ed96 JT |
2006 | /* |
2007 | * We must hold the virtual cell before doing the lookup, otherwise | |
2008 | * there's a race with discard. | |
2009 | */ | |
2010 | build_virtual_key(tc->td, block, &key); | |
2011 | if (dm_bio_detain(tc->pool->prison, &key, bio, &cell1, &cell_result)) | |
2012 | return DM_MAPIO_SUBMITTED; | |
2013 | ||
991d9fa0 JT |
2014 | r = dm_thin_find_block(td, block, 0, &result); |
2015 | ||
2016 | /* | |
2017 | * Note that we defer readahead too. | |
2018 | */ | |
2019 | switch (r) { | |
2020 | case 0: | |
2021 | if (unlikely(result.shared)) { | |
2022 | /* | |
2023 | * We have a race condition here between the | |
2024 | * result.shared value returned by the lookup and | |
2025 | * snapshot creation, which may cause new | |
2026 | * sharing. | |
2027 | * | |
2028 | * To avoid this always quiesce the origin before | |
2029 | * taking the snap. You want to do this anyway to | |
2030 | * ensure a consistent application view | |
2031 | * (i.e. lockfs). | |
2032 | * | |
2033 | * More distant ancestors are irrelevant. The | |
2034 | * shared flag will be set in their case. | |
2035 | */ | |
2036 | thin_defer_bio(tc, bio); | |
c822ed96 | 2037 | cell_defer_no_holder_no_free(tc, &cell1); |
e8088073 | 2038 | return DM_MAPIO_SUBMITTED; |
991d9fa0 | 2039 | } |
e8088073 | 2040 | |
e8088073 | 2041 | build_data_key(tc->td, result.block, &key); |
025b9685 JT |
2042 | if (dm_bio_detain(tc->pool->prison, &key, bio, &cell2, &cell_result)) { |
2043 | cell_defer_no_holder_no_free(tc, &cell1); | |
e8088073 JT |
2044 | return DM_MAPIO_SUBMITTED; |
2045 | } | |
2046 | ||
2047 | inc_all_io_entry(tc->pool, bio); | |
025b9685 JT |
2048 | cell_defer_no_holder_no_free(tc, &cell2); |
2049 | cell_defer_no_holder_no_free(tc, &cell1); | |
e8088073 JT |
2050 | |
2051 | remap(tc, bio, result.block); | |
2052 | return DM_MAPIO_REMAPPED; | |
991d9fa0 JT |
2053 | |
2054 | case -ENODATA: | |
e49e5829 JT |
2055 | if (get_pool_mode(tc->pool) == PM_READ_ONLY) { |
2056 | /* | |
2057 | * This block isn't provisioned, and we have no way | |
8c0f0e8c | 2058 | * of doing so. |
e49e5829 | 2059 | */ |
8c0f0e8c | 2060 | handle_unserviceable_bio(tc->pool, bio); |
c822ed96 | 2061 | cell_defer_no_holder_no_free(tc, &cell1); |
2aab3850 | 2062 | return DM_MAPIO_SUBMITTED; |
e49e5829 JT |
2063 | } |
2064 | /* fall through */ | |
2065 | ||
2066 | case -EWOULDBLOCK: | |
991d9fa0 | 2067 | thin_defer_bio(tc, bio); |
c822ed96 | 2068 | cell_defer_no_holder_no_free(tc, &cell1); |
2aab3850 | 2069 | return DM_MAPIO_SUBMITTED; |
e49e5829 JT |
2070 | |
2071 | default: | |
2072 | /* | |
2073 | * Must always call bio_io_error on failure. | |
2074 | * dm_thin_find_block can fail with -EINVAL if the | |
2075 | * pool is switched to fail-io mode. | |
2076 | */ | |
2077 | bio_io_error(bio); | |
c822ed96 | 2078 | cell_defer_no_holder_no_free(tc, &cell1); |
2aab3850 | 2079 | return DM_MAPIO_SUBMITTED; |
991d9fa0 | 2080 | } |
991d9fa0 JT |
2081 | } |
2082 | ||
2083 | static int pool_is_congested(struct dm_target_callbacks *cb, int bdi_bits) | |
2084 | { | |
991d9fa0 | 2085 | struct pool_c *pt = container_of(cb, struct pool_c, callbacks); |
760fe67e | 2086 | struct request_queue *q; |
991d9fa0 | 2087 | |
760fe67e MS |
2088 | if (get_pool_mode(pt->pool) == PM_OUT_OF_DATA_SPACE) |
2089 | return 1; | |
991d9fa0 | 2090 | |
760fe67e MS |
2091 | q = bdev_get_queue(pt->data_dev->bdev); |
2092 | return bdi_congested(&q->backing_dev_info, bdi_bits); | |
991d9fa0 JT |
2093 | } |
2094 | ||
c140e1c4 | 2095 | static void requeue_bios(struct pool *pool) |
991d9fa0 | 2096 | { |
c140e1c4 MS |
2097 | unsigned long flags; |
2098 | struct thin_c *tc; | |
2099 | ||
2100 | rcu_read_lock(); | |
2101 | list_for_each_entry_rcu(tc, &pool->active_thins, list) { | |
2102 | spin_lock_irqsave(&tc->lock, flags); | |
2103 | bio_list_merge(&tc->deferred_bio_list, &tc->retry_on_resume_list); | |
2104 | bio_list_init(&tc->retry_on_resume_list); | |
2105 | spin_unlock_irqrestore(&tc->lock, flags); | |
2106 | } | |
2107 | rcu_read_unlock(); | |
991d9fa0 JT |
2108 | } |
2109 | ||
2110 | /*---------------------------------------------------------------- | |
2111 | * Binding of control targets to a pool object | |
2112 | *--------------------------------------------------------------*/ | |
9bc142dd MS |
2113 | static bool data_dev_supports_discard(struct pool_c *pt) |
2114 | { | |
2115 | struct request_queue *q = bdev_get_queue(pt->data_dev->bdev); | |
2116 | ||
2117 | return q && blk_queue_discard(q); | |
2118 | } | |
2119 | ||
58051b94 JT |
2120 | static bool is_factor(sector_t block_size, uint32_t n) |
2121 | { | |
2122 | return !sector_div(block_size, n); | |
2123 | } | |
2124 | ||
9bc142dd MS |
2125 | /* |
2126 | * If discard_passdown was enabled verify that the data device | |
0424caa1 | 2127 | * supports discards. Disable discard_passdown if not. |
9bc142dd | 2128 | */ |
0424caa1 | 2129 | static void disable_passdown_if_not_supported(struct pool_c *pt) |
9bc142dd | 2130 | { |
0424caa1 MS |
2131 | struct pool *pool = pt->pool; |
2132 | struct block_device *data_bdev = pt->data_dev->bdev; | |
2133 | struct queue_limits *data_limits = &bdev_get_queue(data_bdev)->limits; | |
2134 | sector_t block_size = pool->sectors_per_block << SECTOR_SHIFT; | |
2135 | const char *reason = NULL; | |
9bc142dd MS |
2136 | char buf[BDEVNAME_SIZE]; |
2137 | ||
0424caa1 | 2138 | if (!pt->adjusted_pf.discard_passdown) |
9bc142dd MS |
2139 | return; |
2140 | ||
0424caa1 MS |
2141 | if (!data_dev_supports_discard(pt)) |
2142 | reason = "discard unsupported"; | |
2143 | ||
2144 | else if (data_limits->max_discard_sectors < pool->sectors_per_block) | |
2145 | reason = "max discard sectors smaller than a block"; | |
9bc142dd | 2146 | |
0424caa1 MS |
2147 | else if (data_limits->discard_granularity > block_size) |
2148 | reason = "discard granularity larger than a block"; | |
2149 | ||
58051b94 | 2150 | else if (!is_factor(block_size, data_limits->discard_granularity)) |
0424caa1 MS |
2151 | reason = "discard granularity not a factor of block size"; |
2152 | ||
2153 | if (reason) { | |
2154 | DMWARN("Data device (%s) %s: Disabling discard passdown.", bdevname(data_bdev, buf), reason); | |
2155 | pt->adjusted_pf.discard_passdown = false; | |
2156 | } | |
9bc142dd MS |
2157 | } |
2158 | ||
991d9fa0 JT |
2159 | static int bind_control_target(struct pool *pool, struct dm_target *ti) |
2160 | { | |
2161 | struct pool_c *pt = ti->private; | |
2162 | ||
e49e5829 | 2163 | /* |
9b7aaa64 | 2164 | * We want to make sure that a pool in PM_FAIL mode is never upgraded. |
e49e5829 | 2165 | */ |
07f2b6e0 | 2166 | enum pool_mode old_mode = get_pool_mode(pool); |
0424caa1 | 2167 | enum pool_mode new_mode = pt->adjusted_pf.mode; |
e49e5829 | 2168 | |
8b64e881 MS |
2169 | /* |
2170 | * Don't change the pool's mode until set_pool_mode() below. | |
2171 | * Otherwise the pool's process_* function pointers may | |
2172 | * not match the desired pool mode. | |
2173 | */ | |
2174 | pt->adjusted_pf.mode = old_mode; | |
2175 | ||
2176 | pool->ti = ti; | |
2177 | pool->pf = pt->adjusted_pf; | |
2178 | pool->low_water_blocks = pt->low_water_blocks; | |
2179 | ||
9bc142dd | 2180 | set_pool_mode(pool, new_mode); |
f402693d | 2181 | |
991d9fa0 JT |
2182 | return 0; |
2183 | } | |
2184 | ||
2185 | static void unbind_control_target(struct pool *pool, struct dm_target *ti) | |
2186 | { | |
2187 | if (pool->ti == ti) | |
2188 | pool->ti = NULL; | |
2189 | } | |
2190 | ||
2191 | /*---------------------------------------------------------------- | |
2192 | * Pool creation | |
2193 | *--------------------------------------------------------------*/ | |
67e2e2b2 JT |
2194 | /* Initialize pool features. */ |
2195 | static void pool_features_init(struct pool_features *pf) | |
2196 | { | |
e49e5829 | 2197 | pf->mode = PM_WRITE; |
9bc142dd MS |
2198 | pf->zero_new_blocks = true; |
2199 | pf->discard_enabled = true; | |
2200 | pf->discard_passdown = true; | |
787a996c | 2201 | pf->error_if_no_space = false; |
67e2e2b2 JT |
2202 | } |
2203 | ||
991d9fa0 JT |
2204 | static void __pool_destroy(struct pool *pool) |
2205 | { | |
2206 | __pool_table_remove(pool); | |
2207 | ||
2208 | if (dm_pool_metadata_close(pool->pmd) < 0) | |
2209 | DMWARN("%s: dm_pool_metadata_close() failed.", __func__); | |
2210 | ||
44feb387 | 2211 | dm_bio_prison_destroy(pool->prison); |
991d9fa0 JT |
2212 | dm_kcopyd_client_destroy(pool->copier); |
2213 | ||
2214 | if (pool->wq) | |
2215 | destroy_workqueue(pool->wq); | |
2216 | ||
2217 | if (pool->next_mapping) | |
2218 | mempool_free(pool->next_mapping, pool->mapping_pool); | |
2219 | mempool_destroy(pool->mapping_pool); | |
44feb387 MS |
2220 | dm_deferred_set_destroy(pool->shared_read_ds); |
2221 | dm_deferred_set_destroy(pool->all_io_ds); | |
991d9fa0 JT |
2222 | kfree(pool); |
2223 | } | |
2224 | ||
a24c2569 | 2225 | static struct kmem_cache *_new_mapping_cache; |
a24c2569 | 2226 | |
991d9fa0 JT |
2227 | static struct pool *pool_create(struct mapped_device *pool_md, |
2228 | struct block_device *metadata_dev, | |
e49e5829 JT |
2229 | unsigned long block_size, |
2230 | int read_only, char **error) | |
991d9fa0 JT |
2231 | { |
2232 | int r; | |
2233 | void *err_p; | |
2234 | struct pool *pool; | |
2235 | struct dm_pool_metadata *pmd; | |
e49e5829 | 2236 | bool format_device = read_only ? false : true; |
991d9fa0 | 2237 | |
e49e5829 | 2238 | pmd = dm_pool_metadata_open(metadata_dev, block_size, format_device); |
991d9fa0 JT |
2239 | if (IS_ERR(pmd)) { |
2240 | *error = "Error creating metadata object"; | |
2241 | return (struct pool *)pmd; | |
2242 | } | |
2243 | ||
2244 | pool = kmalloc(sizeof(*pool), GFP_KERNEL); | |
2245 | if (!pool) { | |
2246 | *error = "Error allocating memory for pool"; | |
2247 | err_p = ERR_PTR(-ENOMEM); | |
2248 | goto bad_pool; | |
2249 | } | |
2250 | ||
2251 | pool->pmd = pmd; | |
2252 | pool->sectors_per_block = block_size; | |
f9a8e0cd MP |
2253 | if (block_size & (block_size - 1)) |
2254 | pool->sectors_per_block_shift = -1; | |
2255 | else | |
2256 | pool->sectors_per_block_shift = __ffs(block_size); | |
991d9fa0 | 2257 | pool->low_water_blocks = 0; |
67e2e2b2 | 2258 | pool_features_init(&pool->pf); |
a195db2d | 2259 | pool->prison = dm_bio_prison_create(); |
991d9fa0 JT |
2260 | if (!pool->prison) { |
2261 | *error = "Error creating pool's bio prison"; | |
2262 | err_p = ERR_PTR(-ENOMEM); | |
2263 | goto bad_prison; | |
2264 | } | |
2265 | ||
df5d2e90 | 2266 | pool->copier = dm_kcopyd_client_create(&dm_kcopyd_throttle); |
991d9fa0 JT |
2267 | if (IS_ERR(pool->copier)) { |
2268 | r = PTR_ERR(pool->copier); | |
2269 | *error = "Error creating pool's kcopyd client"; | |
2270 | err_p = ERR_PTR(r); | |
2271 | goto bad_kcopyd_client; | |
2272 | } | |
2273 | ||
2274 | /* | |
2275 | * Create singlethreaded workqueue that will service all devices | |
2276 | * that use this metadata. | |
2277 | */ | |
2278 | pool->wq = alloc_ordered_workqueue("dm-" DM_MSG_PREFIX, WQ_MEM_RECLAIM); | |
2279 | if (!pool->wq) { | |
2280 | *error = "Error creating pool's workqueue"; | |
2281 | err_p = ERR_PTR(-ENOMEM); | |
2282 | goto bad_wq; | |
2283 | } | |
2284 | ||
7d327fe0 | 2285 | throttle_init(&pool->throttle); |
991d9fa0 | 2286 | INIT_WORK(&pool->worker, do_worker); |
905e51b3 | 2287 | INIT_DELAYED_WORK(&pool->waker, do_waker); |
85ad643b | 2288 | INIT_DELAYED_WORK(&pool->no_space_timeout, do_no_space_timeout); |
991d9fa0 | 2289 | spin_lock_init(&pool->lock); |
991d9fa0 JT |
2290 | bio_list_init(&pool->deferred_flush_bios); |
2291 | INIT_LIST_HEAD(&pool->prepared_mappings); | |
104655fd | 2292 | INIT_LIST_HEAD(&pool->prepared_discards); |
c140e1c4 | 2293 | INIT_LIST_HEAD(&pool->active_thins); |
88a6621b | 2294 | pool->low_water_triggered = false; |
44feb387 MS |
2295 | |
2296 | pool->shared_read_ds = dm_deferred_set_create(); | |
2297 | if (!pool->shared_read_ds) { | |
2298 | *error = "Error creating pool's shared read deferred set"; | |
2299 | err_p = ERR_PTR(-ENOMEM); | |
2300 | goto bad_shared_read_ds; | |
2301 | } | |
2302 | ||
2303 | pool->all_io_ds = dm_deferred_set_create(); | |
2304 | if (!pool->all_io_ds) { | |
2305 | *error = "Error creating pool's all io deferred set"; | |
2306 | err_p = ERR_PTR(-ENOMEM); | |
2307 | goto bad_all_io_ds; | |
2308 | } | |
991d9fa0 JT |
2309 | |
2310 | pool->next_mapping = NULL; | |
a24c2569 MS |
2311 | pool->mapping_pool = mempool_create_slab_pool(MAPPING_POOL_SIZE, |
2312 | _new_mapping_cache); | |
991d9fa0 JT |
2313 | if (!pool->mapping_pool) { |
2314 | *error = "Error creating pool's mapping mempool"; | |
2315 | err_p = ERR_PTR(-ENOMEM); | |
2316 | goto bad_mapping_pool; | |
2317 | } | |
2318 | ||
991d9fa0 | 2319 | pool->ref_count = 1; |
905e51b3 | 2320 | pool->last_commit_jiffies = jiffies; |
991d9fa0 JT |
2321 | pool->pool_md = pool_md; |
2322 | pool->md_dev = metadata_dev; | |
2323 | __pool_table_insert(pool); | |
2324 | ||
2325 | return pool; | |
2326 | ||
991d9fa0 | 2327 | bad_mapping_pool: |
44feb387 MS |
2328 | dm_deferred_set_destroy(pool->all_io_ds); |
2329 | bad_all_io_ds: | |
2330 | dm_deferred_set_destroy(pool->shared_read_ds); | |
2331 | bad_shared_read_ds: | |
991d9fa0 JT |
2332 | destroy_workqueue(pool->wq); |
2333 | bad_wq: | |
2334 | dm_kcopyd_client_destroy(pool->copier); | |
2335 | bad_kcopyd_client: | |
44feb387 | 2336 | dm_bio_prison_destroy(pool->prison); |
991d9fa0 JT |
2337 | bad_prison: |
2338 | kfree(pool); | |
2339 | bad_pool: | |
2340 | if (dm_pool_metadata_close(pmd)) | |
2341 | DMWARN("%s: dm_pool_metadata_close() failed.", __func__); | |
2342 | ||
2343 | return err_p; | |
2344 | } | |
2345 | ||
2346 | static void __pool_inc(struct pool *pool) | |
2347 | { | |
2348 | BUG_ON(!mutex_is_locked(&dm_thin_pool_table.mutex)); | |
2349 | pool->ref_count++; | |
2350 | } | |
2351 | ||
2352 | static void __pool_dec(struct pool *pool) | |
2353 | { | |
2354 | BUG_ON(!mutex_is_locked(&dm_thin_pool_table.mutex)); | |
2355 | BUG_ON(!pool->ref_count); | |
2356 | if (!--pool->ref_count) | |
2357 | __pool_destroy(pool); | |
2358 | } | |
2359 | ||
2360 | static struct pool *__pool_find(struct mapped_device *pool_md, | |
2361 | struct block_device *metadata_dev, | |
e49e5829 JT |
2362 | unsigned long block_size, int read_only, |
2363 | char **error, int *created) | |
991d9fa0 JT |
2364 | { |
2365 | struct pool *pool = __pool_table_lookup_metadata_dev(metadata_dev); | |
2366 | ||
2367 | if (pool) { | |
f09996c9 MS |
2368 | if (pool->pool_md != pool_md) { |
2369 | *error = "metadata device already in use by a pool"; | |
991d9fa0 | 2370 | return ERR_PTR(-EBUSY); |
f09996c9 | 2371 | } |
991d9fa0 JT |
2372 | __pool_inc(pool); |
2373 | ||
2374 | } else { | |
2375 | pool = __pool_table_lookup(pool_md); | |
2376 | if (pool) { | |
f09996c9 MS |
2377 | if (pool->md_dev != metadata_dev) { |
2378 | *error = "different pool cannot replace a pool"; | |
991d9fa0 | 2379 | return ERR_PTR(-EINVAL); |
f09996c9 | 2380 | } |
991d9fa0 JT |
2381 | __pool_inc(pool); |
2382 | ||
67e2e2b2 | 2383 | } else { |
e49e5829 | 2384 | pool = pool_create(pool_md, metadata_dev, block_size, read_only, error); |
67e2e2b2 JT |
2385 | *created = 1; |
2386 | } | |
991d9fa0 JT |
2387 | } |
2388 | ||
2389 | return pool; | |
2390 | } | |
2391 | ||
2392 | /*---------------------------------------------------------------- | |
2393 | * Pool target methods | |
2394 | *--------------------------------------------------------------*/ | |
2395 | static void pool_dtr(struct dm_target *ti) | |
2396 | { | |
2397 | struct pool_c *pt = ti->private; | |
2398 | ||
2399 | mutex_lock(&dm_thin_pool_table.mutex); | |
2400 | ||
2401 | unbind_control_target(pt->pool, ti); | |
2402 | __pool_dec(pt->pool); | |
2403 | dm_put_device(ti, pt->metadata_dev); | |
2404 | dm_put_device(ti, pt->data_dev); | |
2405 | kfree(pt); | |
2406 | ||
2407 | mutex_unlock(&dm_thin_pool_table.mutex); | |
2408 | } | |
2409 | ||
991d9fa0 JT |
2410 | static int parse_pool_features(struct dm_arg_set *as, struct pool_features *pf, |
2411 | struct dm_target *ti) | |
2412 | { | |
2413 | int r; | |
2414 | unsigned argc; | |
2415 | const char *arg_name; | |
2416 | ||
2417 | static struct dm_arg _args[] = { | |
74aa45c3 | 2418 | {0, 4, "Invalid number of pool feature arguments"}, |
991d9fa0 JT |
2419 | }; |
2420 | ||
2421 | /* | |
2422 | * No feature arguments supplied. | |
2423 | */ | |
2424 | if (!as->argc) | |
2425 | return 0; | |
2426 | ||
2427 | r = dm_read_arg_group(_args, as, &argc, &ti->error); | |
2428 | if (r) | |
2429 | return -EINVAL; | |
2430 | ||
2431 | while (argc && !r) { | |
2432 | arg_name = dm_shift_arg(as); | |
2433 | argc--; | |
2434 | ||
e49e5829 | 2435 | if (!strcasecmp(arg_name, "skip_block_zeroing")) |
9bc142dd | 2436 | pf->zero_new_blocks = false; |
e49e5829 JT |
2437 | |
2438 | else if (!strcasecmp(arg_name, "ignore_discard")) | |
9bc142dd | 2439 | pf->discard_enabled = false; |
e49e5829 JT |
2440 | |
2441 | else if (!strcasecmp(arg_name, "no_discard_passdown")) | |
9bc142dd | 2442 | pf->discard_passdown = false; |
991d9fa0 | 2443 | |
e49e5829 JT |
2444 | else if (!strcasecmp(arg_name, "read_only")) |
2445 | pf->mode = PM_READ_ONLY; | |
2446 | ||
787a996c MS |
2447 | else if (!strcasecmp(arg_name, "error_if_no_space")) |
2448 | pf->error_if_no_space = true; | |
2449 | ||
e49e5829 JT |
2450 | else { |
2451 | ti->error = "Unrecognised pool feature requested"; | |
2452 | r = -EINVAL; | |
2453 | break; | |
2454 | } | |
991d9fa0 JT |
2455 | } |
2456 | ||
2457 | return r; | |
2458 | } | |
2459 | ||
ac8c3f3d JT |
2460 | static void metadata_low_callback(void *context) |
2461 | { | |
2462 | struct pool *pool = context; | |
2463 | ||
2464 | DMWARN("%s: reached low water mark for metadata device: sending event.", | |
2465 | dm_device_name(pool->pool_md)); | |
2466 | ||
2467 | dm_table_event(pool->ti->table); | |
2468 | } | |
2469 | ||
7d48935e MS |
2470 | static sector_t get_dev_size(struct block_device *bdev) |
2471 | { | |
2472 | return i_size_read(bdev->bd_inode) >> SECTOR_SHIFT; | |
2473 | } | |
2474 | ||
2475 | static void warn_if_metadata_device_too_big(struct block_device *bdev) | |
b17446df | 2476 | { |
7d48935e | 2477 | sector_t metadata_dev_size = get_dev_size(bdev); |
b17446df JT |
2478 | char buffer[BDEVNAME_SIZE]; |
2479 | ||
7d48935e | 2480 | if (metadata_dev_size > THIN_METADATA_MAX_SECTORS_WARNING) |
b17446df JT |
2481 | DMWARN("Metadata device %s is larger than %u sectors: excess space will not be used.", |
2482 | bdevname(bdev, buffer), THIN_METADATA_MAX_SECTORS); | |
7d48935e MS |
2483 | } |
2484 | ||
2485 | static sector_t get_metadata_dev_size(struct block_device *bdev) | |
2486 | { | |
2487 | sector_t metadata_dev_size = get_dev_size(bdev); | |
2488 | ||
2489 | if (metadata_dev_size > THIN_METADATA_MAX_SECTORS) | |
2490 | metadata_dev_size = THIN_METADATA_MAX_SECTORS; | |
b17446df JT |
2491 | |
2492 | return metadata_dev_size; | |
2493 | } | |
2494 | ||
24347e95 JT |
2495 | static dm_block_t get_metadata_dev_size_in_blocks(struct block_device *bdev) |
2496 | { | |
2497 | sector_t metadata_dev_size = get_metadata_dev_size(bdev); | |
2498 | ||
7d48935e | 2499 | sector_div(metadata_dev_size, THIN_METADATA_BLOCK_SIZE); |
24347e95 JT |
2500 | |
2501 | return metadata_dev_size; | |
2502 | } | |
2503 | ||
ac8c3f3d JT |
2504 | /* |
2505 | * When a metadata threshold is crossed a dm event is triggered, and | |
2506 | * userland should respond by growing the metadata device. We could let | |
2507 | * userland set the threshold, like we do with the data threshold, but I'm | |
2508 | * not sure they know enough to do this well. | |
2509 | */ | |
2510 | static dm_block_t calc_metadata_threshold(struct pool_c *pt) | |
2511 | { | |
2512 | /* | |
2513 | * 4M is ample for all ops with the possible exception of thin | |
2514 | * device deletion which is harmless if it fails (just retry the | |
2515 | * delete after you've grown the device). | |
2516 | */ | |
2517 | dm_block_t quarter = get_metadata_dev_size_in_blocks(pt->metadata_dev->bdev) / 4; | |
2518 | return min((dm_block_t)1024ULL /* 4M */, quarter); | |
2519 | } | |
2520 | ||
991d9fa0 JT |
2521 | /* |
2522 | * thin-pool <metadata dev> <data dev> | |
2523 | * <data block size (sectors)> | |
2524 | * <low water mark (blocks)> | |
2525 | * [<#feature args> [<arg>]*] | |
2526 | * | |
2527 | * Optional feature arguments are: | |
2528 | * skip_block_zeroing: skips the zeroing of newly-provisioned blocks. | |
67e2e2b2 JT |
2529 | * ignore_discard: disable discard |
2530 | * no_discard_passdown: don't pass discards down to the data device | |
787a996c MS |
2531 | * read_only: Don't allow any changes to be made to the pool metadata. |
2532 | * error_if_no_space: error IOs, instead of queueing, if no space. | |
991d9fa0 JT |
2533 | */ |
2534 | static int pool_ctr(struct dm_target *ti, unsigned argc, char **argv) | |
2535 | { | |
67e2e2b2 | 2536 | int r, pool_created = 0; |
991d9fa0 JT |
2537 | struct pool_c *pt; |
2538 | struct pool *pool; | |
2539 | struct pool_features pf; | |
2540 | struct dm_arg_set as; | |
2541 | struct dm_dev *data_dev; | |
2542 | unsigned long block_size; | |
2543 | dm_block_t low_water_blocks; | |
2544 | struct dm_dev *metadata_dev; | |
5d0db96d | 2545 | fmode_t metadata_mode; |
991d9fa0 JT |
2546 | |
2547 | /* | |
2548 | * FIXME Remove validation from scope of lock. | |
2549 | */ | |
2550 | mutex_lock(&dm_thin_pool_table.mutex); | |
2551 | ||
2552 | if (argc < 4) { | |
2553 | ti->error = "Invalid argument count"; | |
2554 | r = -EINVAL; | |
2555 | goto out_unlock; | |
2556 | } | |
5d0db96d | 2557 | |
991d9fa0 JT |
2558 | as.argc = argc; |
2559 | as.argv = argv; | |
2560 | ||
5d0db96d JT |
2561 | /* |
2562 | * Set default pool features. | |
2563 | */ | |
2564 | pool_features_init(&pf); | |
2565 | ||
2566 | dm_consume_args(&as, 4); | |
2567 | r = parse_pool_features(&as, &pf, ti); | |
2568 | if (r) | |
2569 | goto out_unlock; | |
2570 | ||
2571 | metadata_mode = FMODE_READ | ((pf.mode == PM_READ_ONLY) ? 0 : FMODE_WRITE); | |
2572 | r = dm_get_device(ti, argv[0], metadata_mode, &metadata_dev); | |
991d9fa0 JT |
2573 | if (r) { |
2574 | ti->error = "Error opening metadata block device"; | |
2575 | goto out_unlock; | |
2576 | } | |
7d48935e | 2577 | warn_if_metadata_device_too_big(metadata_dev->bdev); |
991d9fa0 JT |
2578 | |
2579 | r = dm_get_device(ti, argv[1], FMODE_READ | FMODE_WRITE, &data_dev); | |
2580 | if (r) { | |
2581 | ti->error = "Error getting data device"; | |
2582 | goto out_metadata; | |
2583 | } | |
2584 | ||
2585 | if (kstrtoul(argv[2], 10, &block_size) || !block_size || | |
2586 | block_size < DATA_DEV_BLOCK_SIZE_MIN_SECTORS || | |
2587 | block_size > DATA_DEV_BLOCK_SIZE_MAX_SECTORS || | |
55f2b8bd | 2588 | block_size & (DATA_DEV_BLOCK_SIZE_MIN_SECTORS - 1)) { |
991d9fa0 JT |
2589 | ti->error = "Invalid block size"; |
2590 | r = -EINVAL; | |
2591 | goto out; | |
2592 | } | |
2593 | ||
2594 | if (kstrtoull(argv[3], 10, (unsigned long long *)&low_water_blocks)) { | |
2595 | ti->error = "Invalid low water mark"; | |
2596 | r = -EINVAL; | |
2597 | goto out; | |
2598 | } | |
2599 | ||
991d9fa0 JT |
2600 | pt = kzalloc(sizeof(*pt), GFP_KERNEL); |
2601 | if (!pt) { | |
2602 | r = -ENOMEM; | |
2603 | goto out; | |
2604 | } | |
2605 | ||
2606 | pool = __pool_find(dm_table_get_md(ti->table), metadata_dev->bdev, | |
e49e5829 | 2607 | block_size, pf.mode == PM_READ_ONLY, &ti->error, &pool_created); |
991d9fa0 JT |
2608 | if (IS_ERR(pool)) { |
2609 | r = PTR_ERR(pool); | |
2610 | goto out_free_pt; | |
2611 | } | |
2612 | ||
67e2e2b2 JT |
2613 | /* |
2614 | * 'pool_created' reflects whether this is the first table load. | |
2615 | * Top level discard support is not allowed to be changed after | |
2616 | * initial load. This would require a pool reload to trigger thin | |
2617 | * device changes. | |
2618 | */ | |
2619 | if (!pool_created && pf.discard_enabled != pool->pf.discard_enabled) { | |
2620 | ti->error = "Discard support cannot be disabled once enabled"; | |
2621 | r = -EINVAL; | |
2622 | goto out_flags_changed; | |
2623 | } | |
2624 | ||
991d9fa0 JT |
2625 | pt->pool = pool; |
2626 | pt->ti = ti; | |
2627 | pt->metadata_dev = metadata_dev; | |
2628 | pt->data_dev = data_dev; | |
2629 | pt->low_water_blocks = low_water_blocks; | |
0424caa1 | 2630 | pt->adjusted_pf = pt->requested_pf = pf; |
55a62eef | 2631 | ti->num_flush_bios = 1; |
9bc142dd | 2632 | |
67e2e2b2 JT |
2633 | /* |
2634 | * Only need to enable discards if the pool should pass | |
2635 | * them down to the data device. The thin device's discard | |
2636 | * processing will cause mappings to be removed from the btree. | |
2637 | */ | |
b60ab990 | 2638 | ti->discard_zeroes_data_unsupported = true; |
67e2e2b2 | 2639 | if (pf.discard_enabled && pf.discard_passdown) { |
55a62eef | 2640 | ti->num_discard_bios = 1; |
9bc142dd | 2641 | |
67e2e2b2 JT |
2642 | /* |
2643 | * Setting 'discards_supported' circumvents the normal | |
2644 | * stacking of discard limits (this keeps the pool and | |
2645 | * thin devices' discard limits consistent). | |
2646 | */ | |
0ac55489 | 2647 | ti->discards_supported = true; |
67e2e2b2 | 2648 | } |
991d9fa0 JT |
2649 | ti->private = pt; |
2650 | ||
ac8c3f3d JT |
2651 | r = dm_pool_register_metadata_threshold(pt->pool->pmd, |
2652 | calc_metadata_threshold(pt), | |
2653 | metadata_low_callback, | |
2654 | pool); | |
2655 | if (r) | |
2656 | goto out_free_pt; | |
2657 | ||
991d9fa0 JT |
2658 | pt->callbacks.congested_fn = pool_is_congested; |
2659 | dm_table_add_target_callbacks(ti->table, &pt->callbacks); | |
2660 | ||
2661 | mutex_unlock(&dm_thin_pool_table.mutex); | |
2662 | ||
2663 | return 0; | |
2664 | ||
67e2e2b2 JT |
2665 | out_flags_changed: |
2666 | __pool_dec(pool); | |
991d9fa0 JT |
2667 | out_free_pt: |
2668 | kfree(pt); | |
2669 | out: | |
2670 | dm_put_device(ti, data_dev); | |
2671 | out_metadata: | |
2672 | dm_put_device(ti, metadata_dev); | |
2673 | out_unlock: | |
2674 | mutex_unlock(&dm_thin_pool_table.mutex); | |
2675 | ||
2676 | return r; | |
2677 | } | |
2678 | ||
7de3ee57 | 2679 | static int pool_map(struct dm_target *ti, struct bio *bio) |
991d9fa0 JT |
2680 | { |
2681 | int r; | |
2682 | struct pool_c *pt = ti->private; | |
2683 | struct pool *pool = pt->pool; | |
2684 | unsigned long flags; | |
2685 | ||
2686 | /* | |
2687 | * As this is a singleton target, ti->begin is always zero. | |
2688 | */ | |
2689 | spin_lock_irqsave(&pool->lock, flags); | |
2690 | bio->bi_bdev = pt->data_dev->bdev; | |
2691 | r = DM_MAPIO_REMAPPED; | |
2692 | spin_unlock_irqrestore(&pool->lock, flags); | |
2693 | ||
2694 | return r; | |
2695 | } | |
2696 | ||
b17446df | 2697 | static int maybe_resize_data_dev(struct dm_target *ti, bool *need_commit) |
991d9fa0 JT |
2698 | { |
2699 | int r; | |
2700 | struct pool_c *pt = ti->private; | |
2701 | struct pool *pool = pt->pool; | |
55f2b8bd MS |
2702 | sector_t data_size = ti->len; |
2703 | dm_block_t sb_data_size; | |
991d9fa0 | 2704 | |
b17446df | 2705 | *need_commit = false; |
991d9fa0 | 2706 | |
55f2b8bd MS |
2707 | (void) sector_div(data_size, pool->sectors_per_block); |
2708 | ||
991d9fa0 JT |
2709 | r = dm_pool_get_data_dev_size(pool->pmd, &sb_data_size); |
2710 | if (r) { | |
4fa5971a MS |
2711 | DMERR("%s: failed to retrieve data device size", |
2712 | dm_device_name(pool->pool_md)); | |
991d9fa0 JT |
2713 | return r; |
2714 | } | |
2715 | ||
2716 | if (data_size < sb_data_size) { | |
4fa5971a MS |
2717 | DMERR("%s: pool target (%llu blocks) too small: expected %llu", |
2718 | dm_device_name(pool->pool_md), | |
55f2b8bd | 2719 | (unsigned long long)data_size, sb_data_size); |
991d9fa0 JT |
2720 | return -EINVAL; |
2721 | ||
2722 | } else if (data_size > sb_data_size) { | |
07f2b6e0 MS |
2723 | if (dm_pool_metadata_needs_check(pool->pmd)) { |
2724 | DMERR("%s: unable to grow the data device until repaired.", | |
2725 | dm_device_name(pool->pool_md)); | |
2726 | return 0; | |
2727 | } | |
2728 | ||
6f7f51d4 MS |
2729 | if (sb_data_size) |
2730 | DMINFO("%s: growing the data device from %llu to %llu blocks", | |
2731 | dm_device_name(pool->pool_md), | |
2732 | sb_data_size, (unsigned long long)data_size); | |
991d9fa0 JT |
2733 | r = dm_pool_resize_data_dev(pool->pmd, data_size); |
2734 | if (r) { | |
b5330655 | 2735 | metadata_operation_failed(pool, "dm_pool_resize_data_dev", r); |
991d9fa0 JT |
2736 | return r; |
2737 | } | |
2738 | ||
b17446df | 2739 | *need_commit = true; |
991d9fa0 JT |
2740 | } |
2741 | ||
2742 | return 0; | |
2743 | } | |
2744 | ||
24347e95 JT |
2745 | static int maybe_resize_metadata_dev(struct dm_target *ti, bool *need_commit) |
2746 | { | |
2747 | int r; | |
2748 | struct pool_c *pt = ti->private; | |
2749 | struct pool *pool = pt->pool; | |
2750 | dm_block_t metadata_dev_size, sb_metadata_dev_size; | |
2751 | ||
2752 | *need_commit = false; | |
2753 | ||
610bba8b | 2754 | metadata_dev_size = get_metadata_dev_size_in_blocks(pool->md_dev); |
24347e95 JT |
2755 | |
2756 | r = dm_pool_get_metadata_dev_size(pool->pmd, &sb_metadata_dev_size); | |
2757 | if (r) { | |
4fa5971a MS |
2758 | DMERR("%s: failed to retrieve metadata device size", |
2759 | dm_device_name(pool->pool_md)); | |
24347e95 JT |
2760 | return r; |
2761 | } | |
2762 | ||
2763 | if (metadata_dev_size < sb_metadata_dev_size) { | |
4fa5971a MS |
2764 | DMERR("%s: metadata device (%llu blocks) too small: expected %llu", |
2765 | dm_device_name(pool->pool_md), | |
24347e95 JT |
2766 | metadata_dev_size, sb_metadata_dev_size); |
2767 | return -EINVAL; | |
2768 | ||
2769 | } else if (metadata_dev_size > sb_metadata_dev_size) { | |
07f2b6e0 MS |
2770 | if (dm_pool_metadata_needs_check(pool->pmd)) { |
2771 | DMERR("%s: unable to grow the metadata device until repaired.", | |
2772 | dm_device_name(pool->pool_md)); | |
2773 | return 0; | |
2774 | } | |
2775 | ||
7d48935e | 2776 | warn_if_metadata_device_too_big(pool->md_dev); |
6f7f51d4 MS |
2777 | DMINFO("%s: growing the metadata device from %llu to %llu blocks", |
2778 | dm_device_name(pool->pool_md), | |
2779 | sb_metadata_dev_size, metadata_dev_size); | |
24347e95 JT |
2780 | r = dm_pool_resize_metadata_dev(pool->pmd, metadata_dev_size); |
2781 | if (r) { | |
b5330655 | 2782 | metadata_operation_failed(pool, "dm_pool_resize_metadata_dev", r); |
24347e95 JT |
2783 | return r; |
2784 | } | |
2785 | ||
2786 | *need_commit = true; | |
2787 | } | |
2788 | ||
2789 | return 0; | |
2790 | } | |
2791 | ||
b17446df JT |
2792 | /* |
2793 | * Retrieves the number of blocks of the data device from | |
2794 | * the superblock and compares it to the actual device size, | |
2795 | * thus resizing the data device in case it has grown. | |
2796 | * | |
2797 | * This both copes with opening preallocated data devices in the ctr | |
2798 | * being followed by a resume | |
2799 | * -and- | |
2800 | * calling the resume method individually after userspace has | |
2801 | * grown the data device in reaction to a table event. | |
2802 | */ | |
2803 | static int pool_preresume(struct dm_target *ti) | |
2804 | { | |
2805 | int r; | |
24347e95 | 2806 | bool need_commit1, need_commit2; |
b17446df JT |
2807 | struct pool_c *pt = ti->private; |
2808 | struct pool *pool = pt->pool; | |
2809 | ||
2810 | /* | |
2811 | * Take control of the pool object. | |
2812 | */ | |
2813 | r = bind_control_target(pool, ti); | |
2814 | if (r) | |
2815 | return r; | |
2816 | ||
2817 | r = maybe_resize_data_dev(ti, &need_commit1); | |
2818 | if (r) | |
2819 | return r; | |
2820 | ||
24347e95 JT |
2821 | r = maybe_resize_metadata_dev(ti, &need_commit2); |
2822 | if (r) | |
2823 | return r; | |
2824 | ||
2825 | if (need_commit1 || need_commit2) | |
020cc3b5 | 2826 | (void) commit(pool); |
b17446df JT |
2827 | |
2828 | return 0; | |
2829 | } | |
2830 | ||
991d9fa0 JT |
2831 | static void pool_resume(struct dm_target *ti) |
2832 | { | |
2833 | struct pool_c *pt = ti->private; | |
2834 | struct pool *pool = pt->pool; | |
2835 | unsigned long flags; | |
2836 | ||
2837 | spin_lock_irqsave(&pool->lock, flags); | |
88a6621b | 2838 | pool->low_water_triggered = false; |
991d9fa0 | 2839 | spin_unlock_irqrestore(&pool->lock, flags); |
c140e1c4 | 2840 | requeue_bios(pool); |
991d9fa0 | 2841 | |
905e51b3 | 2842 | do_waker(&pool->waker.work); |
991d9fa0 JT |
2843 | } |
2844 | ||
2845 | static void pool_postsuspend(struct dm_target *ti) | |
2846 | { | |
991d9fa0 JT |
2847 | struct pool_c *pt = ti->private; |
2848 | struct pool *pool = pt->pool; | |
2849 | ||
905e51b3 | 2850 | cancel_delayed_work(&pool->waker); |
85ad643b | 2851 | cancel_delayed_work(&pool->no_space_timeout); |
991d9fa0 | 2852 | flush_workqueue(pool->wq); |
020cc3b5 | 2853 | (void) commit(pool); |
991d9fa0 JT |
2854 | } |
2855 | ||
2856 | static int check_arg_count(unsigned argc, unsigned args_required) | |
2857 | { | |
2858 | if (argc != args_required) { | |
2859 | DMWARN("Message received with %u arguments instead of %u.", | |
2860 | argc, args_required); | |
2861 | return -EINVAL; | |
2862 | } | |
2863 | ||
2864 | return 0; | |
2865 | } | |
2866 | ||
2867 | static int read_dev_id(char *arg, dm_thin_id *dev_id, int warning) | |
2868 | { | |
2869 | if (!kstrtoull(arg, 10, (unsigned long long *)dev_id) && | |
2870 | *dev_id <= MAX_DEV_ID) | |
2871 | return 0; | |
2872 | ||
2873 | if (warning) | |
2874 | DMWARN("Message received with invalid device id: %s", arg); | |
2875 | ||
2876 | return -EINVAL; | |
2877 | } | |
2878 | ||
2879 | static int process_create_thin_mesg(unsigned argc, char **argv, struct pool *pool) | |
2880 | { | |
2881 | dm_thin_id dev_id; | |
2882 | int r; | |
2883 | ||
2884 | r = check_arg_count(argc, 2); | |
2885 | if (r) | |
2886 | return r; | |
2887 | ||
2888 | r = read_dev_id(argv[1], &dev_id, 1); | |
2889 | if (r) | |
2890 | return r; | |
2891 | ||
2892 | r = dm_pool_create_thin(pool->pmd, dev_id); | |
2893 | if (r) { | |
2894 | DMWARN("Creation of new thinly-provisioned device with id %s failed.", | |
2895 | argv[1]); | |
2896 | return r; | |
2897 | } | |
2898 | ||
2899 | return 0; | |
2900 | } | |
2901 | ||
2902 | static int process_create_snap_mesg(unsigned argc, char **argv, struct pool *pool) | |
2903 | { | |
2904 | dm_thin_id dev_id; | |
2905 | dm_thin_id origin_dev_id; | |
2906 | int r; | |
2907 | ||
2908 | r = check_arg_count(argc, 3); | |
2909 | if (r) | |
2910 | return r; | |
2911 | ||
2912 | r = read_dev_id(argv[1], &dev_id, 1); | |
2913 | if (r) | |
2914 | return r; | |
2915 | ||
2916 | r = read_dev_id(argv[2], &origin_dev_id, 1); | |
2917 | if (r) | |
2918 | return r; | |
2919 | ||
2920 | r = dm_pool_create_snap(pool->pmd, dev_id, origin_dev_id); | |
2921 | if (r) { | |
2922 | DMWARN("Creation of new snapshot %s of device %s failed.", | |
2923 | argv[1], argv[2]); | |
2924 | return r; | |
2925 | } | |
2926 | ||
2927 | return 0; | |
2928 | } | |
2929 | ||
2930 | static int process_delete_mesg(unsigned argc, char **argv, struct pool *pool) | |
2931 | { | |
2932 | dm_thin_id dev_id; | |
2933 | int r; | |
2934 | ||
2935 | r = check_arg_count(argc, 2); | |
2936 | if (r) | |
2937 | return r; | |
2938 | ||
2939 | r = read_dev_id(argv[1], &dev_id, 1); | |
2940 | if (r) | |
2941 | return r; | |
2942 | ||
2943 | r = dm_pool_delete_thin_device(pool->pmd, dev_id); | |
2944 | if (r) | |
2945 | DMWARN("Deletion of thin device %s failed.", argv[1]); | |
2946 | ||
2947 | return r; | |
2948 | } | |
2949 | ||
2950 | static int process_set_transaction_id_mesg(unsigned argc, char **argv, struct pool *pool) | |
2951 | { | |
2952 | dm_thin_id old_id, new_id; | |
2953 | int r; | |
2954 | ||
2955 | r = check_arg_count(argc, 3); | |
2956 | if (r) | |
2957 | return r; | |
2958 | ||
2959 | if (kstrtoull(argv[1], 10, (unsigned long long *)&old_id)) { | |
2960 | DMWARN("set_transaction_id message: Unrecognised id %s.", argv[1]); | |
2961 | return -EINVAL; | |
2962 | } | |
2963 | ||
2964 | if (kstrtoull(argv[2], 10, (unsigned long long *)&new_id)) { | |
2965 | DMWARN("set_transaction_id message: Unrecognised new id %s.", argv[2]); | |
2966 | return -EINVAL; | |
2967 | } | |
2968 | ||
2969 | r = dm_pool_set_metadata_transaction_id(pool->pmd, old_id, new_id); | |
2970 | if (r) { | |
2971 | DMWARN("Failed to change transaction id from %s to %s.", | |
2972 | argv[1], argv[2]); | |
2973 | return r; | |
2974 | } | |
2975 | ||
2976 | return 0; | |
2977 | } | |
2978 | ||
cc8394d8 JT |
2979 | static int process_reserve_metadata_snap_mesg(unsigned argc, char **argv, struct pool *pool) |
2980 | { | |
2981 | int r; | |
2982 | ||
2983 | r = check_arg_count(argc, 1); | |
2984 | if (r) | |
2985 | return r; | |
2986 | ||
020cc3b5 | 2987 | (void) commit(pool); |
0d200aef | 2988 | |
cc8394d8 JT |
2989 | r = dm_pool_reserve_metadata_snap(pool->pmd); |
2990 | if (r) | |
2991 | DMWARN("reserve_metadata_snap message failed."); | |
2992 | ||
2993 | return r; | |
2994 | } | |
2995 | ||
2996 | static int process_release_metadata_snap_mesg(unsigned argc, char **argv, struct pool *pool) | |
2997 | { | |
2998 | int r; | |
2999 | ||
3000 | r = check_arg_count(argc, 1); | |
3001 | if (r) | |
3002 | return r; | |
3003 | ||
3004 | r = dm_pool_release_metadata_snap(pool->pmd); | |
3005 | if (r) | |
3006 | DMWARN("release_metadata_snap message failed."); | |
3007 | ||
3008 | return r; | |
3009 | } | |
3010 | ||
991d9fa0 JT |
3011 | /* |
3012 | * Messages supported: | |
3013 | * create_thin <dev_id> | |
3014 | * create_snap <dev_id> <origin_id> | |
3015 | * delete <dev_id> | |
3016 | * trim <dev_id> <new_size_in_sectors> | |
3017 | * set_transaction_id <current_trans_id> <new_trans_id> | |
cc8394d8 JT |
3018 | * reserve_metadata_snap |
3019 | * release_metadata_snap | |
991d9fa0 JT |
3020 | */ |
3021 | static int pool_message(struct dm_target *ti, unsigned argc, char **argv) | |
3022 | { | |
3023 | int r = -EINVAL; | |
3024 | struct pool_c *pt = ti->private; | |
3025 | struct pool *pool = pt->pool; | |
3026 | ||
3027 | if (!strcasecmp(argv[0], "create_thin")) | |
3028 | r = process_create_thin_mesg(argc, argv, pool); | |
3029 | ||
3030 | else if (!strcasecmp(argv[0], "create_snap")) | |
3031 | r = process_create_snap_mesg(argc, argv, pool); | |
3032 | ||
3033 | else if (!strcasecmp(argv[0], "delete")) | |
3034 | r = process_delete_mesg(argc, argv, pool); | |
3035 | ||
3036 | else if (!strcasecmp(argv[0], "set_transaction_id")) | |
3037 | r = process_set_transaction_id_mesg(argc, argv, pool); | |
3038 | ||
cc8394d8 JT |
3039 | else if (!strcasecmp(argv[0], "reserve_metadata_snap")) |
3040 | r = process_reserve_metadata_snap_mesg(argc, argv, pool); | |
3041 | ||
3042 | else if (!strcasecmp(argv[0], "release_metadata_snap")) | |
3043 | r = process_release_metadata_snap_mesg(argc, argv, pool); | |
3044 | ||
991d9fa0 JT |
3045 | else |
3046 | DMWARN("Unrecognised thin pool target message received: %s", argv[0]); | |
3047 | ||
e49e5829 | 3048 | if (!r) |
020cc3b5 | 3049 | (void) commit(pool); |
991d9fa0 JT |
3050 | |
3051 | return r; | |
3052 | } | |
3053 | ||
e49e5829 JT |
3054 | static void emit_flags(struct pool_features *pf, char *result, |
3055 | unsigned sz, unsigned maxlen) | |
3056 | { | |
3057 | unsigned count = !pf->zero_new_blocks + !pf->discard_enabled + | |
787a996c MS |
3058 | !pf->discard_passdown + (pf->mode == PM_READ_ONLY) + |
3059 | pf->error_if_no_space; | |
e49e5829 JT |
3060 | DMEMIT("%u ", count); |
3061 | ||
3062 | if (!pf->zero_new_blocks) | |
3063 | DMEMIT("skip_block_zeroing "); | |
3064 | ||
3065 | if (!pf->discard_enabled) | |
3066 | DMEMIT("ignore_discard "); | |
3067 | ||
3068 | if (!pf->discard_passdown) | |
3069 | DMEMIT("no_discard_passdown "); | |
3070 | ||
3071 | if (pf->mode == PM_READ_ONLY) | |
3072 | DMEMIT("read_only "); | |
787a996c MS |
3073 | |
3074 | if (pf->error_if_no_space) | |
3075 | DMEMIT("error_if_no_space "); | |
e49e5829 JT |
3076 | } |
3077 | ||
991d9fa0 JT |
3078 | /* |
3079 | * Status line is: | |
3080 | * <transaction id> <used metadata sectors>/<total metadata sectors> | |
3081 | * <used data sectors>/<total data sectors> <held metadata root> | |
3082 | */ | |
fd7c092e MP |
3083 | static void pool_status(struct dm_target *ti, status_type_t type, |
3084 | unsigned status_flags, char *result, unsigned maxlen) | |
991d9fa0 | 3085 | { |
e49e5829 | 3086 | int r; |
991d9fa0 JT |
3087 | unsigned sz = 0; |
3088 | uint64_t transaction_id; | |
3089 | dm_block_t nr_free_blocks_data; | |
3090 | dm_block_t nr_free_blocks_metadata; | |
3091 | dm_block_t nr_blocks_data; | |
3092 | dm_block_t nr_blocks_metadata; | |
3093 | dm_block_t held_root; | |
3094 | char buf[BDEVNAME_SIZE]; | |
3095 | char buf2[BDEVNAME_SIZE]; | |
3096 | struct pool_c *pt = ti->private; | |
3097 | struct pool *pool = pt->pool; | |
3098 | ||
3099 | switch (type) { | |
3100 | case STATUSTYPE_INFO: | |
e49e5829 JT |
3101 | if (get_pool_mode(pool) == PM_FAIL) { |
3102 | DMEMIT("Fail"); | |
3103 | break; | |
3104 | } | |
3105 | ||
1f4e0ff0 AK |
3106 | /* Commit to ensure statistics aren't out-of-date */ |
3107 | if (!(status_flags & DM_STATUS_NOFLUSH_FLAG) && !dm_suspended(ti)) | |
020cc3b5 | 3108 | (void) commit(pool); |
1f4e0ff0 | 3109 | |
fd7c092e MP |
3110 | r = dm_pool_get_metadata_transaction_id(pool->pmd, &transaction_id); |
3111 | if (r) { | |
4fa5971a MS |
3112 | DMERR("%s: dm_pool_get_metadata_transaction_id returned %d", |
3113 | dm_device_name(pool->pool_md), r); | |
fd7c092e MP |
3114 | goto err; |
3115 | } | |
991d9fa0 | 3116 | |
fd7c092e MP |
3117 | r = dm_pool_get_free_metadata_block_count(pool->pmd, &nr_free_blocks_metadata); |
3118 | if (r) { | |
4fa5971a MS |
3119 | DMERR("%s: dm_pool_get_free_metadata_block_count returned %d", |
3120 | dm_device_name(pool->pool_md), r); | |
fd7c092e MP |
3121 | goto err; |
3122 | } | |
991d9fa0 JT |
3123 | |
3124 | r = dm_pool_get_metadata_dev_size(pool->pmd, &nr_blocks_metadata); | |
fd7c092e | 3125 | if (r) { |
4fa5971a MS |
3126 | DMERR("%s: dm_pool_get_metadata_dev_size returned %d", |
3127 | dm_device_name(pool->pool_md), r); | |
fd7c092e MP |
3128 | goto err; |
3129 | } | |
991d9fa0 | 3130 | |
fd7c092e MP |
3131 | r = dm_pool_get_free_block_count(pool->pmd, &nr_free_blocks_data); |
3132 | if (r) { | |
4fa5971a MS |
3133 | DMERR("%s: dm_pool_get_free_block_count returned %d", |
3134 | dm_device_name(pool->pool_md), r); | |
fd7c092e MP |
3135 | goto err; |
3136 | } | |
991d9fa0 JT |
3137 | |
3138 | r = dm_pool_get_data_dev_size(pool->pmd, &nr_blocks_data); | |
fd7c092e | 3139 | if (r) { |
4fa5971a MS |
3140 | DMERR("%s: dm_pool_get_data_dev_size returned %d", |
3141 | dm_device_name(pool->pool_md), r); | |
fd7c092e MP |
3142 | goto err; |
3143 | } | |
991d9fa0 | 3144 | |
cc8394d8 | 3145 | r = dm_pool_get_metadata_snap(pool->pmd, &held_root); |
fd7c092e | 3146 | if (r) { |
4fa5971a MS |
3147 | DMERR("%s: dm_pool_get_metadata_snap returned %d", |
3148 | dm_device_name(pool->pool_md), r); | |
fd7c092e MP |
3149 | goto err; |
3150 | } | |
991d9fa0 JT |
3151 | |
3152 | DMEMIT("%llu %llu/%llu %llu/%llu ", | |
3153 | (unsigned long long)transaction_id, | |
3154 | (unsigned long long)(nr_blocks_metadata - nr_free_blocks_metadata), | |
3155 | (unsigned long long)nr_blocks_metadata, | |
3156 | (unsigned long long)(nr_blocks_data - nr_free_blocks_data), | |
3157 | (unsigned long long)nr_blocks_data); | |
3158 | ||
3159 | if (held_root) | |
e49e5829 JT |
3160 | DMEMIT("%llu ", held_root); |
3161 | else | |
3162 | DMEMIT("- "); | |
3163 | ||
3e1a0699 JT |
3164 | if (pool->pf.mode == PM_OUT_OF_DATA_SPACE) |
3165 | DMEMIT("out_of_data_space "); | |
3166 | else if (pool->pf.mode == PM_READ_ONLY) | |
e49e5829 | 3167 | DMEMIT("ro "); |
991d9fa0 | 3168 | else |
e49e5829 JT |
3169 | DMEMIT("rw "); |
3170 | ||
018debea | 3171 | if (!pool->pf.discard_enabled) |
787a996c | 3172 | DMEMIT("ignore_discard "); |
018debea | 3173 | else if (pool->pf.discard_passdown) |
787a996c MS |
3174 | DMEMIT("discard_passdown "); |
3175 | else | |
3176 | DMEMIT("no_discard_passdown "); | |
3177 | ||
3178 | if (pool->pf.error_if_no_space) | |
3179 | DMEMIT("error_if_no_space "); | |
e49e5829 | 3180 | else |
787a996c | 3181 | DMEMIT("queue_if_no_space "); |
991d9fa0 JT |
3182 | |
3183 | break; | |
3184 | ||
3185 | case STATUSTYPE_TABLE: | |
3186 | DMEMIT("%s %s %lu %llu ", | |
3187 | format_dev_t(buf, pt->metadata_dev->bdev->bd_dev), | |
3188 | format_dev_t(buf2, pt->data_dev->bdev->bd_dev), | |
3189 | (unsigned long)pool->sectors_per_block, | |
3190 | (unsigned long long)pt->low_water_blocks); | |
0424caa1 | 3191 | emit_flags(&pt->requested_pf, result, sz, maxlen); |
991d9fa0 JT |
3192 | break; |
3193 | } | |
fd7c092e | 3194 | return; |
991d9fa0 | 3195 | |
fd7c092e MP |
3196 | err: |
3197 | DMEMIT("Error"); | |
991d9fa0 JT |
3198 | } |
3199 | ||
3200 | static int pool_iterate_devices(struct dm_target *ti, | |
3201 | iterate_devices_callout_fn fn, void *data) | |
3202 | { | |
3203 | struct pool_c *pt = ti->private; | |
3204 | ||
3205 | return fn(ti, pt->data_dev, 0, ti->len, data); | |
3206 | } | |
3207 | ||
3208 | static int pool_merge(struct dm_target *ti, struct bvec_merge_data *bvm, | |
3209 | struct bio_vec *biovec, int max_size) | |
3210 | { | |
3211 | struct pool_c *pt = ti->private; | |
3212 | struct request_queue *q = bdev_get_queue(pt->data_dev->bdev); | |
3213 | ||
3214 | if (!q->merge_bvec_fn) | |
3215 | return max_size; | |
3216 | ||
3217 | bvm->bi_bdev = pt->data_dev->bdev; | |
3218 | ||
3219 | return min(max_size, q->merge_bvec_fn(q, bvm, biovec)); | |
3220 | } | |
3221 | ||
0424caa1 | 3222 | static void set_discard_limits(struct pool_c *pt, struct queue_limits *limits) |
104655fd | 3223 | { |
0424caa1 MS |
3224 | struct pool *pool = pt->pool; |
3225 | struct queue_limits *data_limits; | |
3226 | ||
104655fd JT |
3227 | limits->max_discard_sectors = pool->sectors_per_block; |
3228 | ||
3229 | /* | |
0424caa1 | 3230 | * discard_granularity is just a hint, and not enforced. |
104655fd | 3231 | */ |
0424caa1 MS |
3232 | if (pt->adjusted_pf.discard_passdown) { |
3233 | data_limits = &bdev_get_queue(pt->data_dev->bdev)->limits; | |
09869de5 LC |
3234 | limits->discard_granularity = max(data_limits->discard_granularity, |
3235 | pool->sectors_per_block << SECTOR_SHIFT); | |
f13945d7 | 3236 | } else |
0424caa1 | 3237 | limits->discard_granularity = pool->sectors_per_block << SECTOR_SHIFT; |
104655fd JT |
3238 | } |
3239 | ||
991d9fa0 JT |
3240 | static void pool_io_hints(struct dm_target *ti, struct queue_limits *limits) |
3241 | { | |
3242 | struct pool_c *pt = ti->private; | |
3243 | struct pool *pool = pt->pool; | |
604ea906 MS |
3244 | sector_t io_opt_sectors = limits->io_opt >> SECTOR_SHIFT; |
3245 | ||
3246 | /* | |
3247 | * Adjust max_sectors_kb to highest possible power-of-2 | |
3248 | * factor of pool->sectors_per_block. | |
3249 | */ | |
3250 | if (limits->max_hw_sectors & (limits->max_hw_sectors - 1)) | |
3251 | limits->max_sectors = rounddown_pow_of_two(limits->max_hw_sectors); | |
3252 | else | |
3253 | limits->max_sectors = limits->max_hw_sectors; | |
3254 | ||
3255 | if (limits->max_sectors < pool->sectors_per_block) { | |
3256 | while (!is_factor(pool->sectors_per_block, limits->max_sectors)) { | |
3257 | if ((limits->max_sectors & (limits->max_sectors - 1)) == 0) | |
3258 | limits->max_sectors--; | |
3259 | limits->max_sectors = rounddown_pow_of_two(limits->max_sectors); | |
3260 | } | |
3261 | } else if (block_size_is_power_of_two(pool)) { | |
3262 | /* max_sectors_kb is >= power-of-2 thinp blocksize */ | |
3263 | while (!is_factor(limits->max_sectors, pool->sectors_per_block)) { | |
3264 | if ((limits->max_sectors & (limits->max_sectors - 1)) == 0) | |
3265 | limits->max_sectors--; | |
3266 | limits->max_sectors = rounddown_pow_of_two(limits->max_sectors); | |
3267 | } | |
3268 | } | |
991d9fa0 | 3269 | |
0cc67cd9 MS |
3270 | /* |
3271 | * If the system-determined stacked limits are compatible with the | |
3272 | * pool's blocksize (io_opt is a factor) do not override them. | |
3273 | */ | |
3274 | if (io_opt_sectors < pool->sectors_per_block || | |
604ea906 MS |
3275 | !is_factor(io_opt_sectors, pool->sectors_per_block)) { |
3276 | if (is_factor(pool->sectors_per_block, limits->max_sectors)) | |
3277 | blk_limits_io_min(limits, limits->max_sectors << SECTOR_SHIFT); | |
3278 | else | |
3279 | blk_limits_io_min(limits, pool->sectors_per_block << SECTOR_SHIFT); | |
0cc67cd9 MS |
3280 | blk_limits_io_opt(limits, pool->sectors_per_block << SECTOR_SHIFT); |
3281 | } | |
0424caa1 MS |
3282 | |
3283 | /* | |
3284 | * pt->adjusted_pf is a staging area for the actual features to use. | |
3285 | * They get transferred to the live pool in bind_control_target() | |
3286 | * called from pool_preresume(). | |
3287 | */ | |
b60ab990 MS |
3288 | if (!pt->adjusted_pf.discard_enabled) { |
3289 | /* | |
3290 | * Must explicitly disallow stacking discard limits otherwise the | |
3291 | * block layer will stack them if pool's data device has support. | |
3292 | * QUEUE_FLAG_DISCARD wouldn't be set but there is no way for the | |
3293 | * user to see that, so make sure to set all discard limits to 0. | |
3294 | */ | |
3295 | limits->discard_granularity = 0; | |
0424caa1 | 3296 | return; |
b60ab990 | 3297 | } |
0424caa1 MS |
3298 | |
3299 | disable_passdown_if_not_supported(pt); | |
3300 | ||
3301 | set_discard_limits(pt, limits); | |
991d9fa0 JT |
3302 | } |
3303 | ||
3304 | static struct target_type pool_target = { | |
3305 | .name = "thin-pool", | |
3306 | .features = DM_TARGET_SINGLETON | DM_TARGET_ALWAYS_WRITEABLE | | |
3307 | DM_TARGET_IMMUTABLE, | |
36f12aeb | 3308 | .version = {1, 14, 0}, |
991d9fa0 JT |
3309 | .module = THIS_MODULE, |
3310 | .ctr = pool_ctr, | |
3311 | .dtr = pool_dtr, | |
3312 | .map = pool_map, | |
3313 | .postsuspend = pool_postsuspend, | |
3314 | .preresume = pool_preresume, | |
3315 | .resume = pool_resume, | |
3316 | .message = pool_message, | |
3317 | .status = pool_status, | |
3318 | .merge = pool_merge, | |
3319 | .iterate_devices = pool_iterate_devices, | |
3320 | .io_hints = pool_io_hints, | |
3321 | }; | |
3322 | ||
3323 | /*---------------------------------------------------------------- | |
3324 | * Thin target methods | |
3325 | *--------------------------------------------------------------*/ | |
b10ebd34 JT |
3326 | static void thin_get(struct thin_c *tc) |
3327 | { | |
3328 | atomic_inc(&tc->refcount); | |
3329 | } | |
3330 | ||
3331 | static void thin_put(struct thin_c *tc) | |
3332 | { | |
3333 | if (atomic_dec_and_test(&tc->refcount)) | |
3334 | complete(&tc->can_destroy); | |
3335 | } | |
3336 | ||
991d9fa0 JT |
3337 | static void thin_dtr(struct dm_target *ti) |
3338 | { | |
3339 | struct thin_c *tc = ti->private; | |
c140e1c4 MS |
3340 | unsigned long flags; |
3341 | ||
b10ebd34 JT |
3342 | thin_put(tc); |
3343 | wait_for_completion(&tc->can_destroy); | |
3344 | ||
c140e1c4 MS |
3345 | spin_lock_irqsave(&tc->pool->lock, flags); |
3346 | list_del_rcu(&tc->list); | |
3347 | spin_unlock_irqrestore(&tc->pool->lock, flags); | |
3348 | synchronize_rcu(); | |
991d9fa0 JT |
3349 | |
3350 | mutex_lock(&dm_thin_pool_table.mutex); | |
3351 | ||
3352 | __pool_dec(tc->pool); | |
3353 | dm_pool_close_thin_device(tc->td); | |
3354 | dm_put_device(ti, tc->pool_dev); | |
2dd9c257 JT |
3355 | if (tc->origin_dev) |
3356 | dm_put_device(ti, tc->origin_dev); | |
991d9fa0 JT |
3357 | kfree(tc); |
3358 | ||
3359 | mutex_unlock(&dm_thin_pool_table.mutex); | |
3360 | } | |
3361 | ||
3362 | /* | |
3363 | * Thin target parameters: | |
3364 | * | |
2dd9c257 | 3365 | * <pool_dev> <dev_id> [origin_dev] |
991d9fa0 JT |
3366 | * |
3367 | * pool_dev: the path to the pool (eg, /dev/mapper/my_pool) | |
3368 | * dev_id: the internal device identifier | |
2dd9c257 | 3369 | * origin_dev: a device external to the pool that should act as the origin |
67e2e2b2 JT |
3370 | * |
3371 | * If the pool device has discards disabled, they get disabled for the thin | |
3372 | * device as well. | |
991d9fa0 JT |
3373 | */ |
3374 | static int thin_ctr(struct dm_target *ti, unsigned argc, char **argv) | |
3375 | { | |
3376 | int r; | |
3377 | struct thin_c *tc; | |
2dd9c257 | 3378 | struct dm_dev *pool_dev, *origin_dev; |
991d9fa0 | 3379 | struct mapped_device *pool_md; |
5e3283e2 | 3380 | unsigned long flags; |
991d9fa0 JT |
3381 | |
3382 | mutex_lock(&dm_thin_pool_table.mutex); | |
3383 | ||
2dd9c257 | 3384 | if (argc != 2 && argc != 3) { |
991d9fa0 JT |
3385 | ti->error = "Invalid argument count"; |
3386 | r = -EINVAL; | |
3387 | goto out_unlock; | |
3388 | } | |
3389 | ||
3390 | tc = ti->private = kzalloc(sizeof(*tc), GFP_KERNEL); | |
3391 | if (!tc) { | |
3392 | ti->error = "Out of memory"; | |
3393 | r = -ENOMEM; | |
3394 | goto out_unlock; | |
3395 | } | |
c140e1c4 MS |
3396 | spin_lock_init(&tc->lock); |
3397 | bio_list_init(&tc->deferred_bio_list); | |
3398 | bio_list_init(&tc->retry_on_resume_list); | |
67324ea1 | 3399 | tc->sort_bio_list = RB_ROOT; |
991d9fa0 | 3400 | |
2dd9c257 JT |
3401 | if (argc == 3) { |
3402 | r = dm_get_device(ti, argv[2], FMODE_READ, &origin_dev); | |
3403 | if (r) { | |
3404 | ti->error = "Error opening origin device"; | |
3405 | goto bad_origin_dev; | |
3406 | } | |
3407 | tc->origin_dev = origin_dev; | |
3408 | } | |
3409 | ||
991d9fa0 JT |
3410 | r = dm_get_device(ti, argv[0], dm_table_get_mode(ti->table), &pool_dev); |
3411 | if (r) { | |
3412 | ti->error = "Error opening pool device"; | |
3413 | goto bad_pool_dev; | |
3414 | } | |
3415 | tc->pool_dev = pool_dev; | |
3416 | ||
3417 | if (read_dev_id(argv[1], (unsigned long long *)&tc->dev_id, 0)) { | |
3418 | ti->error = "Invalid device id"; | |
3419 | r = -EINVAL; | |
3420 | goto bad_common; | |
3421 | } | |
3422 | ||
3423 | pool_md = dm_get_md(tc->pool_dev->bdev->bd_dev); | |
3424 | if (!pool_md) { | |
3425 | ti->error = "Couldn't get pool mapped device"; | |
3426 | r = -EINVAL; | |
3427 | goto bad_common; | |
3428 | } | |
3429 | ||
3430 | tc->pool = __pool_table_lookup(pool_md); | |
3431 | if (!tc->pool) { | |
3432 | ti->error = "Couldn't find pool object"; | |
3433 | r = -EINVAL; | |
3434 | goto bad_pool_lookup; | |
3435 | } | |
3436 | __pool_inc(tc->pool); | |
3437 | ||
e49e5829 JT |
3438 | if (get_pool_mode(tc->pool) == PM_FAIL) { |
3439 | ti->error = "Couldn't open thin device, Pool is in fail mode"; | |
1acacc07 | 3440 | r = -EINVAL; |
e49e5829 JT |
3441 | goto bad_thin_open; |
3442 | } | |
3443 | ||
991d9fa0 JT |
3444 | r = dm_pool_open_thin_device(tc->pool->pmd, tc->dev_id, &tc->td); |
3445 | if (r) { | |
3446 | ti->error = "Couldn't open thin internal device"; | |
3447 | goto bad_thin_open; | |
3448 | } | |
3449 | ||
542f9038 MS |
3450 | r = dm_set_target_max_io_len(ti, tc->pool->sectors_per_block); |
3451 | if (r) | |
1acacc07 | 3452 | goto bad_target_max_io_len; |
542f9038 | 3453 | |
55a62eef | 3454 | ti->num_flush_bios = 1; |
16ad3d10 | 3455 | ti->flush_supported = true; |
59c3d2c6 | 3456 | ti->per_bio_data_size = sizeof(struct dm_thin_endio_hook); |
67e2e2b2 JT |
3457 | |
3458 | /* In case the pool supports discards, pass them on. */ | |
b60ab990 | 3459 | ti->discard_zeroes_data_unsupported = true; |
67e2e2b2 | 3460 | if (tc->pool->pf.discard_enabled) { |
0ac55489 | 3461 | ti->discards_supported = true; |
55a62eef | 3462 | ti->num_discard_bios = 1; |
55a62eef AK |
3463 | /* Discard bios must be split on a block boundary */ |
3464 | ti->split_discard_bios = true; | |
67e2e2b2 | 3465 | } |
991d9fa0 JT |
3466 | |
3467 | dm_put(pool_md); | |
3468 | ||
3469 | mutex_unlock(&dm_thin_pool_table.mutex); | |
3470 | ||
b10ebd34 JT |
3471 | atomic_set(&tc->refcount, 1); |
3472 | init_completion(&tc->can_destroy); | |
3473 | ||
5e3283e2 | 3474 | spin_lock_irqsave(&tc->pool->lock, flags); |
c140e1c4 | 3475 | list_add_tail_rcu(&tc->list, &tc->pool->active_thins); |
5e3283e2 | 3476 | spin_unlock_irqrestore(&tc->pool->lock, flags); |
c140e1c4 MS |
3477 | /* |
3478 | * This synchronize_rcu() call is needed here otherwise we risk a | |
3479 | * wake_worker() call finding no bios to process (because the newly | |
3480 | * added tc isn't yet visible). So this reduces latency since we | |
3481 | * aren't then dependent on the periodic commit to wake_worker(). | |
3482 | */ | |
3483 | synchronize_rcu(); | |
3484 | ||
991d9fa0 JT |
3485 | return 0; |
3486 | ||
1acacc07 MS |
3487 | bad_target_max_io_len: |
3488 | dm_pool_close_thin_device(tc->td); | |
991d9fa0 JT |
3489 | bad_thin_open: |
3490 | __pool_dec(tc->pool); | |
3491 | bad_pool_lookup: | |
3492 | dm_put(pool_md); | |
3493 | bad_common: | |
3494 | dm_put_device(ti, tc->pool_dev); | |
3495 | bad_pool_dev: | |
2dd9c257 JT |
3496 | if (tc->origin_dev) |
3497 | dm_put_device(ti, tc->origin_dev); | |
3498 | bad_origin_dev: | |
991d9fa0 JT |
3499 | kfree(tc); |
3500 | out_unlock: | |
3501 | mutex_unlock(&dm_thin_pool_table.mutex); | |
3502 | ||
3503 | return r; | |
3504 | } | |
3505 | ||
7de3ee57 | 3506 | static int thin_map(struct dm_target *ti, struct bio *bio) |
991d9fa0 | 3507 | { |
4f024f37 | 3508 | bio->bi_iter.bi_sector = dm_target_offset(ti, bio->bi_iter.bi_sector); |
991d9fa0 | 3509 | |
7de3ee57 | 3510 | return thin_bio_map(ti, bio); |
991d9fa0 JT |
3511 | } |
3512 | ||
7de3ee57 | 3513 | static int thin_endio(struct dm_target *ti, struct bio *bio, int err) |
eb2aa48d JT |
3514 | { |
3515 | unsigned long flags; | |
59c3d2c6 | 3516 | struct dm_thin_endio_hook *h = dm_per_bio_data(bio, sizeof(struct dm_thin_endio_hook)); |
eb2aa48d | 3517 | struct list_head work; |
a24c2569 | 3518 | struct dm_thin_new_mapping *m, *tmp; |
eb2aa48d JT |
3519 | struct pool *pool = h->tc->pool; |
3520 | ||
3521 | if (h->shared_read_entry) { | |
3522 | INIT_LIST_HEAD(&work); | |
44feb387 | 3523 | dm_deferred_entry_dec(h->shared_read_entry, &work); |
eb2aa48d JT |
3524 | |
3525 | spin_lock_irqsave(&pool->lock, flags); | |
3526 | list_for_each_entry_safe(m, tmp, &work, list) { | |
3527 | list_del(&m->list); | |
50f3c3ef | 3528 | __complete_mapping_preparation(m); |
eb2aa48d JT |
3529 | } |
3530 | spin_unlock_irqrestore(&pool->lock, flags); | |
3531 | } | |
3532 | ||
104655fd JT |
3533 | if (h->all_io_entry) { |
3534 | INIT_LIST_HEAD(&work); | |
44feb387 | 3535 | dm_deferred_entry_dec(h->all_io_entry, &work); |
563af186 JT |
3536 | if (!list_empty(&work)) { |
3537 | spin_lock_irqsave(&pool->lock, flags); | |
3538 | list_for_each_entry_safe(m, tmp, &work, list) | |
daec338b | 3539 | list_add_tail(&m->list, &pool->prepared_discards); |
563af186 JT |
3540 | spin_unlock_irqrestore(&pool->lock, flags); |
3541 | wake_worker(pool); | |
3542 | } | |
104655fd JT |
3543 | } |
3544 | ||
eb2aa48d JT |
3545 | return 0; |
3546 | } | |
3547 | ||
738211f7 | 3548 | static void thin_presuspend(struct dm_target *ti) |
991d9fa0 | 3549 | { |
738211f7 JT |
3550 | struct thin_c *tc = ti->private; |
3551 | ||
991d9fa0 | 3552 | if (dm_noflush_suspending(ti)) |
738211f7 JT |
3553 | noflush_work(tc, do_noflush_start); |
3554 | } | |
3555 | ||
3556 | static void thin_postsuspend(struct dm_target *ti) | |
3557 | { | |
3558 | struct thin_c *tc = ti->private; | |
3559 | ||
3560 | /* | |
3561 | * The dm_noflush_suspending flag has been cleared by now, so | |
3562 | * unfortunately we must always run this. | |
3563 | */ | |
3564 | noflush_work(tc, do_noflush_stop); | |
991d9fa0 JT |
3565 | } |
3566 | ||
e5aea7b4 JT |
3567 | static int thin_preresume(struct dm_target *ti) |
3568 | { | |
3569 | struct thin_c *tc = ti->private; | |
3570 | ||
3571 | if (tc->origin_dev) | |
3572 | tc->origin_size = get_dev_size(tc->origin_dev->bdev); | |
3573 | ||
3574 | return 0; | |
3575 | } | |
3576 | ||
991d9fa0 JT |
3577 | /* |
3578 | * <nr mapped sectors> <highest mapped sector> | |
3579 | */ | |
fd7c092e MP |
3580 | static void thin_status(struct dm_target *ti, status_type_t type, |
3581 | unsigned status_flags, char *result, unsigned maxlen) | |
991d9fa0 JT |
3582 | { |
3583 | int r; | |
3584 | ssize_t sz = 0; | |
3585 | dm_block_t mapped, highest; | |
3586 | char buf[BDEVNAME_SIZE]; | |
3587 | struct thin_c *tc = ti->private; | |
3588 | ||
e49e5829 JT |
3589 | if (get_pool_mode(tc->pool) == PM_FAIL) { |
3590 | DMEMIT("Fail"); | |
fd7c092e | 3591 | return; |
e49e5829 JT |
3592 | } |
3593 | ||
991d9fa0 JT |
3594 | if (!tc->td) |
3595 | DMEMIT("-"); | |
3596 | else { | |
3597 | switch (type) { | |
3598 | case STATUSTYPE_INFO: | |
3599 | r = dm_thin_get_mapped_count(tc->td, &mapped); | |
fd7c092e MP |
3600 | if (r) { |
3601 | DMERR("dm_thin_get_mapped_count returned %d", r); | |
3602 | goto err; | |
3603 | } | |
991d9fa0 JT |
3604 | |
3605 | r = dm_thin_get_highest_mapped_block(tc->td, &highest); | |
fd7c092e MP |
3606 | if (r < 0) { |
3607 | DMERR("dm_thin_get_highest_mapped_block returned %d", r); | |
3608 | goto err; | |
3609 | } | |
991d9fa0 JT |
3610 | |
3611 | DMEMIT("%llu ", mapped * tc->pool->sectors_per_block); | |
3612 | if (r) | |
3613 | DMEMIT("%llu", ((highest + 1) * | |
3614 | tc->pool->sectors_per_block) - 1); | |
3615 | else | |
3616 | DMEMIT("-"); | |
3617 | break; | |
3618 | ||
3619 | case STATUSTYPE_TABLE: | |
3620 | DMEMIT("%s %lu", | |
3621 | format_dev_t(buf, tc->pool_dev->bdev->bd_dev), | |
3622 | (unsigned long) tc->dev_id); | |
2dd9c257 JT |
3623 | if (tc->origin_dev) |
3624 | DMEMIT(" %s", format_dev_t(buf, tc->origin_dev->bdev->bd_dev)); | |
991d9fa0 JT |
3625 | break; |
3626 | } | |
3627 | } | |
3628 | ||
fd7c092e MP |
3629 | return; |
3630 | ||
3631 | err: | |
3632 | DMEMIT("Error"); | |
991d9fa0 JT |
3633 | } |
3634 | ||
36f12aeb MS |
3635 | static int thin_merge(struct dm_target *ti, struct bvec_merge_data *bvm, |
3636 | struct bio_vec *biovec, int max_size) | |
3637 | { | |
3638 | struct thin_c *tc = ti->private; | |
3639 | struct request_queue *q = bdev_get_queue(tc->pool_dev->bdev); | |
3640 | ||
3641 | if (!q->merge_bvec_fn) | |
3642 | return max_size; | |
3643 | ||
3644 | bvm->bi_bdev = tc->pool_dev->bdev; | |
3645 | bvm->bi_sector = dm_target_offset(ti, bvm->bi_sector); | |
3646 | ||
3647 | return min(max_size, q->merge_bvec_fn(q, bvm, biovec)); | |
3648 | } | |
3649 | ||
991d9fa0 JT |
3650 | static int thin_iterate_devices(struct dm_target *ti, |
3651 | iterate_devices_callout_fn fn, void *data) | |
3652 | { | |
55f2b8bd | 3653 | sector_t blocks; |
991d9fa0 | 3654 | struct thin_c *tc = ti->private; |
55f2b8bd | 3655 | struct pool *pool = tc->pool; |
991d9fa0 JT |
3656 | |
3657 | /* | |
3658 | * We can't call dm_pool_get_data_dev_size() since that blocks. So | |
3659 | * we follow a more convoluted path through to the pool's target. | |
3660 | */ | |
55f2b8bd | 3661 | if (!pool->ti) |
991d9fa0 JT |
3662 | return 0; /* nothing is bound */ |
3663 | ||
55f2b8bd MS |
3664 | blocks = pool->ti->len; |
3665 | (void) sector_div(blocks, pool->sectors_per_block); | |
991d9fa0 | 3666 | if (blocks) |
55f2b8bd | 3667 | return fn(ti, tc->pool_dev, 0, pool->sectors_per_block * blocks, data); |
991d9fa0 JT |
3668 | |
3669 | return 0; | |
3670 | } | |
3671 | ||
991d9fa0 JT |
3672 | static struct target_type thin_target = { |
3673 | .name = "thin", | |
36f12aeb | 3674 | .version = {1, 14, 0}, |
991d9fa0 JT |
3675 | .module = THIS_MODULE, |
3676 | .ctr = thin_ctr, | |
3677 | .dtr = thin_dtr, | |
3678 | .map = thin_map, | |
eb2aa48d | 3679 | .end_io = thin_endio, |
e5aea7b4 | 3680 | .preresume = thin_preresume, |
738211f7 | 3681 | .presuspend = thin_presuspend, |
991d9fa0 JT |
3682 | .postsuspend = thin_postsuspend, |
3683 | .status = thin_status, | |
36f12aeb | 3684 | .merge = thin_merge, |
991d9fa0 | 3685 | .iterate_devices = thin_iterate_devices, |
991d9fa0 JT |
3686 | }; |
3687 | ||
3688 | /*----------------------------------------------------------------*/ | |
3689 | ||
3690 | static int __init dm_thin_init(void) | |
3691 | { | |
3692 | int r; | |
3693 | ||
3694 | pool_table_init(); | |
3695 | ||
3696 | r = dm_register_target(&thin_target); | |
3697 | if (r) | |
3698 | return r; | |
3699 | ||
3700 | r = dm_register_target(&pool_target); | |
3701 | if (r) | |
a24c2569 MS |
3702 | goto bad_pool_target; |
3703 | ||
3704 | r = -ENOMEM; | |
3705 | ||
a24c2569 MS |
3706 | _new_mapping_cache = KMEM_CACHE(dm_thin_new_mapping, 0); |
3707 | if (!_new_mapping_cache) | |
3708 | goto bad_new_mapping_cache; | |
3709 | ||
a24c2569 MS |
3710 | return 0; |
3711 | ||
a24c2569 | 3712 | bad_new_mapping_cache: |
a24c2569 MS |
3713 | dm_unregister_target(&pool_target); |
3714 | bad_pool_target: | |
3715 | dm_unregister_target(&thin_target); | |
991d9fa0 JT |
3716 | |
3717 | return r; | |
3718 | } | |
3719 | ||
3720 | static void dm_thin_exit(void) | |
3721 | { | |
3722 | dm_unregister_target(&thin_target); | |
3723 | dm_unregister_target(&pool_target); | |
a24c2569 | 3724 | |
a24c2569 | 3725 | kmem_cache_destroy(_new_mapping_cache); |
991d9fa0 JT |
3726 | } |
3727 | ||
3728 | module_init(dm_thin_init); | |
3729 | module_exit(dm_thin_exit); | |
3730 | ||
80c57893 MS |
3731 | module_param_named(no_space_timeout, no_space_timeout_secs, uint, S_IRUGO | S_IWUSR); |
3732 | MODULE_PARM_DESC(no_space_timeout, "Out of data space queue IO timeout in seconds"); | |
3733 | ||
7cab8bf1 | 3734 | MODULE_DESCRIPTION(DM_NAME " thin provisioning target"); |
991d9fa0 JT |
3735 | MODULE_AUTHOR("Joe Thornber <dm-devel@redhat.com>"); |
3736 | MODULE_LICENSE("GPL"); |