forked from torvalds/linux
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdm-clone-target.c
2231 lines (1807 loc) · 54.6 KB
/
dm-clone-target.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
// SPDX-License-Identifier: GPL-2.0-only
/*
* Copyright (C) 2019 Arrikto, Inc. All Rights Reserved.
*/
#include <linux/mm.h>
#include <linux/bio.h>
#include <linux/err.h>
#include <linux/hash.h>
#include <linux/list.h>
#include <linux/log2.h>
#include <linux/init.h>
#include <linux/slab.h>
#include <linux/wait.h>
#include <linux/dm-io.h>
#include <linux/mutex.h>
#include <linux/atomic.h>
#include <linux/bitops.h>
#include <linux/blkdev.h>
#include <linux/kdev_t.h>
#include <linux/kernel.h>
#include <linux/module.h>
#include <linux/jiffies.h>
#include <linux/mempool.h>
#include <linux/spinlock.h>
#include <linux/blk_types.h>
#include <linux/dm-kcopyd.h>
#include <linux/workqueue.h>
#include <linux/backing-dev.h>
#include <linux/device-mapper.h>
#include "dm.h"
#include "dm-clone-metadata.h"
#define DM_MSG_PREFIX "clone"
/*
* Minimum and maximum allowed region sizes
*/
#define MIN_REGION_SIZE (1 << 3) /* 4KB */
#define MAX_REGION_SIZE (1 << 21) /* 1GB */
#define MIN_HYDRATIONS 256 /* Size of hydration mempool */
#define DEFAULT_HYDRATION_THRESHOLD 1 /* 1 region */
#define DEFAULT_HYDRATION_BATCH_SIZE 1 /* Hydrate in batches of 1 region */
#define COMMIT_PERIOD HZ /* 1 sec */
/*
* Hydration hash table size: 1 << HASH_TABLE_BITS
*/
#define HASH_TABLE_BITS 15
DECLARE_DM_KCOPYD_THROTTLE_WITH_MODULE_PARM(clone_hydration_throttle,
"A percentage of time allocated for hydrating regions");
/* Slab cache for struct dm_clone_region_hydration */
static struct kmem_cache *_hydration_cache;
/* dm-clone metadata modes */
enum clone_metadata_mode {
CM_WRITE, /* metadata may be changed */
CM_READ_ONLY, /* metadata may not be changed */
CM_FAIL, /* all metadata I/O fails */
};
struct hash_table_bucket;
struct clone {
struct dm_target *ti;
struct dm_dev *metadata_dev;
struct dm_dev *dest_dev;
struct dm_dev *source_dev;
unsigned long nr_regions;
sector_t region_size;
unsigned int region_shift;
/*
* A metadata commit and the actions taken in case it fails should run
* as a single atomic step.
*/
struct mutex commit_lock;
struct dm_clone_metadata *cmd;
/* Region hydration hash table */
struct hash_table_bucket *ht;
atomic_t ios_in_flight;
wait_queue_head_t hydration_stopped;
mempool_t hydration_pool;
unsigned long last_commit_jiffies;
/*
* We defer incoming WRITE bios for regions that are not hydrated,
* until after these regions have been hydrated.
*
* Also, we defer REQ_FUA and REQ_PREFLUSH bios, until after the
* metadata have been committed.
*/
spinlock_t lock;
struct bio_list deferred_bios;
struct bio_list deferred_discard_bios;
struct bio_list deferred_flush_bios;
struct bio_list deferred_flush_completions;
/* Maximum number of regions being copied during background hydration. */
unsigned int hydration_threshold;
/* Number of regions to batch together during background hydration. */
unsigned int hydration_batch_size;
/* Which region to hydrate next */
unsigned long hydration_offset;
atomic_t hydrations_in_flight;
/*
* Save a copy of the table line rather than reconstructing it for the
* status.
*/
unsigned int nr_ctr_args;
const char **ctr_args;
struct workqueue_struct *wq;
struct work_struct worker;
struct delayed_work waker;
struct dm_kcopyd_client *kcopyd_client;
enum clone_metadata_mode mode;
unsigned long flags;
};
/*
* dm-clone flags
*/
#define DM_CLONE_DISCARD_PASSDOWN 0
#define DM_CLONE_HYDRATION_ENABLED 1
#define DM_CLONE_HYDRATION_SUSPENDED 2
/*---------------------------------------------------------------------------*/
/*
* Metadata failure handling.
*/
static enum clone_metadata_mode get_clone_mode(struct clone *clone)
{
return READ_ONCE(clone->mode);
}
static const char *clone_device_name(struct clone *clone)
{
return dm_table_device_name(clone->ti->table);
}
static void __set_clone_mode(struct clone *clone, enum clone_metadata_mode new_mode)
{
const char *descs[] = {
"read-write",
"read-only",
"fail"
};
enum clone_metadata_mode old_mode = get_clone_mode(clone);
/* Never move out of fail mode */
if (old_mode == CM_FAIL)
new_mode = CM_FAIL;
switch (new_mode) {
case CM_FAIL:
case CM_READ_ONLY:
dm_clone_metadata_set_read_only(clone->cmd);
break;
case CM_WRITE:
dm_clone_metadata_set_read_write(clone->cmd);
break;
}
WRITE_ONCE(clone->mode, new_mode);
if (new_mode != old_mode) {
dm_table_event(clone->ti->table);
DMINFO("%s: Switching to %s mode", clone_device_name(clone),
descs[(int)new_mode]);
}
}
static void __abort_transaction(struct clone *clone)
{
const char *dev_name = clone_device_name(clone);
if (get_clone_mode(clone) >= CM_READ_ONLY)
return;
DMERR("%s: Aborting current metadata transaction", dev_name);
if (dm_clone_metadata_abort(clone->cmd)) {
DMERR("%s: Failed to abort metadata transaction", dev_name);
__set_clone_mode(clone, CM_FAIL);
}
}
static void __reload_in_core_bitset(struct clone *clone)
{
const char *dev_name = clone_device_name(clone);
if (get_clone_mode(clone) == CM_FAIL)
return;
/* Reload the on-disk bitset */
DMINFO("%s: Reloading on-disk bitmap", dev_name);
if (dm_clone_reload_in_core_bitset(clone->cmd)) {
DMERR("%s: Failed to reload on-disk bitmap", dev_name);
__set_clone_mode(clone, CM_FAIL);
}
}
static void __metadata_operation_failed(struct clone *clone, const char *op, int r)
{
DMERR("%s: Metadata operation `%s' failed: error = %d",
clone_device_name(clone), op, r);
__abort_transaction(clone);
__set_clone_mode(clone, CM_READ_ONLY);
/*
* dm_clone_reload_in_core_bitset() may run concurrently with either
* dm_clone_set_region_hydrated() or dm_clone_cond_set_range(), but
* it's safe as we have already set the metadata to read-only mode.
*/
__reload_in_core_bitset(clone);
}
/*---------------------------------------------------------------------------*/
/* Wake up anyone waiting for region hydrations to stop */
static inline void wakeup_hydration_waiters(struct clone *clone)
{
wake_up_all(&clone->hydration_stopped);
}
static inline void wake_worker(struct clone *clone)
{
queue_work(clone->wq, &clone->worker);
}
/*---------------------------------------------------------------------------*/
/*
* bio helper functions.
*/
static inline void remap_to_source(struct clone *clone, struct bio *bio)
{
bio_set_dev(bio, clone->source_dev->bdev);
}
static inline void remap_to_dest(struct clone *clone, struct bio *bio)
{
bio_set_dev(bio, clone->dest_dev->bdev);
}
static bool bio_triggers_commit(struct clone *clone, struct bio *bio)
{
return op_is_flush(bio->bi_opf) &&
dm_clone_changed_this_transaction(clone->cmd);
}
/* Get the address of the region in sectors */
static inline sector_t region_to_sector(struct clone *clone, unsigned long region_nr)
{
return ((sector_t)region_nr << clone->region_shift);
}
/* Get the region number of the bio */
static inline unsigned long bio_to_region(struct clone *clone, struct bio *bio)
{
return (bio->bi_iter.bi_sector >> clone->region_shift);
}
/* Get the region range covered by the bio */
static void bio_region_range(struct clone *clone, struct bio *bio,
unsigned long *rs, unsigned long *nr_regions)
{
unsigned long end;
*rs = dm_sector_div_up(bio->bi_iter.bi_sector, clone->region_size);
end = bio_end_sector(bio) >> clone->region_shift;
if (*rs >= end)
*nr_regions = 0;
else
*nr_regions = end - *rs;
}
/* Check whether a bio overwrites a region */
static inline bool is_overwrite_bio(struct clone *clone, struct bio *bio)
{
return (bio_data_dir(bio) == WRITE && bio_sectors(bio) == clone->region_size);
}
static void fail_bios(struct bio_list *bios, blk_status_t status)
{
struct bio *bio;
while ((bio = bio_list_pop(bios))) {
bio->bi_status = status;
bio_endio(bio);
}
}
static void submit_bios(struct bio_list *bios)
{
struct bio *bio;
struct blk_plug plug;
blk_start_plug(&plug);
while ((bio = bio_list_pop(bios)))
submit_bio_noacct(bio);
blk_finish_plug(&plug);
}
/*
* Submit bio to the underlying device.
*
* If the bio triggers a commit, delay it, until after the metadata have been
* committed.
*
* NOTE: The bio remapping must be performed by the caller.
*/
static void issue_bio(struct clone *clone, struct bio *bio)
{
if (!bio_triggers_commit(clone, bio)) {
submit_bio_noacct(bio);
return;
}
/*
* If the metadata mode is RO or FAIL we won't be able to commit the
* metadata, so we complete the bio with an error.
*/
if (unlikely(get_clone_mode(clone) >= CM_READ_ONLY)) {
bio_io_error(bio);
return;
}
/*
* Batch together any bios that trigger commits and then issue a single
* commit for them in process_deferred_flush_bios().
*/
spin_lock_irq(&clone->lock);
bio_list_add(&clone->deferred_flush_bios, bio);
spin_unlock_irq(&clone->lock);
wake_worker(clone);
}
/*
* Remap bio to the destination device and submit it.
*
* If the bio triggers a commit, delay it, until after the metadata have been
* committed.
*/
static void remap_and_issue(struct clone *clone, struct bio *bio)
{
remap_to_dest(clone, bio);
issue_bio(clone, bio);
}
/*
* Issue bios that have been deferred until after their region has finished
* hydrating.
*
* We delegate the bio submission to the worker thread, so this is safe to call
* from interrupt context.
*/
static void issue_deferred_bios(struct clone *clone, struct bio_list *bios)
{
struct bio *bio;
unsigned long flags;
struct bio_list flush_bios = BIO_EMPTY_LIST;
struct bio_list normal_bios = BIO_EMPTY_LIST;
if (bio_list_empty(bios))
return;
while ((bio = bio_list_pop(bios))) {
if (bio_triggers_commit(clone, bio))
bio_list_add(&flush_bios, bio);
else
bio_list_add(&normal_bios, bio);
}
spin_lock_irqsave(&clone->lock, flags);
bio_list_merge(&clone->deferred_bios, &normal_bios);
bio_list_merge(&clone->deferred_flush_bios, &flush_bios);
spin_unlock_irqrestore(&clone->lock, flags);
wake_worker(clone);
}
static void complete_overwrite_bio(struct clone *clone, struct bio *bio)
{
unsigned long flags;
/*
* If the bio has the REQ_FUA flag set we must commit the metadata
* before signaling its completion.
*
* complete_overwrite_bio() is only called by hydration_complete(),
* after having successfully updated the metadata. This means we don't
* need to call dm_clone_changed_this_transaction() to check if the
* metadata has changed and thus we can avoid taking the metadata spin
* lock.
*/
if (!(bio->bi_opf & REQ_FUA)) {
bio_endio(bio);
return;
}
/*
* If the metadata mode is RO or FAIL we won't be able to commit the
* metadata, so we complete the bio with an error.
*/
if (unlikely(get_clone_mode(clone) >= CM_READ_ONLY)) {
bio_io_error(bio);
return;
}
/*
* Batch together any bios that trigger commits and then issue a single
* commit for them in process_deferred_flush_bios().
*/
spin_lock_irqsave(&clone->lock, flags);
bio_list_add(&clone->deferred_flush_completions, bio);
spin_unlock_irqrestore(&clone->lock, flags);
wake_worker(clone);
}
static void trim_bio(struct bio *bio, sector_t sector, unsigned int len)
{
bio->bi_iter.bi_sector = sector;
bio->bi_iter.bi_size = to_bytes(len);
}
static void complete_discard_bio(struct clone *clone, struct bio *bio, bool success)
{
unsigned long rs, nr_regions;
/*
* If the destination device supports discards, remap and trim the
* discard bio and pass it down. Otherwise complete the bio
* immediately.
*/
if (test_bit(DM_CLONE_DISCARD_PASSDOWN, &clone->flags) && success) {
remap_to_dest(clone, bio);
bio_region_range(clone, bio, &rs, &nr_regions);
trim_bio(bio, region_to_sector(clone, rs),
nr_regions << clone->region_shift);
submit_bio_noacct(bio);
} else
bio_endio(bio);
}
static void process_discard_bio(struct clone *clone, struct bio *bio)
{
unsigned long rs, nr_regions;
bio_region_range(clone, bio, &rs, &nr_regions);
if (!nr_regions) {
bio_endio(bio);
return;
}
if (WARN_ON(rs >= clone->nr_regions || (rs + nr_regions) < rs ||
(rs + nr_regions) > clone->nr_regions)) {
DMERR("%s: Invalid range (%lu + %lu, total regions %lu) for discard (%llu + %u)",
clone_device_name(clone), rs, nr_regions,
clone->nr_regions,
(unsigned long long)bio->bi_iter.bi_sector,
bio_sectors(bio));
bio_endio(bio);
return;
}
/*
* The covered regions are already hydrated so we just need to pass
* down the discard.
*/
if (dm_clone_is_range_hydrated(clone->cmd, rs, nr_regions)) {
complete_discard_bio(clone, bio, true);
return;
}
/*
* If the metadata mode is RO or FAIL we won't be able to update the
* metadata for the regions covered by the discard so we just ignore
* it.
*/
if (unlikely(get_clone_mode(clone) >= CM_READ_ONLY)) {
bio_endio(bio);
return;
}
/*
* Defer discard processing.
*/
spin_lock_irq(&clone->lock);
bio_list_add(&clone->deferred_discard_bios, bio);
spin_unlock_irq(&clone->lock);
wake_worker(clone);
}
/*---------------------------------------------------------------------------*/
/*
* dm-clone region hydrations.
*/
struct dm_clone_region_hydration {
struct clone *clone;
unsigned long region_nr;
struct bio *overwrite_bio;
bio_end_io_t *overwrite_bio_end_io;
struct bio_list deferred_bios;
blk_status_t status;
/* Used by hydration batching */
struct list_head list;
/* Used by hydration hash table */
struct hlist_node h;
};
/*
* Hydration hash table implementation.
*
* Ideally we would like to use list_bl, which uses bit spin locks and employs
* the least significant bit of the list head to lock the corresponding bucket,
* reducing the memory overhead for the locks. But, currently, list_bl and bit
* spin locks don't support IRQ safe versions. Since we have to take the lock
* in both process and interrupt context, we must fall back to using regular
* spin locks; one per hash table bucket.
*/
struct hash_table_bucket {
struct hlist_head head;
/* Spinlock protecting the bucket */
spinlock_t lock;
};
#define bucket_lock_irqsave(bucket, flags) \
spin_lock_irqsave(&(bucket)->lock, flags)
#define bucket_unlock_irqrestore(bucket, flags) \
spin_unlock_irqrestore(&(bucket)->lock, flags)
#define bucket_lock_irq(bucket) \
spin_lock_irq(&(bucket)->lock)
#define bucket_unlock_irq(bucket) \
spin_unlock_irq(&(bucket)->lock)
static int hash_table_init(struct clone *clone)
{
unsigned int i, sz;
struct hash_table_bucket *bucket;
sz = 1 << HASH_TABLE_BITS;
clone->ht = kvmalloc(sz * sizeof(struct hash_table_bucket), GFP_KERNEL);
if (!clone->ht)
return -ENOMEM;
for (i = 0; i < sz; i++) {
bucket = clone->ht + i;
INIT_HLIST_HEAD(&bucket->head);
spin_lock_init(&bucket->lock);
}
return 0;
}
static void hash_table_exit(struct clone *clone)
{
kvfree(clone->ht);
}
static struct hash_table_bucket *get_hash_table_bucket(struct clone *clone,
unsigned long region_nr)
{
return &clone->ht[hash_long(region_nr, HASH_TABLE_BITS)];
}
/*
* Search hash table for a hydration with hd->region_nr == region_nr
*
* NOTE: Must be called with the bucket lock held
*/
static struct dm_clone_region_hydration *__hash_find(struct hash_table_bucket *bucket,
unsigned long region_nr)
{
struct dm_clone_region_hydration *hd;
hlist_for_each_entry(hd, &bucket->head, h) {
if (hd->region_nr == region_nr)
return hd;
}
return NULL;
}
/*
* Insert a hydration into the hash table.
*
* NOTE: Must be called with the bucket lock held.
*/
static inline void __insert_region_hydration(struct hash_table_bucket *bucket,
struct dm_clone_region_hydration *hd)
{
hlist_add_head(&hd->h, &bucket->head);
}
/*
* This function inserts a hydration into the hash table, unless someone else
* managed to insert a hydration for the same region first. In the latter case
* it returns the existing hydration descriptor for this region.
*
* NOTE: Must be called with the hydration hash table lock held.
*/
static struct dm_clone_region_hydration *
__find_or_insert_region_hydration(struct hash_table_bucket *bucket,
struct dm_clone_region_hydration *hd)
{
struct dm_clone_region_hydration *hd2;
hd2 = __hash_find(bucket, hd->region_nr);
if (hd2)
return hd2;
__insert_region_hydration(bucket, hd);
return hd;
}
/*---------------------------------------------------------------------------*/
/* Allocate a hydration */
static struct dm_clone_region_hydration *alloc_hydration(struct clone *clone)
{
struct dm_clone_region_hydration *hd;
/*
* Allocate a hydration from the hydration mempool.
* This might block but it can't fail.
*/
hd = mempool_alloc(&clone->hydration_pool, GFP_NOIO);
hd->clone = clone;
return hd;
}
static inline void free_hydration(struct dm_clone_region_hydration *hd)
{
mempool_free(hd, &hd->clone->hydration_pool);
}
/* Initialize a hydration */
static void hydration_init(struct dm_clone_region_hydration *hd, unsigned long region_nr)
{
hd->region_nr = region_nr;
hd->overwrite_bio = NULL;
bio_list_init(&hd->deferred_bios);
hd->status = 0;
INIT_LIST_HEAD(&hd->list);
INIT_HLIST_NODE(&hd->h);
}
/*---------------------------------------------------------------------------*/
/*
* Update dm-clone's metadata after a region has finished hydrating and remove
* hydration from the hash table.
*/
static int hydration_update_metadata(struct dm_clone_region_hydration *hd)
{
int r = 0;
unsigned long flags;
struct hash_table_bucket *bucket;
struct clone *clone = hd->clone;
if (unlikely(get_clone_mode(clone) >= CM_READ_ONLY))
r = -EPERM;
/* Update the metadata */
if (likely(!r) && hd->status == BLK_STS_OK)
r = dm_clone_set_region_hydrated(clone->cmd, hd->region_nr);
bucket = get_hash_table_bucket(clone, hd->region_nr);
/* Remove hydration from hash table */
bucket_lock_irqsave(bucket, flags);
hlist_del(&hd->h);
bucket_unlock_irqrestore(bucket, flags);
return r;
}
/*
* Complete a region's hydration:
*
* 1. Update dm-clone's metadata.
* 2. Remove hydration from hash table.
* 3. Complete overwrite bio.
* 4. Issue deferred bios.
* 5. If this was the last hydration, wake up anyone waiting for
* hydrations to finish.
*/
static void hydration_complete(struct dm_clone_region_hydration *hd)
{
int r;
blk_status_t status;
struct clone *clone = hd->clone;
r = hydration_update_metadata(hd);
if (hd->status == BLK_STS_OK && likely(!r)) {
if (hd->overwrite_bio)
complete_overwrite_bio(clone, hd->overwrite_bio);
issue_deferred_bios(clone, &hd->deferred_bios);
} else {
status = r ? BLK_STS_IOERR : hd->status;
if (hd->overwrite_bio)
bio_list_add(&hd->deferred_bios, hd->overwrite_bio);
fail_bios(&hd->deferred_bios, status);
}
free_hydration(hd);
if (atomic_dec_and_test(&clone->hydrations_in_flight))
wakeup_hydration_waiters(clone);
}
static void hydration_kcopyd_callback(int read_err, unsigned long write_err, void *context)
{
blk_status_t status;
struct dm_clone_region_hydration *tmp, *hd = context;
struct clone *clone = hd->clone;
LIST_HEAD(batched_hydrations);
if (read_err || write_err) {
DMERR_LIMIT("%s: hydration failed", clone_device_name(clone));
status = BLK_STS_IOERR;
} else {
status = BLK_STS_OK;
}
list_splice_tail(&hd->list, &batched_hydrations);
hd->status = status;
hydration_complete(hd);
/* Complete batched hydrations */
list_for_each_entry_safe(hd, tmp, &batched_hydrations, list) {
hd->status = status;
hydration_complete(hd);
}
/* Continue background hydration, if there is no I/O in-flight */
if (test_bit(DM_CLONE_HYDRATION_ENABLED, &clone->flags) &&
!atomic_read(&clone->ios_in_flight))
wake_worker(clone);
}
static void hydration_copy(struct dm_clone_region_hydration *hd, unsigned int nr_regions)
{
unsigned long region_start, region_end;
sector_t tail_size, region_size, total_size;
struct dm_io_region from, to;
struct clone *clone = hd->clone;
if (WARN_ON(!nr_regions))
return;
region_size = clone->region_size;
region_start = hd->region_nr;
region_end = region_start + nr_regions - 1;
total_size = region_to_sector(clone, nr_regions - 1);
if (region_end == clone->nr_regions - 1) {
/*
* The last region of the target might be smaller than
* region_size.
*/
tail_size = clone->ti->len & (region_size - 1);
if (!tail_size)
tail_size = region_size;
} else {
tail_size = region_size;
}
total_size += tail_size;
from.bdev = clone->source_dev->bdev;
from.sector = region_to_sector(clone, region_start);
from.count = total_size;
to.bdev = clone->dest_dev->bdev;
to.sector = from.sector;
to.count = from.count;
/* Issue copy */
atomic_add(nr_regions, &clone->hydrations_in_flight);
dm_kcopyd_copy(clone->kcopyd_client, &from, 1, &to, 0,
hydration_kcopyd_callback, hd);
}
static void overwrite_endio(struct bio *bio)
{
struct dm_clone_region_hydration *hd = bio->bi_private;
bio->bi_end_io = hd->overwrite_bio_end_io;
hd->status = bio->bi_status;
hydration_complete(hd);
}
static void hydration_overwrite(struct dm_clone_region_hydration *hd, struct bio *bio)
{
/*
* We don't need to save and restore bio->bi_private because device
* mapper core generates a new bio for us to use, with clean
* bi_private.
*/
hd->overwrite_bio = bio;
hd->overwrite_bio_end_io = bio->bi_end_io;
bio->bi_end_io = overwrite_endio;
bio->bi_private = hd;
atomic_inc(&hd->clone->hydrations_in_flight);
submit_bio_noacct(bio);
}
/*
* Hydrate bio's region.
*
* This function starts the hydration of the bio's region and puts the bio in
* the list of deferred bios for this region. In case, by the time this
* function is called, the region has finished hydrating it's submitted to the
* destination device.
*
* NOTE: The bio remapping must be performed by the caller.
*/
static void hydrate_bio_region(struct clone *clone, struct bio *bio)
{
unsigned long region_nr;
struct hash_table_bucket *bucket;
struct dm_clone_region_hydration *hd, *hd2;
region_nr = bio_to_region(clone, bio);
bucket = get_hash_table_bucket(clone, region_nr);
bucket_lock_irq(bucket);
hd = __hash_find(bucket, region_nr);
if (hd) {
/* Someone else is hydrating the region */
bio_list_add(&hd->deferred_bios, bio);
bucket_unlock_irq(bucket);
return;
}
if (dm_clone_is_region_hydrated(clone->cmd, region_nr)) {
/* The region has been hydrated */
bucket_unlock_irq(bucket);
issue_bio(clone, bio);
return;
}
/*
* We must allocate a hydration descriptor and start the hydration of
* the corresponding region.
*/
bucket_unlock_irq(bucket);
hd = alloc_hydration(clone);
hydration_init(hd, region_nr);
bucket_lock_irq(bucket);
/* Check if the region has been hydrated in the meantime. */
if (dm_clone_is_region_hydrated(clone->cmd, region_nr)) {
bucket_unlock_irq(bucket);
free_hydration(hd);
issue_bio(clone, bio);
return;
}
hd2 = __find_or_insert_region_hydration(bucket, hd);
if (hd2 != hd) {
/* Someone else started the region's hydration. */
bio_list_add(&hd2->deferred_bios, bio);
bucket_unlock_irq(bucket);
free_hydration(hd);
return;
}
/*
* If the metadata mode is RO or FAIL then there is no point starting a
* hydration, since we will not be able to update the metadata when the
* hydration finishes.
*/
if (unlikely(get_clone_mode(clone) >= CM_READ_ONLY)) {
hlist_del(&hd->h);
bucket_unlock_irq(bucket);
free_hydration(hd);
bio_io_error(bio);
return;
}
/*
* Start region hydration.
*
* If a bio overwrites a region, i.e., its size is equal to the
* region's size, then we don't need to copy the region from the source
* to the destination device.
*/
if (is_overwrite_bio(clone, bio)) {
bucket_unlock_irq(bucket);
hydration_overwrite(hd, bio);
} else {
bio_list_add(&hd->deferred_bios, bio);
bucket_unlock_irq(bucket);
hydration_copy(hd, 1);
}
}
/*---------------------------------------------------------------------------*/
/*
* Background hydrations.
*/
/*
* Batch region hydrations.
*
* To better utilize device bandwidth we batch together the hydration of
* adjacent regions. This allows us to use small region sizes, e.g., 4KB, which
* is good for small, random write performance (because of the overwriting of
* un-hydrated regions) and at the same time issue big copy requests to kcopyd
* to achieve high hydration bandwidth.
*/
struct batch_info {
struct dm_clone_region_hydration *head;
unsigned int nr_batched_regions;
};
static void __batch_hydration(struct batch_info *batch,
struct dm_clone_region_hydration *hd)
{
struct clone *clone = hd->clone;
unsigned int max_batch_size = READ_ONCE(clone->hydration_batch_size);
if (batch->head) {
/* Try to extend the current batch */
if (batch->nr_batched_regions < max_batch_size &&
(batch->head->region_nr + batch->nr_batched_regions) == hd->region_nr) {
list_add_tail(&hd->list, &batch->head->list);
batch->nr_batched_regions++;
hd = NULL;
}
/* Check if we should issue the current batch */
if (batch->nr_batched_regions >= max_batch_size || hd) {
hydration_copy(batch->head, batch->nr_batched_regions);
batch->head = NULL;
batch->nr_batched_regions = 0;
}
}