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