forked from torvalds/linux
-
Notifications
You must be signed in to change notification settings - Fork 0
/
dm-writecache.c
2769 lines (2382 loc) · 68.9 KB
/
dm-writecache.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
/*
* Copyright (C) 2018 Red Hat. All rights reserved.
*
* This file is released under the GPL.
*/
#include <linux/device-mapper.h>
#include <linux/module.h>
#include <linux/init.h>
#include <linux/vmalloc.h>
#include <linux/kthread.h>
#include <linux/dm-io.h>
#include <linux/dm-kcopyd.h>
#include <linux/dax.h>
#include <linux/pfn_t.h>
#include <linux/libnvdimm.h>
#include <linux/delay.h>
#include "dm-io-tracker.h"
#define DM_MSG_PREFIX "writecache"
#define HIGH_WATERMARK 50
#define LOW_WATERMARK 45
#define MAX_WRITEBACK_JOBS 0
#define ENDIO_LATENCY 16
#define WRITEBACK_LATENCY 64
#define AUTOCOMMIT_BLOCKS_SSD 65536
#define AUTOCOMMIT_BLOCKS_PMEM 64
#define AUTOCOMMIT_MSEC 1000
#define MAX_AGE_DIV 16
#define MAX_AGE_UNSPECIFIED -1UL
#define PAUSE_WRITEBACK (HZ * 3)
#define BITMAP_GRANULARITY 65536
#if BITMAP_GRANULARITY < PAGE_SIZE
#undef BITMAP_GRANULARITY
#define BITMAP_GRANULARITY PAGE_SIZE
#endif
#if IS_ENABLED(CONFIG_ARCH_HAS_PMEM_API) && IS_ENABLED(CONFIG_DAX_DRIVER)
#define DM_WRITECACHE_HAS_PMEM
#endif
#ifdef DM_WRITECACHE_HAS_PMEM
#define pmem_assign(dest, src) \
do { \
typeof(dest) uniq = (src); \
memcpy_flushcache(&(dest), &uniq, sizeof(dest)); \
} while (0)
#else
#define pmem_assign(dest, src) ((dest) = (src))
#endif
#if IS_ENABLED(CONFIG_ARCH_HAS_COPY_MC) && defined(DM_WRITECACHE_HAS_PMEM)
#define DM_WRITECACHE_HANDLE_HARDWARE_ERRORS
#endif
#define MEMORY_SUPERBLOCK_MAGIC 0x23489321
#define MEMORY_SUPERBLOCK_VERSION 1
struct wc_memory_entry {
__le64 original_sector;
__le64 seq_count;
};
struct wc_memory_superblock {
union {
struct {
__le32 magic;
__le32 version;
__le32 block_size;
__le32 pad;
__le64 n_blocks;
__le64 seq_count;
};
__le64 padding[8];
};
struct wc_memory_entry entries[];
};
struct wc_entry {
struct rb_node rb_node;
struct list_head lru;
unsigned short wc_list_contiguous;
bool write_in_progress
#if BITS_PER_LONG == 64
:1
#endif
;
unsigned long index
#if BITS_PER_LONG == 64
:47
#endif
;
unsigned long age;
#ifdef DM_WRITECACHE_HANDLE_HARDWARE_ERRORS
uint64_t original_sector;
uint64_t seq_count;
#endif
};
#ifdef DM_WRITECACHE_HAS_PMEM
#define WC_MODE_PMEM(wc) ((wc)->pmem_mode)
#define WC_MODE_FUA(wc) ((wc)->writeback_fua)
#else
#define WC_MODE_PMEM(wc) false
#define WC_MODE_FUA(wc) false
#endif
#define WC_MODE_SORT_FREELIST(wc) (!WC_MODE_PMEM(wc))
struct dm_writecache {
struct mutex lock;
struct list_head lru;
union {
struct list_head freelist;
struct {
struct rb_root freetree;
struct wc_entry *current_free;
};
};
struct rb_root tree;
size_t freelist_size;
size_t writeback_size;
size_t freelist_high_watermark;
size_t freelist_low_watermark;
unsigned long max_age;
unsigned long pause;
unsigned uncommitted_blocks;
unsigned autocommit_blocks;
unsigned max_writeback_jobs;
int error;
unsigned long autocommit_jiffies;
struct timer_list autocommit_timer;
struct wait_queue_head freelist_wait;
struct timer_list max_age_timer;
atomic_t bio_in_progress[2];
struct wait_queue_head bio_in_progress_wait[2];
struct dm_target *ti;
struct dm_dev *dev;
struct dm_dev *ssd_dev;
sector_t start_sector;
void *memory_map;
uint64_t memory_map_size;
size_t metadata_sectors;
size_t n_blocks;
uint64_t seq_count;
sector_t data_device_sectors;
void *block_start;
struct wc_entry *entries;
unsigned block_size;
unsigned char block_size_bits;
bool pmem_mode:1;
bool writeback_fua:1;
bool overwrote_committed:1;
bool memory_vmapped:1;
bool start_sector_set:1;
bool high_wm_percent_set:1;
bool low_wm_percent_set:1;
bool max_writeback_jobs_set:1;
bool autocommit_blocks_set:1;
bool autocommit_time_set:1;
bool max_age_set:1;
bool writeback_fua_set:1;
bool flush_on_suspend:1;
bool cleaner:1;
bool cleaner_set:1;
bool metadata_only:1;
bool pause_set:1;
unsigned high_wm_percent_value;
unsigned low_wm_percent_value;
unsigned autocommit_time_value;
unsigned max_age_value;
unsigned pause_value;
unsigned writeback_all;
struct workqueue_struct *writeback_wq;
struct work_struct writeback_work;
struct work_struct flush_work;
struct dm_io_tracker iot;
struct dm_io_client *dm_io;
raw_spinlock_t endio_list_lock;
struct list_head endio_list;
struct task_struct *endio_thread;
struct task_struct *flush_thread;
struct bio_list flush_list;
struct dm_kcopyd_client *dm_kcopyd;
unsigned long *dirty_bitmap;
unsigned dirty_bitmap_size;
struct bio_set bio_set;
mempool_t copy_pool;
struct {
unsigned long long reads;
unsigned long long read_hits;
unsigned long long writes;
unsigned long long write_hits_uncommitted;
unsigned long long write_hits_committed;
unsigned long long writes_around;
unsigned long long writes_allocate;
unsigned long long writes_blocked_on_freelist;
unsigned long long flushes;
unsigned long long discards;
} stats;
};
#define WB_LIST_INLINE 16
struct writeback_struct {
struct list_head endio_entry;
struct dm_writecache *wc;
struct wc_entry **wc_list;
unsigned wc_list_n;
struct wc_entry *wc_list_inline[WB_LIST_INLINE];
struct bio bio;
};
struct copy_struct {
struct list_head endio_entry;
struct dm_writecache *wc;
struct wc_entry *e;
unsigned n_entries;
int error;
};
DECLARE_DM_KCOPYD_THROTTLE_WITH_MODULE_PARM(dm_writecache_throttle,
"A percentage of time allocated for data copying");
static void wc_lock(struct dm_writecache *wc)
{
mutex_lock(&wc->lock);
}
static void wc_unlock(struct dm_writecache *wc)
{
mutex_unlock(&wc->lock);
}
#ifdef DM_WRITECACHE_HAS_PMEM
static int persistent_memory_claim(struct dm_writecache *wc)
{
int r;
loff_t s;
long p, da;
pfn_t pfn;
int id;
struct page **pages;
sector_t offset;
wc->memory_vmapped = false;
s = wc->memory_map_size;
p = s >> PAGE_SHIFT;
if (!p) {
r = -EINVAL;
goto err1;
}
if (p != s >> PAGE_SHIFT) {
r = -EOVERFLOW;
goto err1;
}
offset = get_start_sect(wc->ssd_dev->bdev);
if (offset & (PAGE_SIZE / 512 - 1)) {
r = -EINVAL;
goto err1;
}
offset >>= PAGE_SHIFT - 9;
id = dax_read_lock();
da = dax_direct_access(wc->ssd_dev->dax_dev, offset, p, &wc->memory_map, &pfn);
if (da < 0) {
wc->memory_map = NULL;
r = da;
goto err2;
}
if (!pfn_t_has_page(pfn)) {
wc->memory_map = NULL;
r = -EOPNOTSUPP;
goto err2;
}
if (da != p) {
long i;
wc->memory_map = NULL;
pages = kvmalloc_array(p, sizeof(struct page *), GFP_KERNEL);
if (!pages) {
r = -ENOMEM;
goto err2;
}
i = 0;
do {
long daa;
daa = dax_direct_access(wc->ssd_dev->dax_dev, offset + i, p - i,
NULL, &pfn);
if (daa <= 0) {
r = daa ? daa : -EINVAL;
goto err3;
}
if (!pfn_t_has_page(pfn)) {
r = -EOPNOTSUPP;
goto err3;
}
while (daa-- && i < p) {
pages[i++] = pfn_t_to_page(pfn);
pfn.val++;
if (!(i & 15))
cond_resched();
}
} while (i < p);
wc->memory_map = vmap(pages, p, VM_MAP, PAGE_KERNEL);
if (!wc->memory_map) {
r = -ENOMEM;
goto err3;
}
kvfree(pages);
wc->memory_vmapped = true;
}
dax_read_unlock(id);
wc->memory_map += (size_t)wc->start_sector << SECTOR_SHIFT;
wc->memory_map_size -= (size_t)wc->start_sector << SECTOR_SHIFT;
return 0;
err3:
kvfree(pages);
err2:
dax_read_unlock(id);
err1:
return r;
}
#else
static int persistent_memory_claim(struct dm_writecache *wc)
{
return -EOPNOTSUPP;
}
#endif
static void persistent_memory_release(struct dm_writecache *wc)
{
if (wc->memory_vmapped)
vunmap(wc->memory_map - ((size_t)wc->start_sector << SECTOR_SHIFT));
}
static struct page *persistent_memory_page(void *addr)
{
if (is_vmalloc_addr(addr))
return vmalloc_to_page(addr);
else
return virt_to_page(addr);
}
static unsigned persistent_memory_page_offset(void *addr)
{
return (unsigned long)addr & (PAGE_SIZE - 1);
}
static void persistent_memory_flush_cache(void *ptr, size_t size)
{
if (is_vmalloc_addr(ptr))
flush_kernel_vmap_range(ptr, size);
}
static void persistent_memory_invalidate_cache(void *ptr, size_t size)
{
if (is_vmalloc_addr(ptr))
invalidate_kernel_vmap_range(ptr, size);
}
static struct wc_memory_superblock *sb(struct dm_writecache *wc)
{
return wc->memory_map;
}
static struct wc_memory_entry *memory_entry(struct dm_writecache *wc, struct wc_entry *e)
{
return &sb(wc)->entries[e->index];
}
static void *memory_data(struct dm_writecache *wc, struct wc_entry *e)
{
return (char *)wc->block_start + (e->index << wc->block_size_bits);
}
static sector_t cache_sector(struct dm_writecache *wc, struct wc_entry *e)
{
return wc->start_sector + wc->metadata_sectors +
((sector_t)e->index << (wc->block_size_bits - SECTOR_SHIFT));
}
static uint64_t read_original_sector(struct dm_writecache *wc, struct wc_entry *e)
{
#ifdef DM_WRITECACHE_HANDLE_HARDWARE_ERRORS
return e->original_sector;
#else
return le64_to_cpu(memory_entry(wc, e)->original_sector);
#endif
}
static uint64_t read_seq_count(struct dm_writecache *wc, struct wc_entry *e)
{
#ifdef DM_WRITECACHE_HANDLE_HARDWARE_ERRORS
return e->seq_count;
#else
return le64_to_cpu(memory_entry(wc, e)->seq_count);
#endif
}
static void clear_seq_count(struct dm_writecache *wc, struct wc_entry *e)
{
#ifdef DM_WRITECACHE_HANDLE_HARDWARE_ERRORS
e->seq_count = -1;
#endif
pmem_assign(memory_entry(wc, e)->seq_count, cpu_to_le64(-1));
}
static void write_original_sector_seq_count(struct dm_writecache *wc, struct wc_entry *e,
uint64_t original_sector, uint64_t seq_count)
{
struct wc_memory_entry me;
#ifdef DM_WRITECACHE_HANDLE_HARDWARE_ERRORS
e->original_sector = original_sector;
e->seq_count = seq_count;
#endif
me.original_sector = cpu_to_le64(original_sector);
me.seq_count = cpu_to_le64(seq_count);
pmem_assign(*memory_entry(wc, e), me);
}
#define writecache_error(wc, err, msg, arg...) \
do { \
if (!cmpxchg(&(wc)->error, 0, err)) \
DMERR(msg, ##arg); \
wake_up(&(wc)->freelist_wait); \
} while (0)
#define writecache_has_error(wc) (unlikely(READ_ONCE((wc)->error)))
static void writecache_flush_all_metadata(struct dm_writecache *wc)
{
if (!WC_MODE_PMEM(wc))
memset(wc->dirty_bitmap, -1, wc->dirty_bitmap_size);
}
static void writecache_flush_region(struct dm_writecache *wc, void *ptr, size_t size)
{
if (!WC_MODE_PMEM(wc))
__set_bit(((char *)ptr - (char *)wc->memory_map) / BITMAP_GRANULARITY,
wc->dirty_bitmap);
}
static void writecache_disk_flush(struct dm_writecache *wc, struct dm_dev *dev);
struct io_notify {
struct dm_writecache *wc;
struct completion c;
atomic_t count;
};
static void writecache_notify_io(unsigned long error, void *context)
{
struct io_notify *endio = context;
if (unlikely(error != 0))
writecache_error(endio->wc, -EIO, "error writing metadata");
BUG_ON(atomic_read(&endio->count) <= 0);
if (atomic_dec_and_test(&endio->count))
complete(&endio->c);
}
static void writecache_wait_for_ios(struct dm_writecache *wc, int direction)
{
wait_event(wc->bio_in_progress_wait[direction],
!atomic_read(&wc->bio_in_progress[direction]));
}
static void ssd_commit_flushed(struct dm_writecache *wc, bool wait_for_ios)
{
struct dm_io_region region;
struct dm_io_request req;
struct io_notify endio = {
wc,
COMPLETION_INITIALIZER_ONSTACK(endio.c),
ATOMIC_INIT(1),
};
unsigned bitmap_bits = wc->dirty_bitmap_size * 8;
unsigned i = 0;
while (1) {
unsigned j;
i = find_next_bit(wc->dirty_bitmap, bitmap_bits, i);
if (unlikely(i == bitmap_bits))
break;
j = find_next_zero_bit(wc->dirty_bitmap, bitmap_bits, i);
region.bdev = wc->ssd_dev->bdev;
region.sector = (sector_t)i * (BITMAP_GRANULARITY >> SECTOR_SHIFT);
region.count = (sector_t)(j - i) * (BITMAP_GRANULARITY >> SECTOR_SHIFT);
if (unlikely(region.sector >= wc->metadata_sectors))
break;
if (unlikely(region.sector + region.count > wc->metadata_sectors))
region.count = wc->metadata_sectors - region.sector;
region.sector += wc->start_sector;
atomic_inc(&endio.count);
req.bi_op = REQ_OP_WRITE;
req.bi_op_flags = REQ_SYNC;
req.mem.type = DM_IO_VMA;
req.mem.ptr.vma = (char *)wc->memory_map + (size_t)i * BITMAP_GRANULARITY;
req.client = wc->dm_io;
req.notify.fn = writecache_notify_io;
req.notify.context = &endio;
/* writing via async dm-io (implied by notify.fn above) won't return an error */
(void) dm_io(&req, 1, ®ion, NULL);
i = j;
}
writecache_notify_io(0, &endio);
wait_for_completion_io(&endio.c);
if (wait_for_ios)
writecache_wait_for_ios(wc, WRITE);
writecache_disk_flush(wc, wc->ssd_dev);
memset(wc->dirty_bitmap, 0, wc->dirty_bitmap_size);
}
static void ssd_commit_superblock(struct dm_writecache *wc)
{
int r;
struct dm_io_region region;
struct dm_io_request req;
region.bdev = wc->ssd_dev->bdev;
region.sector = 0;
region.count = max(4096U, wc->block_size) >> SECTOR_SHIFT;
if (unlikely(region.sector + region.count > wc->metadata_sectors))
region.count = wc->metadata_sectors - region.sector;
region.sector += wc->start_sector;
req.bi_op = REQ_OP_WRITE;
req.bi_op_flags = REQ_SYNC | REQ_FUA;
req.mem.type = DM_IO_VMA;
req.mem.ptr.vma = (char *)wc->memory_map;
req.client = wc->dm_io;
req.notify.fn = NULL;
req.notify.context = NULL;
r = dm_io(&req, 1, ®ion, NULL);
if (unlikely(r))
writecache_error(wc, r, "error writing superblock");
}
static void writecache_commit_flushed(struct dm_writecache *wc, bool wait_for_ios)
{
if (WC_MODE_PMEM(wc))
pmem_wmb();
else
ssd_commit_flushed(wc, wait_for_ios);
}
static void writecache_disk_flush(struct dm_writecache *wc, struct dm_dev *dev)
{
int r;
struct dm_io_region region;
struct dm_io_request req;
region.bdev = dev->bdev;
region.sector = 0;
region.count = 0;
req.bi_op = REQ_OP_WRITE;
req.bi_op_flags = REQ_PREFLUSH;
req.mem.type = DM_IO_KMEM;
req.mem.ptr.addr = NULL;
req.client = wc->dm_io;
req.notify.fn = NULL;
r = dm_io(&req, 1, ®ion, NULL);
if (unlikely(r))
writecache_error(wc, r, "error flushing metadata: %d", r);
}
#define WFE_RETURN_FOLLOWING 1
#define WFE_LOWEST_SEQ 2
static struct wc_entry *writecache_find_entry(struct dm_writecache *wc,
uint64_t block, int flags)
{
struct wc_entry *e;
struct rb_node *node = wc->tree.rb_node;
if (unlikely(!node))
return NULL;
while (1) {
e = container_of(node, struct wc_entry, rb_node);
if (read_original_sector(wc, e) == block)
break;
node = (read_original_sector(wc, e) >= block ?
e->rb_node.rb_left : e->rb_node.rb_right);
if (unlikely(!node)) {
if (!(flags & WFE_RETURN_FOLLOWING))
return NULL;
if (read_original_sector(wc, e) >= block) {
return e;
} else {
node = rb_next(&e->rb_node);
if (unlikely(!node))
return NULL;
e = container_of(node, struct wc_entry, rb_node);
return e;
}
}
}
while (1) {
struct wc_entry *e2;
if (flags & WFE_LOWEST_SEQ)
node = rb_prev(&e->rb_node);
else
node = rb_next(&e->rb_node);
if (unlikely(!node))
return e;
e2 = container_of(node, struct wc_entry, rb_node);
if (read_original_sector(wc, e2) != block)
return e;
e = e2;
}
}
static void writecache_insert_entry(struct dm_writecache *wc, struct wc_entry *ins)
{
struct wc_entry *e;
struct rb_node **node = &wc->tree.rb_node, *parent = NULL;
while (*node) {
e = container_of(*node, struct wc_entry, rb_node);
parent = &e->rb_node;
if (read_original_sector(wc, e) > read_original_sector(wc, ins))
node = &parent->rb_left;
else
node = &parent->rb_right;
}
rb_link_node(&ins->rb_node, parent, node);
rb_insert_color(&ins->rb_node, &wc->tree);
list_add(&ins->lru, &wc->lru);
ins->age = jiffies;
}
static void writecache_unlink(struct dm_writecache *wc, struct wc_entry *e)
{
list_del(&e->lru);
rb_erase(&e->rb_node, &wc->tree);
}
static void writecache_add_to_freelist(struct dm_writecache *wc, struct wc_entry *e)
{
if (WC_MODE_SORT_FREELIST(wc)) {
struct rb_node **node = &wc->freetree.rb_node, *parent = NULL;
if (unlikely(!*node))
wc->current_free = e;
while (*node) {
parent = *node;
if (&e->rb_node < *node)
node = &parent->rb_left;
else
node = &parent->rb_right;
}
rb_link_node(&e->rb_node, parent, node);
rb_insert_color(&e->rb_node, &wc->freetree);
} else {
list_add_tail(&e->lru, &wc->freelist);
}
wc->freelist_size++;
}
static inline void writecache_verify_watermark(struct dm_writecache *wc)
{
if (unlikely(wc->freelist_size + wc->writeback_size <= wc->freelist_high_watermark))
queue_work(wc->writeback_wq, &wc->writeback_work);
}
static void writecache_max_age_timer(struct timer_list *t)
{
struct dm_writecache *wc = from_timer(wc, t, max_age_timer);
if (!dm_suspended(wc->ti) && !writecache_has_error(wc)) {
queue_work(wc->writeback_wq, &wc->writeback_work);
mod_timer(&wc->max_age_timer, jiffies + wc->max_age / MAX_AGE_DIV);
}
}
static struct wc_entry *writecache_pop_from_freelist(struct dm_writecache *wc, sector_t expected_sector)
{
struct wc_entry *e;
if (WC_MODE_SORT_FREELIST(wc)) {
struct rb_node *next;
if (unlikely(!wc->current_free))
return NULL;
e = wc->current_free;
if (expected_sector != (sector_t)-1 && unlikely(cache_sector(wc, e) != expected_sector))
return NULL;
next = rb_next(&e->rb_node);
rb_erase(&e->rb_node, &wc->freetree);
if (unlikely(!next))
next = rb_first(&wc->freetree);
wc->current_free = next ? container_of(next, struct wc_entry, rb_node) : NULL;
} else {
if (unlikely(list_empty(&wc->freelist)))
return NULL;
e = container_of(wc->freelist.next, struct wc_entry, lru);
if (expected_sector != (sector_t)-1 && unlikely(cache_sector(wc, e) != expected_sector))
return NULL;
list_del(&e->lru);
}
wc->freelist_size--;
writecache_verify_watermark(wc);
return e;
}
static void writecache_free_entry(struct dm_writecache *wc, struct wc_entry *e)
{
writecache_unlink(wc, e);
writecache_add_to_freelist(wc, e);
clear_seq_count(wc, e);
writecache_flush_region(wc, memory_entry(wc, e), sizeof(struct wc_memory_entry));
if (unlikely(waitqueue_active(&wc->freelist_wait)))
wake_up(&wc->freelist_wait);
}
static void writecache_wait_on_freelist(struct dm_writecache *wc)
{
DEFINE_WAIT(wait);
prepare_to_wait(&wc->freelist_wait, &wait, TASK_UNINTERRUPTIBLE);
wc_unlock(wc);
io_schedule();
finish_wait(&wc->freelist_wait, &wait);
wc_lock(wc);
}
static void writecache_poison_lists(struct dm_writecache *wc)
{
/*
* Catch incorrect access to these values while the device is suspended.
*/
memset(&wc->tree, -1, sizeof wc->tree);
wc->lru.next = LIST_POISON1;
wc->lru.prev = LIST_POISON2;
wc->freelist.next = LIST_POISON1;
wc->freelist.prev = LIST_POISON2;
}
static void writecache_flush_entry(struct dm_writecache *wc, struct wc_entry *e)
{
writecache_flush_region(wc, memory_entry(wc, e), sizeof(struct wc_memory_entry));
if (WC_MODE_PMEM(wc))
writecache_flush_region(wc, memory_data(wc, e), wc->block_size);
}
static bool writecache_entry_is_committed(struct dm_writecache *wc, struct wc_entry *e)
{
return read_seq_count(wc, e) < wc->seq_count;
}
static void writecache_flush(struct dm_writecache *wc)
{
struct wc_entry *e, *e2;
bool need_flush_after_free;
wc->uncommitted_blocks = 0;
del_timer(&wc->autocommit_timer);
if (list_empty(&wc->lru))
return;
e = container_of(wc->lru.next, struct wc_entry, lru);
if (writecache_entry_is_committed(wc, e)) {
if (wc->overwrote_committed) {
writecache_wait_for_ios(wc, WRITE);
writecache_disk_flush(wc, wc->ssd_dev);
wc->overwrote_committed = false;
}
return;
}
while (1) {
writecache_flush_entry(wc, e);
if (unlikely(e->lru.next == &wc->lru))
break;
e2 = container_of(e->lru.next, struct wc_entry, lru);
if (writecache_entry_is_committed(wc, e2))
break;
e = e2;
cond_resched();
}
writecache_commit_flushed(wc, true);
wc->seq_count++;
pmem_assign(sb(wc)->seq_count, cpu_to_le64(wc->seq_count));
if (WC_MODE_PMEM(wc))
writecache_commit_flushed(wc, false);
else
ssd_commit_superblock(wc);
wc->overwrote_committed = false;
need_flush_after_free = false;
while (1) {
/* Free another committed entry with lower seq-count */
struct rb_node *rb_node = rb_prev(&e->rb_node);
if (rb_node) {
e2 = container_of(rb_node, struct wc_entry, rb_node);
if (read_original_sector(wc, e2) == read_original_sector(wc, e) &&
likely(!e2->write_in_progress)) {
writecache_free_entry(wc, e2);
need_flush_after_free = true;
}
}
if (unlikely(e->lru.prev == &wc->lru))
break;
e = container_of(e->lru.prev, struct wc_entry, lru);
cond_resched();
}
if (need_flush_after_free)
writecache_commit_flushed(wc, false);
}
static void writecache_flush_work(struct work_struct *work)
{
struct dm_writecache *wc = container_of(work, struct dm_writecache, flush_work);
wc_lock(wc);
writecache_flush(wc);
wc_unlock(wc);
}
static void writecache_autocommit_timer(struct timer_list *t)
{
struct dm_writecache *wc = from_timer(wc, t, autocommit_timer);
if (!writecache_has_error(wc))
queue_work(wc->writeback_wq, &wc->flush_work);
}
static void writecache_schedule_autocommit(struct dm_writecache *wc)
{
if (!timer_pending(&wc->autocommit_timer))
mod_timer(&wc->autocommit_timer, jiffies + wc->autocommit_jiffies);
}
static void writecache_discard(struct dm_writecache *wc, sector_t start, sector_t end)
{
struct wc_entry *e;
bool discarded_something = false;
e = writecache_find_entry(wc, start, WFE_RETURN_FOLLOWING | WFE_LOWEST_SEQ);
if (unlikely(!e))
return;
while (read_original_sector(wc, e) < end) {
struct rb_node *node = rb_next(&e->rb_node);
if (likely(!e->write_in_progress)) {
if (!discarded_something) {
if (!WC_MODE_PMEM(wc)) {
writecache_wait_for_ios(wc, READ);
writecache_wait_for_ios(wc, WRITE);
}
discarded_something = true;
}
if (!writecache_entry_is_committed(wc, e))
wc->uncommitted_blocks--;
writecache_free_entry(wc, e);
}
if (unlikely(!node))
break;
e = container_of(node, struct wc_entry, rb_node);
}
if (discarded_something)
writecache_commit_flushed(wc, false);
}
static bool writecache_wait_for_writeback(struct dm_writecache *wc)
{
if (wc->writeback_size) {
writecache_wait_on_freelist(wc);
return true;
}
return false;
}
static void writecache_suspend(struct dm_target *ti)
{
struct dm_writecache *wc = ti->private;
bool flush_on_suspend;
del_timer_sync(&wc->autocommit_timer);
del_timer_sync(&wc->max_age_timer);
wc_lock(wc);
writecache_flush(wc);
flush_on_suspend = wc->flush_on_suspend;
if (flush_on_suspend) {
wc->flush_on_suspend = false;
wc->writeback_all++;
queue_work(wc->writeback_wq, &wc->writeback_work);
}
wc_unlock(wc);
drain_workqueue(wc->writeback_wq);
wc_lock(wc);
if (flush_on_suspend)
wc->writeback_all--;
while (writecache_wait_for_writeback(wc));
if (WC_MODE_PMEM(wc))
persistent_memory_flush_cache(wc->memory_map, wc->memory_map_size);
writecache_poison_lists(wc);
wc_unlock(wc);
}
static int writecache_alloc_entries(struct dm_writecache *wc)
{
size_t b;
if (wc->entries)
return 0;
wc->entries = vmalloc(array_size(sizeof(struct wc_entry), wc->n_blocks));
if (!wc->entries)
return -ENOMEM;
for (b = 0; b < wc->n_blocks; b++) {
struct wc_entry *e = &wc->entries[b];
e->index = b;
e->write_in_progress = false;
cond_resched();
}
return 0;
}
static int writecache_read_metadata(struct dm_writecache *wc, sector_t n_sectors)
{
struct dm_io_region region;
struct dm_io_request req;
region.bdev = wc->ssd_dev->bdev;
region.sector = wc->start_sector;
region.count = n_sectors;
req.bi_op = REQ_OP_READ;
req.bi_op_flags = REQ_SYNC;
req.mem.type = DM_IO_VMA;
req.mem.ptr.vma = (char *)wc->memory_map;
req.client = wc->dm_io;
req.notify.fn = NULL;
return dm_io(&req, 1, ®ion, NULL);
}
static void writecache_resume(struct dm_target *ti)
{
struct dm_writecache *wc = ti->private;
size_t b;
bool need_flush = false;
__le64 sb_seq_count;
int r;