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