forked from microsoft/WSL2-Linux-Kernel
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdm-bufio.c
2940 lines (2395 loc) · 69 KB
/
dm-bufio.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) 2009-2011 Red Hat, Inc.
*
* Author: Mikulas Patocka <mpatocka@redhat.com>
*
* This file is released under the GPL.
*/
#include <linux/dm-bufio.h>
#include <linux/device-mapper.h>
#include <linux/dm-io.h>
#include <linux/slab.h>
#include <linux/sched/mm.h>
#include <linux/jiffies.h>
#include <linux/vmalloc.h>
#include <linux/shrinker.h>
#include <linux/module.h>
#include <linux/rbtree.h>
#include <linux/stacktrace.h>
#include <linux/jump_label.h>
#include "dm.h"
#define DM_MSG_PREFIX "bufio"
/*
* Memory management policy:
* Limit the number of buffers to DM_BUFIO_MEMORY_PERCENT of main memory
* or DM_BUFIO_VMALLOC_PERCENT of vmalloc memory (whichever is lower).
* Always allocate at least DM_BUFIO_MIN_BUFFERS buffers.
* Start background writeback when there are DM_BUFIO_WRITEBACK_PERCENT
* dirty buffers.
*/
#define DM_BUFIO_MIN_BUFFERS 8
#define DM_BUFIO_MEMORY_PERCENT 2
#define DM_BUFIO_VMALLOC_PERCENT 25
#define DM_BUFIO_WRITEBACK_RATIO 3
#define DM_BUFIO_LOW_WATERMARK_RATIO 16
/*
* Check buffer ages in this interval (seconds)
*/
#define DM_BUFIO_WORK_TIMER_SECS 30
/*
* Free buffers when they are older than this (seconds)
*/
#define DM_BUFIO_DEFAULT_AGE_SECS 300
/*
* The nr of bytes of cached data to keep around.
*/
#define DM_BUFIO_DEFAULT_RETAIN_BYTES (256 * 1024)
/*
* Align buffer writes to this boundary.
* Tests show that SSDs have the highest IOPS when using 4k writes.
*/
#define DM_BUFIO_WRITE_ALIGN 4096
/*
* dm_buffer->list_mode
*/
#define LIST_CLEAN 0
#define LIST_DIRTY 1
#define LIST_SIZE 2
/*--------------------------------------------------------------*/
/*
* Rather than use an LRU list, we use a clock algorithm where entries
* are held in a circular list. When an entry is 'hit' a reference bit
* is set. The least recently used entry is approximated by running a
* cursor around the list selecting unreferenced entries. Referenced
* entries have their reference bit cleared as the cursor passes them.
*/
struct lru_entry {
struct list_head list;
atomic_t referenced;
};
struct lru_iter {
struct lru *lru;
struct list_head list;
struct lru_entry *stop;
struct lru_entry *e;
};
struct lru {
struct list_head *cursor;
unsigned long count;
struct list_head iterators;
};
/*--------------*/
static void lru_init(struct lru *lru)
{
lru->cursor = NULL;
lru->count = 0;
INIT_LIST_HEAD(&lru->iterators);
}
static void lru_destroy(struct lru *lru)
{
WARN_ON_ONCE(lru->cursor);
WARN_ON_ONCE(!list_empty(&lru->iterators));
}
/*
* Insert a new entry into the lru.
*/
static void lru_insert(struct lru *lru, struct lru_entry *le)
{
/*
* Don't be tempted to set to 1, makes the lru aspect
* perform poorly.
*/
atomic_set(&le->referenced, 0);
if (lru->cursor) {
list_add_tail(&le->list, lru->cursor);
} else {
INIT_LIST_HEAD(&le->list);
lru->cursor = &le->list;
}
lru->count++;
}
/*--------------*/
/*
* Convert a list_head pointer to an lru_entry pointer.
*/
static inline struct lru_entry *to_le(struct list_head *l)
{
return container_of(l, struct lru_entry, list);
}
/*
* Initialize an lru_iter and add it to the list of cursors in the lru.
*/
static void lru_iter_begin(struct lru *lru, struct lru_iter *it)
{
it->lru = lru;
it->stop = lru->cursor ? to_le(lru->cursor->prev) : NULL;
it->e = lru->cursor ? to_le(lru->cursor) : NULL;
list_add(&it->list, &lru->iterators);
}
/*
* Remove an lru_iter from the list of cursors in the lru.
*/
static inline void lru_iter_end(struct lru_iter *it)
{
list_del(&it->list);
}
/* Predicate function type to be used with lru_iter_next */
typedef bool (*iter_predicate)(struct lru_entry *le, void *context);
/*
* Advance the cursor to the next entry that passes the
* predicate, and return that entry. Returns NULL if the
* iteration is complete.
*/
static struct lru_entry *lru_iter_next(struct lru_iter *it,
iter_predicate pred, void *context)
{
struct lru_entry *e;
while (it->e) {
e = it->e;
/* advance the cursor */
if (it->e == it->stop)
it->e = NULL;
else
it->e = to_le(it->e->list.next);
if (pred(e, context))
return e;
}
return NULL;
}
/*
* Invalidate a specific lru_entry and update all cursors in
* the lru accordingly.
*/
static void lru_iter_invalidate(struct lru *lru, struct lru_entry *e)
{
struct lru_iter *it;
list_for_each_entry(it, &lru->iterators, list) {
/* Move c->e forwards if necc. */
if (it->e == e) {
it->e = to_le(it->e->list.next);
if (it->e == e)
it->e = NULL;
}
/* Move it->stop backwards if necc. */
if (it->stop == e) {
it->stop = to_le(it->stop->list.prev);
if (it->stop == e)
it->stop = NULL;
}
}
}
/*--------------*/
/*
* Remove a specific entry from the lru.
*/
static void lru_remove(struct lru *lru, struct lru_entry *le)
{
lru_iter_invalidate(lru, le);
if (lru->count == 1) {
lru->cursor = NULL;
} else {
if (lru->cursor == &le->list)
lru->cursor = lru->cursor->next;
list_del(&le->list);
}
lru->count--;
}
/*
* Mark as referenced.
*/
static inline void lru_reference(struct lru_entry *le)
{
atomic_set(&le->referenced, 1);
}
/*--------------*/
/*
* Remove the least recently used entry (approx), that passes the predicate.
* Returns NULL on failure.
*/
enum evict_result {
ER_EVICT,
ER_DONT_EVICT,
ER_STOP, /* stop looking for something to evict */
};
typedef enum evict_result (*le_predicate)(struct lru_entry *le, void *context);
static struct lru_entry *lru_evict(struct lru *lru, le_predicate pred, void *context)
{
unsigned long tested = 0;
struct list_head *h = lru->cursor;
struct lru_entry *le;
if (!h)
return NULL;
/*
* In the worst case we have to loop around twice. Once to clear
* the reference flags, and then again to discover the predicate
* fails for all entries.
*/
while (tested < lru->count) {
le = container_of(h, struct lru_entry, list);
if (atomic_read(&le->referenced)) {
atomic_set(&le->referenced, 0);
} else {
tested++;
switch (pred(le, context)) {
case ER_EVICT:
/*
* Adjust the cursor, so we start the next
* search from here.
*/
lru->cursor = le->list.next;
lru_remove(lru, le);
return le;
case ER_DONT_EVICT:
break;
case ER_STOP:
lru->cursor = le->list.next;
return NULL;
}
}
h = h->next;
cond_resched();
}
return NULL;
}
/*--------------------------------------------------------------*/
/*
* Buffer state bits.
*/
#define B_READING 0
#define B_WRITING 1
#define B_DIRTY 2
/*
* Describes how the block was allocated:
* kmem_cache_alloc(), __get_free_pages() or vmalloc().
* See the comment at alloc_buffer_data.
*/
enum data_mode {
DATA_MODE_SLAB = 0,
DATA_MODE_GET_FREE_PAGES = 1,
DATA_MODE_VMALLOC = 2,
DATA_MODE_LIMIT = 3
};
struct dm_buffer {
/* protected by the locks in dm_buffer_cache */
struct rb_node node;
/* immutable, so don't need protecting */
sector_t block;
void *data;
unsigned char data_mode; /* DATA_MODE_* */
/*
* These two fields are used in isolation, so do not need
* a surrounding lock.
*/
atomic_t hold_count;
unsigned long last_accessed;
/*
* Everything else is protected by the mutex in
* dm_bufio_client
*/
unsigned long state;
struct lru_entry lru;
unsigned char list_mode; /* LIST_* */
blk_status_t read_error;
blk_status_t write_error;
unsigned int dirty_start;
unsigned int dirty_end;
unsigned int write_start;
unsigned int write_end;
struct list_head write_list;
struct dm_bufio_client *c;
void (*end_io)(struct dm_buffer *b, blk_status_t bs);
#ifdef CONFIG_DM_DEBUG_BLOCK_STACK_TRACING
#define MAX_STACK 10
unsigned int stack_len;
unsigned long stack_entries[MAX_STACK];
#endif
};
/*--------------------------------------------------------------*/
/*
* The buffer cache manages buffers, particularly:
* - inc/dec of holder count
* - setting the last_accessed field
* - maintains clean/dirty state along with lru
* - selecting buffers that match predicates
*
* It does *not* handle:
* - allocation/freeing of buffers.
* - IO
* - Eviction or cache sizing.
*
* cache_get() and cache_put() are threadsafe, you do not need to
* protect these calls with a surrounding mutex. All the other
* methods are not threadsafe; they do use locking primitives, but
* only enough to ensure get/put are threadsafe.
*/
struct buffer_tree {
struct rw_semaphore lock;
struct rb_root root;
} ____cacheline_aligned_in_smp;
struct dm_buffer_cache {
struct lru lru[LIST_SIZE];
/*
* We spread entries across multiple trees to reduce contention
* on the locks.
*/
unsigned int num_locks;
struct buffer_tree trees[];
};
static inline unsigned int cache_index(sector_t block, unsigned int num_locks)
{
return dm_hash_locks_index(block, num_locks);
}
static inline void cache_read_lock(struct dm_buffer_cache *bc, sector_t block)
{
down_read(&bc->trees[cache_index(block, bc->num_locks)].lock);
}
static inline void cache_read_unlock(struct dm_buffer_cache *bc, sector_t block)
{
up_read(&bc->trees[cache_index(block, bc->num_locks)].lock);
}
static inline void cache_write_lock(struct dm_buffer_cache *bc, sector_t block)
{
down_write(&bc->trees[cache_index(block, bc->num_locks)].lock);
}
static inline void cache_write_unlock(struct dm_buffer_cache *bc, sector_t block)
{
up_write(&bc->trees[cache_index(block, bc->num_locks)].lock);
}
/*
* Sometimes we want to repeatedly get and drop locks as part of an iteration.
* This struct helps avoid redundant drop and gets of the same lock.
*/
struct lock_history {
struct dm_buffer_cache *cache;
bool write;
unsigned int previous;
unsigned int no_previous;
};
static void lh_init(struct lock_history *lh, struct dm_buffer_cache *cache, bool write)
{
lh->cache = cache;
lh->write = write;
lh->no_previous = cache->num_locks;
lh->previous = lh->no_previous;
}
static void __lh_lock(struct lock_history *lh, unsigned int index)
{
if (lh->write)
down_write(&lh->cache->trees[index].lock);
else
down_read(&lh->cache->trees[index].lock);
}
static void __lh_unlock(struct lock_history *lh, unsigned int index)
{
if (lh->write)
up_write(&lh->cache->trees[index].lock);
else
up_read(&lh->cache->trees[index].lock);
}
/*
* Make sure you call this since it will unlock the final lock.
*/
static void lh_exit(struct lock_history *lh)
{
if (lh->previous != lh->no_previous) {
__lh_unlock(lh, lh->previous);
lh->previous = lh->no_previous;
}
}
/*
* Named 'next' because there is no corresponding
* 'up/unlock' call since it's done automatically.
*/
static void lh_next(struct lock_history *lh, sector_t b)
{
unsigned int index = cache_index(b, lh->no_previous); /* no_previous is num_locks */
if (lh->previous != lh->no_previous) {
if (lh->previous != index) {
__lh_unlock(lh, lh->previous);
__lh_lock(lh, index);
lh->previous = index;
}
} else {
__lh_lock(lh, index);
lh->previous = index;
}
}
static inline struct dm_buffer *le_to_buffer(struct lru_entry *le)
{
return container_of(le, struct dm_buffer, lru);
}
static struct dm_buffer *list_to_buffer(struct list_head *l)
{
struct lru_entry *le = list_entry(l, struct lru_entry, list);
if (!le)
return NULL;
return le_to_buffer(le);
}
static void cache_init(struct dm_buffer_cache *bc, unsigned int num_locks)
{
unsigned int i;
bc->num_locks = num_locks;
for (i = 0; i < bc->num_locks; i++) {
init_rwsem(&bc->trees[i].lock);
bc->trees[i].root = RB_ROOT;
}
lru_init(&bc->lru[LIST_CLEAN]);
lru_init(&bc->lru[LIST_DIRTY]);
}
static void cache_destroy(struct dm_buffer_cache *bc)
{
unsigned int i;
for (i = 0; i < bc->num_locks; i++)
WARN_ON_ONCE(!RB_EMPTY_ROOT(&bc->trees[i].root));
lru_destroy(&bc->lru[LIST_CLEAN]);
lru_destroy(&bc->lru[LIST_DIRTY]);
}
/*--------------*/
/*
* not threadsafe, or racey depending how you look at it
*/
static inline unsigned long cache_count(struct dm_buffer_cache *bc, int list_mode)
{
return bc->lru[list_mode].count;
}
static inline unsigned long cache_total(struct dm_buffer_cache *bc)
{
return cache_count(bc, LIST_CLEAN) + cache_count(bc, LIST_DIRTY);
}
/*--------------*/
/*
* Gets a specific buffer, indexed by block.
* If the buffer is found then its holder count will be incremented and
* lru_reference will be called.
*
* threadsafe
*/
static struct dm_buffer *__cache_get(const struct rb_root *root, sector_t block)
{
struct rb_node *n = root->rb_node;
struct dm_buffer *b;
while (n) {
b = container_of(n, struct dm_buffer, node);
if (b->block == block)
return b;
n = block < b->block ? n->rb_left : n->rb_right;
}
return NULL;
}
static void __cache_inc_buffer(struct dm_buffer *b)
{
atomic_inc(&b->hold_count);
WRITE_ONCE(b->last_accessed, jiffies);
}
static struct dm_buffer *cache_get(struct dm_buffer_cache *bc, sector_t block)
{
struct dm_buffer *b;
cache_read_lock(bc, block);
b = __cache_get(&bc->trees[cache_index(block, bc->num_locks)].root, block);
if (b) {
lru_reference(&b->lru);
__cache_inc_buffer(b);
}
cache_read_unlock(bc, block);
return b;
}
/*--------------*/
/*
* Returns true if the hold count hits zero.
* threadsafe
*/
static bool cache_put(struct dm_buffer_cache *bc, struct dm_buffer *b)
{
bool r;
cache_read_lock(bc, b->block);
BUG_ON(!atomic_read(&b->hold_count));
r = atomic_dec_and_test(&b->hold_count);
cache_read_unlock(bc, b->block);
return r;
}
/*--------------*/
typedef enum evict_result (*b_predicate)(struct dm_buffer *, void *);
/*
* Evicts a buffer based on a predicate. The oldest buffer that
* matches the predicate will be selected. In addition to the
* predicate the hold_count of the selected buffer will be zero.
*/
struct evict_wrapper {
struct lock_history *lh;
b_predicate pred;
void *context;
};
/*
* Wraps the buffer predicate turning it into an lru predicate. Adds
* extra test for hold_count.
*/
static enum evict_result __evict_pred(struct lru_entry *le, void *context)
{
struct evict_wrapper *w = context;
struct dm_buffer *b = le_to_buffer(le);
lh_next(w->lh, b->block);
if (atomic_read(&b->hold_count))
return ER_DONT_EVICT;
return w->pred(b, w->context);
}
static struct dm_buffer *__cache_evict(struct dm_buffer_cache *bc, int list_mode,
b_predicate pred, void *context,
struct lock_history *lh)
{
struct evict_wrapper w = {.lh = lh, .pred = pred, .context = context};
struct lru_entry *le;
struct dm_buffer *b;
le = lru_evict(&bc->lru[list_mode], __evict_pred, &w);
if (!le)
return NULL;
b = le_to_buffer(le);
/* __evict_pred will have locked the appropriate tree. */
rb_erase(&b->node, &bc->trees[cache_index(b->block, bc->num_locks)].root);
return b;
}
static struct dm_buffer *cache_evict(struct dm_buffer_cache *bc, int list_mode,
b_predicate pred, void *context)
{
struct dm_buffer *b;
struct lock_history lh;
lh_init(&lh, bc, true);
b = __cache_evict(bc, list_mode, pred, context, &lh);
lh_exit(&lh);
return b;
}
/*--------------*/
/*
* Mark a buffer as clean or dirty. Not threadsafe.
*/
static void cache_mark(struct dm_buffer_cache *bc, struct dm_buffer *b, int list_mode)
{
cache_write_lock(bc, b->block);
if (list_mode != b->list_mode) {
lru_remove(&bc->lru[b->list_mode], &b->lru);
b->list_mode = list_mode;
lru_insert(&bc->lru[b->list_mode], &b->lru);
}
cache_write_unlock(bc, b->block);
}
/*--------------*/
/*
* Runs through the lru associated with 'old_mode', if the predicate matches then
* it moves them to 'new_mode'. Not threadsafe.
*/
static void __cache_mark_many(struct dm_buffer_cache *bc, int old_mode, int new_mode,
b_predicate pred, void *context, struct lock_history *lh)
{
struct lru_entry *le;
struct dm_buffer *b;
struct evict_wrapper w = {.lh = lh, .pred = pred, .context = context};
while (true) {
le = lru_evict(&bc->lru[old_mode], __evict_pred, &w);
if (!le)
break;
b = le_to_buffer(le);
b->list_mode = new_mode;
lru_insert(&bc->lru[b->list_mode], &b->lru);
}
}
static void cache_mark_many(struct dm_buffer_cache *bc, int old_mode, int new_mode,
b_predicate pred, void *context)
{
struct lock_history lh;
lh_init(&lh, bc, true);
__cache_mark_many(bc, old_mode, new_mode, pred, context, &lh);
lh_exit(&lh);
}
/*--------------*/
/*
* Iterates through all clean or dirty entries calling a function for each
* entry. The callback may terminate the iteration early. Not threadsafe.
*/
/*
* Iterator functions should return one of these actions to indicate
* how the iteration should proceed.
*/
enum it_action {
IT_NEXT,
IT_COMPLETE,
};
typedef enum it_action (*iter_fn)(struct dm_buffer *b, void *context);
static void __cache_iterate(struct dm_buffer_cache *bc, int list_mode,
iter_fn fn, void *context, struct lock_history *lh)
{
struct lru *lru = &bc->lru[list_mode];
struct lru_entry *le, *first;
if (!lru->cursor)
return;
first = le = to_le(lru->cursor);
do {
struct dm_buffer *b = le_to_buffer(le);
lh_next(lh, b->block);
switch (fn(b, context)) {
case IT_NEXT:
break;
case IT_COMPLETE:
return;
}
cond_resched();
le = to_le(le->list.next);
} while (le != first);
}
static void cache_iterate(struct dm_buffer_cache *bc, int list_mode,
iter_fn fn, void *context)
{
struct lock_history lh;
lh_init(&lh, bc, false);
__cache_iterate(bc, list_mode, fn, context, &lh);
lh_exit(&lh);
}
/*--------------*/
/*
* Passes ownership of the buffer to the cache. Returns false if the
* buffer was already present (in which case ownership does not pass).
* eg, a race with another thread.
*
* Holder count should be 1 on insertion.
*
* Not threadsafe.
*/
static bool __cache_insert(struct rb_root *root, struct dm_buffer *b)
{
struct rb_node **new = &root->rb_node, *parent = NULL;
struct dm_buffer *found;
while (*new) {
found = container_of(*new, struct dm_buffer, node);
if (found->block == b->block)
return false;
parent = *new;
new = b->block < found->block ?
&found->node.rb_left : &found->node.rb_right;
}
rb_link_node(&b->node, parent, new);
rb_insert_color(&b->node, root);
return true;
}
static bool cache_insert(struct dm_buffer_cache *bc, struct dm_buffer *b)
{
bool r;
if (WARN_ON_ONCE(b->list_mode >= LIST_SIZE))
return false;
cache_write_lock(bc, b->block);
BUG_ON(atomic_read(&b->hold_count) != 1);
r = __cache_insert(&bc->trees[cache_index(b->block, bc->num_locks)].root, b);
if (r)
lru_insert(&bc->lru[b->list_mode], &b->lru);
cache_write_unlock(bc, b->block);
return r;
}
/*--------------*/
/*
* Removes buffer from cache, ownership of the buffer passes back to the caller.
* Fails if the hold_count is not one (ie. the caller holds the only reference).
*
* Not threadsafe.
*/
static bool cache_remove(struct dm_buffer_cache *bc, struct dm_buffer *b)
{
bool r;
cache_write_lock(bc, b->block);
if (atomic_read(&b->hold_count) != 1) {
r = false;
} else {
r = true;
rb_erase(&b->node, &bc->trees[cache_index(b->block, bc->num_locks)].root);
lru_remove(&bc->lru[b->list_mode], &b->lru);
}
cache_write_unlock(bc, b->block);
return r;
}
/*--------------*/
typedef void (*b_release)(struct dm_buffer *);
static struct dm_buffer *__find_next(struct rb_root *root, sector_t block)
{
struct rb_node *n = root->rb_node;
struct dm_buffer *b;
struct dm_buffer *best = NULL;
while (n) {
b = container_of(n, struct dm_buffer, node);
if (b->block == block)
return b;
if (block <= b->block) {
n = n->rb_left;
best = b;
} else {
n = n->rb_right;
}
}
return best;
}
static void __remove_range(struct dm_buffer_cache *bc,
struct rb_root *root,
sector_t begin, sector_t end,
b_predicate pred, b_release release)
{
struct dm_buffer *b;
while (true) {
cond_resched();
b = __find_next(root, begin);
if (!b || (b->block >= end))
break;
begin = b->block + 1;
if (atomic_read(&b->hold_count))
continue;
if (pred(b, NULL) == ER_EVICT) {
rb_erase(&b->node, root);
lru_remove(&bc->lru[b->list_mode], &b->lru);
release(b);
}
}
}
static void cache_remove_range(struct dm_buffer_cache *bc,
sector_t begin, sector_t end,
b_predicate pred, b_release release)
{
unsigned int i;
for (i = 0; i < bc->num_locks; i++) {
down_write(&bc->trees[i].lock);
__remove_range(bc, &bc->trees[i].root, begin, end, pred, release);
up_write(&bc->trees[i].lock);
}
}
/*----------------------------------------------------------------*/
/*
* Linking of buffers:
* All buffers are linked to buffer_cache with their node field.
*
* Clean buffers that are not being written (B_WRITING not set)
* are linked to lru[LIST_CLEAN] with their lru_list field.
*
* Dirty and clean buffers that are being written are linked to
* lru[LIST_DIRTY] with their lru_list field. When the write
* finishes, the buffer cannot be relinked immediately (because we
* are in an interrupt context and relinking requires process
* context), so some clean-not-writing buffers can be held on
* dirty_lru too. They are later added to lru in the process
* context.
*/
struct dm_bufio_client {
struct block_device *bdev;
unsigned int block_size;
s8 sectors_per_block_bits;
bool no_sleep;
struct mutex lock;
spinlock_t spinlock;
int async_write_error;
void (*alloc_callback)(struct dm_buffer *buf);
void (*write_callback)(struct dm_buffer *buf);
struct kmem_cache *slab_buffer;
struct kmem_cache *slab_cache;
struct dm_io_client *dm_io;
struct list_head reserved_buffers;
unsigned int need_reserved_buffers;
unsigned int minimum_buffers;
sector_t start;
struct shrinker shrinker;
struct work_struct shrink_work;
atomic_long_t need_shrink;
wait_queue_head_t free_buffer_wait;
struct list_head client_list;
/*
* Used by global_cleanup to sort the clients list.
*/
unsigned long oldest_buffer;
struct dm_buffer_cache cache; /* must be last member */
};
static DEFINE_STATIC_KEY_FALSE(no_sleep_enabled);
/*----------------------------------------------------------------*/
#define dm_bufio_in_request() (!!current->bio_list)
static void dm_bufio_lock(struct dm_bufio_client *c)
{
if (static_branch_unlikely(&no_sleep_enabled) && c->no_sleep)
spin_lock_bh(&c->spinlock);
else
mutex_lock_nested(&c->lock, dm_bufio_in_request());
}
static void dm_bufio_unlock(struct dm_bufio_client *c)
{
if (static_branch_unlikely(&no_sleep_enabled) && c->no_sleep)
spin_unlock_bh(&c->spinlock);
else