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