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